I have a school project where I have to find solutions of the game "Lights Out" ( https://en.wikipedia.org/wiki/Lights_Out_(game) ) with a SAT Solver but I am having troubles trying to set a conjuctive normal form of the game.
The game consists of a 5 by 5 grid of lights. Pressing any of the lights will toggle it and the four adjacent lights. The goal of the puzzle is to switch all the lights off.
How I tried until now:
For a 3x3 grid (to begin with), I set 9 terms (for each button) thus:
C11 : the button at the position 1,1 is lighten up
C12 : the button at the position 1,2 is lighten up
C13 : the button at the position 1,3 is lighten up.
[...]
Since the button at 1,1 lights off the button at the position 1,2 and 2,1
I did C11 => C12 and C21
the button at 1,2 lights off the button at the position 1,1 and 1,3 and 2,2 I did C12 => C11 and C13 and C22
and continued for the other:
C13 => C12 and C23
C21 => C11 and C22 and C31
C22 => C12 and C21 and C23 and C32
C23 => C13 and C22 and C33
C31 => C21 and C32
C32 => C31 and C33 and C22
C33 => C23 and C32
then I just converted these to CNF to get the clauses I need for the sat solver, but it seems that what I did was wrong..
can Anyone help me to write this game into CNF form ?
Thank you very much !
Here is the game if you need to understand it better:
https://www.geogebra.org/m/JexnDJpt#material/KArehWn8
The wikipedia article you posted cites something that looks like a pretty good solution: Marlow Anderson, Todd Feil (1998). "Turning Lights Out with Linear Algebra" (PDF). Mathematics Magazine. 71 (4): 300–303. You will need to understand the math in the paper and how to encode Z_2 operations into CNF. (IMO less implementation effort than BMC.) Best of luck on the assignment!
A way to solve the problem is to encode the sequence of actions needed to reach your goal.
One way of doing this is to consider that it takes K moves to solve the puzzle. You will then encode for each step the select cell, and the impact on the related cells, and ask the solver for a model such that the K-th configuration has all lights off.
This technique is called bounded model checking, and you should find several explanations on how to convert to sat.
Related
I'm looking for an algorithm to determine if the distance between 2 points (p1, p2), moving around a circle at know velocity is increasing or decreasing from the perspective of the smaller arc between them.
I know the position of p1 & p2 in degrees/radians.
I know the speed (piV, p2V) of both objects on a uniform scale.
I know the absolute value of the short arc between p1 & p2 in degrees/radians (always a positive value). But, if it helps I can also know the short arc as a negative or positive value in the sense that it is negative from the perspective of p1 when p2 is behind (right) and positive if p2 is in front (left).
Speed is positive when they are moving counter clockwise and negative if they are "retrograde" (moving clockwise).
The real challenge of this question lies in dealing with the polar coordinate singularity at 180 degrees, so I will skip this and use vector math instead, which is slightly less efficient but much easier to understand. Hopefully someone else more adept with modular arithmetic can resolve this issue.
Converting the angular position to Cartesian:
(X, Y) = R * (cos θ, sin θ)
Assume the objects have Cartesian coordinates P1, P2. Introduce the 2D cross-product:
A ^ B = Ax By - Ay Bx
This is positive if A is clockwise rotated with respect to Be, and vice versa.
Assume the objects have angular velocities W1, W2, where a positive sign means travelling anti-clockwise as is conventional for polar coordinates.
When object 2 is clockwise rotated w.r.t. object 1 (P1 ^ P2 < 0, as in your diagram):
If W1, W2 > 0 and W2 > W1
or, if W1, W2 < 0 and -W1 > -W2
or if W1 < 0 and W2 > 0
... the objects are travelling towards each other; and vice versa for the opposite situation, swapping object labels. Compiling this into a single condition:
sign(W1 - W2) == sign(P1 ^ P2) != 0
Btw, P1 ^ P2 = R^2 sin(θ2 - θ1) using a trig identity, so simply checking the sign of sin(θ2 - θ1) would work.
Edit: turns out reducing the condition down to a single sine term makes the modular logic much clearer, by considering the behaviour of the sine function -
sign(P1 ^ P2) = sign(180 - [θ2 - θ1] % 360)
Thanks to #beta for his comment and which prodded me in the right direction. I had missed the fact that I could use the direction (positive or negative) that p1 is from p2 and which was already known. There was therefore no need to convert the values into other coordinate systems.
The answer is quite simple:
SIGN((p2V - p1V) * Angle) = (+ if separating, - when closing or approaching)
I reduced this to the following C code:
return ((p2V - p1V) * angle) > 0 ? 1 : -1;
This works well if one is using a signed separating Angle that is positive if p2 is counter clockwise (left) from p1 and negative when p2 is clockwise (right) from p1.
Move the reference system to p1. Now you have a static point p1 and p2 moving with velocity of p2V - p1V starting from position p2-p1. Now the question is: when will p2 hit the origin?
Unfortunately, there are three cases here, depending on sign of p2V - p1V:
The speed is zero, points will never collide.
The speed is positive, meaning that p2 will go round the circle and hit 360 degrees.
The speed is negative, meaning that p2 will go straight to the origin and hit 0 degrees.
The only other gotcha is to calculate initial position of p2 - p1 correctly. I'd recommend assuming that initial positions are between 0 and 359, and then adjusting p2 - p1 correspondingly (if it's negative, add 360). Basically, calculating everything "modulo 360".
I hope I'll be clear.
C13 has a Formula =E13/1.21
I want that if F13 has an X then C13 should have =E13
E13 has the base price of the item
I need C13 to show the price including VAT (%21 here) unless there is an X in F13, then C13 would show the same as E13
thx
Say I have a n-dimensional orthogonal matrix, with some of its elements given and these others unknown. Does there exist an effective algorithm to find out the unknown elements and restore the whole matrix (it only needs to find one solution if there are many, and gives an error when no solution exists)?
Thanks!
Solution depends on how many element per row/column is missing.
I can see two ways how to approach this
1. max 1 element missing per row/column
in this case you can exploit that any row / column of orthogonal matrix is unit vector
so for any row/column { a1,a2,a3=??,a4,...,an }
the unknown element is a3=sqrt(1-a1^2-a2^2-a4^2-a5^2-...-an^2)
if you have more then one unknown elements in row use column and vice versa
if there is too many unknowns in row and columns at the same time
then you need to use different approach for that element
2. more then 1 missing element
first find all unknowns that can be found by approach 1
then you can exploit that Inverse(Q)==Transpose(Q) if Q is orthogonal
so derive algebraic formula per unknown element from inverse Q
and compare it to transposed one
this will create one linear equation
do this for all unknowns and solve the system
a11 a12 a13
Q = a21 a22=?? a23=??
a31 a32=?? a33=??
a11 a21 a31
transpose(Q) = a12 a22=?? a32=??
a13 a23=?? a33=??
i11 i12 i13
inverse(Q)= i21 i22 i23
i31 i32 i33
det=a11.a22.a33+a21.a32.a13+a31.a12.a23-a11.a32.a23-a31.a22.a13-a21.a12.a33
a22=i22=(a11.a33-a13.a31)/det
a32=i23=(a13.a21-a11.a23)/det
a23=i32=(a12.a31-a11.a32)/det
a33=i33=(a11.a22-a12.a21)/det
this should be solvable ...
if not then you can also add equations from bullet 1
if still not then either solution cant be found or you need to use different approach
Before computing you should check first if the |elements| are not too big
so sum all the known elements^2 in row/column and the result should be <=1
if not then this is nor orthogonal matrix ...
for more info see Orthogonal matrixes (Wiki)
3. if you have element=1.0
then all elements in that row/column will be zero
I'm trying to set up collision groups in Farseer so that the items in the picture collide as follows:
G1 Collides with All.
B1 and B2 collide with each other and G1, but not R1 or R2
R1 and R2 collide with each other and G1, but not B1 or B2.
I've been playing around with _Body.CollidesWith = Category.Cat1; and _Body.CollisionCategories = ..., but I'm basically just guessing. Haven't really found any usefull examples in the docs, but I might not have been looking in the right place either.
Edit 1:
Ok, so experimenting some more.
Assuming _Body is B1 (and also applied to B2) in the picture, and Cat1 is G1, and Cat2 is all blue items..
_Body.CollidesWith = Category.Cat1 & Category.Cat2;
_Body.CollisionCategories = Category.Cat2;
Should this not then allow B1 to collide with the ground (G1) and all other blues (B# items)?
Applying the above code makes all blue items collide with nothing not even each other...
_Body.CollisionCategories = Category.Cat1 | Category.Cat2;
instead of
_Body.CollisionCategories = Category.Cat1 & Category.Cat2;
I need algorithm to get shift trace of given polygon by given 2d-vector.
Given valid polygon with no holes, but possibly concave.
Operation performs on plane, so the result might be a polygon, possibly with holes.
If it simplifies the task, outer polygon is enough.
It looks simple to describe, but I find it complex to realize, so I look for some ready solutions, preferably in c#.
Suppose you have a polygon P given by the points A1, A2, ... , An.
Now you decide to shift it by X on the x-axis and Y on the y-axis.
You can do this to each point individually to get the ending location of the polygon.
Let's call the shifted polygon Q givng by points B1, B2, ... , Bn.
Then all you need to do is draw the following parallelograms:
(A1 A2 B2 B1), (A2 A3 B3 B2), (A3 A4 B4 B3), ... , (An-1 An Bn Bn-1) , (An A1 B1 Bn)
At this point you will have filled in the shape you want.
Some of the parallelograms will overlap, but that is okay, since you are just filling them all in with the same red color.
By doing it this way, you also get your second example to turn out correctly, (the bottom right corner of the hole in the middle should be diagonal, because of the lip sliding into position).