Initializing help system before first use

Using NumPy arrays to create variables


Type: Programming
Rating: 2 (easy-medium)
Description: Use NumPy arrays for creating a 3-dimensional array of variables, then use it to create a model.
File(s): example_array.py


example_array.py
# Use NumPy arrays for creating a 3-dimensional array of variables,
# then use it to create a model.
#
# (C) Fair Isaac Corp., 1983-2024

import numpy as np
import xpress as xp

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

m = xp.problem()

# Create a NumPy array of variables using the xp.npvar keyword. This
# is to ensure NumPy handles Xpress variable objects.
h = np.array([[[m.addVariable(vartype=xp.binary) for i in S1]
               for j in S2] for k in S3], dtype=xp.npvar)

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] * h[0][0][0] + \
         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)

# By default the problem is solved to global optimality.
# Setting the nlpsolver control to one ensures the problem is
# solved the local nonlinear solver.
m.controls.nlpsolver = 1

m.optimize()

# Get the matrix representation of the quadratic part of the single
# 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)

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