| |||||||||||||||||||||
| |||||||||||||||||||||
|
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 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()
| |||||||||||||||||||||
| © Copyright 2025 Fair Isaac Corporation. |