Initializing help system before first use

problem.getSolution

problem.getSolution


Purpose
Returns the solution to an optimization problem if called after the solve() function has terminated. This function works with both continuous and mixed-integer optimization problems.
Synopsis
x = problem.getSolution(args=None, flatten=False)
Arguments
args 
(optional) specify indices, names, or objects whose solution value is requested. If None, it is assumed that all indices of the problem's variables are requested. Starting with version 8.8, args can contain expressions, both linear and nonlinear, and dictionaries thereof, in order to allow for more flexible evaluation of functions of the problem solution
flatten 
(optional) allows for backward compatibility with previous versions of the Xpress Python interface. Regardless of whether the passed object is a (nested) list, tuples, the returned value is a flattened list containing all requested values.
Example 1
Below are a few possible uses of the function. Note that one can specify variable names, variable indices, or variable objects, and embed them in lists, dictionaries, NumPy arrays, and tuples.
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(range(4))        # Gets the value of v1 and x[0], x[1], x[2], i.e.
                                   # the first four variables of the problem
c = m.getSolution('Var1')          # Gets the value of v1 by its name
e = m.getSolution({1: x, 2: 0,
                   3: 'Var1'})     # Returns a dictionary containing the same keys as
                                   # in the arguments and the values of the
                                   # variables/expressions passed
d = m.getSolution(v1 + 3*x)        # Gets the value of an expression under the
                                   # current solution
e = m.getSolution(np.array(x))     # Gets a NumPy array with the solution of x

y=xpress.var(name='var1')
x=xpress.var(name='var2')
[...]
p.solve()
print("solution:",   p.getSolution())
print("x is",        p.getSolution(x))
print("first two var:",   p.getSolution([0,1]))
print("x and y are", p.getSolution(['var1', 'var2']))
Example 2
The next examples show how to use the flatten argument, which ensures that the returned value is a flattened list.
y=xpress.var(name='var1')
x=xpress.var(name='var2')
[...]
p.solve()
print("x is",           p.getSolution([[x,y],[x,y]], flatten=True)) # will return [0,1,0,1]
print("first two var:", p.getSolution(0,1, flatten=True))  # will return the list [0,1]
Further information

For efficiency reasons it is preferable that one call to getSolution is made, as the whole vector is obtained at each call and only the desired portion is returned.

The function xpress.evaluate is more flexible in that it allows more argument types. Apart from the case where the args argument contains indices and names of the variables, this function is equivalent to a call to xpress.evaluate.


Related topics

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