Initializing help system before first use

Introductory example: constraint handling


Type: Programming
Rating: 1 (simple)
Description: Introductory example: scheduling meetings, stated using disequality constraints; default enumeration.
  • meeting.mos: basic version, stating disequality constraints
  • meeting2.mos: Additional constraints, immediate posting of (unnamed) constraints
  • meeting3.mos: Named constraints posted without control of result
  • meeting4.mos: Explicit posting of constraints with 'cp_post' to retrieve the result
File(s): meeting.mos, meeting2.mos, meeting3.mos, meeting4.mos


meeting.mos
(!****************************************************************
   CP example problems
   ===================
   
   file meeting.mos
   ````````````````
   Introductory example.

   (c) 2008 Artelys S.A. and Fair Isaac Corporation
       
*****************************************************************!)

model "Meeting"
 uses "kalis"
 
 declarations
  MEETINGS = {'A','B','C','D'}        ! Set of meetings
  TIME = 1..3                         ! Set of time slots   
  plan: array(MEETINGS) of cpvar      ! Time slot per meeting
 end-declarations

 forall(m in MEETINGS) setdomain(plan(m), TIME)

! Respect incompatibilities
 plan('A') <> plan('B')
 plan('A') <> plan('D')
 plan('B') <> plan('C')
 plan('B') <> plan('D')  

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

! Solution printing
 forall(m in MEETINGS)
  writeln("Meeting ", m, ": ", getsol(plan(m)))

end-model

meeting2.mos
(!****************************************************************
   CP example problems
   ===================
   
   file meeting2.mos
   `````````````````
   Introductory example.
   - Additional constraints. -

   (c) 2008 Artelys S.A. and Fair Isaac Corporation
       
*****************************************************************!)

model "Meeting (2)"
 uses "kalis"
 
 declarations
  MEETINGS = {'A','B','C','D'}        ! Set of meetings
  TIME = 1..3                         ! Set of time slots   
  plan: array(MEETINGS) of cpvar      ! Time slot per meeting
 end-declarations

 forall(m in MEETINGS) do
  setdomain(plan(m), TIME)
  setname(plan(m), "plan"+m)
 end-do
 writeln("Original domains: ", plan)
 
 plan('B') <= 2                       ! Meeting B before day 3
 plan('D') <> 2                       ! Meeting D not on day 2
 plan('A') = 1                        ! Meeting A on day 1
 writeln("With constraints: ", plan)
 
! Respect incompatibilities
 plan('A') <> plan('B')
 plan('A') <> plan('D')
 plan('B') <> plan('C')
 plan('B') <> plan('D')  

! Solve the problem
 if not(cp_find_next_sol) then
  writeln("Problem is infeasible")
  exit(1)
 end-if   
 
! Solution printing
 forall(m in MEETINGS)
  writeln("Meeting ", m, ": ", getsol(plan(m)))

end-model

meeting3.mos
(!****************************************************************
   CP example problems
   ===================
   
   file meeting3.mos
   `````````````````
   Introductory example.
   - Named constraints. -

   (c) 2008 Artelys S.A. and Fair Isaac Corporation
       
*****************************************************************!)

model "Meeting (3)"
 uses "kalis"
 
 declarations
  MEETINGS = {'A','B','C','D'}        ! Set of meetings
  TIME = 1..3                         ! Set of time slots   
  plan: array(MEETINGS) of cpvar      ! Time slot per meeting
  Ctr: array(range) of cpctr
 end-declarations

 forall(m in MEETINGS) do
  setdomain(plan(m), TIME)
  setname(plan(m), "plan"+m)
 end-do
 writeln("Original domains: ", plan)
  
 Ctr(1):= plan('B') <= 2              ! Meeting B before day 3
 Ctr(2):= plan('D') <> 2              ! Meeting D not on day 2
 Ctr(3):= plan('A') = 1               ! Meeting A on day 1
 writeln("After definition of constraints:\n ", plan)
 
 forall(i in 1..3) Ctr(i) 
 writeln("After posting of constraints:\n ", plan)
 
! Respect incompatibilities
 plan('A') <> plan('B')
 plan('A') <> plan('D')
 plan('B') <> plan('C')
 plan('B') <> plan('D')  

! Solve the problem
 if not(cp_find_next_sol) then
  writeln("Problem is infeasible")
  exit(1)
 end-if   
 
! Solution printing
 forall(m in MEETINGS) writeln("Meeting ", m, ": ", getsol(plan(m)))

end-model

meeting4.mos
(!****************************************************************
   CP example problems
   ===================
   
   file meeting4.mos
   `````````````````
   Introductory example.
   - Explicit posting of constraints. -

   (c) 2008 Artelys S.A. and Fair Isaac Corporation
       
*****************************************************************!)

model "Meeting (4)"
 uses "kalis"
 
 declarations
  MEETINGS = {'A','B','C','D'}        ! Set of meetings
  TIME = 1..3                         ! Set of time slots   
  plan: array(MEETINGS) of cpvar      ! Time slot per meeting
  Ctr: array(range) of cpctr
 end-declarations

 forall(m in MEETINGS) do
  setdomain(plan(m), TIME)
  setname(plan(m), "plan"+m)
 end-do
 writeln("Original domains: ", plan)
 
 Ctr(1):= plan('B') <= 2              ! Meeting B before day 3
 Ctr(2):= plan('D') <> 2              ! Meeting D not on day 2
 Ctr(3):= plan('A') = 1               ! Meeting A on day 1
 writeln("After definition of constraints:\n ", plan)
 
! Retrieve result of posting constraints
 forall(i in 1..3) 
  if not cp_post(Ctr(i)) then 
   writeln("Constraint ", i, " makes problem infeasible")
   exit(1)
  end-if
 
 writeln("After posting of constraints:\n ", plan)
 
! Respect incompatibilities
 plan('A') <> plan('B')
 plan('A') <> plan('D')
 plan('B') <> plan('C')
 plan('B') <> plan('D')  

! Solve the problem
 if not(cp_find_next_sol) then
  writeln("Problem is infeasible")
  exit(1)
 end-if   
 
! Solution printing
 forall(m in MEETINGS) writeln("Meeting ", m, ": ", getsol(plan(m)))

end-model

© 2001-2020 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.