Create optimization solution structure from solver outputs
[sol,fval,exitflag,output,lambda] = mapSolution(prob,xin,fin,eflag,outpt,lambdain,solver)
[
formats an optimization solution in the form that sol
,fval
,exitflag
,output
,lambda
] = mapSolution(prob
,xin
,fin
,eflag
,outpt
,lambdain
,solver
)solve
returns.
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)
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]
prob
— Optimization problemOptimizationProblem
objectOptimization 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
.
xin
— Optimization solutionOptimization 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
fin
— Objective function value at solutionObjective 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
eflag
— Exit flagExit flag, specified as an integer.
Data Types: double
outpt
— Output structureOutput 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
lambdain
— Lagrange multipliersLagrange multipliers, specified as a structure. For details of the fields of this structure, see lambda.
Data Types: struct
solver
— Solver name'intlinprog'
| 'linprog'
Solver name, specified as 'intlinprog'
or
'linprog'
.
Data Types: char
| string
sol
— SolutionSolution, returned as a structure. The fields of the structure are the names of the optimization variables. See optimvar
.
fval
— Objective function value at the solutionObjective function value at the solution, returned as a real number.
exitflag
— Reason the solver stoppedReason the solver stopped, returned as a categorical variable. For the
intlinprog
solver, the exit flag is:
Exit Flag | Numeric Equivalent | Meaning |
---|---|---|
IntegerFeasible | 2 | intlinprog stopped prematurely. Integer feasible
point found. |
OptimalSolution |
|
Function converged to a solution |
SolverLimitExceeded |
|
See Tolerances and Stopping Criteria. |
OutputFcnStop | -1 | intlinprog stopped by an output function or plot
function. |
NoFeasiblePointFound |
|
No feasible point was found. |
Unbounded |
|
Problem is unbounded. |
FoundNaN |
|
A |
PrimalDualInfeasible |
|
Both primal and dual problems are infeasible. |
DirectionTooSmall |
|
Search direction became too small. No further progress could be made. |
For the linprog
solver, the exit flag is:
Exit Flag | Numeric Equivalent | Meaning |
---|---|---|
OptimalSolution | 1 |
Function converged to a solution |
SolverLimitExceeded | 0 |
Number of iterations exceeded
|
NoFeasiblePointFound | -2 |
No feasible point was found. |
Unbounded | -3 |
Problem is unbounded. |
FoundNaN | -4 |
A |
PrimalDualInfeasible | -5 |
Both primal and dual problems are infeasible. |
DirectionTooSmall | -7 |
Search direction became too small. No further progress could be made. |
output
— Information about the optimization processInformation 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 Name | Meaning |
---|---|
Solver | Which solver solve used, 'intlinprog' or 'linprog' |
lambda
— Lagrange multipliers at the solutionLagrange 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