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

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.

Further explanation of this example: 'Xpress Python Reference Manual'

burglar_python.zip[download all files]

Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
burglar.py[download]
burglari.py[download]
burglarl.py[download]
burglar_rec.py[download]

Data Files





burglari.py

'''*******************************************************
  * Python Example Problems                             *
  *                                                     *
  * file burglari.py                                    *
  * Example for the use of the Python language          *
  * (Burglar problem)                                   *
  *                                                     *
  * (c) 2018-2024 Fair Isaac Corporation                *
  *******************************************************'''

from __future__ import print_function
import xpress as xp

Items = set(["camera", "necklace", "vase", "picture", "tv", "video",
             "chest", "brick"])  # Index set for items

WTMAX = 102  # Max weight allowed for haul

VALUE = {"camera": 15, "necklace": 100, "vase": 90, "picture": 60,
         "tv": 40, "video": 15, "chest": 10, "brick": 1}

WEIGHT = {"camera": 2, "necklace": 20, "vase": 20, "picture": 30,
          "tv": 40, "video": 30, "chest": 60, "brick": 10}

p = xp.problem()

x = p.addVariables(Items, vartype=xp.binary)  # 1 if we take item i; 0 otherwise

# Objective: maximize total value
p.setObjective(xp.Sum(VALUE[i]*x[i] for i in Items), sense=xp.maximize)

# Weight restriction
p.addConstraint(xp.Sum(WEIGHT[i]*x[i] for i in Items) <= WTMAX)

p.optimize()                   # Solve the MIP-problem

# Print out the solution
print("Solution:\n Objective: ", p.getObjVal())
for i in Items:
    print(" x(", i, "): ", p.getSolution(x[i]))

Back to examples browserPrevious exampleNext example