Prolog geometric triangle rules - prolog

Any examples on how to create rules in prolog for equilateral and isosceles triangle in 2d point?
Start with these:
equilateral(point2d(x,y), point2d(x,y), point2d(x,y))
isosceles(point2d(x,y), point2d(x,y), point2d(x,y))

To check if the triangle is equilateral, it's quite simple. To check if it is isosceles, it's a little less trivial. Here my solution:
seg_length(X1,Y1,X2,Y2,D):-
D is sqrt(((X2-X1)**2)+((Y2-Y1)**2)).
equilateral(point2d(X1,Y1), point2d(X2,Y2), point2d(X3,Y3)):-
seg_length(X1,Y1,X2,Y2,D),
seg_length(X2,Y2,X3,Y3,D),
seg_length(X3,Y3,X1,Y1,D).
isosceles(point2d(X1,Y1), point2d(X2,Y2), point2d(X3,Y3)):-
seg_length(X1,Y1,X2,Y2,D1),
seg_length(X2,Y2,X3,Y3,D2),
seg_length(X3,Y3,X1,Y1,D3),
L = [D1,D2,D3],
sort(L,LS),
length(L,LenL),
length(LS,LenLS),
LenL \== LenLS.
To check if at least two sides are equal, i use sort/2 which removes duplicates from the list. If the two lists have the same lenght, it means that all three sides are different and so the triangle is not isosceles.

Related

Prolog - Multiplication of matrix with its transpose

I need to find the product of the matrix A with its transpose A^t, so I should get a third matrix B=A*A^t.
Example: A=[[1,2],[3,4]] (then A^t=[[1,3],[2,4]]) so B=[[5,11],[11,25]]
(each sub list is a row of the matrix)
Firstly I think that this should be easier as the columns of A^t are the rows of A. So for the dot product of the row of A with the column of A^t I think I can use this:
sum([M|Ms],[N|Ns],Sum) :-
sum(Ms,Ns,S),
Sum is S+M*N.
sum([],[],0).
I also can't use clpfd or if-else.
I've been stuck and don't know what to do next.
It is correct that you can use the dot-product of the rows of matrix A for this. Although you probably better name this function dot_product/3 instead of sum/3.
The only task that is left, is to calculate this dot product for every two rows in A. So the i,j-th element of B is the dot product of Ai and Aj.
We can construct a predicate that calculates a row of B with:
transprod_row(A, AI, Row) :-
maplist(dot_product(AI), A, Row).
Furthermore we can then calculate the transprod/3 with another maplist/3:
transprod(A, B) :-
maplist(transprod_row(A),A,B).
or in full:
dot_product([],[],0).
dot_product([M|Ms],[N|Ns],Sum) :-
dot_product(Ms,Ns,S),
Sum is S+M*N.
transprod_row(A, AI, Row) :-
maplist(dot_product(AI), A, Row).
transprod(A, B) :-
maplist(transprod_row(A),A,B).
This then generates:
?- transprod([[1,2],[3,4]],B).
B = [[5, 11], [11, 25]].
The code is not yet the most elegant, nor is it very efficient, since Bij is equal to Bji so we can save half of the work. Furthermore if elements in the matrix are missing, then we can not calculate the product. I leave this as an exercise.

Prolog project. Labyrinth. Checking if next move is possible

Noob at prolog.
I need to do a school project related to a labyrinth.
My question is:
In the project I need to make a function "possible moves".
It gets a labyrinth, a current position and previous moves
Lab is represented by (these are the walls positions):
[[[down,left,up],[left,down,up],[right,up],[up],[,up],[right,left,up]],
[[left,down],[down,up],[down,up],[],[down],[right,down]],
[[left,up],[down,up],[down,up],[down],[down,up],[right,down,up]],
[[right,left],[left,up],[up],[up],[up],[right,up]],
[[left,down],[right,down],[left,down],[down],[down],[right,down]]]
And Poss_moves like:
Poss_moves(Lab, current_poss, previous_moves, possible_moves)
which is called as follows.
?- ..., poss_moves(Lab1, (2,5),
[(beginning, 1, 6), (down, 2, 6), (left, 2, 5)], possible_moves).
Lab1 = ...,
Poss = [ (up, 1, 5), (left, 2, 4)].
Important:
--- You can only move up, down, left or right.
PS: Sorry for my bad english.
PS: Edited.
PS: Can I do in prolog:
distance((Line1, Column1), (Line2, Column2), Dist) :-
Dist is abs(Line1 - Line2) + abs(Column1 - Column2).
PS: The lab that matches the picture.
[[[right,left,up],[left,down,up],[down,up],[up],[right,up],[right,left,up]],
[[left,down],[down,up],[b,up],[],[down],[right,down]],
[[left,up],[down,up],[down,up],[down],[down,up],[right,down,up]],
[[right,left],[left,up],[up],[up],[up],[right,up]],
[[left,down],[right,down],[left,down],[down],[down],[right,down]]]
By the way, the lab can change.
Thanks
EDIT 2:
I made this changes:
% predicates
lookup(Lab,(X,Y),Walls)
calc(Direction,(X1,Y1),(X2,Y2)
map_calc((X,Y),L,R)
poss_moves(Lab, (X,Y), PreviousMoves, PossibleMoves)
% clauses
nth(1, [H|T], H).
nth(N,[_|T],R) :-
M is N-1,
nth(M,T,R).
lookup(Lab, (X, Y), Walls) :-
nth(N,Lab,R),
Y == R,
X == Walls.
calc(up,(X,Y1),(X,Y2)) :-
Y2 is Y1-1.
calc(down,(X,Y1),(X,Y2)) :-
Y2 is Y1+1.
calc(left,(X,Y1),(X,Y2)) :-
X2 is X1-1.
calc(right,(X,Y1),(X,Y2)) :-
X2 is X1+1.
map_calc(_,[],[]).
map_calc((X,Y),[H|T],[(H,X1,Y1)|S]) :-
calc(H,(X,Y),(X1,Y1)),
map_calc((X,Y),T,S).
% main predicates
poss_moves(Lab, (X,Y), PreviousMoves, PossibleMoves) :-
lookup(Lab, (X,Y), Walls),
map_calc((X,Y), Lab, PossibleMoves).
I'm almost 100% sure that the lookup is incorrect.
Thanks
The first thing you need to do is define a way of looking up a cell in the Labryinth datastructure. You need something like:
lookup(Lab,(X,Y),Walls)
which is true if Walls is the list of walls present at cell (X,Y) in Lab. To do this you'll need an 'nth' predicate which finds the nth element of the list.
nth(1,[H|T],H).
nth(N,[_|T],R) :- M is N-1, nth(M,T,R).
Normally one would use 0 to return the first element of a list but your maze co-ordinates start at (1,1) so I've made nth do the same.
Now you can build lookup(Lab,(X,Y),Walls) which is true if the Yth element of Lab is Row and the Xth element of Row is Walls.
Next you need a way of turning the list of Walls in to a list of possible moves. A move consists of a direction and the co-ordinates of the new position so first write some helpers to calculate the new co-ordinates:
calc(Direction,(X1,Y1),(X2,Y2)
should be true if (X2,Y2) is the co-ordinate you get to if you move in Direction from (X1,Y1). Here is an example clause of calc, the others are similar:
calc(up,(X,Y1),(X,Y2)) :- Y2 is Y1-1.
But we need to apply calc to every element of the list of Walls to get the list of moves (this is called 'map' in functional programming)
map_calc((X,Y),L,R)
should be true if R is the result of applying calc to every direction in L starting from co-ordinate (X,Y).
map_calc(_,[],[]).
map_calc((X,Y),[H|T],[(H,X1,Y1)|S]) :- calc(H,(X,Y),(X1,Y1)),
map_calc((X,Y),T,S).
Now you can write poss_moves:
poss_moves(Lab, (X,Y), PreviousMoves, PossibleMoves)
i.e. PossibleMoves is the list of moves you can make from (X,Y) given that lookup(Lab,(X,Y),Walls) is true, and that map_calc on Walls gives you PossibleMoves.

Beginner Prolog issue

We have a box that contains red & yellow balls.
A man comes daily and gets 2 balls from the box, if he couldn't get 2 balls the game finishes.
There is a heap of red balls next to the box.
If the 2 balls which the man has withdrawn from the box were similar, he puts red ball into the box,
while if they were different, he puts yellow ball in the box.
We suppose that the box is represented like this
initialCan([y, r, y, r, y, r, y, r, y, r]).
y represents yellow ball, r represents red ball.
The man withdraws the 2 balls from the beginning of the list,
then he puts back 1 ball also to the beginning of the list.
So what is the procedure in Prolog which gives the color of the last ball in the box whatever
was the box containing at the beginning?
You might abstract your problem as a search in the space of possible states.
search(FinalState, FinalState):-
is_final(FinalState).
search(CurrentState, FinalState):-
transition(CurrentState, NextState),
search(NextState, FinalState).
solution(FinalState):-
initial_state(State0),
search(State0, FinalState).
So you jump from state to state until you reach the final one which becomes your solution. You need to do some things:
design a representation for a state (for example, a state might be a list like [r,y,r,...])
write a predicate initial_state(S0) which is satisfied if S0 is the initial state of the game
write a predicate transition(S1, S2) which is true if you can get from S1 to S2
write a predicate is_final(S) which is true if S is a final state
It is even easier to design the state as just box(Yellow_count, Red_count) and not bother with any particular list (after all, the balls are all identical, like electrons). Here is my try. I'm probably writing someone's homework here, but this is actually interesting.
Also consider checking out "Why correctness must be a mathematical concern" by Edsger W. Dijkstra, wherein this problem is described.
% last_ball(box(Yellow_initial_count, Red_initial_count), Last_ball_color, Time_at_end)
% ---------- TRIVIAL CASES ---------
% if there is only 1 yellow ball, the color is 'yellow' and we needed zero steps to reach this state
last_ball(box(1,0), yellow, 0).
% if there is only 1 red ball, the color is 'red' and we needed zero steps to reach this state
last_ball(box(0,1), red, 0).
% ---------- CASES DEFINING INDUCTION OVER Yellow+Red BALLS -----------
% take two yellow: check that this is possible for the given box,
% then find out what the last color is from the reduced counts, then define the number of steps to be higher by 1
last_ball(box(YI, RI), LBC, TAE) :- YI>=2, YIp is (YI-2), RIp is (RI+1), last_ball(box(YIp,RIp),LBC,TAEp), TAE is (TAEp+1).
% take two red: check that this is possible for the given box,
% then find out what the last color is from the reduced counts, then define the number of steps to be higher by 1
last_ball(box(YI, RI), LBC, TAE) :- RI>=2, YIp is YI, RIp is (RI-2+1), last_ball(box(YIp,RIp),LBC,TAEp), TAE is (TAEp+1).
% take a red and a yellow: check that this is possible for the given box,
% then find out what the last color is from the reduced counts, then define the number of steps to be higher by 1
last_ball(box(YI, RI), LBC, TAE) :- RI>=1, YI>=1, YIp is (YI-1+1), RIp is (RI-1), last_ball(box(YIp,RIp),LBC,TAEp), TAE is (TAEp+1).
% Now ask for example:
% ?- last_ball(box(2,1), C, T).
% ===================================
% This problem is of course Edsger W. Dijkstra's "balls in the urn" problem, and
% there is a very easy way to deduce the color without exhautsive check of the move tree, as Prolog does in the above.
% See: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD07xx/EWD720.html
last_ball_ewd(box(YI, _), red) :- 0 is (YI mod 2).
last_ball_ewd(box(YI, _), yellow) :- 1 is (YI mod 2).
% We can test this by trying to find a counterexample of the result of last_ball_ewsd for the other color via '\+'
othercolor(red,yellow).
othercolor(yellow,red).
verify(box(YI, RI)) :- last_ball_ewd(box(YI, RI), LBC), othercolor(LBC,LBCO), \+last_ball(box(YI, RI), LBCO, _).
% Now ask for example:
% ?- verify(box(2, 1))

How to find the intersection of two NFA

In DFA we can do the intersection of two automata by doing the cross product of the states of the two automata and accepting those states that are accepting in both the initial automata.
Union is performed similarly. How ever although i can do union in NFA easily using epsilon transition how do i do their intersection?
You can use the cross-product construction on NFAs just as you would DFAs. The only changes are how you'd handle ε-transitions. Specifically, for each state (qi, rj) in the cross-product automaton, you add an ε-transition from that state to each pair of states (qk, rj) where there's an ε-transition in the first machine from qi to qk and to each pair of states (qi, rk) where there's an ε-transition in the second machine from rj to rk.
Alternatively, you can always convert the NFAs into DFAs and then compute the cross product of those DFAs.
Hope this helps!
We can also use De Morgan's Laws: A intersection B = (A' U B')'
Taking the union of the compliments of the two NFA's is comparatively simpler, especially if you are used to the epsilon method of union.
There is a huge mistake in templatetypedef's answer.
The product automaton of L1 and L2 which are NFAs :
New states Q = product of the states of L1 and L2.
Now the transition function:
a is a symbol in the union of both automatons' alphabets
delta( (q1,q2) , a) = delta_L1(q1 , a) X delta_L2(q2 , a)
which means you should multiply the set that is the result of delta_L1(q1 , a) with the set that results from delta_L2(q1 , a).
The problem in the templatetypedef's answer is that the product result (qk ,rk) is not mentioned.
Probably a late answer, but since I had the similar problem today I felt like sharing it. Realise the meaning of intersection first. Here, it means that given the string e, e should be accepted by both automata.
Consider the folowing automata:
m1 accepting the language {w | w contains '11' as a substring}
m2 accepting the language {w | w contains '00' as a substring}
Intuitively, m = m1 ∩ m2 is the automaton accepting the strings containing both '11' and '00' as substrings. The idea is to simulate both automata simultaneously.
Let's now formally define the intersection.
m = (Q, Σ, Δ, q0, F)
Let's start by defining the states for m; this is, as mentioned above the Cartesian product of the states in m1 and m2. So, if we have a1, a2 as labels for the states in m1, and b1, b2 the states in m2, Q will consist of following states: a1b1, a2b1, a1b2, a2b2. The idea behind this product construction is to keep track of where we are in both m1 and m2.
Σ most likely remains the same, however in some cases they differ and we just take the union of alphabets in m1 and m2.
q0 is now the state in Q containing both the start state of m1 and the start state of m2. (a1b1, to give an example.)
F contains state s IF and only IF both states mentioned in s are accept states of m1, m2 respectively.
Last but not least, Δ; we define delta again in terms of the Cartesian product, as follows: Δ(a1b1, E) = Δ(m1)(a1, E) x Δ(m2)(b1, E), as also mentioned in one of the answers above (if I am not mistaken). The intuitive idea behind this construction for Δ is just to tear a1b1 apart and consider the states a1 and b1 in their original automaton. Now we 'iterate' each possible edge, let's pick E for example, and see where it brings us in the original automaton. After that, we glue these results together using the Cartesian product. If (a1, E) is present in m1 but not Δ(b1, E) in m2, then the edge will not exist in m; otherwise we'll have some kind of a union construction.
An alternative to constructing the product automaton is allowing more complicated acceptance criteria. Ordinarily, an NFA accepts an input string when it has reached any one of a set of accepting final states. That can be extended to boolean combinations of states. Specifically, you construct the automaton for the intersection like you do for the union, but consider the resulting automaton to accept an input string only when it is in (what corresponds to) accepting final states in both automata.

Some explanation about 8 queen solution that use permutation

I am studyin Prolog for an universitary exam on SWI Prolog implementation.
I have some doubt about how work this version of 8 Queen problem that solve the problem using the permutations:
solution(Queens) :-
permutation([1,2,3,4,5,6,7,8], Queens),
safe(Queens).
permutation([],[]).
permutation([Head|Tail],PermList) :-
permutation(Tail,PermTail),
del(Head,PermList,PermTail).
del(Item,[Item|List],List).
del(Item,[First|List],[First|List1]) :-
del(Item,List,List1).
safe([]).
safe([Queen|Others]) :-
safe(Others),
noattack(Queen,Others,1).
noattack(_,[],_).
noattack(Y,[Y1|Ylist],Xdist) :-
Y1-Y=\=Xdist,
Y-Y1=\=Xdist,
Dist1 is Xdist + 1,
noattack(Y,Ylist,Dist1).
In a previous resolution I used this solution template: [1\Y1, 2\Y2, 3\Y3, 4\Y4, 5\Y5, 6\Y6, 7\Y7, 8\Y8] that simply say that every queen have to be on different rows the X calue could be constrained.
This version of the program it's pretty difference because we can observe that to prevent vertical attack all the queens have to be on different rows, so I have a queen for each row.
So without losing information so I can say that the solution will be a permutation of the list: [1,2,3,4,5,6,7,8] in wich every element rappresent the Y coordinate of a queen.
so in this case I am rappresenting a queen position only by its Y coordinate (its row index)
So I have the solution relation than say that a list Queens is a solution if Queens is a premutation of [1,2,3,4,5,6,7,8] original list and if this permutation is safe (every queen in this permutation list don't attack the others queen).
Ok, this is clear...now it is defined the safe relation.
There is a base case that say that if the list is empty then this is safe because there is no attack.
And there is a general case in wich the list is not empty. If the list is not empty I can divide it in [Queen|Others] where Queen is the first queen in the list and Others is the remaining sublist...so the original list [Queen|Others] is safe if the sublist Others it is itself a solution (if it is safe, if there is no attack in Others) and if the first item Queen do not attack the other queen in others sublist...
Ok...until now I think that it is clear for me...
Now I have some problems with the definition of noattack relation !!!
The problem is that I rappresent a queen position only by its Y coordinate and the X coordinate it is
not explicity present.
I have understand that for circumvent this limitation of the rappresentation there is the folowing generalizzation (I hope to have understood well...I am not so sure...): ** I know also that there must be a queen on each column of the board, so the X distance from the first queen in the list (Queen) and the sublist Others must be 1**
Where the distance from the first item Queen and the sublist Others is the X distance from the first item Queen and the queen nearest to it, is it righ my reasoning?
So the noattack relation is TRUE if the queen are on different columns (always true for construction) and I can express that have to be on different row sayng that the X distance from Queen and the nearest queen in the Others sublist is 1.
But, if my reasoning is truem I am finding many trouble trying to understand how rappresent this thing in this part of code:
noattack(Y,[Y1|Ylist],Xdist) :-
Y1-Y=\=Xdist,
Y-Y1=\=Xdist,
Dist1 is Xdist + 1,
noattack(Y,Ylist,Dist1).
i think your problem is only these 2 lines:
Y-Y1=/=Xdist,
Y1-Y=/=Xdist,
it checks whether the queen which has Y, attacks the queen in the row with a distance of Xdist in a diagonal way or not. (|Y - Y1| = Xdist --> diagonal attack ).
the other way to attack other queens is to qttack them in the same row, that doesn't happen simply because the solution is a permutation of [1,2,3,4,5,6,7,8]. so something like [1,1,3,4,5,6,7,8] never happens and thats enough to check diagonals.
I hope it solved the problem.
p.s. note that Y1 is the Y coordinate of the head of the Others from the rule Safe/1. so it is a queen with a Xdist of 1 at first, then it backtracks to other rows.
clarification
we are talking about noattack/3. you give it three arguments:
first: Y coordinate of current queen,
second: a right-most list [Y1| Ylist] the begins somewhere after where Y is in the list, and continues to the end of the primary list.(yes this is a sublist of the solution), and
third: Xdist is the index-distance between current queen (which has Y) and the queen which is gonna be checked with the current queen (which has Y1 and is the head of second argument).
third argument is necessary because without it we can not check diagonal interaction between the current queen and and the queen which has Y1. it is really basic mathematics, you have 2 points in the coordinate system with only natural numbers. lets say these 2 points attack each other in a diagonal way if and only-if abs(x1 - x2) = abs(y1 - y2).
Example #1 . and if you understood my explanations well, check it as Accepted.
P1 = (3, 4) and P2 = (1, 2) --> these points have a diagonal attack because abs(3-1) = abs(4-2) = 2
Example #2
P1 = (3, 4) and P2 = (7, 0) --> these points have a diagonal attack because abs(3-7) = abs(4-0) = 4
this is why we check both Y1-Y =\= Xdist and Y-Y1 =\= Xdist . because whenever there is a diagonal attack at least one of them is gonna be true. when non of them was true, it means that there is no diagonal attack between the queen with Y and the queen with Y1. so we can proceed to checking the next queen which is the head of Ylist.
I hope that's enough. this is a very easy problem, Just read it carefully and if you couldn't understand again try to trace the algorithm on a piece of paper yourself. that's a way always works.
I have the same confusion when solving the same problem in recursion using C. There are two possible directions for diagonal attacks. Number each square with x,y coordinates with top-left being 0,0. You will then see that these two attacking diagonal detection or calculation satisfies the conditions:
x1 - y1 == x2 - y2
x1 + y1 == x2 + y2
I annotated the comparison above in C equality notation and not Prolog notation. Between two points (x1,y1) and (x2,y2), there is queen diagonal attack if one of conditions above is meet.

Resources