FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

'distribute' and 'occurrence' constraints

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.
Further explanation of this example: 'Xpress Kalis Reference Manual'[download all files]

Source Files


   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)

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

 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) 
 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
 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) 

Back to examples browserPrevious exampleNext example