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

Using NumPy and Xpress

Description
An example of printing a matrix of random numbers and a problem formulation that uses the xpress.Dot() operator to formulate constraints simply. Note that the NumPy dot operator is not suitable here as the result is an expression in the Xpress variables.

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

NumPy2_python.zip[download all files]

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





example_numpy2.py

# An example of a problem formulation that uses the xpress.Dot() operator
# to formulate constraints simply. Note that the NumPy dot operator is not
# suitable here as the result is an expression in the Xpress variables.
#
# (C) Fair Isaac Corp., 1983-2024

import xpress as xp
import numpy as np

A = np.random.random(30).reshape(6, 5)  # A is a 6x5 matrix
Q = np.random.random(25).reshape(5, 5)  # Q is a 5x5 matrix

# Create a NumPy array of variables by using the xp.vars() function
x = xp.vars(5)
x0 = np.random.random(5)  # random vector

Q += 4 * np.eye(5)  # add 5 * the identity matrix

# 6 constraints (rows of A)
Lin_sys = xp.Dot(A, x) <= np.array([3, 4, 1, 4, 8, 7])

# One quadratic constraint
Conv_c = xp.Dot(x, Q, x) <= 1

p = xp.problem()

p.addVariable(x)
p.addConstraint(Lin_sys, Conv_c)
p.setObjective(xp.Dot(x-x0, x-x0))

p.optimize()

Back to examples browserPrevious exampleNext example