mapSolution

Create optimization solution structure from solver outputs

Syntax

[sol,fval,exitflag,output,lambda] = mapSolution(prob,xin,fin,eflag,outpt,lambdain,solver)

Description

example

[sol,fval,exitflag,output,lambda] = mapSolution(prob,xin,fin,eflag,outpt,lambdain,solver) formats an optimization solution in the form that solve returns.

Note

You can request any subset of the output arguments. If you do, you can include only those input arguments that are required for the output arguments that you request. For example,

[sol,fval] = mapSolution(prob,xin,fin)
% or
[sol,~,~,~,lambda] = mapSolution(prob,xin,[],[],[],lambdain,solver)

Examples

collapse all

Specify a linear programming problem in the problem framework.

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;

Convert the problem to a structure to enable solution by a different solver than solve.

problem = prob2struct(prob);

Solve the problem using the linprog solver.

options = optimoptions('linprog','Algorithm','dual-simplex');
[xin,fin,eflag,outpt,lambdain] = linprog(problem);
Optimal solution found.

Transform the solution to the form that solve returns.

[sol,fval,exitflag,output,lambda] = mapSolution(prob,xin,fin,eflag,outpt,lambdain,'linprog')
sol = struct with fields:
    x: 0.6667
    y: 1.3333

fval = -1.1111
exitflag = 
OptimalSolution
output = struct with fields:
         iterations: 3
    constrviolation: 0
            message: 'Optimal solution found.'
          algorithm: 'dual-simplex'
      firstorderopt: 0
             solver: 'linprog'

lambda = struct with fields:
      Variables: [1×1 struct]
    Constraints: [1×1 struct]

Input Arguments

collapse all

Optimization problem, specified as an OptimizationProblem object. Typically, the other input variables such as xin come from a solution of the problem as converted to matrix form using prob2struct.

Optimization solution, specified as a real vector. The length of the vector is the sum of the number of elements in the problem variables.

Data Types: double

Objective function value at solution, specified as a real scalar.

If there is an additive constant for the objective function, include it in fin. In other words, if the objective function is a + f'*x, then include the value a, not just f'*x, in fin.

Data Types: double

Exit flag, specified as an integer.

Data Types: double

Output structure, specified as a structure. This structure is copied to the output variable, and the solver input is appended as the solver field of the resulting structure.

Data Types: struct

Lagrange multipliers, specified as a structure. For details of the fields of this structure, see lambda.

Data Types: struct

Solver name, specified as 'intlinprog' or 'linprog'.

Data Types: char | string

Output Arguments

collapse all

Solution, returned as a structure. The fields of the structure are the names of the optimization variables. See optimvar.

Objective function value at the solution, returned as a real number.

Reason the solver stopped, returned as a categorical variable. For the intlinprog solver, the exit flag is:

Exit FlagNumeric EquivalentMeaning
IntegerFeasible2intlinprog stopped prematurely. Integer feasible point found.
OptimalSolution

1

Function converged to a solution x.

SolverLimitExceeded

0

intlinprog exceeded one of the following tolerances:

  • LPMaxIterations

  • MaxNodes

  • MaxTime

  • RootLPMaxIterations

See Tolerances and Stopping Criteria. solve can also return this exit flag when it runs out of memory at the root node.

OutputFcnStop-1intlinprog stopped by an output function or plot function.
NoFeasiblePointFound

-2

No feasible point was found.

Unbounded

-3

Problem is unbounded.

FoundNaN

-4

A NaN value was encountered during execution of the algorithm.

PrimalDualInfeasible

-5

Both primal and dual problems are infeasible.

DirectionTooSmall

-7

Search direction became too small. No further progress could be made.

For the linprog solver, the exit flag is:

Exit FlagNumeric EquivalentMeaning
OptimalSolution1

Function converged to a solution x.

SolverLimitExceeded0

Number of iterations exceeded options.MaxIterations.

NoFeasiblePointFound-2

No feasible point was found.

Unbounded-3

Problem is unbounded.

FoundNaN-4

A NaN value was encountered during execution of the algorithm.

PrimalDualInfeasible-5

Both primal and dual problems are infeasible.

DirectionTooSmall-7

Search direction became too small. No further progress could be made.

Information about the optimization process, returned as a structure. The output structure contains the fields in the 'intlinprog' output structure, or the 'linprog' output structure, depending on which solver solve called. solve includes an additional field in the output structure:

Field NameMeaning
SolverWhich solver solve used, 'intlinprog' or 'linprog'

Lagrange multipliers at the solution, returned as a structure. For the intlinprog solver, lambda is empty, []. For the linprog solver, lambda has these fields:

  • Variables – Contains fields for each problem variable. Each problem variable name is a structure with two fields:

    • Lower – Lagrange multipliers associated with the variable LowerBound property, returned as an array of the same size as the variable. Nonzero entries mean that the solution is at the lower bound. These multipliers are in the structure lambda.Variables.variablename.Lower.

    • Upper – Lagrange multipliers associated with the variable UpperBound property, returned as an array of the same size as the variable. Nonzero entries mean that the solution is at the upper bound. These multipliers are in the structure lambda.Variables.variablename.Upper.

  • Constraints – Contains a field for each problem constraint. Each problem constraint is in a structure whose name is the constraint name, and whose value is a numeric array of the same size as the constraint. Nonzero entries mean that the constraint is active at the solution. These multipliers are in the structure lambda.Constraints.constraintname.

Introduced in R2017b

Was this topic helpful?