[Newbie] If statement and contraint programming

Raphael Collet raph at info.ucl.ac.be
Mon Apr 28 09:46:03 CEST 2003


Thomas LUDWIG wrote:
> 
> I tried this code:
> 
> fun {ContreSujet S}
>    proc {$ C}
>       {FD.tuple contresujet {Width S} 0#127 C}
>       {For 2 {Width S} 2
>        proc {$ I}
>           C.I <: 2
>        end
>       }
>       {For 4 {Width S} 2
>        proc {$ I}
>           if C.(I-1) \= C.(I-3) then C.I =: 1 end
>        end
>       }
>       {FD.distribute general(order:nbSusps value:mid) C}
>    end
> end
> 
> But, instead of giving me something like contresujet(64 0 64 0 64 0 ......)
> It gives me contresujet(_(0#127)_ _(0#1)_ _(0#127)_ _(0#1)_ _(0#127)_
> _(0#1)_ .....)
> 
> Can't we use "if" in constaint programming ?
> Then what is the trick to obtain the 'if then else' behavior without
> having _(x#y)_'s  ?

There's no trick, but rather a bug in your program.  Your script simply
never reach the call to FD.distribute because your 'if' statements above
it block the thread!  The boolean operator '\=' in the statement

	if C.(I-1) \= C.(I-3) then C.I =: 1 end

blocks until both sides are determined, then returns true or false.  You
should simply avoid it to block everything.  The simplest way is to put
each 'if' statement in its own thread, i.e., replace the above line by

	thread if C.(I-1) \= C.(I-3) then C.I =: 1 end end

A different implementation would use reification instead.  This means to
turn the truth value of a constraint into a constraint on a boolean
variable.  Here's a substitution proposal for the 'if' statement.  As
those statements all create propagators, nothing will block in the
middle of your script.

	local B1 B2 in
	   [B1 B2] ::: 0#1              % binary FD variables
	   B1 = (C.(I-1) \=: C.(I-3))   % B1=1 iff the condition is true
	   B2 = (C.I =: 1)              % B2=1 iff C.I=1
	   {FD.impl B1 B2 1}            % B1=1 implies B2=1
	end


Cheers,

-- 
Raphaël Collet - raph at info.ucl.ac.be - http://www.info.ucl.ac.be/~raph/
-
Please send submissions to users at mozart-oz.org
and administriva mail to users-request at mozart-oz.org.
The Mozart Oz web site is at http://www.mozart-oz.org/.
Please send bug reports to bugs at mozart-oz.org.





More information about the mozart-users mailing list