Initializing help system before first use

Examples using NumPy

Using NumPy multidimensional arrays to create variables

Use NumPy arrays for creating a 3-dimensional array of variables, then use it to create a mode.

S1 = range(2)
S2 = range(3)
S3 = range(4)

m = xp.problem()

h = np.array([[[xp.var(vartype = xp.binary)
                for i in S1]
                for j in S2]
                for k in S3])

m.addVariable(h)
	
m.setObjective (h[0][0][0] * h[0][0][0] +
                h[1][0][0] * h[0][0][0] +
                h[1][0][0] * h[1][0][0] +
                xp.Sum(h[i][j][k] for i in S3 for j in S2 for k in S1))

cons00 = - h[0][0][0] ** 2 +
         xp.Sum(i * j * k * h[i][j][k]for i in S3 for j in S2 for k in S1) >= 11

m.addConstraint(cons00)

m.solve()

The final part of the code retrieves the matrix representation of the quadratic part of the only constraint.

mstart1=[]
mclind1=[]
dqe1=[]
m.getqrowqmatrix(cons00, mstart1, mclind1, dqe1, 29, h[0][0][0], h[3][2][1])
print("row 0:", mstart1, mclind1, dqe1)

Using the dot product to create arrays of expressions

Here we use NumPy arrays to print the product of a matrix by a random vector, and the xpress.Dot function on a matrix and a vector. Note that the NumPy dot operator works perfectly fine here, but should be avoided for reasons of performance, especially when handling large arrays where at least one contains optimization variables or expressions.

x = np.array([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()) # a vector of size 5
columns = A*sol         # not a matrix-vector product!
v = np.dot(A,sol)      # an array: matrix-vector product A*sol
w = xp.Dot(A,x)        # an array of expressions

print(v,w)

Using the Dot product to create constraints and quadratic functions

This is an example of a problem formulation that uses the xpress.Dot operator to formulate constraints in a concise fashion. Note that the NumPy dot operator is not suitable here as the result is an expression in the Xpress variables.

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

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

p = xp.problem()

p.addVariable(x)
p.addConstraint(Lin_sys, Conv_c)
p.setObjective(xp.Dot(x-x0, x-x0)) # minimize distance from x0

p.solve()

Using NumPy to create quadratic optimization problems

This example creates and solves a simple quadratic optimization problem. Given an n×n matrix Q and a point x0, minimize the quadratic function xT (Q + n3 I) x subject to the linear system (x - x0)T Q + e = 0, where e is the vector of all ones, the inequalities Q x ≥ 0, and nonnegativity on all variables. Report solution if available.

n = 10

Q  = np.arange(1, n**2 + 1).reshape(n, n)
x  = np.array([xp.var() for i in range(n)])
x0 = np.random.random(n)

p = xp.problem()

p.addVariable(x)

c1 = xp.Dot((x - x0), Q) + 1 == 0
c2 = xp.Dot(Q, x) >= 0

p.addConstraint(c1,c2)
p.setObjective(xp.Dot(x, Q + N**3 * np.eye(N), x))

p.solve('')

print("nrows, ncols:", p.attributes.rows, p.attributes.cols)
print("solution:", p.getSolution())

p.write("test5-qp", "lp")

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