Search code examples
ozmozart

Using "If statement" - OZ


I have problem using "if statement" in Mozart. Program starts but only result it gives is: 1#_. I'd like to know why it's now working.

declare PSO

proc{PSO W}
  X1 X2 X3 X4
  Y1 Y2 Y3 Y4
in   
   X1::1#6
   X2::1#6
   X3::1#6
   X4::1#6
   Y1::1#6
   Y2::1#6
   Y3::1#6
   Y4::1#6

   if X1 > 1 then Y1 =: 2 else Y2=:3
   end

   W=w(x1:X1 y1:Y1 x2:X2 y2:Y2 x3:X3 y3:Y3 x4:X4 y4:Y4)

   {FD.distribute ff W}   
end
{ExploreOne PSO}

Solution

  • The problem is: You are trying to evaluate X > 1 although X is not fully determined at this point. So the procedure simply blocks at this point.

    You can either move the offending statement after the distribute:

    W=w(x1:X1 y1:Y1 x2:X2 y2:Y2 x3:X3 y3:Y3 x4:X4 y4:Y4)
    {FD.distribute ff W}   
    
    if X1 > 1 then Y1 =: 2 else Y2=:3
    end
    

    After distribute all variables will have concrete values.

    Or you replace the if-else-statement with a logic programming construct:

    choice
       X1 >: 1
       Y1 =: 2
    [] X1 =<: 1
       Y2 =: 3
    end
    

    The latter will typically be more efficient in constraint programming problems.