Dear Chris,

As you know, you can define your own abstractions in Oz including higher-order procedures. You can use this expressive power when defining constraint problems too.

For example, in order to apply a constraint too all pairwise combinations of a list or to neighbouring pairs in a list you can create some higher order procedures.

/** %% Applies the binary procedure P on all pairwise combinations of Xs, i.e. {P Xs1 Xs2} .. {P Xs1 XsN} {P Xs2 Xs3} .. {P XsN-1 XsN}.
   %% */
   proc {ForPairwise Xs P}
      case Xs of nil then skip
      else
         X1 = Xs.1
      in
         {ForAll Xs.2 proc {$ X2} {P X1 X2} end}
         {ForPairwise Xs.2 P}
      end
   end

{ForPairwise Sol proc {$ X Y} X >=: Y end}

Actually, in this particular case you do not need pairwise, constraint propagation does that for you so the following is sufficient. Of course, you could also use the loop inside this definition directly, but if you have many constraint applications like this then For2Neighbours is a bit more concise.

/** %% Traverses through Xs by applying the binary procedure Proc on neighboring elements in Xs.
   %% {For2Neighbours [1 2 3] P} -> {P 1 2} {P 2 3}
   */
   proc {For2Neighbours Xs Proc}
      for X in {List.take Xs {Length Xs}-1}
         Y in Xs.2
      do {Proc X Y}
      end
   end

{For2Neighbours Sol proc {$ X Y} X >=: Y end}

Finally, there is a constraint already available which constraints that all elements of a list (vector) are pairwise distinct.

{FD.distinct [RA RB RC ...]}

Best
Torsten

On Apr 14, 2009, at 3:45 AM, Chris Rathman wrote:

I have a couple of questions concerning specification of constraints. I want to know if there is a shorthand way to specify the relationship between the solutions in the variables. In the first case, I want to specify a greater than or equal relation. In the second case, I want to specify a distinct relationship. I can do this painstakingly by drawing out the complete relationships. I'd just like to know if there is a shorter way to do it.

1). If I have a list of constraint variables, is there a shorter way to define that the variables involve a decreasing value? In project euler problem #240, there are five 6-sided dice (sides numbered 1 to 6) that can be rolled so that the top three sum to 15. (I've already solved problem #240, it just gets to be long when we have twenty 12-sided dice).

proc {TopDiceFiveSix ?Sol}
   D01|D02|D03|D04|D05|nil = Sol
in
   D01::1#6 D02::1#6 D03::1#6 D04::1#6 D05::1#6

   D01 + D02 + D03 =: 15

   % This is the relations what I want to describe in shorthand???
   D01>=:D02
   D01>=:D03  D02>=:D03
   D01>=:D04  D02>=:D04  D03>=:D04
   D01>=:D05  D02>=:D05  D03>=:D05  D04>=:D05

  {FD.distribute split Sol}
end

2). If I have a list of constraint variables, is it possible to define that not of the values can be equal? In project euler problem #68, we have the following "magic" 3-gon ring, filled with the numbers 1 to 6, and each line adding to nine. (I've also already solved problem #68, but it too gets to be long when dealing with a five-gon).
proc {ThreeGonRing ?Sol}
   XA#RA#RB # XB#RB#RC # XC#RC#RA = Sol
in
   RA::1#6 RB::1#6 RC::1#6
   XA::1#6 XB::1#6 XC::1#6

   % none of the nodes is equal (should be a shorthand for this???)
   RA\=:RB  RB\=:RC  RC\=:XA  XA\=:XB  XB\=:XC
   RA\=:RC  RB\=:XA  RC\=:XB  XA\=:XC
   RA\=:XA  RB\=:XB  RC\=:XC
   RA\=:XB  RB\=:XC
   RA\=:XC

   % each line adds to n
   XA + RA + RB =: XB + RB + RC
   XB + RB + RC =: XC + RC + RA

% Working clockwise, starting from the group of three with the numerically lowest external node
   XA <: XB
   XA <: XC

  {FD.distribute split Sol}
end

Thanks,
Chris Rathman

<ATT00001.txt>


--
Torsten Anders
Interdisciplinary Centre for Computer Music Research
University of Plymouth
Office: +44-1752-586219
Private: +44-1752-558917
http://strasheela.sourceforge.net
http://www.torsten-anders.de



_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to