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





burglar.py

'''*******************************************************
  * Python Example Problems                             *
  *                                                     *
  * file burglar.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 = range(8)

WTMAX = 102  # Max weight allowed for haul

p = xp.problem()

x = [p.addVariable(vartype=xp.binary) for _ in Items]

VALUE = [15, 100, 90, 60, 40, 15, 10, 1]
WEIGHT = [2, 20, 20, 30, 40, 30, 60, 10]

# 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