06.03.2013 Views

Artificial Intelligence and Soft Computing: Behavioral ... - Arteimi.info

Artificial Intelligence and Soft Computing: Behavioral ... - Arteimi.info

Artificial Intelligence and Soft Computing: Behavioral ... - Arteimi.info

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

19.5 Constraint Logic Programming<br />

We already introduced Constraint logic programming (CLP) in chapter 6.<br />

Here, we will show how efficiently we can code a problem in CLP. We start<br />

with the well-known Crypto-arithmetic problem that needs to find the integer<br />

values of the variables {S, E, N, D, M, O, R, Y} in the range [0,9], so as to<br />

add to produce<br />

SEND<br />

+<br />

MORE<br />

MONEY<br />

We could solve the problem by the method covered in the last section.<br />

But a simple <strong>and</strong> efficient approach is to code in CLP language.<br />

Crypto-arith (S, E, N, D, M,O, R,Y):-<br />

[S,E,N,D,M,O,R,Y] : : [0…9],<br />

Constraint ( [S,E,N,D,M,O,R,Y] ),<br />

labeling ( [ S,E,N,D,M,O,R,Y ] ).<br />

Constraint ( [ S,E,N,D,M,O,R,Y ] ):-<br />

S ≠ 0 , M ≠ 0,<br />

All-unequal ( [ S,E,N,D,M,O,R,Y ] ),<br />

(1000*S + 100 * E + 10 * N + D ) +<br />

(1000* M + 100 * O + 10 * R + E )<br />

= 10000 * M + 1000 * O + 100 * N + 10 * E + Y .<br />

Goal: Crypto-arith ( S,E,N,D,M,O,R,Y ) →<br />

Given the above goal, the program correcting determines<br />

{ S=9, E=5, N=6, D=7, M=1, O=0, R=8, Y=2}.<br />

The predicate labeling in the above program is a library function that<br />

ensures the search in [0, 9] for each variable. The rest of the algorithm is<br />

simple <strong>and</strong> clear from the statements themselves.<br />

When no satisfiable solution for the constraint is available, the constraint<br />

is broken into priority-wise components. Let C be the constraint, which is<br />

broken into C1 ,C2 <strong>and</strong> C3 say. Let the priority of C1> priority of C2 >priority

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!