Initializing help system before first use

'distribute' and 'occurrence' constraints


Type: Programming
Rating: 2 (easy-medium)
Description: The 'occurrence' (=cardinality) constraint expresses a relation on the frequency with which a value occurs in a set of decision variables. The 'distribute' constraint generalizes this constraint by extending the cardinality relation to a list of values.
  • occurrence.mos: simple example of the use of occurrence constraints
  • distribute.mos: using 'distribute' or 'occurrence' constraints in the formulation of a personnel planning problem.
File(s): distribute.mos, occurrence.mos


distribute.mos
(!****************************************************************
   CP example problems
   ===================
   
   file distribute.mos
   ```````````````````
   Distribute and occurrence constraints.
   A movie theatre director has to decide in which location each
   of his 8 employees should be posted. Each of the four locations
   has a given personnel requirement.

   (c) 2008 Artelys S.A. and Fair Isaac Corporation
       Creation: 2005, rev. 2007
*****************************************************************!)

model "Distribute example"
 uses "kalis"
  
 declarations
  PERS = {"David","Andrew","Leslie","Jason","Oliver","Michael",
          "Jane","Marilyn"}          ! Set of personnel
  LOC = 1..4                         ! Set of locations
  REQ: array(LOC) of integer         ! No. of pers. req. per loc.
  place: array(PERS) of cpvar        ! Workplace for each peson
 end-declarations

! Initialize data
 REQ:: [3, 2, 2, 1]

! Each variable has a lower bound of 1 (Ticket office) and 
!  an upper bound of 4 (Cloakroom)
 forall(p in PERS) do
  setname(place(p), "workplace("+p+")")
  1 <= place(p); place(p) <= 4  
 end-do

! Creation of a resource constraint of for every location
 forall(d in LOC) occurrence(d, place) = REQ(d) 

! Elegant way to declare theses constraints, 
! moreover achieving stronger prunning (using global 
! cardinality constraint)
 distribute(place, LOC, REQ)

! Solve the problem
 if not(cp_find_next_sol) then
  writeln("Problem is infeasible")
  exit(1)
 end-if 

! Solution printout 
 writeln(place)  
 
end-model


occurrence.mos
(!****************************************************************
   CP example problems
   ===================
   
   file occurrence.mos
   ```````````````````
   Cardinality (=occurrence) constraints.

   (c) 2008 Artelys S.A. and Fair Isaac Corporation
       Creation: 2005, rev. Mar. 2013
*****************************************************************!)
model "Cardinality"
 uses "kalis"

 setparam("KALIS_DEFAULT_LB", 0)

 declarations
  R = 1..6
  S = 1..10
  x: array(R) of cpvar
  y: array(S) of cpvar
  a,b,c: cpvar
  Card: cpctr
  Vlist: cpvarlist
 end-declarations

 forall(i in R) setname(x(i), "x"+i)
 forall(i in 1..3) x(i) = 1
 forall(i in 4..6) x(i) <= 10 
 setname(c, "c")
 c <= 15 
 
 writeln("Initial domains:\n ", x, "  ", c) 

! Explicit posting of an occurrence constraint
 Card:= occurrence(1, x) = c 
 if cp_post(Card) then 
  writeln("With occurrence constraint:\n ", x, "  ", c) 
 else exit(1) 
 end-if 
 
 c = 6
 writeln("Fixing occurrence to 6:\n ", x, "  ", c)

 forall(i in S) do
  setname(y(i), "y"+i)
  i <= y(i); y(i) <= i*2
 end-do
 setname(a, "a"); setname(b,"b")

 writeln("Initial domains:\n ", y, "  ", a, "  ", b)  

! Occurrence constraint on an array of variables 
 occurrence(4, y) <= 2

! Occurrence constraint on a list of variables 
 Vlist += y(1); Vlist += y(2); Vlist += y(4)
 occurrence(2, Vlist) = 0

! Occurrence constraint on a set of variables  
 occurrence(9, {y(6), y(7), y(9)}) >= 3

! Occurrences bounded by variables   
 occurrence(5, y) >= a
 occurrence(8, {y(4), y(5), y(6), y(7), y(8)}) <= b
 writeln("With all constraints:\n ", y, "  ", a, "  ", b) 
 
 if cp_find_next_sol then
  writeln("A solution:\n ", y, "  ", a, "  ", b) 
 end-if
 
end-model