| |||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||
|
Introductory examples Description
Further explanation of this example: 'Applications of optimization with Xpress-MP', Introductory examples (Chapters 1 to 5) of the book 'Applications of optimization with Xpress-MP'
Source Files By clicking on a file name, a preview is opened at the bottom of this page. Data Files burglari.mos
(!******************************************************
Mosel Example Problems
======================
file burglari.mos
`````````````````
Knapsack problem
-- Using string indices --
-- Using string indices --
A burglar considers eight items that have different values
and weights. He wants to take a group of items that maximizes
the total value while the total weight is not more than the
maximum 'WTMAX' he can carry.
This IP model represents the so-called knapsack problem where
binary decision variable 'take(i)' takes value 1 if item 'i'
is taken; 0 otherwise. This implementation illustrates the
use of string indices having the data embedded in the model file.
(c) 2008 Fair Isaac Corporation
author: R.C. Daniel, Jul. 2002
*******************************************************!)
model "Burglar 1 (index set)"
uses "mmxprs"
declarations
ITEMS = {"camera", "necklace", "vase", "picture", "tv", "video",
"chest", "brick"} ! Set for items
WTMAX = 102 ! Maximum weight allowed
VALUE: array(ITEMS) of real ! Value of items
WEIGHT: array(ITEMS) of real ! Weight of items
take: 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
(! Alternative data initialization:
VALUE :: (["camera", "necklace", "vase", "picture", "tv", "video",
"chest", "brick"])[15, 100, 90, 60, 40, 15, 10, 1]
WEIGHT:: (["camera", "necklace", "vase", "picture", "tv", "video",
"chest", "brick"])[ 2, 20, 20, 30, 40, 30, 60, 10]
!)
! Objective: maximize total value
MaxVal:= sum(i in ITEMS) VALUE(i)*take(i)
! Weight restriction
sum(i in ITEMS) WEIGHT(i)*take(i) <= WTMAX
! All variables are 0/1
forall(i in ITEMS) take(i) is_binary
maximize(MaxVal) ! Solve the MIP-problem
! Print out the solution
writeln("Solution:\n Objective: ", getobjval)
forall(i in ITEMS) writeln(" take(", i, "): ", getsol(take(i)))
end-model
| |||||||||||||||||||||||||||||||||||||||
| © Copyright 2025 Fair Isaac Corporation. | |||||||||||||||||||||||||||||||||||||||