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

Multi-objective knapsack problem

Example of a multi-objective knapsack problem formulated with the Xpress Python interface.

Further explanation of this example: 'Xpress Python Reference Manual'[download all files]

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

# Multi-objective knapsack example
# (c) Fair Isaac Corp., 2022

import xpress as xp
import numpy as np

n = 15            # Number of items
max_weight = 10   # Maximum weight that can be carried

# Random weights and two random value metrics
weights = np.random.randint(1, 4, n)
values = np.random.randint(1, 6, (2, n))

# Decision variables for each item
take = xp.vars(n, vartype=xp.binary)

# Total weight cannot exceed maximum weight
ctr = xp.Dot(take, weights) <= max_weight

p = xp.problem(take, ctr, sense=xp.maximize)

# Define the two objectives
p.setObjective(xp.Dot(take, values[0]), objidx=0, priority=2)
p.setObjective(xp.Dot(take, values[1]), objidx=1, priority=1)

# Solve the problem

if p.attributes.solvestatus == xp.SolveStatus.COMPLETED and p.attributes.solstatus == xp.SolStatus.OPTIMAL:
  print('Problem was solved to optimality')
  sol = p.getSolution(take)
  print('Items selected:', ', '.join(str(i) for i in range(n) if sol[i]))
  print('Total weight:', xp.Dot(sol, weights))
  print('First objective:', p.calcobjn(0))
  print('Second objective:', p.calcobjn(1))
elif p.attributes.solvedobjs == 1:
  print(f'Failed to solve first objective')
  print('Solved first objective but failed to solve second objective')

Back to examples browserPrevious exampleNext example