cryptarithmetic puzzle - Prolog dot product with variables (constraint satisfaction) -


i'm working on prolog assignment , i'm very close solution. so, problem constraint satisfaction problem have find values set of variables such conditions true. specifically, given 3 words (w1,w2,w3), assign variables such w1+w2=w3. example of send+more=money, or it+is=me.

the constraints are: (1) have add correctly, (2) starting letter cannot 0 (3) , variables must distinct. , has work general word problem. issue happening when try ensure add correctly (i've met other conditions , understand problem). in terms of second word problem should have:

 10*i + 1*t +10*i + 1*s ___________  10*m + 1*e 

so, have made function makes lists of powers of 10 in length, so:

powlist(1,l) :-     append([1.0],[],l).  powlist(n,l) :-     n1 n-1,     x 10**n1,     powlist(n1,l1),     append([x],l1,l),     !. 

i have actual list of letters, say, [i,t,i,s,m,e]. constructed list of coefficients (i'll explain part later) out of powlist have following: [10,1,10,1,-10,-1]. did if took dot product between list of coefficients , list of letters, , zero, constraint satisfied. but, can't dot product theory work. have line says:

   scalar_product(coefficients, letters, #=, 0) 

but giving me following error:

! instantiation error in argument 2 of is/2
! goal: _102 0+10.0*_109

i'm not sure how define dot product can work on variables (instead of atoms). of rest of code works (and don't want put on here because common question introductory prolog courses, , don't want give lazy people answers). guys suggest?

your strategy indeed sound , work, @ least swi-prolog clp(fd) using built-in scalar_product/4. unfamiliar definition of predicate in sicstus, it's interface appears same in swi-prolog.

i can make couple of suggestions. firstly, perhaps aspect of code you've written generating choicepoints which, when executed in backtracking (e.g., seek alternate solutions, such via label/1), interpreter executes subgoal _102 0+10.0*_109 _109 unintentionally unbound. have written predicate contains such line? if not, recommend double checking code ensure not generate unnecessary choicepoints, such definition of powlist/2. recommend try following instead:

powlist(1, [1]) :- !. powlist(n, [f|fs]) :-     n > 1,     n1 n - 1,     f 10 ** n1,     powlist(n1, fs). 

this version leaves no choicepoints prolog interpreter backtrack to, might resolve problem (though, without seeing more code, can't tell).

otherwise, if correct , error indeed emanating within definition of scalar_product/4 (though i'd surprised), perhaps generate scalar product constraint term , add store yourself, manually. example, consider:

my_scalar_product([v|vs], [c|cs], op, value) :-     construct_constraint(vs, cs, (v * c), constr),     constraint =.. [op, constr, value],     constraint.  construct_constraint([], [], acc, acc). construct_constraint([v|vs], [f|fs], acc, res) :-     construct_constraint(vs, fs, '+'(acc, (v * f)), res). 

this version (my_scalar_product/4) assumes same interface built-in scalar_product/4, adds constraint store instead of attempting execute using is/2.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -