I want to predicate solve Kakuro.
My kakuro is this
I have to create a predicate using the word solve (solve/1) for the game kakuro. I tried 3-4 codes and all of them have errors. The variables have to be between 1-9(A value is between 1-9) and all the variables have to be different in one rows&columns(24=A+B+C not 24=A+A+C).
First code is:
:- use_module(library(clpfd)).
solve(L):-
L= [A,B,C,E,F,G,J,K,L,N,O,P],
all_different(L),
L ins 1..9,
A #= 24-B-C,
B #= 26-F-J-N,
C #= 15-G-K-O,
E #= 11-F-G,
E #= 17-A,
J #= 22-K-L,
N #= 14-O-P,
L #= 13-P.
Second code:
:- use_module(library(clpfd)).
sum_list([],0).
sum_list([Head|Tail], Sum):-sum_list(Tail, Sum1),Sum is Head+Sum1.
go:-
Vars=[A,B,C,E,F,G,J,K,L,N,O,P],
Vars ins 1..9,
word([A,B,C],24),
word([B,F,J,N],26),
word([C,G,K,O],15),
word([E,F,G],11),
word([A,E],17),
word([J,K,L],22),
word([N,O,P],14),
word([L,P],13),
labeling(Vars),
writeln(Vars).
word(L,Sum):-
sum_list(L,X),
X=:=Sum,
all_different(L).
Third code:
:- use_module(library(clpfd)).
kakuro(L):-
L = [A,B,C,E,F,G,J,K,L,N,O,P],
L ins 1..9,
Z1 = [A,B,C],
all_different(Z1),
A =:= 24-B-C,
Z2 = [B,F,J,N],
all_different(Z2),
B =:=26-F-J-N,
Z3 = [C,G,K,O],
all_different(Z3),
C =:= 15-G-K-O,
Z4 = [E,F,G],
all_different(Z4),
E =:= 11-F-G,
Z5 = [A,E],
all_different(Z5),
E =:= 17-A,
Z6 = [J,K,L],
all_different(Z6),
J =:=22-K-L,
A1 = [N,O,P],
all_different(A1),
N =:= 14-O-P,
A2 = [L,P],
all_different(A2),
L =:=13-P,
labeling([], L).
Also how do I start the solving process? Will it be like: ?-solve(L). ?? Also the L ins 1..9 is not working.
The following is a solution based on DuDa's, but using the sum/3 constraint and delaying labeling to the end. This makes for a more efficient and more declarative solution.
:- use_module(library(clpfd)).
kakuro(Vars):-
Vars=[A,B,C,E,F,G,J,K,L,N,O,P],
Vars ins 1..9,
word([A,B,C],24),
word([B,F,J,N],26),
word([C,G,K,O],15),
word([E,F,G],11),
word([A,E],17),
word([J,K,L],22),
word([N,O,P],14),
word([L,P],13),
labeling([],Vars).
word(L,Sum):-
all_different(L),
sum(L,#=,Sum).
At first before fixing the ins/labeling problem: there are issues with your code #1 and #3. In code #1 you state that all your variables need to have different values. This will not provide a valid solution. In code #1 and #3 you have used the variable L once as container for the variables and once as one element.
I have tested my code with Swish. This is the code:
:- use_module(library(clpfd)).
sum_list([],0).
sum_list([Head|Tail], Sum):-
sum_list(Tail, Sum1),
Sum is Head+Sum1.
kakuro(Vars):-
Vars=[A,B,C,E,F,G,J,K,L,N,O,P],
Vars ins 1..9,
word([A,B,C],24),
word([B,F,J,N],26),
word([C,G,K,O],15),
word([E,F,G],11),
word([A,E],17),
word([J,K,L],22),
word([N,O,P],14),
word([L,P],13),
writeln(Vars).
word(L,Sum):-
labeling([],L),
all_different(L),
sum_list(L,Sum).
This is the (only) output for ?- kakuro(L).:
[9, 8, 7, 8, 2, 1, 9, 5, 8, 7, 2, 5]
At first you need to have the ins/2 predicate running. This should work with SWI prolog. Second you know what to do with the labeling/2 predicate. This one will set values given from their domain (given via ins/2) to the entry. In SWI Prolog the first attribute is a list of options, no need to worry. AFTER labeling your variables you can "test" them and put constraints on them. You can do the labeling once for all variables at the beginning but this is pretty slow because all your progress is lost. So I recommend to do it in the word/2 predicate. If a dead end occurs the backtracking goes back to the word/2 predicate and not to the beginning of the kakuro/1 predicate.
Related
As a beginner, I am trying to solve a matrix problem, in which the solution is either the summation or the multiplication of the distinct digit in the matrix. For example,[[14,_,_,_],[15,_,_,_],[28,_,1,_]]. The predicate should generate the solution according to the matrix. However, I bumped into a problem that my combined predicate failed, but each of them succeeded independently.
I broke the problem down to summation and multiplication. Therefore, using clpfd library, and some predicates, for summation:
removeHead([Head|Tail],Tail).
get_head_element([],_).
get_head_element([Head|Rest],Head).
solve_sum(Row,RestRow):-
get_head_element(Row, Goal),
removeHead(Row,RestRow),
RestRow ins 1..9,
all_distinct(RestRow),
sum(RestRow, #=, Goal).
For multiplication:
multiply(List,Result):-
multiply(List,1,Result).
multiply([Element|RestList],PrevResult,Result):-
NextResult #= PrevResult * Element,
multiply(RestList,NextResult, Result).
multiply([], Result, Result).
solve_multiply(Row,RestRow):-
get_head_element(Row, Goal),
removeHead(Row,RestRow),
RestRow ins 1..9,
multiply(RestRow,Goal),
all_distinct(RestRow).
Both solve_sum and solve_multiply works for a single row, but here I combine these two predicates to:
solve_row_sum_or_multiply([],_).
solve_row_sum_or_multiply([HeadRow|Matrix],Solution):-
maplist(all_distinct,Matrix),
get_head_element(HeadRow,Goal),
( Goal >= 25
-> solve_multiply(HeadRow,Solution),
write(Solution)
; ( solve_sum(HeadRow,Solution),
write(Solution)
; solve_multiply(HeadRow,Solution),
write(Solution))
),solve_row_sum_or_multiply(Matrix,Solution).
When I use the solve_row_sum_or_multiply([[14,_,_,_],[15,_,_,_],[28,_,_,_]],X), it will give me the possible combinations. However, when solve_row_sum_or_multiply([[14,_,_,_],[15,_,_,_],[28,_,1,_]],X), it gave me false, but there's a solution possible there, such as [[14,7,2,1],[15,3,7,5],[28,4,1,7]].
I am wondering what could be wrong. Any help is highly appreciated!
EDIT
The problem is inside the combined predicate, and it's better to write the predicate which the answer suggests instead of a nested if-then-else predicate.
Let's review this code.
intro code
:- use_module(library(clpfd)).
% ---
% Make sure you see all the information in a list printed at the
% toplevel, i.e. tell the toplevel to not elide large lists (print
% them in full, don't use '...')
% ---
my_print :-
Depth=100,
Options=[quoted(true), portray(true), max_depth(Depth), attributes(portray)],
set_prolog_flag(answer_write_options,Options),
set_prolog_flag(debugger_write_options,Options).
:- my_print.
multiply/2
Recode the multiply/2 predicate so that it can be read more easily. Add test code to make sure it actually does what we think it does:
% ---
% Set up constraint: all the elements in List, multiplied
% together, must yield Result. If the List is empty, the
% Result must be 1.
% ---
multiply(Factors,Product):-
multiply_2(Factors,1,Product).
multiply_2([Factor|Factors],PartialProduct,Product):-
PartialProduct2 #= PartialProduct * Factor,
multiply_2(Factors,PartialProduct2,Product).
multiply_2([],Product,Product).
solve_multiply([Product|Factors]):-
Factors ins 1..9,
multiply(Factors,Product),
all_distinct(Factors).
% ---
% Testing
% ---
:- begin_tests(multiply).
test(1) :-
multiply([X,Y,Z],6),
[X,Y,Z] ins 1..9,
X #=< Y, Y #=< Z,
bagof([X,Y,Z],label([X,Y,Z]),Bag),
sort(Bag,BagS),
assertion(BagS == [[1, 1, 6], [1, 2, 3]]).
test(2) :-
multiply([],Result),
assertion(Result == 1).
test(3) :-
multiply([X,Y],3),
[X,Y] ins 1..9,
X #=< Y,
bagof([X,Y],label([X,Y]),Bag),
sort(Bag,BagS),
assertion(BagS == [[1,3]]).
test(4) :-
solve_multiply([6,X,Y,Z]),
bagof([X,Y,Z],label([X,Y,Z]),Bag),
sort(Bag,BagS),
assertion(BagS == [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]).
test(5) :-
solve_multiply([362880,F1,F2,F3,F4,F5,F6,F7,F8,F9]),
F1 #=< F2,
F2 #=< F3,
F3 #=< F4,
F4 #=< F5,
F5 #=< F6,
F6 #=< F7,
F7 #=< F8,
F8 #=< F9,
bagof([F1,F2,F3,F4,F5,F6,F7,F8,F9],label([F1,F2,F3,F4,F5,F6,F7,F8,F9]),Bag),
sort(Bag,BagS),
assertion(BagS == [[1,2,3,4,5,6,7,8,9]]).
test(6,fail) :-
solve_multiply([-1,X,Y,Z]).
:- end_tests(multiply).
solve_sum/1
For summation, after simplifying and re-coding to Prolog's "pattern matching" style instead of (for want of a better word) "Algol calling style" as is used in the original the question (which confused me greatly I have to say).
This looks ok too. Note that the predicate solve_sum/1 is quite different-in-argument-style from multiply/2 It is better - and easier on the reader (and oneself) - if one keeps the same calling conventions.
solve_sum([Sum|Terms]):-
Terms ins 1..9,
all_distinct(Terms),
sum(Terms, #=, Sum).
% ---
% Testing
% ---
:- begin_tests(sum).
test(0) :-
solve_sum([0]).
test(1) :-
solve_sum([6,X,Y,Z]),
X #=< Y, Y #=< Z,
bagof([X,Y,Z],label([X,Y,Z]),Bag),
sort(Bag,BagS),
assertion(BagS == [[1, 2, 3]]).
test(2) :-
solve_sum([9,X,Y,Z]),
X #=< Y, Y #=< Z,
bagof([X,Y,Z],label([X,Y,Z]),Bag),
sort(Bag,BagS),
assertion(BagS == [[1,2,6],[1,3,5],[2,3,4]]).
test(3,fail) :-
solve_sum([1,_X,_Y,_Z]).
:- end_tests(sum).
A printing predicate to get nice results
Prolog makes it easy to construct arbitrary structures "on the fly", one just has to stipulate exactly how the structure shall look.
Our "solution" is:
A list
With each element of the list a "row" (standing for one of the expressions)
And the row a term: row(Result,Op,RowValues) where Result is the left-hand-side of an equation, Op is one of add or mult and RowValues is a list with the values found for the values in the equation.
print_solution([]).
print_solution([Labeling|Labelings]) :-
format("---~n"),
print_rows_of_solution(Labeling),
format("---~n"),
print_solution(Labelings).
print_rows_of_solution([]).
print_rows_of_solution([row(Result,Op,RowValues)|Rows]) :-
print_row(Result,RowValues,Op),
print_rows_of_solution(Rows).
print_row(Result,[RowEntry|RowEntries],Op) :-
format("~q = ~q",[Result,RowEntry]),
print_row_2(RowEntries,Op).
print_row_2([RowEntry|RowEntries],Op) :-
((Op == mult) -> OpChar = '*'
; (Op == add) -> OpChar = '+'
; OpChar = '?'),
format(" ~q ~q",[OpChar,RowEntry]),
print_row_2(RowEntries,Op).
print_row_2([],_) :-
format("~n").
solve_row_sum_or_multiply/2
This has been modified to also collect the operation selected in a second argument (a list).
Note that the "maplist-all-distinct over the matrix" is not done here because that seems wrong.
solve_row_sum_or_multiply([],[]).
solve_row_sum_or_multiply([Row|MoreRows],[mult|Ops]) :-
Row = [X|_Xs],
X >= 25,
solve_multiply(Row), % we now have imposed a product constraint on the current Row
solve_row_sum_or_multiply(MoreRows,Ops).
solve_row_sum_or_multiply([Row|MoreRows],[add|Ops]) :-
Row = [X|_Xs],
X < 25,
solve_sum(Row), % we now have imposed a sum constraint on the current Row
solve_row_sum_or_multiply(MoreRows,Ops).
solve_row_sum_or_multiply([Row|MoreRows],[mult|Ops]) :-
Row = [X|_Xs],
X < 25,
solve_multiply(Row), % alternatively, we now have imposed a product constraint on the current Row
solve_row_sum_or_multiply(MoreRows,Ops).
And finally a "main" predicate
In trial_run/1 we stipulate that all variables except X32 be distinct and that X32 be 1. If X32 were in the "distinct set", none of the variables could take on the value 1 - there is no solution in that case.
We also impose a sorting to get only a set of "canonical" solutions:
We collect all possible solutions for solving and labeling using bagof/3:
trial_run(Solutions) :-
all_distinct([X11,X12,X13,X21,X22,X23,X31,X33]),
X32=1,
X11 #=< X12, X12 #=< X13,
X21 #=< X22, X22 #=< X23,
X31 #=< X33,
% multiple solutions solving x labeling may exist; collect them all
bagof(
[
row(14,Op1,[X11,X12,X13]),
row(15,Op2,[X21,X22,X23]),
row(28,Op3,[X31,X32,X33])
],
(
solve_row_sum_or_multiply( [[14,X11,X12,X13],[15,X21,X22,X23],[28,X31,X32,X33]], [Op1,Op2,Op3] ),
label([X11,X12,X13,X21,X22,X23,X31,X32,X33])
),
Solutions).
Does it work?
Apparently yes, and there is only one solution:
?- trial_run(Solutions),print_solution(Solutions).
---
14 = 2 + 3 + 9
15 = 1 + 6 + 8
28 = 4 * 1 * 7
---
Solutions = [[row(14,add,[2,3,9]),row(15,add,[1,6,8]),row(28,mult,[4,1,7])]].
I'm trying to write a predicate in SICStus Prolog such that given an expression, I can evaluate it (possible several times). The following works as expected:
?- A is 1, H = A+2+2, C is H.
C = 5 ?
yes
And so does this more elaborate code:
testing(Variables, Updates, Values, Result):-
assert(temp(Variables, Updates)),
temp(Values, Result),
abolish(temp/2).
evaluate([],[]).
evaluate([Term|T1],[H|T2]):-
H is Term,
evaluate(T1,T2).
Now, if instead A is an indexical, say A in 1..3, it doesn't work anymore. Any ideas about how to fix it?
The longer code is supposed to be used as follows:
?- testing([A,B,C], [A+1,B+C,max(A,B)], [0,0,0], Result), evaluate(Result, R).
Result = [0+1,0+0,max(0,0)],
R = [1,0,0] ?
yes
But is suffers from the same problem as the small example: I can't provide ranges of values in this way:
?- Val1 in 1..2, Val2 in 3..10, testing([A,B], [A+1,B+A], [Val1,Val2], Result), evaluate(Result, R).
Any suggestions?
My current solution requires replacing is/2 with val_of/2. It works, but I still believe there should be a better/faster implementation.
testing(Variables, Updates, Values, Result):-
assert(temp(Variables, Updates)),
temp(Values, Result),
abolish(temp/2).
evaluate([],[]).
evaluate([Term|T1],[H|T2]):-
val_of(H,Term),
evaluate(T1,T2).
val_of(E,E):- number(E),!.
val_of(E,E):- var(E),!.
val_of(V,E1+E2):- !, val_of(V1,E1), val_of(V2,E2), V #= V1+V2.
val_of(V,E1-E2):- !, val_of(V1,E1), val_of(V2,E2), V #= V1-V2.
val_of(V,max(E1,E2)):- !, val_of(V1,E1), val_of(V2,E2), V #= max(V1,V2).
val_of(V,min(E1,E2)):- !, val_of(V1,E1), val_of(V2,E2), V #= min(V1,V2).
val_of(V,abs(E1,E2)):- !, val_of(V1,E1), val_of(V2,E2), V #= abs(V1,V2).
A test example:
| ?- X in 1..3, testing([A,B], [A+1,B], [X,0], R), evaluate(R,R1).
R = [X+1,0],
R1 = [_A,0],
X in 1..3,
_A in 2..4 ?
yes
I think that all you need is just
evaluate([],[]).
evaluate([Term|T1],[H|T2]):-
H #= Term,
evaluate(T1,T2).
but the temp/2 relation is unnecessary, so a real simplification could be:
testing(Variables, Updates, Values, Result):-
maplist(#=, Updates, Values), Result=Variables.
results in
?- testing([A,B,C], [A+1,B+C,max(A,B)], [0,0,0], Result).
A = -1,
B = C, C = 0,
Result = [-1, 0, 0].
(note: tested in SWI-Prolog, after ?- [library(clpfd)].)
My final solution is a modified version of my original code based on the useful answers and comments provided by #CapelliC and #false:
testing(Variables, Updates, Values, Result):-
copy_term(Variables-Updates, Values-Result).
evaluate([],[]).
evaluate([Term|T1],[H|T2]):-
call(H #= Term),
evaluate(T1,T2).
The main issue in my original code was the missing call/1 in evaluate/2.
A test example in SICStus Prolog looks like this:
?- A in 1..3, testing([C,D,R],[C+1,max(D,5),R],[A,0,0],Res), evaluate(Res,T).
Res = [A+1,max(0,5),0],
T = [_A,5,0],
A in 1..3,
_A in 2..4 ?
yes
I've written a tail-recursive predicate in Prolog which outputs the integers between A and B in a list K. I've used "reverse" to bring the numbers into the right order:
numbers(A,B,K) :- numbers(A,B,[],K).
numbers(Y,Y,X,K) :- !, reverse([Y|X],K).
numbers(A,B,X,K) :- A<B, C is A+1, numbers(C,B,[A|X],K).
Query:
?- numbers(3,6, K).
K=[3,4,5,6]
All works fine. What I now want to do is that I only want to have odd numbers of the range between A and B in the list K. How can I do that? Thanks in advance!
Firstly, I would try to avoid using reverse/2. If you have such a solution, it's often an indicator that there's a better way to get the answer forwards more directly. Not always, but most often. reverse/2 is probably the 2nd favorite band-aid in Prolog right behind use of the cut. :)
In many problems, an auxiliary accumulator is needed. In this particular case, it is not. Also, I would tend to use CLP(FD) operations when involving integers since it's the more relational approach to reasoning over integers. But you can use the solution below with is/2, etc, if you wish. It just won't be as general.
numbers(S, E, []) :- S #> E. % null case
numbers(X, X, [X]).
numbers(S, E, [S|T]) :-
S #< E,
S1 #= S + 1,
numbers(S1, E, T).
| ?- numbers(3, 8, L).
L = [3,4,5,6,7,8] ? ;
no
| ?- numbers(A, B, [2,3,4,5]).
A = 2
B = 5 ? ;
no
| ?-
This solution avoids reverse/2 and is tail recursive.
To update it for odd integers, the first thought is that we can easily modify the above to do every other number by just adding 2 instead of 1:
every_other_number(S, E, []) :- S #> E.
every_other_number(X, X, [X]).
every_other_number(S, E, [S|T]) :-
S #< E,
S1 #= S + 2,
every_other_number(S1, E, T).
| ?- every_other_number(3, 7, L).
L = [3,5,7] ? ;
no
| ?- every_other_number(3, 8, L).
L = [3,5,7] ? ;
no
| ?- every_other_number(4, 8, L).
L = [4,6,8] ? ;
no
| ?-
Then we can do odd numbers by creating an initial predicate to ensure the condition that the first value is odd and calling every_other_number/3:
odd_numbers(S, E, L) :-
S rem 2 #= 1,
every_other_number(S, E, L).
odd_numbers(S, E, L) :-
S rem 2 #= 0,
S1 #= S + 1,
every_other_number(S1, E, L).
| ?- odd_numbers(2, 8, L).
L = [3,5,7] ? ;
no
| ?- odd_numbers(2, 9, L).
L = [3,5,7,9] ? ;
no
| ?- odd_numbers(3, 8, L).
L = [3,5,7] ? ;
no
| ?-
This could be a solution, using mod/2 operator.
numbers(A,B,K) :-
B1 is B+1,
numbers(A,B1,[],K).
numbers(Y,Y1,X,K) :-
Y = Y1,
reverse(X,K).
numbers(A,B,X,K) :-
A<B,
C is A+1,
C1 is mod(C,2),
(C1 = 0 ->
numbers(C,B,[A|X],K)
; numbers(C,B,X,K)).
Another possibility is to use DCG :
numbers(A,B,K) :-
phrase(odd(A,B), K).
odd(A,B) --> {A > B, !}, [].
odd(A,B) --> {A mod2 =:= 0, !, C is A+1}, odd(C,B).
odd(A,B) --> {C is A+2}, [A], odd(C, B).
Given the following code:
solve(G,L) :-
G = [A0,B0,C0,D0],
L = [A1,B1,C1,D1,A2,B2,C2,D2,A3,B3,C3,D3,A4,B4,C4,D4],
G ins 0..4,
L ins 0..max(G).
I want to constraint L to not contain values that are higher than the maximum value contained in G, but I get a "domain error" when using this syntax. Is there another way to express this?
Essentially you were on the right track. But L ins 0..max(G) does not work, because the bounds that you pass to ins/2 need to be integers or inf or sup.
SWI-Prolog clpfd manual page supports max in
finite domain arithmetic expressions, so we first state that MaxG is the maximum of A0, B0, C0, and D0. Then we state that MaxG is greater than or equal to each item in list L.
Putting it all together:
:- use_module(library(clpfd)).
gte_than(X,Y) :-
X #>= Y.
solve(G,L) :-
G = [A0,B0,C0,D0],
L = [A1,B1,C1,D1,A2,B2,C2,D2,A3,B3,C3,D3,A4,B4,C4,D4],
G ins 0..4,
L ins 0..sup,
MaxG #= max(max(A0,B0),max(C0,D0)),
maplist(gte_than(MaxG),L).
Here are some queries:
?- solve([0,1,2,1], [0,1,2,1,1,2,1,2,1,0,1,2,1,1,1,2]).
true.
?- solve([0,1,2,1], [0,3,2,1,1,2,1,2,1,0,1,2,1,1,1,2]).
false.
?- solve([0,3,2,1], [0,3,2,1,1,2,1,2,1,0,1,2,1,1,1,2]).
true.
?- solve([0,3,2,1], [0,3,2,1,1,2,4,4,1,0,1,2,1,1,1,2]).
false.
?- solve([4,3,2,1], [0,3,2,1,1,2,4,4,1,0,1,2,1,1,1,2]).
true.
Side note: SICStus Prolog has a specialized arithmetic constraint named maximum/2 we could use here, but that constraint is not (yet) available in clpfd with SWI-Prolog.
example
divisible([L1],X) :-
L1 mod X =:= 0.
query
divisible([4,6,8,7],2).
response
[4,6,8]
Any guidance?
divisible([], _, []).
divisible([H|T], X, [H|T1]) :- H mod X =:= 0, divisible(T, X, T1).
divisible([H|T], X, T1) :- H mod X =\= 0, divisible(T, X, T1).
You are going to need a three-argument predicate (input list, value to test for divisibility, and output list). After that, think about the three cases: input list is empty, first element is not divisible by number, and first element is divisible by number. You should be able to write three clauses, one for each of those, and get a correct predicate.
SWI-Prolog has a nice predicate include/3 which you can use like this:
?- include(divides(2), [4, 6, 8, 7], L).
L = [4, 6, 8].
given that you have defined divides/2:
% Succeeds if X divides Y
divides(X, Y) :-
Y mod X =:= 0.
Use meta-predicate tfilter/3 in tandem with the reified test predicate divisor_of_t/3:
?- tfilter(divisor_of_t(2),[4,6,8,7],Zs).
Zs = [4, 6, 8].
Based on clpfd and bool01_truth/2, we can define divisor_of_t/3 as follows:
:- use_module(library(clpfd)).
divisor_of_t(Y,X,Truth) :- X mod Y #= 0 #<==> B, bool01_truth(B,Truth).