Initializing help system before first use

Defining a linear relaxation


Type: Programming
Rating: 4 (medium-difficult)
Description: Example of defining linear relaxations (this feature requires Xpress Optimizer in addition to Xpress Kalis)
  • knapsackalld_cp.mos: Integer knapsack problem with 'alldifferent' constraint solved with standard CP search.
  • knapsackalld_relax.mos: Defining a linear relaxation and corresponding search.
  • customrelax.mos: Defining a customized linear relaxation
File(s): customrelax.mos, knapsackalld_cp.mos, knapsackalld_relax.mos

customrelax.mos
(!****************************************************************
   CP example problems
   ===================
   
   file customrelax.mos
   ````````````````````
   Defining customized linear relaxations.

   (c) 2009 Artelys S.A. and Fair Isaac Corporation
       Creation: 2009
*****************************************************************!)
model "Custom relaxations"
 uses "kalis"

 declarations		
  myrelax: cplinrelax               ! A linear relaxation	
  a1,a2  : cpauxvar                 ! Auxiliary variables for relaxation	
  taux   : array(1..4) of cpauxvar  ! Array of auxiliary variables
	
  x1,x2,x3: cpvar                   ! Finite domain variables	
  z       : cpfloatvar              ! CP variable with continuous domain	
  CAlld   : cpctr                   ! A CP constraint
 end-declarations
	
! Define an 'alldifferent' constraint	
 CAlld := all_different({x1,x2,x3})

! Build an automatic 'LP' oriented linear relaxation
 myrelax1 := cp_get_linrelax(0)

! Setting bounds on the auxiliary variables
 setdomain(a2,0,1)
 setdomain(a1,0.56,18.67)

! Adding linear inequalities to the relaxation
 myrelax += 3*a1      <= 3
 myrelax += a1+a2     >= 3.1
 myrelax += 2*a1-4*a2  = 3
 myrelax += a1-x1      <= 3.4
 myrelax += a1+4*a2-z <= 3
 myrelax += get_linrelax(CAlld,0)
 myrelax += 
   get_indicator(x1,1) + get_indicator(x2,1) + get_indicator(x3,1) <= 1
 myrelax += sum(i in 1..4) taux(i) = 4

! Set integrality condition for variables in the relaxation
 set_integer(myrelax,a2,true)
 set_integer(myrelax,x1,true)
	
! Output the resulting relaxation to the screen
 cp_show_relax(myrelax)
 
end-model 

knapsackalld_cp.mos
(!****************************************************************
   CP example problems
   ===================
   
   file knapsackalld_cp.mos
   ````````````````````````
   Knapsack problem with additional alldifferent 
   constraint solved by standard CP search.

   (c) 2009 Artelys S.A. and Fair Isaac Corporation
       Creation: 2009, rev. Mar. 2013       
*****************************************************************!)
model "Knapsack with side constraints"
 uses "kalis"

 declarations
  x1,x2,x3: cpvar                   ! Decision variables
  benefit : cpvar                   ! The objective to minimize
 end-declarations

! Enable output printing
 setparam("kalis_verbose_level", 1)

! Setting name of variables for pretty printing
 setname(x1,"x1"); setname(x2,"x2"); setname(x3,"x3") 
 setname(benefit,"benefit")

! Set initial domains for variables
 setdomain(x1, {1,3,8,12})
 setdomain(x2, {1,3,8,12})
 setdomain(x3, {1,3,8,12})

! Knapsack constraint
 3*x1 + 5*x2 + 2*x3 >= 30

! Additional global constraint
 all_different({x1,x2,x3})

! Objective function
 benefit = 5*x1 + 8*x2 + 4*x3

! Initial propagation
 res := cp_propagate

! Display bounds on objective after constraint propagation 
 writeln("Constraints propagation objective ", benefit)

! Solve the problem
 if (cp_minimize(benefit)) then  
  cp_show_sol                      ! Output optimal solution to screen
 end-if

end-model

knapsackalld_relax.mos
(!****************************************************************
   CP example problems
   ===================
   
   file knapsackalld_relax.mos
   ```````````````````````````
   Knapsack problem with additional alldifferent 
   constraint solved using linear relaxations.

   (c) 2009 Artelys S.A. and Fair Isaac Corporation
       Creation: 2009, rev. Mar. 2013       
*****************************************************************!)
model "Knapsack with side constraints"
 uses "kalis"

 declarations
  x1,x2,x3: cpvar                   ! Decision variables
  benefit : cpvar                   ! The objective to minimize
 end-declarations

! Enable output printing
 setparam("kalis_verbose_level", 1)

! Setting name of variables for pretty printing
 setname(x1,"x1"); setname(x2,"x2"); setname(x3,"x3") 
 setname(benefit,"benefit")

! Set initial domains for variables
 setdomain(x1, {1,3,8,12})
 setdomain(x2, {1,3,8,12})
 setdomain(x3, {1,3,8,12})

! Knapsack constraint
 3*x1 + 5*x2 + 2*x3 >= 30

! Additional global constraint
 all_different({x1,x2,x3})

! Objective function
 benefit = 5*x1 + 8*x2 + 4*x3

! Initial propagation
 res := cp_propagate

! Display bounds on objective after constraint propagation 
 writeln("Constraints propagation objective ", benefit)


 declarations
  myrelaxall: cplinrelax
 end-declarations
	
! Build an automatic 'LP' oriented linear relaxation
 myrelaxall:= cp_get_linrelax(0)
  
! Output the relaxation to the screen
 cp_show_relax(myrelaxall)  

 mysolver:= get_linrelax_solver(myrelaxall, benefit, KALIS_MINIMIZE, 
                KALIS_SOLVE_AS_MIP, KALIS_TOPNODE_RELAX_SOLVER)

! Define the linear relaxation 
 cp_add_linrelax_solver(mysolver)

! Define a 'MIP' style branching scheme using the solution of the 
! optimal relaxation
 cp_set_branching(assign_var(KALIS_LARGEST_REDUCED_COST(mysolver), 
                             KALIS_NEAREST_RELAXED_VALUE(mysolver)))

! Solve the problem
 if (cp_minimize(benefit)) then  
  cp_show_sol                      ! Output optimal solution to screen
 end-if

end-model