 FICO Xpress Optimization Examples Repository
 FICO Optimization Community FICO Xpress Optimization Home   Basic MIP tasks: binary variables; logic constraints

Description
We wish to choose among items of different value and weight those that result in the maximum total value for a given weight limit.
• small MIP problem
• alternative use of number-valued ranges and sets of strings for indexing variables and data
• definition of binary variables
• forall statement
• formulation of logic constraints using indicators or advmod functionality (burglarl.mos)
Further explanation of this example: 'Mosel User Guide', Section 2.1 The burglar problem.

Source Files

burglarl.mos

```(!*******************************************************
* Mosel Example Problems                              *
* ======================                              *
*                                                     *
* file burglarl.mos                                   *
* `````````````````                                   *
* Example for the use of the Mosel language           *
* (Burglar problem)                                   *
* -- Formulation of logical constraints --            *
*                                                     *
* (c) 2009 Fair Isaac Corporation                     *
*     author: S. Heipcke, June 2009                   *
*******************************************************!)

model "Burglar (logic constraints)"

uses "advmod"                       ! Use logic constraints package

declarations
Items={"camera", "necklace", "vase", "picture", "tv", "video",
"chest", "brick"}           ! Index set for items

VALUE: array(Items) of real        ! Value of items
WEIGHT: array(Items) of real       ! Weight of items
WTMAX=102                          ! Max weight allowed for haul

x: array(Items) of mpvar           ! 1 if we take item i; 0 otherwise
end-declarations

VALUE("camera")  := 15;  WEIGHT("camera")  :=  2
VALUE("necklace"):=100;  WEIGHT("necklace"):= 20
VALUE("vase")    := 90;  WEIGHT("vase")    := 20
VALUE("picture") := 60;  WEIGHT("picture") := 30
VALUE("tv")      := 40;  WEIGHT("tv")      := 40
VALUE("video")   := 15;  WEIGHT("video")   := 30
VALUE("chest")   := 10;  WEIGHT("chest")   := 60
VALUE("brick")   :=  1;  WEIGHT("brick")   := 10

MaxVal:= sum(i in Items) VALUE(i)*x(i)  ! Objective: maximize total value

! Weight restriction
WtMax:= sum(i in Items) WEIGHT(i)*x(i) <= WTMAX

forall(i in Items) x(i) is_binary  ! All x are 0/1

! *** Logic constraint:
! *** Either take "vase" and "picture" or "tv" and "video" (but not both pairs).

! * Values within each pair are the same
Log1:= x("vase") = x("picture")
Log2:= x("tv") = x("video")

! * Choose exactly one pair (uncomment one of the 3 formulations A, B, or C)

! (A) MIP formulation
!  Log3:= x("tv") = 1 - x("vase")

! (B) Logic constraint
Log3:= xor(x("vase")+x("picture")>=2, x("tv")+x("video")>=2)

! (C) Alternative logic formulation (does not create additional binaries)
(!
Log3a:= indicator(1, x("vase"), x("tv")+x("video") <= 0)
! x("vase")=1 -> x("tv")+x("video")=0
Log3b:= indicator(-1, x("vase"), x("tv")+x("video") >= 2)
! x("vase")=0 -> x("tv")+x("video")=2
!)

maximize(MaxVal)                   ! Solve the MIP-problem

! Print out the solution
writeln("Solution:\n Objective: ", getobjval)
forall(i in Items)  writeln(" x(", i, "): ", x(i).sol)

end-model

```   