Initializing help system before first use

# problem.getSolution

Purpose
Obtains the incumbent solution during or after optimization with problem.optimize, problem.mipOptimize, problem.lpOptimize or problem.nlpOptimize.
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
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.

    x = p.addVariables(name='X')
    [...]
    a = p.getSolution())              # Returns a list with an optimal solution
    b = p.getSolution(x[0]))          # Only returns the value of x[0]
    c = p.getSolution(0)              # As above
    d = p.getSolution(x)              # Gets the values of all variables in the vector x
    e = p.getSolution(range(4))       # Gets the value of x[0], x[1], x[2], x[3], i.e.,
                                      # the first four variables of the problem
    f = p.getSolution('X(0)')         # Gets the value of x[0] by its name
    g = p.getSolution({               # Returns a dictionary containing:
      1: x,                           #   1: value of all variables
      2: 1,                           #   2: value of x[1]
      3: 'X(2)'                       #   3: value of x[2]
    })
    h = p.getSolution([               # Returns a list of lists: [
      [x[0], x[1]],                   #   [value of x[0], value of x[1]],
      [x[2], x[3]]                    #   [value of x[1], value of x[2]]
    ])                                # ]
    h = p.getSolution([               # Returns a flat list containing the values of
      [x[0], x[1]],                   # x[0], x[1], x[2], x[3]
      [x[2], x[3]]
    ], flatten=True)
    i = p.getSolution(x[0] + 3*x[1])  # Gets the value of an expression under the
                                      # current solution
    j = p.getSolution(np.array(x))    # Gets a NumPy array with the solution of x]

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, getSolution is equivalent to a call to xpress.evaluate.

Related topics
xpress.evaluate, problem.getDuals, problem.getSlacks, problem.getRedCosts, problem.getCallbackSolution, problem.getCallbackPresolveSolution, var.getSolution.
Parent Topic Methods of the class problem

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