Constraint programming: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 41:
Digits = [S,E,N,D,M,O,R,Y], % Create variables
Digits '''::''' [0..9], % Associate domains to variables
 
S '''#\=''' 0, % Constraint: S must be different from 0
M '''#\=''' 0,
Line 50 ⟶ 49:
'''labeling'''(Digits). % Start the search
 
The constraint solver creates the variables, and the domains, so all the variables range over the set of values {0,1,2,3, ..., 9}. S#\=0 (which means <math>S \neq 0</math>) is imposed, and value 0 is removed from the ___domain of variable S. Same for variable M. Then, the constraint alldifferent(Digits) is imposed: it cannot propagate any deletion, so it simply suspends. Then the last constraint is imposed. From the last constraint, the solver infers that M=1. All the constraints involving variable M are awaken: in particular alldifferent can remove value 1 from the ___domain of all the remaining variables. When the [[AC-3 algorithm]] reached quiescence, the domains have been reduced, and the problem is hopefully easier to solve.
 
Predicate '''labeling''' starts the depth first search: it takes the first value in the ___domain of the first variable, and assigns it to the variable. Then, the [[AC-3 algorithm]] is resumed and performs further ___domain reductions. If one ___domain becomes empty, '''labeling''' backtracks and tries the second value in the ___domain of the first variable. Otherwise, it tries the first value in the ___domain of the second variable, and so on, until a solution is found.
 
==References==