Initializing help system before first use

occurrence

occurrence


Purpose
Constrains the number of occurrences of a specific value or a set of values among a list/set of decision variables
Synopsis
occurrence(value,vars) op target with target:integer and op one of =, ≥, ≤
target op occurrence(value,vars) with target:integer and op one of =, ≥, ≤
occurrence(value,vars) op occvar with occvar:cpvar and op one of =, ≥, ≤
occvar op occurrence(value,vars) with occvar:cpvar and op one of =, ≥, ≤
occurrence(varset:set of cpvar, values:set of integer, minocc:integer, maxocc:integer)
Arguments
target 
the (fixed) target occurrence count for the specified value
occvar 
the variable occurrence count for the specified value
value 
the integer value whose occurences in the variable list should be constrained
vars 
the list of decision variables: {set of cpvar | array of cpvar | cpvarlist}
varset 
a set of decision variables
values 
a set of values
minocc 
minimum number of occurrences
maxocc 
maximum number of occurrences
Return value
An occurrence constraint over the given arguments
Example
The following example shows how to use the occurence constraint:
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


Related topics