Documentation

Examine Optimization Solution

Obtain Numeric Solution

The solve function returns a solution as a structure, with each variable in the problem having a field in the structure. To obtain numerical values of expressions in the problem from this structure easily, use the evaluate function.

For example, solve a linear programming problem in two variables.

x = optimvar('x');
y = optimvar('y');
prob = optimproblem;
prob.Objective = -x -y/3;
prob.Constraints.cons1 = x + y <= 2;
prob.Constraints.cons2 = x + y/4 <= 1;
prob.Constraints.cons3 = x - y <= 2;
prob.Constraints.cons4 = x/4 + y >= -1;
prob.Constraints.cons5 = x + y >= 1;
prob.Constraints.cons6 = -x + y <= 2;

sol = solve(prob)
sol =

struct with fields:

x: 0.6667
y: 1.3333

Suppose that you want the objective function value at the solution. You can rerun the problem, this time asking for the objective function value and the solution.

[sol,fval] = solve(prob)
sol =

struct with fields:

x: 0.6667
y: 1.3333

fval =

-1.1111

Alternatively, for a time-consuming problem, save time by evaluating the objective function at the solution using evaluate.

fval = evaluate(prob.Objective,sol)
fval =

-1.1111

Examine Solution Quality

To check whether the reported solution is accurate, you can review outputs from solve. Return all solve outputs

[sol,fval,exitflag,output,lambda] = solve(prob);
• Check the exit flag. exitflag = OptimalSolution generally means that solve converged to the solution. For an explanation of the other exitflag values, see exitflag.

• Check the exit message at the command line or in the output structure. When the exit message states that the solver converged to a solution, then generally the solution is reliable. This message corresponds to exitflag = OptimalSolution.

• When you have integer constraints, check the absolute gap and the relative gap in the exit message or in the output structure. When these gaps are zero or nearly zero, the solution is reliable.

Infeasible Solution

If solve reports that your problem is infeasible (the exit flag is NoFeasiblePointFound), examine the problem infeasibility at a variety of points to see which constraints might be overly restrictive. Suppose that you have a single continuous optimization variable named x that has finite bounds on all components, and you have constraints constr1 through constr20.

N = 100; % check 100 points
infeas = zeros(N,20); % allocate
L = x.LowerBound;
U = x.UpperBound;
S = numel(L);
pthist = cell(N);
for k = 1:N
pt = L + rand(size(L)).*(U-L);
pthist{k} = pt;
for j = 1:20
infeas(k,j) = infeasibility(['constr',num2str(j)],pt);
end
end

The result infeas(a,b) has nonzero values wherever the associated point pt{a} is infeasible for constraint b.

Solution Takes Too Long

If solve takes a long time, there are a few possible causes and remedies.

• Problem formulation is slow. If you have defined objective or constraint expressions in nested loops, then solve can take a long time to convert the problem internally to a matrix form. To speed the solution, try to formulate your expressions in a vectorized fashion. See Create Efficient Optimization Problems.

• Mixed-integer linear programming solution is slow. Sometimes you can speed up an integer problem by setting options. You can also reformulate the problem to make it faster to solve. See Tuning Integer Linear Programming.

• Nonlinear programming solution is slow. For suggestions, see Solver Takes Too Long. For further suggestions, see When the Solver Fails.

• Solver Limit Exceeded. To solve some problems, solve can take more than the default number of solution steps. For problems with integer constraints, increase the number of allowed steps by increasing the LPMaxIterations, MaxNodes,MaxTime, or RootLPMaxIterations options to higher-than-default values. To set these options, use optimoptions('intlinprog',...). For non-integer problems, increase the MaxIterations option using optimoptions('linprog','MaxIterations',...). See options.