FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious example

Defining a linear relaxation

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
Further explanation of this example: 'Xpress Kalis Mosel Reference Manual'

customrelax.zip[download all files]

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





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, rev. Jun. 2023      
*****************************************************************!)
model "Knapsack with side constraints"
 uses "kalis"

 declarations
  x1,x2,x3: cpvar                   ! Decision variables
  cost: 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(cost,"cost")

! 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
 cost = 5*x1 + 8*x2 + 4*x3

! Initial propagation
 if not cp_propagate: exit(1)

! Display bounds on objective after constraint propagation
 writeln("Constraint propagation objective ", cost)


 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, cost, 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
 br1 := KALIS_LARGEST_REDUCED_COST(mysolver)
 br2 := KALIS_NEAREST_RELAXED_VALUE(mysolver)
 cp_set_branching(assign_var(br1, br2))

! Solve the problem
 if cp_minimize(cost): 
   cp_show_sol                  ! Output optimal solution to screen

end-model

Back to examples browserPrevious example