Initializing help system before first use

Querying a problem

It is useful, after solving a problem, to obtain the value of an optimal solution. After solving a continuous or mixed integer problem, the two methods getSolution and getSlack return the vector (of portions thereof) of an optimal solution or the slack of the constraints. If an optimal solution was not found but a feasible solution is available, these methods will return data based on this solution. They can be used in multiple ways as shown in the following examples:

import xpress as xp

v1 = xp.var (name = 'Var1')
x  = [xp.var (lb = -1, ub = 1, vartype = xp.integer) for i in range(10)]

m  = xp.problem ()

m.addVariable (v1, x)

[...] # add constraints and objective

m.solve()

print (m.getSolution ())            # prints a list with an optimal solution
print ("v1 is", m.getSolution (v1)) # only prints the value of v1
a = m.getSolution (x)               # gets the values of all variables in the vector x
b = m.getSolution (0:4)             # gets the value of v1 and x[0], x[1], x[2]
c = m.getSolution ('Var1')          # gets the value of v1 by its name

Consider the last five lines. The first of them returns a list of ncol floating point scalars, where ncol is the number of variables of the problem (nrow is the number of constraints, the size of the vector returned by getSlack) containing the full solution. The second example retrieves the value of the single variable v1. The third example returns an array of the same size as x with the value of all variables of the list x. The fourth example shows that a range of indices can be specified in order to obtain a vector of values without specifying the corresponding variables. Recall that the column and row indices begin at 0. Finally, the last line shows that a variable can be passed by name.

The method getSlack works similarly, except constraints, integer indices, or constraint names must be passed. The following examples illustrate a few possible uses.

import xpress as xp

N = 10

x  = [xp.var (vartype = xp.binary) for i in range(N)]

m  = xp.problem ()

m.addVariable (x)

con1 = xp.Sum (x[i] * i for i in range (N)) <= N)
con2 = (x[i] >= x[i+1]  for i in range (N-1))

m.addConstraints (con1, con2)
m.setObjective (xp.Sum (x[i] for i in range (N))
m.solve ()

print (m.getSlack ())                   # prints a list of slacks for all N constraints
print ("slack_1 is", m.getSlack (con1)) # only prints the slack of con1

a = m.getSlack (con2)                # gets the slack of N-1 constraints con2
b = m.getSlack (0:2)                 # gets the slack of con1 and con2[0]

In addition, for problems with only continuous variables, the two methods getDual and getRCost return the the vector (or a portion thereof) of dual variables and reduced costs, respectively. Their usage is similar to that for getSolution and getSlack.

Note that the inner workings of the Python interface obtain a copy of the whole solution, slack, dual, or reduced cost vectors, even if only one element is requested. It is therefore advisable that instead of repeated calls (for instance, in a loop) to getSolution, getSlack, etc. only one call is made and the result is stored in a list to be consulted in the loop. Hence, in the following example:

import xpress as xp

n = 10000
N = range (n)

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

p = xp.problem ()

p.addVariable (x)
m.addConstraints (xp.Sum (x[i] * i for i in N) <= n))
m.setObjective (xp.Sum (x[i] for i in N)
m.solve ()

for i in N:
    if m.getSolution (x[i]) > 1e-3:
        print (i)

the last three lines should be substituted as follows, as this will prevent repeatedly copying a large (10,000) vector:

sol = m.getSolution ()

for i in N:
    if sol[i] > 1e-3:
        print (i)

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