(!****************************************************************
CP example problems
===================
file ac2001.mos
```````````````
Generic binary constraints.
(c) 2008 Artelys S.A. and Fair Isaac Corporation
Creation: 2005, rev. Jul. 2022
*****************************************************************!)
model "generic_binary_constraint example"
uses "kalis"
forward function truth_value(v1:integer, v2:integer): boolean
declarations
x1 : cpvar
x2 : cpvar
C : integer
end-declarations
C := 4
0 <= x1 ; x1 <= 10
0 <= x2 ; x2 <= 5
! Define and post the user constraint
generic_binary_constraint(x1, x2, ->truth_value)
cp_show_prob
! Search for all solutions and print them out
while (cp_find_next_sol)
writeln("A solution has been found with x1 = ",
getsol(x1), " and x2 = ", getsol(x2) )
! Implementation of the user constraint
function truth_value(v1:integer, v2:integer): boolean
returned := (v1 = v2 mod C)
end-function
end-model
|
(!****************************************************************
CP example problems
===================
file gac2001.mos
````````````````
Generic n-ary constraints.
(c) 2009 Artelys S.A. and Fair Isaac Corporation
Creation: Nov. 2009, rev. Jul. 2022
*****************************************************************!)
model "generic_n_ary_constraint example"
uses "kalis"
parameters
N = 6
end-parameters
forward function greater_than_previous(tuple: cptuple, flag:integer): boolean
declarations
R = 1..N
vars : array(R) of cpvar
end-declarations
forall(i in R) setdomain(vars(i),0,N)
! Define and post the user constraint
generic_nary_constraint(vars, ->greater_than_previous, 1)
cp_show_prob
! Search for all solutions and print them out
while (cp_find_next_sol)
cp_show_sol
!**** Implementation of the user constraint: the value of each
!**** tuple element must be strictly larger than its predecessor
function greater_than_previous(tuple: cptuple, flag:integer): boolean
returned := true
first := true
lastv := 0
forall(indexv in 1..getsize(tuple)) do
v := getelt(tuple,indexv)
if first then
lastv := v
first := false
else
if v < lastv + 1 then
returned:= false
break
end-if
lastv := v
end-if
end-do
end-function
end-model
|