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.

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

Source Files
By clicking on a file name, a preview is opened at the bottom of this page.

Data Files

burglar_rec.py

'''******************************************************
Python Example Problems

file burglar_rec.py

(c) 2018-2024 Fair Isaac Corporation
*******************************************************'''

from __future__ import print_function
import xpress as xp
from Data.burglar_rec_dat import I

WTMAX = 102  # Maximum weight allowed
ITEMS = set(["camera", "necklace", "vase", "picture", "tv", "video",
"chest", "brick"])  # Index set for items

p = xp.problem()

take = {i: p.addVariable(vartype=xp.binary) for i in I.keys()}

# Objective: maximize total value
p.setObjective(xp.Sum(I[i][0] * take[i] for i in ITEMS),
sense=xp.maximize)

# Weight restriction
p.addConstraint(xp.Sum(I[i][0] * take[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(" take(", i, "): ", p.getSolution(take[i]))