Courses/CS 460/Fall 2005/Notes for Oct 29
From CSWiki
References | Homework | Questions | Course notes | Oz notes
Contents |
Constraint Propagation
[edit] The simplest of all CP problems
local
proc {Test X}
X::[1 2]
X \=: 1
{FD.distribute ff [X]}
end
in
{Browse {SearchAll Test}}
end
[edit] Propagators
:: ::: =: \=: <: >: =<: >=:
See also: propagators.
[edit] Slightly more complex
local
proc {Test X}
X::[3 1 9 5#7]
{FD.distribute ff [X]}
X >: 2
X <: 9
end
in
{Browse {SearchAll Test}}
{ExploreAll Test}
end
The answer is [3 5 6 7].
[edit] Watching propagators
- Enter the following statements.
- Use Ozcar to step through them line-by-line.
- Observe the shrinking domains of the variables X, Y, and Z.
local X Y Z in {Browse [X Y Z]} % [X Y Z] X :: 1#13 % [X[1#13] Y Z] Y :: 0#27 % [X[1#13] Y[0#27] Z] Z :: 1#12 % [X[1#13] Y[0#27] Z[1#12]] 2*Y =: Z % [X[1#13] Y[1#6] Z[2#12]] X <: Y % [X[1#5] Y[2#6] Z[4#12]] Z <: 7 % [X[1#2] Y[2#3] Z[4#6]] X \=: 1 % [2 3 6] end(From the FD Tutorial.)
[edit] Using procs
local
proc {LT A B} A <: B end
proc {Test Ans}
X Y
in
Ans = [X Y]
Ans ::: 3#5
{FD.distribute ff Ans}
end
in
{Browse {SearchAll Test}}
{ExploreAll Test}
end
[edit] FD.distinct
local
proc {Test Ans}
X Y
in
Ans = [X Y]
Ans ::: 3#5
{FD.distribute ff Ans}
{FD.distinct Ans}
end
in
{Browse {SearchAll Test}}
{ExploreAll Test}
end
[edit] Subprogram trap
local
fun {Plus2 X Y} X + Y end
proc {Plus3 X Y Z} X + Y =: Z end
proc {Test Ans}
A B
in
Ans = [A B]
Ans ::: 1#9
% A + B =: 5 % OK
% {Plus3 A B 5} % OK
% {Plus2 A B} =: 5 % Not OK
{FD.distribute ff Ans}
{FD.distinct Ans}
end
in
{Browse {SearchAll Test}}
end
[edit] Simple SEND + MORE = MONEY
local
proc {SimpleMoney SMM}
Send More Money
in
SMM = Send#More#Money
[Send More Money] ::: 1#9
Send <: More
Send + More =: Money
{FD.distribute ff [Send More Money]}
end
in
{Browse {SearchAll SimpleMoney}}
end
[edit] SEND + MORE = MONEY
local
proc {Money Solution}
S E N D M O R Y
Vars = [S E N D M O R Y]
in
Solution = [S E N D]#[M O R E]#[M O N E Y]
Vars ::: 0#9
{FD.distinct Vars}
S \=: 0
M \=: 0
1000*S + 100*E + 10*N + D
+ 1000*M + 100*O + 10*R + E
=: 10000*M + 1000*O + 100*N + 10*E + Y
{FD.distribute ff Vars}
end
in
{Browse {SearchAll Money}}
end
[edit] Propagating =: example
local
proc {PropEq L1 L2}
case L1 of
nil then skip
[] H1 | T1 then
H2 T2 in
L2 = H2 | T2
H1 =: H2
{PropEq T1 T2}
end
end
proc {Test Answer}
A B
in
Answer = [A B]
Answer ::: 0#9
{PropEq Answer [4 6]}
{FD.distribute ff Answer}
end
in
{Browse {SearchAll Test}}
end

