Initializing help system before first use

Robust formulations of the single knapsack problem


Type: Knapsack problem
Rating: 2 (easy-medium)
Description: Introductory example from the whitepaper 'Robust Optimization with Xpress'. Alternative robust formulations of a small single knapsack problem:
  • deterministic version: knapsack_basic.mos
  • ellipsoidal uncertainty: knapsack_ellipsoid.mos
  • polyhedral uncertainty: knapsack_polyhedron.mos
  • historical data used as uncertainty scenario: knapsack_scenario.mos
  • simple bounded uncertains: knapsack_simplebounds.mos, knapsack_simplebounds_direct.mos
File(s): knapsack_basic.mos, knapsack_ellipsoid.mos, knapsack_polyhedron.mos, knapsack_scenario.mos, knapsack_simplebounds.mos, knapsack_simplebounds_direct.mos


knapsack_basic.mos
(!******************************************************
   Mosel Example Problems
   ======================   

   Example model for the 
   Robust Optimization with Xpress white paper

   (c) 2014 Fair Isaac Corporation
       
*******************************************************!)
model Knapsack
 uses "mmrobust"                                 ! Load the robust library

 parameters
  NUM=5                                          ! Number of items
  MAXVAL=100                                     ! Maximum value
  MAXWEIGHT=80                                   ! Maximum weight
  WTMAX=102                                      ! Max weight allowed for haul
 end-parameters

 declarations
  Items=1..NUM                                   ! Index range for items
  VALUE: array(Items) of real                    ! Value of items
  WEIGHT: array(Items) of real                   ! Weight of items
  x: array(Items) of mpvar                       ! Decision variables
 end-declarations
 
 setrandseed(5);
 forall(i in Items) do
  VALUE(i):=50+random*MAXVAL
  WEIGHT(i):=1+random*MAXWEIGHT
 end-do
 forall(i in Items) x(i) is_binary               ! All x are 0/1

 MaxVal:= sum(i in Items) VALUE(i)*x(i)          ! Objective: maximize total value
 WtMax:= sum(i in Items) WEIGHT(i)*x(i) <= WTMAX ! Weight restriction

 maximize(MaxVal)

 writeln("Solution:\n Objective: ", getobjval)
 writeln("Item  Weight  Value")
 forall(i in Items)
   writeln(i, ": ", getsol(x(i)), strfmt(WEIGHT(i),8,2), strfmt(VALUE(i),8,2))

end-model

knapsack_ellipsoid.mos
(!******************************************************
   Mosel Example Problems
   ======================   

   Example model for the 
   Robust Optimization with Xpress white paper

   (c) 2014 Fair Isaac Corporation
       rev. May 2017       
*******************************************************!)
model Knapsack
 uses "mmrobust"                               ! Load the robust library

 parameters
  NUM=5                                        ! Number of items
  MAXVAL=100                                   ! Maximum value
  MAXWEIGHT=80                                 ! Maximum weight
  WTMAX=102                                    ! Max weight allowed for haul
  WTPERCENT=0.3                                ! Uncertainty as a percentage 
 end-parameters

 declarations
  Items=1..NUM                                 ! Index range for items
  VALUE: array(Items) of real                  ! Value of items
  WEIGHT: array(Items) of real                 ! Weight of items
  x: array(Items) of mpvar                     ! Decision variables
  WeightUncertainty: array(Items) of uncertain ! Uncertains representing
                                               ! deviation from weight
 end-declarations

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

 setrandseed(5);
 forall(i in Items) do
  VALUE(i):=50+random*MAXVAL
  WEIGHT(i):=1+random*MAXWEIGHT
 end-do

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

 sum(i in Items) WeightUncertainty(i)^2 <= WTPERCENT * sum(i in Items) WEIGHT(i)
 WtMax:= sum(i in Items) (WEIGHT(i) + WeightUncertainty(i))*x(i) <= WTMAX
                                               ! Weight restriction

 maximize(MaxVal) 

 writeln("Solution:\n Objective: ", getobjval)
 writeln("Item  Weight  Value  Uncertain")
 forall(i in Items)
   writeln(i, ": ", getsol(x(i)), strfmt(WEIGHT(i),8,2), strfmt(VALUE(i),8,2), strfmt(getsol(WeightUncertainty(i)),8,2))
 writeln("Total weight: ", getact(WtMax))

end-model

knapsack_polyhedron.mos
(!******************************************************
   Mosel Example Problems
   ======================   

   Example model for the 
   Robust Optimization with Xpress white paper

   (c) 2014 Fair Isaac Corporation
       rev. May 2017
*******************************************************!)
model Knapsack
 uses "mmrobust"                               ! Load the robust library

 parameters
  NUM=5                                        ! Number of items
  MAXVAL=100                                   ! Maximum value
  MAXWEIGHT=80                                 ! Maximum weight
  WTMAX=102                                    ! Max weight allowed for haul
  WTPERCENT=0.3                                ! Uncertainty as a percentage 
 end-parameters

 declarations
  Items=1..NUM                                 ! Index range for items
  VALUE: array(Items) of real                  ! Value of items
  WEIGHT: array(Items) of real                 ! Weight of items
  x: array(Items) of mpvar                     ! Decision variables
  WeightUncertainty: array(Items) of uncertain ! Uncertains representing 
                                               ! deviation from weight
 end-declarations

 forall(i in Items) x(i) is_binary             ! All x are 0/1
 
 setrandseed(5);
 forall(i in Items) do
  VALUE(i):=50+random*MAXVAL
  WEIGHT(i):=1+random*MAXWEIGHT
 end-do

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

 forall(i in Items) do  
  WeightUncertainty(i) >=  0
 end-do

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

 maximize(MaxVal)

 writeln("Solution:\n Objective: ", getobjval)
 writeln("Item  Weight  Value")
 forall(i in Items)
   writeln(i, ": ", getsol(x(i)), strfmt(WEIGHT(i),8,2), strfmt(VALUE(i),8,2))
 writeln("Total weight: ", getact(WtMax)) 

end-model

knapsack_scenario.mos
(!******************************************************
   Mosel Example Problems
   ======================   

   Example model for the 
   Robust Optimization with Xpress white paper

   (c) 2014 Fair Isaac Corporation
       rev. May 2017 
*******************************************************!)
model Knapsack
 uses "mmrobust"                         ! Load the robust library

 parameters
  NUM=5                                  ! Number of items
  MAXVAL=100                             ! Maximum value
  MAXWEIGHT=80                           ! Maximum weight
  WTMAX=102                              ! Max weight allowed for haul
  UNCERTAINTY_LEVEL=0.3                  ! How much we are uncertain about the weight
  HISTORIC_PERIODS=100                   ! Number of scenarios
 end-parameters

 declarations
  Items=1..NUM                           ! Index range for items
  VALUE: array(Items) of real            ! Value of items
  WEIGHT: array(Items) of real           ! Weight of items
  UncertainWeight:array(Items) of uncertain 
  x: array(Items) of mpvar               ! 1 if we take item i; 0 otherwise
  historical_weights: array(range, set of uncertain) of real
 end-declarations 
 
 forall(i in Items) x(i) is_binary       ! All x are 0/1

 setrandseed(5);
 forall(i in Items) do
  VALUE(i):=50+random*MAXVAL
  WEIGHT(i):=1+random*MAXWEIGHT
 end-do

 MaxVal:= sum(i in Items) VALUE(i)*x(i)  ! Objective: maximize total value
 WtMax:= sum(i in Items) (WEIGHT(i)+UncertainWeight(i))*x(i) <= WTMAX 

 ! Generate historical data, this would be data collected from actual realizations
 forall(period in 1..HISTORIC_PERIODS, i in Items)
   historical_weights(period, UncertainWeight(i)) :=
     WEIGHT(i)*UNCERTAINTY_LEVEL*random
 ! Generate a solution that would be feasible for ALL historic realizations
 scenario(historical_weights)

 maximize(MaxVal)   

 writeln("Solution:\n Objective: ", getobjval)
 writeln("Item  Weight  Value")
 forall(i in Items)
   writeln(i, ": ", getsol(x(i)), strfmt(WEIGHT(i),8,2), strfmt(VALUE(i),8,2))
 writeln("Total weight: ", getact(WtMax)) 

end-model

knapsack_simplebounds.mos
(!******************************************************
   Mosel Example Problems
   ======================   

   Example model for the 
   Robust Optimization with Xpress white paper

   (c) 2014 Fair Isaac Corporation
       rev. May 2017 
*******************************************************!)
model Knapsack
 uses "mmrobust"                               ! Load the robust library

 parameters
  NUM=5                                        ! Number of items
  MAXVAL=100                                   ! Maximum value
  MAXWEIGHT=80                                 ! Maximum weight
  WTMAX=102                                    ! Max weight allowed for haul
  WTPERCENT=0.3                                ! Uncertainty as a percentage 
 end-parameters

 declarations
  Items=1..NUM                                 ! Index range for items
  VALUE: array(Items) of real                  ! Value of items
  WEIGHT: array(Items) of real                 ! Weight of items
  x: array(Items) of mpvar  
  WeightUncertainty: array(Items) of uncertain ! Uncertains representing 
                                               ! deviation from weight
 end-declarations

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

 setrandseed(5);
 forall(i in Items) do
  VALUE(i):=50+random*MAXVAL
  WEIGHT(i):=1+random*MAXWEIGHT
 end-do

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

 forall(i in Items) do
  WeightUncertainty(i) <=  WTPERCENT*WEIGHT(i) ! Uncertainty is a percentage of
                                               ! the expected weight
  WeightUncertainty(i) >=  0                   ! and only expected to go up this time
 end-do
 WtMax:= sum(i in Items) (WEIGHT(i) + WeightUncertainty(i))*x(i) <= WTMAX 
                                               ! Weight restriction

 maximize(MaxVal) 

 writeln("Solution:\n Objective: ", getobjval)
 writeln("Item  Weight  Value")
 forall(i in Items)
   writeln(i, ": ", getsol(x(i)), strfmt(WEIGHT(i),8,2), strfmt(VALUE(i),8,2))
 writeln("Total weight: ", getact(WtMax)) 

end-model

knapsack_simplebounds_direct.mos
(!******************************************************
   Mosel Example Problems
   ======================   

   Example model for the 
   Robust Optimization with Xpress white paper

   (c) 2014 Fair Isaac Corporation
       rev. May 2017
*******************************************************!)
model Knapsack
 uses "mmrobust"                               ! Load the robust library

parameters
  NUM=5                                        ! Number of items
  MAXVAL=100                                   ! Maximum value
  MAXWEIGHT=80                                 ! Maximum weight
  WTMAX=102                                    ! Max weight allowed for haul
  WTPERCENT=0.3                                ! Uncertainty as a percentage 
 end-parameters

 declarations
  Items=1..NUM                                 ! Index range for items
  VALUE: array(Items) of real                  ! Value of items
  WEIGHT: array(Items) of real                 ! Weight of items
  x: array(Items) of mpvar                     ! Decision variables
 end-declarations

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

 setrandseed(5);
 forall(i in Items) do
  VALUE(i):=50+random*MAXVAL
  WEIGHT(i):=1+random*MAXWEIGHT
 end-do

 MaxVal:= sum(i in Items) VALUE(i)*x(i)        ! Objective: maximize total value
 WtMax:= sum(i in Items) WEIGHT(i)*(1+WTPERCENT)*x(i) <= WTMAX
                                               ! Weight restriction

 maximize(MaxVal)

 writeln("Solution:\n Objective: ", getobjval)
 writeln("Item  Weight  Value")
 forall(i in Items)
   writeln(i, ": ", getsol(x(i)), strfmt(WEIGHT(i),8,2), strfmt(VALUE(i),8,2))
 writeln("Total weight: ", getact(WtMax)) 

end-model

© 2001-2024 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.