Prolog discarding permutation - prolog

I haven't been able to discard repetition permutations.For example, I want to get
?-permutation([1,2,1],L).
L = [1, 2, 1] ;
L = [1, 1, 2] ;
L = [2, 1, 1] ;
.
but i am getting
?-permutation([1,2,1],L).
L = [1, 2, 1] ;
L = [1, 1, 2] ;
L = [2, 1, 1] ;
L = [2, 1, 1] ;
L = [1, 1, 2] ;
L = [1, 2, 1] ;
.

I think the simpler way could be
perm_no_rep(L, P) :-
setof(P, permutation(L, P), Ps),
member(P, Ps).
it's almost ok:
?- perm_no_rep([1,2,1],P).
P = [1, 1, 2] ;
P = [1, 2, 1] ;
P = [2, 1, 1].
beware that will be factorial(Num_Distinct_Elements_in_L) = length(Ps) i.e. possibly very large lists.

Related

Map Coloring Prolog

I wrote this code to solve the map coloring problem using prolog:
coloring([A,B,C,D,E,F]):-
maplist(=\=(A),[B,C,D,E]),
maplist(=\=(B),[C,D,F]),
C=\=D,
maplist(=\=(D),[E,F]),
E=\=F.
I try to get solutions using the query:
?- coloring(Cs), Cs ins [1,2,3,4], Cs = [1|_].
but I always get the error "Arguments not sufficiently instantiated", how to fix this?
Use #\=/2 instead of =\=/2, and fix the domain in ins/2:
:- use_module(library(clpfd)).
coloring([A,B,C,D,E,F]):-
maplist(#\=(A),[B,C,D,E]),
maplist(#\=(B),[C,D,F]),
C#\=D,
maplist(#\=(D),[E,F]),
E#\=F.
Sample runs:
?- coloring(Cs), Cs ins 1..4, Cs=[1|_].
Cs = [1, _A, _B, _C, _D, _E],
_A in 2..4,
_A#\=_E,
_A#\=_C,
_A#\=_B,
_B in 2..4,
_B#\=_C,
_C in 2..4,
_C#\=_E,
_C#\=_D,
_D in 2..4,
_D#\=_E,
_E in 1..4.
with labeling:
?- coloring(Cs), Cs ins 1..4, Cs=[1|_], label(Cs).
Cs = [1, 2, 3, 4, 2, 1] ;
Cs = [1, 2, 3, 4, 2, 3] ;
Cs = [1, 2, 3, 4, 3, 1] ;
Cs = [1, 2, 4, 3, 2, 1] ;
Cs = [1, 2, 4, 3, 2, 4] ;
...

Combinations in all orders

I want to generate all pairs of all the numbers of a range 1..N in all orders i.e. [X,Y] and [Y,X]
I found these here : http://kti.ms.mff.cuni.cz/~bartak/prolog/combinatorics.html
comb(0,_,[]).
comb(N,[X|T],[X|Comb]):-N>0,N1 is N-1,comb(N1,T,Comb).
comb(N,[_|T],Comb):-N>0,comb(N,T,Comb).
comb_rep(0,_,[]).
comb_rep(N,[X|T],[X|RComb]):-N>0,N1 is N-1,comb_rep(N1,[X|T],RComb).
comb_rep(N,[_|T],RComb):-N>0,comb_rep(N,T,RComb).
?- findall(E1,comb_rep(2,[1,2,3],E1),C1),findall(E2,comb(2,[3,2,1],E2),C2),append(C1,C2,C),write(C).
[[1,1],[1,2],[1,3],[2,2],[2,3],[3,3],[3,2],[3,1],[2,1]]
There have to be an easier way ?
Also i would prefer to do it as GENERATOR, rather than a FULLY rendered list (like the ex above)
I suspect I am missing something because this does so much less than the code you posted, but this seems to create the pairs how you ask:
limit_pairs(N, [X, Y]) :-
between(1, N, X),
between(1, N, Y).
e.g.
?- limit_pairs(3, P).
P = [1, 1]
P = [1, 2]
P = [1, 3]
P = [2, 1]
P = [2, 2]
P = [2, 3]
P = [3, 1]
P = [3, 2]
P = [3, 3]

Write a program in prolog that determines if there are exactly three values in such a way that they add upto a sum of N

Eg:
List[1,2,3,4,5,6] with N equal to 6 should print true because there are exactly 3 values that add upto 6. 1+2+3.
List[2,5,7,9] with N equal to 12 should print false as there are no 3 elements that add upto 12.
Let's maybe start with a more general predicate that describes the relation between a list, a sublist of said list and the sum of numbers in the sublist. To make it obvious which argument is what, it is opportune to chose a descriptive name for the predicate, say sum_ofsub_fromlist/3. Now let's observe that if the first argument is the sum of the numbers in the sublist, then successively subtracting those numbers from the sum yields zero, e.g.: X=A+B → X-A-B=0. So there will be a base case that contains 0 as the sum and [] as the sublist (rule 1) and a recursive rule that subtracts the elements of the sublist from the sum (rule 2). And since a sublist does not contain all elements of the list it's taken from in general, there will be a recursive rule for skipping elements of the list that do not occur in the sublist (rule 3). This rule is only needed as long as there are still elements in the sublist, so a constraint would be beneficial, that prevents this rule from succeeding once the sublist is empty. These ideas can be realized in Prolog like so:
sum_ofsub_fromlist(0,[],_L). % rule 1
sum_ofsub_fromlist(X,[A|Bs],[A|As]) :- % rule 2
X0 is X-A,
sum_ofsub_fromlist(X0,Bs,As).
sum_ofsub_fromlist(X,Bs,[_A|As]) :- % rule 3
dif(Bs,[]), % constraint: sublist not empty
sum_ofsub_fromlist(X,Bs,As).
You can query this predicate to assure yourself that it delivers all sublists for the given sum in your examples:
?- sum_ofsub_fromlist(6,S,[1,2,3,4,5,6]).
S = [1, 2, 3] ;
S = [1, 5] ;
S = [2, 4] ;
S = [6] ;
false.
?- sum_ofsub_fromlist(12,S,[2,5,7,9]).
S = [5, 7] ;
false.
Building on this you can then write a calling predicate that only succeeds for sublists of length three:
sum_oftriple_fromlist(S,T,L) :-
T=[_,_,_], % T has to be a triple
sum_ofsub_fromlist(S,T,L).
This predicate yields the answers you desire:
?- sum_oftriple_fromlist(6,T,[1,2,3,4,5,6]).
T = [1, 2, 3] ;
false.
?- sum_oftriple_fromlist(12,T,[2,5,7,9]).
false.
Note that the predicate is also working with negative numbers:
?- sum_oftriple_fromlist(6,T,[-5,-3,-1,2,4,7,8,9]).
T = [-5, 2, 9] ;
T = [-5, 4, 7] ;
T = [-3, 2, 7] ;
false.
?- sum_oftriple_fromlist(-6,T,[-6,-5,-4,-3,-2,-1,2,4]).
T = [-6, -4, 4] ;
T = [-6, -2, 2] ;
T = [-5, -3, 2] ;
T = [-3, -2, -1] ;
false.
However, due to the use of is/2, the predicate only works if the first and the third arguments are ground:
?- sum_oftriple_fromlist(S,T,[1,2,3,4,5,6]).
ERROR: is/2: Arguments are not sufficiently instantiated
Exception: (7) sum_ofsub_fromlist(_G918, [_G1016, _G1019, _G1022], [1, 2, 3, 4, 5, 6]) ?
?- sum_oftriple_fromlist(6,T,[A,B,C,D,E,F]).
ERROR: is/2: Arguments are not sufficiently instantiated
Exception: (7) sum_ofsub_fromlist(6, [_G2121, _G2124, _G2127], [_G1945, _G1948, _G1951, _G1954, _G1957, _G1960]) ?
If that's fine with you, you can stop here. Alternatively, you could opt to make the predicate more versatile by using CLP(FD). Just apply these minor changes to your code:
:- use_module(library(clpfd)). % <- new
sum_oftriple_fromlist(S,T,L) :-
T=[_,_,_],
sum_ofsub_fromlist(S,T,L).
sum_ofsub_fromlist(0,[],_L).
sum_ofsub_fromlist(X,[A|Bs],[A|As]) :-
X0 #= X-A, % <- change
sum_ofsub_fromlist(X0,Bs,As).
sum_ofsub_fromlist(X,Bs,[_A|As]) :-
dif(Bs,[]),
sum_ofsub_fromlist(X,Bs,As).
Now the above queries deliver answers:
?- sum_oftriple_fromlist(S,T,[1,2,3,4,5,6]).
S = 6,
T = [1, 2, 3] ;
S = 7,
T = [1, 2, 4] ;
S = 8,
T = [1, 2, 5] ;
. % another
. % seventeen
. % results here
The second query, however, yields residual goals (see documentation for details) as results:
?- sum_oftriple_fromlist(6,T,[A,B,C,D,E,F]).
T = [A, B, C],
_G2424+A#=6,
C+B#=_G2424 ;
T = [A, B, D],
_G2424+A#=6,
D+B#=_G2424 ;
.
.
.
To get actual numbers, you have to restrict the range of the numbers and subsequently label the variables in the list:
?- L=[A,B,C,D,E,F], sum_oftriple_fromlist(6,T,L), L ins 1..6, label(L).
L = [1, 1, 4, 1, 1, 1],
A = B, B = D, D = E, E = F, F = 1,
C = 4,
T = [1, 1, 4] ;
L = [1, 1, 4, 1, 1, 2],
A = B, B = D, D = E, E = 1,
C = 4,
F = 2,
T = [1, 1, 4] ;
.
.
.
Possibly you are only interested in lists where every number only appears once:
?- L=[A,B,C,D,E,F], all_distinct(L), sum_oftriple_fromlist(6,T,L), L ins 1..6, label(L).
L = [1, 2, 3, 4, 5, 6],
A = 1,
B = 2,
C = 3,
D = 4,
E = 5,
F = 6,
T = [1, 2, 3] ;
L = [1, 2, 3, 4, 6, 5],
A = 1,
B = 2,
C = 3,
D = 4,
E = 6,
F = 5,
T = [1, 2, 3] ;
.
.
.
Or maybe you don't even want to restrict the sum:
?- L=[A,B,C,D,E,F], all_distinct(L), sum_oftriple_fromlist(S,T,L), L ins 1..6, label(L).
L = [1, 2, 3, 4, 5, 6],
A = 1,
B = 2,
C = 3,
D = 4,
E = 5,
F = S, S = 6, % sum = 6
T = [1, 2, 3] ;
.
.
.
L = [1, 2, 4, 3, 5, 6],
A = 1,
B = 2,
C = 4,
D = 3,
E = 5,
F = 6,
S = 7, % sum = 7
T = [1, 2, 4] ;
.
.
.
As you can see the CLP(FD) version of the predicate resembles a true relation as opposed to the non-CLP(FD) version. And of course your example queries yield the same answers with both versions.
Your code only considers the first 3 items in the list, and not any other combinations.
The most natural way to structure a solution involving a list is to base your recursion on the structure of the list. So:
If the first element of the list (say, X) is to be included in the 3 values that sum to N, we need to find a way to find 2 values in the rest of the list that sum to N-X.
If it isn't, just try to solve the problem using the rest of the list.
Note that you may need a "helper" version of your predicate that allows you to add other parameters. In this case, knowing how many values you need to add up would be helpful.

All possible knight positions in n moves - infinite loop in prolog

I have a problem with backtracking in Prolog when calculation solution for all possible knight positions in n moves with knowing the exact path.
My solution print some of the first results and then never terminate while looking for impossible results.
This is my code:
move([X, Y], [A, B]) :- X + 1 < 8, Y + 2 < 8, A is X + 1, B is Y + 2.
move([X, Y], [A, B]) :- X + 2 < 8, Y + 1 < 8, A is X + 2, B is Y + 1.
move([X, Y], [A, B]) :- X + 2 < 8, Y - 1 >= 0, A is X + 2, B is Y - 1.
move([X, Y], [A, B]) :- X + 1 < 8, Y - 2 >= 0, A is X + 1, B is Y - 2.
move([X, Y], [A, B]) :- X - 1 >= 0, Y - 2 >= 0, A is X - 1, B is Y - 2.
move([X, Y], [A, B]) :- X - 2 >= 0, Y - 1 >= 0, A is X - 2, B is Y - 1.
move([X, Y], [A, B]) :- X - 2 >= 0, Y + 1 < 8, A is X - 2, B is Y + 1.
move([X, Y], [A, B]) :- X - 1 >= 0, Y + 2 < 8, A is X - 1, B is Y + 2.
knight_move(X,Y,[X,Y],1) :- move(X,Y).
knight_move(X,Y,[X|P],C) :- move(X,Z), knight_move(Z,Y,P,Cn), C is Cn+1.
predict_moves(X,C,P) :- knight_move(X,_,P,C).
Sample call:
predict_moves([1,1],3,P).
In result I expect all possible paths in n 3 moves. Can sb help me with adding condition to my code to stop my code from backtracking to move and looping to infinity?
Before actually removing the problem, let's narrow down the source of non-termination. In your case, it is particularly tricky, for you get answers that are nice and correct. Only then there is a problem. The easiest way to narrow down the problem is by adding false goals into your program. If the resulting program still loops, we can continue adding further such goals. Here is what I came up with:
move([X, Y], [A, B]) :- X+1 < 8, Y+2 < 8, A is X+1, B is Y+2.
move([X, Y], [A, B]) :- false, X+2 < 8, Y+1 < 8, A is X+2, B is Y+1.
move([X, Y], [A, B]) :- false, X+2 < 8, Y-1 >= 0, A is X+2, B is Y-1.
move([X, Y], [A, B]) :- false, X+1 < 8, Y-2 >= 0, A is X+1, B is Y-2.
move([X, Y], [A, B]) :- X-1 >= 0, Y-2 >= 0, A is X-1, B is Y-2.
move([X, Y], [A, B]) :- false, X-2 >= 0, Y-1 >= 0, A is X-2, B is Y-1.
move([X, Y], [A, B]) :- false, X-2 >= 0, Y+1 < 8, A is X-2, B is Y+1.
move([X, Y], [A, B]) :- false, X-1 >= 0, Y+2 < 8, A is X-1, B is Y+2.
knight_move(X,Y,[X,Y],1) :- false, move(X,Y).
knight_move(X,Y,[X|P],C) :- move(X,Z), knight_move(Z,Y,P,Cn), false, C is Cn+1.
predict_moves(X,C,P) :- knight_move(X,_,P,C), false.
?- predict_moves([1,1],3,P), false.
All parts that are now striked through have no influence at all to termination. That might be a little irritating at first, for that code is actually executed, but still: no influence on termination. Note that in particular the variable C in knight_move/4 is now a singleton!
You need to modify the remaining visible part to remove the error.
If you use CLP(FD) for reasoning over integers, and change the order of your constraints so that you constrain the counter before you recurse, that will eliminate your looping issue:
move([X, Y], [A, B]) :- X + 1 #< 8, Y + 2 #< 8, A #= X + 1, B #= Y + 2.
move([X, Y], [A, B]) :- X + 2 #< 8, Y + 1 #< 8, A #= X + 2, B #= Y + 1.
move([X, Y], [A, B]) :- X + 2 #< 8, Y - 1 #>= 0, A #= X + 2, B #= Y - 1.
move([X, Y], [A, B]) :- X + 1 #< 8, Y - 2 #>= 0, A #= X + 1, B #= Y - 2.
move([X, Y], [A, B]) :- X - 1 #>= 0, Y - 2 #>= 0, A #= X - 1, B #= Y - 2.
move([X, Y], [A, B]) :- X - 2 #>= 0, Y - 1 #>= 0, A #= X - 2, B #= Y - 1.
move([X, Y], [A, B]) :- X - 2 #>= 0, Y + 1 #< 8, A #= X - 2, B #= Y + 1.
move([X, Y], [A, B]) :- X - 1 #>= 0, Y + 2 #< 8, A #= X - 1, B #= Y + 2.
knight_move(X,Y,[X,Y], 1) :- move(X,Y).
# NOTE the constraint of C #= Cn + 1 before the recursive call
knight_move(X,Y,[X|P], C) :- C #> 1, move(X,Z), C #= Cn + 1, knight_move(Z,Y,P,Cn).
predict_moves(X,C,P) :- knight_move(X,_,P,C).
Which results in:
| ?- predict_moves([1,1], 3, P).
P = [[1,1],[2,3],[3,5],[4,7]] ? a
P = [[1,1],[2,3],[3,5],[5,6]]
P = [[1,1],[2,3],[3,5],[5,4]]
P = [[1,1],[2,3],[3,5],[4,3]]
P = [[1,1],[2,3],[3,5],[2,3]]
P = [[1,1],[2,3],[3,5],[1,4]]
P = [[1,1],[2,3],[3,5],[1,6]]
P = [[1,1],[2,3],[3,5],[2,7]]
P = [[1,1],[2,3],[4,4],[5,6]]
P = [[1,1],[2,3],[4,4],[6,5]]
P = [[1,1],[2,3],[4,4],[6,3]]
P = [[1,1],[2,3],[4,4],[5,2]]
P = [[1,1],[2,3],[4,4],[3,2]]
P = [[1,1],[2,3],[4,4],[2,3]]
P = [[1,1],[2,3],[4,4],[2,5]]
P = [[1,1],[2,3],[4,4],[3,6]]
P = [[1,1],[2,3],[4,2],[5,4]]
P = [[1,1],[2,3],[4,2],[6,3]]
P = [[1,1],[2,3],[4,2],[6,1]]
P = [[1,1],[2,3],[4,2],[5,0]]
P = [[1,1],[2,3],[4,2],[3,0]]
P = [[1,1],[2,3],[4,2],[2,1]]
P = [[1,1],[2,3],[4,2],[2,3]]
P = [[1,1],[2,3],[4,2],[3,4]]
P = [[1,1],[2,3],[3,1],[4,3]]
P = [[1,1],[2,3],[3,1],[5,2]]
P = [[1,1],[2,3],[3,1],[5,0]]
P = [[1,1],[2,3],[3,1],[1,0]]
P = [[1,1],[2,3],[3,1],[1,2]]
P = [[1,1],[2,3],[3,1],[2,3]]
P = [[1,1],[2,3],[1,1],[2,3]]
P = [[1,1],[2,3],[1,1],[3,2]]
P = [[1,1],[2,3],[1,1],[3,0]]
P = [[1,1],[2,3],[1,1],[0,3]]
P = [[1,1],[2,3],[0,2],[1,4]]
P = [[1,1],[2,3],[0,2],[2,3]]
P = [[1,1],[2,3],[0,2],[2,1]]
P = [[1,1],[2,3],[0,2],[1,0]]
P = [[1,1],[2,3],[0,4],[1,6]]
P = [[1,1],[2,3],[0,4],[2,5]]
P = [[1,1],[2,3],[0,4],[2,3]]
P = [[1,1],[2,3],[0,4],[1,2]]
P = [[1,1],[2,3],[1,5],[2,7]]
P = [[1,1],[2,3],[1,5],[3,6]]
P = [[1,1],[2,3],[1,5],[3,4]]
P = [[1,1],[2,3],[1,5],[2,3]]
P = [[1,1],[2,3],[1,5],[0,3]]
P = [[1,1],[2,3],[1,5],[0,7]]
P = [[1,1],[3,2],[4,4],[5,6]]
P = [[1,1],[3,2],[4,4],[6,5]]
P = [[1,1],[3,2],[4,4],[6,3]]
P = [[1,1],[3,2],[4,4],[5,2]]
P = [[1,1],[3,2],[4,4],[3,2]]
P = [[1,1],[3,2],[4,4],[2,3]]
P = [[1,1],[3,2],[4,4],[2,5]]
P = [[1,1],[3,2],[4,4],[3,6]]
P = [[1,1],[3,2],[5,3],[6,5]]
P = [[1,1],[3,2],[5,3],[7,4]]
P = [[1,1],[3,2],[5,3],[7,2]]
P = [[1,1],[3,2],[5,3],[6,1]]
P = [[1,1],[3,2],[5,3],[4,1]]
P = [[1,1],[3,2],[5,3],[3,2]]
P = [[1,1],[3,2],[5,3],[3,4]]
P = [[1,1],[3,2],[5,3],[4,5]]
P = [[1,1],[3,2],[5,1],[6,3]]
P = [[1,1],[3,2],[5,1],[7,2]]
P = [[1,1],[3,2],[5,1],[7,0]]
P = [[1,1],[3,2],[5,1],[3,0]]
P = [[1,1],[3,2],[5,1],[3,2]]
P = [[1,1],[3,2],[5,1],[4,3]]
P = [[1,1],[3,2],[4,0],[5,2]]
P = [[1,1],[3,2],[4,0],[6,1]]
P = [[1,1],[3,2],[4,0],[2,1]]
P = [[1,1],[3,2],[4,0],[3,2]]
P = [[1,1],[3,2],[2,0],[3,2]]
P = [[1,1],[3,2],[2,0],[4,1]]
P = [[1,1],[3,2],[2,0],[0,1]]
P = [[1,1],[3,2],[2,0],[1,2]]
P = [[1,1],[3,2],[1,1],[2,3]]
P = [[1,1],[3,2],[1,1],[3,2]]
P = [[1,1],[3,2],[1,1],[3,0]]
P = [[1,1],[3,2],[1,1],[0,3]]
P = [[1,1],[3,2],[1,3],[2,5]]
P = [[1,1],[3,2],[1,3],[3,4]]
P = [[1,1],[3,2],[1,3],[3,2]]
P = [[1,1],[3,2],[1,3],[2,1]]
P = [[1,1],[3,2],[1,3],[0,1]]
P = [[1,1],[3,2],[1,3],[0,5]]
P = [[1,1],[3,2],[2,4],[3,6]]
P = [[1,1],[3,2],[2,4],[4,5]]
P = [[1,1],[3,2],[2,4],[4,3]]
P = [[1,1],[3,2],[2,4],[3,2]]
P = [[1,1],[3,2],[2,4],[1,2]]
P = [[1,1],[3,2],[2,4],[0,3]]
P = [[1,1],[3,2],[2,4],[0,5]]
P = [[1,1],[3,2],[2,4],[1,6]]
P = [[1,1],[3,0],[4,2],[5,4]]
P = [[1,1],[3,0],[4,2],[6,3]]
P = [[1,1],[3,0],[4,2],[6,1]]
P = [[1,1],[3,0],[4,2],[5,0]]
P = [[1,1],[3,0],[4,2],[3,0]]
P = [[1,1],[3,0],[4,2],[2,1]]
P = [[1,1],[3,0],[4,2],[2,3]]
P = [[1,1],[3,0],[4,2],[3,4]]
P = [[1,1],[3,0],[5,1],[6,3]]
P = [[1,1],[3,0],[5,1],[7,2]]
P = [[1,1],[3,0],[5,1],[7,0]]
P = [[1,1],[3,0],[5,1],[3,0]]
P = [[1,1],[3,0],[5,1],[3,2]]
P = [[1,1],[3,0],[5,1],[4,3]]
P = [[1,1],[3,0],[1,1],[2,3]]
P = [[1,1],[3,0],[1,1],[3,2]]
P = [[1,1],[3,0],[1,1],[3,0]]
P = [[1,1],[3,0],[1,1],[0,3]]
P = [[1,1],[3,0],[2,2],[3,4]]
P = [[1,1],[3,0],[2,2],[4,3]]
P = [[1,1],[3,0],[2,2],[4,1]]
P = [[1,1],[3,0],[2,2],[3,0]]
P = [[1,1],[3,0],[2,2],[1,0]]
P = [[1,1],[3,0],[2,2],[0,1]]
P = [[1,1],[3,0],[2,2],[0,3]]
P = [[1,1],[3,0],[2,2],[1,4]]
P = [[1,1],[0,3],[1,5],[2,7]]
P = [[1,1],[0,3],[1,5],[3,6]]
P = [[1,1],[0,3],[1,5],[3,4]]
P = [[1,1],[0,3],[1,5],[2,3]]
P = [[1,1],[0,3],[1,5],[0,3]]
P = [[1,1],[0,3],[1,5],[0,7]]
P = [[1,1],[0,3],[2,4],[3,6]]
P = [[1,1],[0,3],[2,4],[4,5]]
P = [[1,1],[0,3],[2,4],[4,3]]
P = [[1,1],[0,3],[2,4],[3,2]]
P = [[1,1],[0,3],[2,4],[1,2]]
P = [[1,1],[0,3],[2,4],[0,3]]
P = [[1,1],[0,3],[2,4],[0,5]]
P = [[1,1],[0,3],[2,4],[1,6]]
P = [[1,1],[0,3],[2,2],[3,4]]
P = [[1,1],[0,3],[2,2],[4,3]]
P = [[1,1],[0,3],[2,2],[4,1]]
P = [[1,1],[0,3],[2,2],[3,0]]
P = [[1,1],[0,3],[2,2],[1,0]]
P = [[1,1],[0,3],[2,2],[0,1]]
P = [[1,1],[0,3],[2,2],[0,3]]
P = [[1,1],[0,3],[2,2],[1,4]]
P = [[1,1],[0,3],[1,1],[2,3]]
P = [[1,1],[0,3],[1,1],[3,2]]
P = [[1,1],[0,3],[1,1],[3,0]]
P = [[1,1],[0,3],[1,1],[0,3]]
(3 ms) no
| ?-
The problem: you write:
knight_move(X,Y,[X|P],C) :- move(X,Z), knight_move(Z,Y,P,Cn), C is Cn+1.
there is no cut nor any other mechanism that prevents you from taking this branch, so Prolog can keep taking this branch. Furthermore you should decrement the counter Cn is C-1 and do this before the recursive call.
First of all, I think it is better to construct some sort of validation predicate instead of writing all these bounds checks:
valid_position(X,Y) :-
X >= 0,
Y >= 0,
X < 8,
Y < 8.
We can also construct a predicate plusneg/3 such that for posneg(X,DX,Y), Y is both X+DX and X-DX:
posneg(X,DX,Y) :-
Y is X+DX.
posneg(X,DX,Y) :-
Y is X-DX.
then we can describe the "possible moves" of the knight:
possible(X, Y, A, B) :-
posneg(X,2,A),
posneg(Y,1,B).
possible(X, Y, A, B) :-
posneg(X,1,A),
posneg(Y,2,B).
but these are not per se "valid moves", since we need to check if the new coordinate is valid. So we can write:
move([X,Y], [A,B]) :-
possible(X,Y,A,B),
valid_position(A,B).
although this introduces some additiona predicates, and is perhaps a litte less efficient, it is now clear that all the moves are valid ones.
Now for the knigt_move/4 with the counter, we can write a clause that says that if the counter has dropped below zero, no more moves are done:
knight_move(P1,P1,[P1],C) :-
C < 1.
In case the count is one or more, the knight can still do a move, so we can write it as:
knight_move(P1,PZ,[P1|PT],C) :-
C >= 1,
C1 is C-1,
move(P1,P2),
knight_move(P2,PZ,PT,C1).
Or putting this all together:
valid_position(X,Y) :-
X >= 0,
Y >= 0,
X < 8,
Y < 8.
posneg(X,DX,Y) :-
Y is X+DX.
posneg(X,DX,Y) :-
Y is X-DX.
possible(X, Y, A, B) :-
posneg(X,2,A),
posneg(Y,1,B).
possible(X, Y, A, B) :-
posneg(X,1,A),
posneg(Y,2,B).
move([X,Y], [A,B]) :-
possible(X,Y,A,B),
valid_position(A,B).
knight_move(P1,P1,[P1],C) :-
C < 1.
knight_move(P1,PZ,[P1|PT],C) :-
C >= 1,
C1 is C-1,
move(P1,P2),
knight_move(P2,PZ,PT,C1).
If we ask what fields we reach with exactly two moves (and how), we got:
?- knight_move([1,1],End,Path,2).
End = [5, 3],
Path = [[1, 1], [3, 2], [5, 3]] ;
End = [5, 1],
Path = [[1, 1], [3, 2], [5, 1]] ;
End = [1, 3],
Path = [[1, 1], [3, 2], [1, 3]] ;
End = [1, 1],
Path = [[1, 1], [3, 2], [1, 1]] ;
End = [4, 4],
Path = [[1, 1], [3, 2], [4, 4]] ;
End = [4, 0],
Path = [[1, 1], [3, 2], [4, 0]] ;
End = [2, 4],
Path = [[1, 1], [3, 2], [2, 4]] ;
End = [2, 0],
Path = [[1, 1], [3, 2], [2, 0]] ;
End = [5, 1],
Path = [[1, 1], [3, 0], [5, 1]] ;
End = [1, 1],
Path = [[1, 1], [3, 0], [1, 1]] ;
End = [4, 2],
Path = [[1, 1], [3, 0], [4, 2]] ;
End = [2, 2],
Path = [[1, 1], [3, 0], [2, 2]] ;
End = [4, 4],
Path = [[1, 1], [2, 3], [4, 4]] ;
End = [4, 2],
Path = [[1, 1], [2, 3], [4, 2]] ;
End = [0, 4],
Path = [[1, 1], [2, 3], [0, 4]] ;
End = [0, 2],
Path = [[1, 1], [2, 3], [0, 2]] ;
End = [3, 5],
Path = [[1, 1], [2, 3], [3, 5]] ;
End = [3, 1],
Path = [[1, 1], [2, 3], [3, 1]] ;
End = [1, 5],
Path = [[1, 1], [2, 3], [1, 5]] ;
End = [1, 1],
Path = [[1, 1], [2, 3], [1, 1]] ;
End = [2, 4],
Path = [[1, 1], [0, 3], [2, 4]] ;
End = [2, 2],
Path = [[1, 1], [0, 3], [2, 2]] ;
End = [1, 5],
Path = [[1, 1], [0, 3], [1, 5]] ;
End = [1, 1],
Path = [[1, 1], [0, 3], [1, 1]] ;
false.
So we can make 24 paths with exactly two moves. Note that there are duplicates, if we use setof/3 we can determine that we can reach 15 squares with two moves. For three moves there are 148 paths to reach 30 squares:
?- findall(End,knight_move([1,1],End,_,2),Ends), length(Ends,N).
Ends = [[5, 3], [5, 1], [1, 3], [1, 1], [4, 4], [4, 0], [2, 4], [2|...], [...|...]|...],
N = 24.
?- setof(End,Pa^knight_move([1,1],End,Pa,2),Ends), length(Ends,N).
Ends = [[0, 2], [0, 4], [1, 1], [1, 3], [1, 5], [2, 0], [2, 2], [2|...], [...|...]|...],
N = 15.
?- findall(End,knight_move([1,1],End,_,3),Ends), length(Ends,N).
Ends = [[7, 4], [7, 2], [3, 4], [3, 2], [6, 5], [6, 1], [4, 5], [4|...], [...|...]|...],
N = 148.
?- setof(End,Pa^knight_move([1,1],End,Pa,3),Ends), length(Ends,N).
Ends = [[0, 1], [0, 3], [0, 5], [0, 7], [1, 0], [1, 2], [1, 4], [1|...], [...|...]|...],
N = 30.

Get list of sets where the sum of each set is X

I'm trying to figure out how to generate a list of sets, where each set has a length of N and the sum of each set is X.
I found this code:
num_split(0,[]).
num_split(N, [X | List]):-
between(1,N,X),
plus(X,Y,N),
num_split(Y,List).
And I can use that to get a list of sets with sum X:
num_split(6,List),length(List,5).
List = [1, 1, 1, 1, 2] ;
List = [1, 1, 1, 2, 1] ;
List = [1, 1, 2, 1, 1] ;
List = [1, 2, 1, 1, 1] ;
List = [2, 1, 1, 1, 1] ;
false.
The problem is that those are all permutations, and I'm looking for combinations. The output I'm looking for should be something like get_combos(Sum,Length,List):
get_combos(6,2,List).
List = [5,1];
List = [4,2];
List = [3,3];
false.
Any pointers?
If you have access to a CLP(FD) library, you can use this code:
:- [library(clpfd)].
get_combos(Sum, Length, List) :-
length(List, Length),
List ins 1 .. Sum,
% all_distinct(List), not really useful here
sum(List, #=, Sum),
chain(List, #<),
label(List).
test:
?- get_combos(10,3,L).
L = [1, 2, 7] ;
L = [1, 3, 6] ;
L = [1, 4, 5] ;
L = [2, 3, 5] ;
Maybe I misunderstood your question. Use this chain
...
chain(List, #=<),
....
to get possible duplicates values:
?- get_combos(10,3,L).
L = [1, 1, 8] ;
L = [1, 2, 7] ;
L = [1, 3, 6] ;
L = [1, 4, 5] ;
L = [2, 2, 6] ;
L = [2, 3, 5] ;
L = [2, 4, 4] ;
L = [3, 3, 4] ;
false.
Enforce an "equal or greater" restriction between successive values in the array.
You can add it on as another predicate:
is_combination([]).
is_combination([_]).
is_combination([A,B|List]) :- A =< B, is_combination([B|List]).
get_combos(Sum, Length, List) :-
num_split(Sum, Length, List),
is_combination(List).
Unfortunately, tacking it on the end of the num_split/3 does not necessarily increase its performance, so adding it directly into the algorithm would be marginally better:
get_combos(_, 0, []).
get_combos(Sum, 1, [Sum]).
get_combos(Sum, Length, [A, B|List]) :-
between(1, Sum, A),
plus(A, NextSum, Sum),
plus(1, NextLength, Length),
get_combos(NextSum, NextLength, [B|List]),
A =< B.
I'm not sure just how much more performance this gets, as the comparison has to be after the recursion, due to the less-than-or-equals operator (=<) requiring both operands to be fully instantiated for it to work.

Resources