| burglar.py |
# Example for the use of the Python language (Burglar problem).
#
# Data given in the model.
#
# (C) 2018-2025 Fair Isaac Corporation
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.attributes.objval)
xsol = p.getSolution(x)
for i in Items:
print(" x(", i, "): ", xsol[i])
|
|
| burglari.py |
# Example for the use of the Python language (Burglar problem).
#
# Data with index set for items given in the model.
#
# (C) 2018-2025 Fair Isaac Corporation
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.attributes.objval)
xsol = p.getSolution(x)
for i in Items:
print(" x(", i, "): ", xsol[i])
|
|
| burglarl.py |
# Example for the use of the Python language (Burglar problem).
#
# Formulation of logical constraints.
#
# (C) 2018-2025 Fair Isaac Corporation
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)
# *** Logic constraint:
# *** Either take "vase" and "picture" or "tv" and "video"
# (but not both pairs).
# * Values within each pair are the same.
p.addConstraint(x["vase"] == x["picture"])
p.addConstraint(x["tv"] == x["video"])
# * Choose exactly one pair (uncomment one of the 3 formulations A, B, or C).
# (A) MIP formulation.
# p.addConstraint(x["tv"] == 1 - x["vase"])
# (B) Logic constraint.
# Note: Xpress Python interface doesn't use xor.
# Need to introduce extra variable.
y = p.addVariable(vartype=xp.binary)
# (C) Alternative logic formulation.
p.addIndicator(y == 1, x["tv"] + x["video"] >= 2)
p.addIndicator(y == 0, x["vase"] + x["picture"] >= 2)
p.addConstraint(x["tv"] + x["video"] + x["vase"] + x["picture"] <= 3)
p.optimize() # Solve the MIP-problem.
# Print out the solution.
print("Solution:\n Objective: ", p.attributes.objval)
xsol = p.getSolution(x)
for i in Items:
print(" x(", i, "): ", xsol[i])
|
|
| burglar_rec.py |
# Example for the use of the Python language (Burglar problem).
#
# Reading data from file.
#
# (C) 2018-2025 Fair Isaac Corporation
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][1] * take[i] for i in ITEMS) <= WTMAX)
p.optimize() # Solve the MIP-problem.
# Print out the solution.
print("Solution:\n Objective: ", p.attributes.objval)
takesol = p.getSolution(take)
for i in ITEMS:
print(" take(", i, "): ", takesol[i])
|
|
| knapsack.py |
# Example of a knapsack problem formulated with the Xpress Python interface.
#
# (C) 1983-2025 Fair Isaac Corporation
import xpress as xp
import numpy as np
S = range(5) # The set {0,1,2,3,4}
value = np.array([102, 512, 218, 332, 41])
weight = np.array([21, 98, 44, 59, 9])
p = xp.problem("knapsack")
x = p.addVariables(5, vartype=xp.binary)
profit = xp.Dot(value,x)
p.addConstraint(xp.Dot(weight,x) <= 130)
p.setObjective(profit, sense=xp.maximize)
p.optimize()
|
|