Initializing help system before first use

Using NumPy and Xpress


Type: Programming
Rating: 2 (easy-medium)
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.
File(s): example_numpy1.py, example_numpy2.py


example_numpy1.py
#
# Example: use numpy to print the product of a matrix by a random vector.
#
# Uses xpress.Dot to on a matrix and a vector. Note that the NumPy dot operator
# works perfectly fine here.
#

from __future__ import print_function

import numpy as np
import xpress as xp

x = [xp.var() for i in range(5)]

p = xp.problem()
p.addVariable(x)
p.addConstraint(xp.Sum(x) >= 2)

p.setObjective(xp.Sum(x[i]**2 for i in range(5)))
p.solve()

A = np.array(range(30)).reshape(6, 5)  # A is a 6x5 matrix
sol = np.array(p.getSolution())  # suppose it's a vector of size 5
columns = A*sol         # not a matrix-vector product!
v = xp.Dot(A, sol)      # this is a matrix-vector product A*sol

print(v)

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.
#

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
x = np.array([xp.var() for i in range(5)])  # vector of variables
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.solve()

© 2001-2019 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.