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

Basic LP tasks: problem statement and solving; solution analysis

Description
The first model (file chess.mos) is a small, introductory problem to modeling with Mosel that shows the basic features of the Mosel language:
• formulation of a simple LP/IP problem
• solving an optimization problem
• solution printout
The second model (file chess2.mos) shows some more advanced features of Mosel, namely the data structures set and array:
• formulating and solving a simple LP problem
• defining a set of variables and an array of descriptions for the variables (to be used in the output printing)
Detailed solution information for MIP problems as may be required when performing sensitivity analysis, such as dual and reduced cost values (file chessfixg.mos) or ranging information for variables and constraints (file chessrng.mos), is not immediately available during or after the branch-and-bound search. The following procedure needs to be used to generate the sensitivity analysis data:
1. Solve the MIP problem.
2. Fix all discrete variables to their solution values.
3. Re-solve the remaining LP problem.
4. Retrieve the solution information.
Further explanation of this example: 'Mosel User Guide', Chapter 1 Getting started with Mosel and Section 8.1 Initializing sets, or the book 'Applications of optimization with Xpress-MP', Section 1.3 Solving the chess set problem.

Source Files
By clicking on a file name, a preview is opened at the bottom of this page.

chessfixgcb.mos

(!******************************************************
Mosel Example Problems
======================

file chessfixgcb.mos

Display solution information for a MIP problem.

(c) 2008 Fair Isaac Corporation
author: S. Heipcke, Dec. 2007, rev. Mar. 2022
*******************************************************!)

model "Chess (fixglobal)"
uses "mmxprs"

forward procedure printsol

public declarations
R = 1..2                               ! Index range
DUR, WOOD, PROFIT: array(R) of real    ! Coefficients
x: array(R) of mpvar                   ! Array of variables
ifusesoft: mpvar                       ! Whether soft wood is used
CtrSet: set of linctr                  ! Set of constraints
MTime, Wood: linctr                    ! Constraints
end-declarations

DUR   :: [3, 2]                         ! Initialize data arrays
WOOD  :: [1, 3]
PROFIT:: [5, 20]

Wood:= sum(i in R) WOOD(i)*x(i) <= 200  ! Limit on raw material
! Add. capacity if using soft wood
MTime:= sum(i in R) DUR(i)*x(i) <= 160 + 40*ifusesoft
ifusesoft is_binary

CtrSet:= {MTime, Wood}

Profit:= sum(i in R) PROFIT(i)*x(i)

! Set the integer solution callback
setcallback(XPRS_CB_INTSOL, ->printsol)

! setparam("XPRS_verbose", true)
setparam("realfmt","%.4g")             ! Set real number display format
maximize(Profit)
writeln("\nAfter global: Solution: ", getobjval)

fixglobal                              ! Fix discrete variables

maximize(XPRS_LIN, Profit)             ! Solve reduced problems as LP

writeln("\nAfter fixglobal: Solution: ", getobjval)
writeln("Ctr: \tDual  \t Slack + Activity = RHS")
forall(c in CtrSet)
writeln(" ", getname(c), ":\t", getdual(c), "\t ", getslack(c), "\t",
getact(c), "\t", -1*getcoeff(c))

writeln("Var:   Solution\t RCost")
forall(i in R)
writeln(" x(", i, "):  ", getsol(x(i)), "\t ", getrcost(x(i)))

!******************************************************************
! Definition of the integer solution callback
procedure printsol
writeln("CB: Solution: ", getsol(Profit))
writeln("Ctr: \tDual  \t Slack + Activity = RHS")
forall(c in CtrSet)
writeln(" ", getname(c), ":\t", getdual(c), "\t ", getslack(c), "\t",
getact(c), "\t", -1*getcoeff(c))

writeln("Var:   Solution\t RCost")
forall(i in R)
writeln(" x(", i, "):  ", getsol(x(i)), "\t ", getrcost(x(i)))
end-procedure

end-model