xpress.evaluate
xpress.evaluate |
Returns the evaluation of one or more expressions for a given assignment of values to optimization variables.
v = xpress.evaluate(*args, problem=None, solution=None)
args
|
One or more objects to be evaluated. These can be variables, linear or nonlinear expressions; they can also be tuples, lists, dictionaries, or NumPy arrays of variables and expressions.
|
problem
|
The
xpress.problem object this function is referring to. If
problem is not
None, then
solution is either
None (in which case the current solution is used) or it is to be intended referred to the indices of variables in
problem. If
problem is
None,
solution must provide this information directly, i.e. it must be a dictionary mapping variable objects to their value
|
solution
|
Either a list or NumPy array of values (in which case
problem must not be
None) or a dictionary mapping variable objects to their value. As mentioned above, if it is
None then
problem must be passed and the assignment for the function is assumed to be the solution as retrieved via
problem.getSolution
|
import xpress as xp x = xp.var() y = xp.var(vartype=xp.binary) # Uses evaluate without a problem but by assigning the variables # explicitly. Note that the dictionary is necessary as no problem is # defined. The result should be [5.4, 124.71633781453677]. v1 = xp.evaluate([x + y, x**3 - xp.cos(x)], solution={x:5, y:0.4}) p = xp.problem(x, y) # Create a problem and add variables x and y # Similar to the computation of v1 but with a vector of numbers; the # order in which the variables were added to p means that this x=2, # y=3 here. The result should be {'exp1':11, 'exp2':6, 'exp3':9}. v2 = xp.evaluate({'exp1':x + 3*y, 'exp2':x*y, 'exp3':y**2}, problem=p, solution=[2,3]) p.addConstraint(x + y >= 3) p.setObjective(x + 2*y) p.solve() l = np.array([x**2 * y, x * y**2, x**3], dtype=xp.npexpr) # No solution is passed, so the solution of p as computed with solve() # above is used. It is easy to show that the solution is x=3, y=0, so # the result is np.array([0, 0, 27]). v3 = xp.evaluate(l, problem=p)
When using evaluate with piecewise linear functions that have a step discontinuity, for example with the constraint y == xp.pwl({(0,3): x, (3,5): 10*x}), if at an optimal solution x=3the Optimizer library will compute a value for y that is anywhere between 3 and 30, because of numerical issues associated with discontinuities.
In such cases, evaluate is unaware of the link between the function and y and, by convention, will return a value of the function that correspond to the second interval, i.e., the function will be evaluated at 30. In order to obtain the value of the piecewise linear function, evaluate should be run on y instead.