FICO Xpress Optimization Examples Repository
 FICO Optimization Community FICO Xpress Optimization Home

Generic binary and n-ary constraints

Description
Implementation of user-defined constraints:
• ac2001.mos: Generic binary constraint
• gac2001.mos: Generic n-ary constraints
Further explanation of this example: 'Xpress Kalis Mosel Reference Manual'

 ac2001.zip [download all files]

Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
 ac2001.mos [download] gac2001.mos [download]

gac2001.mos

(!****************************************************************
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