Initializing help system before first use

Implementation with Python

Topics covered in this section:

The Python interface contains classes and methods for stating optimization models in a convenient way.

The following Python program implements the LP example introduced in Chapter 2:

import xpress as xp

# Problem data
NSHARES = 10
RET = [5, 17, 26, 12, 8, 9, 7, 6, 31, 21]
RISK = [1, 2, 3, 8, 9]
NA = [0, 1, 2, 3]

p = xp.problem("Folio")

# VARIABLES.
frac = p.addVariables(NSHARES, ub=0.3, name="frac")

# CONSTRAINTS.
# Limit the percentage of high-risk values.
p.addConstraint(xp.Sum(frac[i] for i in RISK) <= 1/3)

# Minimum amount of North-American values.
p.addConstraint(xp.Sum(frac[i] for i in NA) >= 0.5)

# Spend all the capital.
p.addConstraint(xp.Sum(frac) == 1)

# Objective: maximize total return.
p.setObjective(xp.Sum(frac[i] * RET[i] for i in range(NSHARES)), sense=xp.maximize)

# Solve.
p.optimize()

# Print problem status.
print(f"Problem status: \n\t Solve status: {p.attributes.solvestatus.name} \n\t Sol status: \
    {p.attributes.solstatus.name}")

# Solution printing.
print("Total return:", p.attributes.objval)
sol = p.getSolution(frac)
for i in range(NSHARES):
    print(f"{frac[i].name} : {sol[i]*100:.2f} %")

Let us now have a closer look at what we have just written.

Initialization

To use the Python interface you need to import the xpress package, which you use to create an empty problem, and later add elements such as variables and constraints.

The Xpress Python interface is initialized when you create the first problem instance:

p = xp.problem("Folio")

The optional name argument can be used to give a name to the problem.

General structure

The definition of the model itself starts with the creation of the decision variables (method addVariables), followed by the definition of the objective function and the constraints. The argument ub is used to set the upper bounds on the decision variables frac.

You can create constraints by using linear expressions, as shown in the example. Equivalently, they can be constructed from expression objects using the xpress.constraint class, such as the constraint limiting the percentage of high-risk shares:

constr = xp.constraint(body=xp.Sum(frac[i] for i in RISK), type=xp.leq, rhs=1/3, name='constr')
p.addConstraint(constr)

Using xpress.constraint allows you to give a name to constraint objects. Names can be useful for debugging but otherwise have no effect on the optimizer.

A more efficient way of modelling the objective function is by using the xpress.Dot operator for the dot-product between vectors frac and RET. Besides importing the numpy package, this method would require RET to be defined as a NumPy array:

import xpress as xp
import numpy as np
...
RET = np.array([5, 17, 26, 12, 8, 9, 7, 6, 31, 21])
...
p.setObjective(xp.Dot(frac, RET), sense=xp.maximize)

Using NumPy arrays and the xp.Dot operator may lead to substantial gains in model building performance for large scale instances by leveraging NumPy's underlying C implementation of vectorized operations.

Solving

Prior to launching Xpress, the objective expression xp.Sum(frac[i] * RET[i] for i in range(NSHARES)) is set to be maximized with a call to the setObjective method. With the method optimize, Xpress is called to maximize the objective function subject to all constraints that have been defined. Since the problem contains only continuous variables, an LP algorithm will be determined automatically. The method returns the solve and solution statuses of the problem after the run, which are printed in our example.

Output printing

The last few lines print out the value of the optimal solution and the solution values for all variables.


© 2001-2025 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.