square([A1, A2, A3|_], [B1, B2, B3|_], [C1, C2, C3|_]):-
all_diff([A1, A2, A3, B1, B2, B3, C1, C2, C3]).
squares([[]|_]):- !.
squares([[A1,A2,A3|A4], [B1,B2,B3|B4], [C1,C2,C3|C4], [D1,D2,D3|D4], [E1,E2,E3|E4], [F1,F2,F3|F4], [G1,G2,G3|G4], [H1,H2,H3|H4], [I1,I2,I3|I4]]):-
square([[A1,A2,A3], [B1,B2,B3], [C1,C2,C3]]),
square([[D1,D2,D3], [E1,E2,E3], [F1,F2,F3]]),
square([[G1,G2,G3], [H1,H2,H3], [I1,I2,I3]]),
squares([A4, B4, C4, D4, E4, F4, G4, H4, I4]).
I'm getting a uncaught exception: error(existence_error(procedure,square/1),squares/1) - I'm new to Prolog so I'm not quite sure what this is saying (well, where it's saying it's going wrong).
This is inside a sudoku program, by the way.
squares iterates through the 3x3 squares of the sudoku puzzle starting with the top left, middle left, and bottom left - moving to the top middle, middle middle, bottom middle, and so on.
square checks the focus square - A1, A2, A3 are the first three values of the first row
B1, B2, B3 are the first three values of the second row, and so on. It assembles them into one array and checks to make sure they are all different.
on the lines
square([[A1,A2,A3], [B1,B2,B3], [C1,C2,C3]]),
square([[D1,D2,D3], [E1,E2,E3], [F1,F2,F3]]),
square([[G1,G2,G3], [H1,H2,H3], [I1,I2,I3]]),
you'll note that the type is ([[],[],[]]), however, the function, square is ([],[],[]).
Changing the lines to
square([A1,A2,A3], [B1,B2,B3], [C1,C2,C3]),
square([D1,D2,D3], [E1,E2,E3], [F1,F2,F3]),
square([G1,G2,G3], [H1,H2,H3], [I1,I2,I3]),
fixes the problem.
Related
I want to do numerical integration in Mathematica over a large dataset like: {{x1,y1(c)}, {x2,y2(c)}, {x3,y3(c)}, {x4,y4(c)}..} where y1=(a1+c)/b1, y2= (a2+c)/b2, y3=(a3+c)/b3, y4=(a4+c)/b4.... and a1, a2, a3, a4, b1, b2, b3, b4..... are just numbers and c is a constant. After the integration, I want to plot the resulting function as a function of c. How can I do that?
I have to determine whether two rectangles overlap or not, I can do this but I am struggling with figuring out how to grab my given data, and compare it to eachother to determine larger values.
%This is :what would be happening :
%separate(rectangle(0,10,10,0), rectangle(4,6,6,4))
separate(R1,R2) :-
%I Have to figure out how to take the values from R1 and R2 and compare
%them to one another.
.
It is called "pattern matching".
separated(R1, R2) :-
R1 = rectangle(A1, B1, C1, D1),
R2 = rectangle(A2, B2, C2, D2),
/* now just use your As and Bs */
and in many cases it is better to write straight away:
separated(rectangle(A1, B1, C1, D1), rectangle(A2, B2, C2, D2)) :-
/* now just use your As and Bs */
Lets say I have package A version 1 and package A version 2, Will call them A1 and A2 respectively.
If I have a pool of packages: A1, A2, B1, B2, C1, C2, D1, D2
A1 depends on B1, will represent as (A1, (B1)).
Plus A1 depends on any version of package C "C1 or C2 satisfy A1", will represent as (A1, (C1, C2))
combining A1 deps together, then A1 data-structure becomes: (A1, (B1), (C1, C2))
Also B1 depends on D1: (B1, (D1))
A1 structure becomes: (A1, ((B1, (D1))), (C1, C2))
similarly A2 structure is (A2, ((B2, (D2))), (C1, C2))
My question is: How can I select best candidate of package A, where I can select based on a condition (for example, the condition is the package does not conflict with current installed packages).
by combining A1 and A2: ((A1, ((B1, (D1))), (C1, C2)), (A2, ((B2, (D2))), (C1, C2)))
How can I traverse this data structure
So start with A1, if doesn't conflict check B1, if doesn't conflict check D1, if doesn't conflict check (C1, C2), and take one only either C1 or C2.
With this I end up selecting (A1, B1, D1, C1).
In case if A1 or any of its deps did not meet the condition, (for example if B1 conflicts with installed packages), then drop A1 entirely and move to check A2. then end up with (A2, B2, D2, C1).
What kind of traversal would that be?
I have been reading about in-order, pre-order, post-order traversal, and wondering if I need to do something similar here.
Assuming you are asking traversal on a more generic problem rather than working on this instance, I don't think there exists such a traversal.
Note that in-order is only applicable to BINARY trees. Any other kind of tree does not have in-order traversal. If your generic problem has B1, B2, B3, then apparently there wouldn't be a binary tree representation.
One property about traversal, is that the tree has all the information inclusively in the itself. When you traverse over a tree you never worry about "external information". In your case, your tree is not complete in information - you need to depend on external information to see if there is a conflict. e.g. B1 is installed - this information is never in the tree.
You can use adjacency list to represent the data:
Suppose the packages are A1, A2, B1, B2, C1, C2.
And A1 depends on B1 and C2, A2 depends on B1 and C1 and C2.
The above data can be represented as
[A1] -> [B1, C2]
[A2] -> [B1, C1, C2]
Use Topological Sorting to get the order of dependencies
Basically I am trying to implement a program in prolog that can solve a game called Yin Yang, independente of the size of the board.
The game is basically to paint every cell of the board of a specific color (either black or white) and all cells of same color are connected to each other, vertically or horizontally. I am representing this with a list of lists. Example initial->solution: https://i.gyazo.com/24a70d868934dfbf1540343e89d14c4b.png
However there is a rule I'm having trouble with: "No 2X2 group of cells can contain circles of a single color."
Any ideas how I can assure this doesn't happen using the clpfd library?
List representing board example:
[[0,0,0,0,0,0],
[0,0,0,1,0,0],
[0,0,1,1,2,0],
[0,2,0,0,2,2],
[1,0,2,0,1,0],
[0,1,0,0,0,1]]
I managed to do a variant of the problem :
get-elem(B, R, C, E) :- get-line(B, R, Row), get-line(Row, C, E).
get-line([R|_], 0, R).
get-line([_|A], N, R) :- get-line(A, M, R), N is M + 1.
twoXTwo(B, R, C) :- \+ (R1 is R+ 1, C1 is C+1,
get-elem(B, R, C, E1),
get-elem(B, R1, C, E2), E1 = E2,
get-elem(B, R, C1, E3), E2 = E3,
get-elem(B, R1, C1, E4), E3 = E4).
where I check the element at positions [(r+1,c), (r,c+1), (r+1,c+1)] are not the same element. I am also not sure what the numbers mean given you have either black (1) or white (0).
I solved the issue: This is the code I used for solving the problem described above.
twoByTwo([_],[_]).
twoByTwo([F1,S1|T1],[F2,S2|T2]):-
F1 + S1 + F2 + S2 #> 4,
F1 + S1 + F2 + S2 #< 8,
twoByTwo([S1|T1],[S2|T2]).
Basically twoByTwo receives a line of the board (in order) on each argument.
So basically this function is called recursively: twoByTwo(Line1,Line2), twoByTwo(Line2,Line3), etc.
Is there any special design pattern or algorithm available for this problem?
There are multiple items (A1,A2,A3 ..An) and I want to arrange them, some of them are related to the other ones and only can comes before or after them.
For example A2 can be placed only after A4 and “An” can be placed at the end of the set. But the point is that the sequence of some items can be interchangeable and based on sequence some items should not be in the set.
For examlpe consider this scenario
There are 6 items
A1, A2, A3, A4, A5, A6
And the rules are
A1 must be at the first place (always),
A2 can be after A4,
A5 can be in the set only if A3 has been there before it
A6 comes at the end of the set and it is a mandatory member but it only can be there if all other valid items have been in the set before it !
Valid sets are like this
A1, A4, A3, A2, A5, A6
A1, A4, A2, A6
Invalid sets
A4, A3, A2, A5, A6 (A1 is missed)
A1, A4, A3, A2, A5 (A6 is missed)
A1, A3, A2, A6 (A2 only comes after A4)
Note: I have to validate the input! and input can have any order! I mean I don’t want to sort the items I want to validate an input set from the user
As a sample, based on my example above the below sets are all valid
{A1, A4, A3, A2, A5, A6}
{A1, A4, A2, A3, A5, A6}
{A1, A3, A4, A2, A5, A6}
{A1, A3, A5, A4, A2, A6}
So user might enter any of these as an input and all of them are valid based on the defined conditions!
Any idea of any special design pattern or algorithm which can be applied to this problem? Number of items or the rules might get change in the future!
“BalusC” had been removed my “Design pattern” tag! But so far I think the best way to handle this problem might be the command pattern. I mean I considered each item as a command from the user and I defined a validation process for the command (“canExecute”) I am going to code it in C# and since ICommand interface in .Net has “canExecute” method I think I will use it to validate the command based on condition. (Execute method just added the item to the result set!) I have not coded it yet so I am not sure how complicated the validation process might be. I thought maybe someone has some idea how I can combine the command pattern and a validation algorithm to achieve the goals.
I might be wrong so any idea or suggestion can be helpful. Thanks.
This should be solvable with a variation of a topological sort.
Basically, you build a directed acyclic graph where there is an edge from Ai to Aj if Ai must come before Aj in the result. The topological sort will then give you a valid order for the A's.
This will not deal with the rule that some items may be missing, but that should be simple to layer on top of this.
A1, A4, A2, A6
cannot be a valid set, because
A6 comes at the end of the set and it is a mandatory member but it
only can be there if all other valid items have been in the set before
it