solve

Solve optimization problem

Syntax

sol = solve(prob)
sol = solve(prob,solver)
sol = solve(___,options)
[sol,fval] = solve(___)
[sol,fval,exitflag,output,lambda] = solve(___)

Description

example

sol = solve(prob) solves the optimization problem prob.

example

sol = solve(prob,solver) solves prob using the specified solver.

example

sol = solve(___,options), where ___ means any previous input arguments, solves prob using the specified options.

[sol,fval] = solve(___), for any input syntax, also returns the objective function value at the solution.

example

[sol,fval,exitflag,output,lambda] = solve(___) also returns an exit flag describing the exit condition, an output structure containing additional information about the solution process, and for non-integer problems, a Lagrange multiplier structure.

Examples

collapse all

Solve a linear programming problem defined by an optimization problem.

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)
Optimal solution found.
sol = struct with fields:
    x: 0.6667
    y: 1.3333

Solve the problem

without showing iterative display.

x = optimvar('x',2,1,'LowerBound',0);
x3 = optimvar('x3','Type','integer','LowerBound',0,'UpperBound',1);
prob = optimproblem;
prob.Objective = -3*x(1) - 2*x(2) - x3;
prob.Constraints.cons1 = x(1) + x(2) + x3 <= 7;
prob.Constraints.cons2 = 4*x(1) + 2*x(2) + x3 == 12;

options = optimoptions('intlinprog','Display','off');

sol = solve(prob,options)
sol = struct with fields:
     x: [2×1 double]
    x3: 1

Examine the solution.

sol.x
ans = 

         0
    5.5000

sol.x3
ans = 1

Force solve to use intlinprog as the solver for a linear programming problem.

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,'intlinprog')
LP:                Optimal objective value is -1.111111.                                            


Optimal solution found.

No integer variables specified. Intlinprog solved the linear problem.
sol = struct with fields:
    x: 0.6667
    y: 1.3333

Solve the mixed-integer linear programming problem described in Solve Integer Programming Problem with Nondefault Options and examine all of the output data.

x = optimvar('x',2,1,'LowerBound',0);
x3 = optimvar('x3','Type','integer','LowerBound',0,'UpperBound',1);
prob = optimproblem;
prob.Objective = -3*x(1) - 2*x(2) - x3;
prob.Constraints.cons1 = x(1) + x(2) + x3 <= 7;
prob.Constraints.cons2 = 4*x(1) + 2*x(2) + x3 == 12;

[sol,fval,exitflag,output] = solve(prob)
LP:                Optimal objective value is -12.000000.                                           


Optimal solution found.

Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal
value, options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are
integer within tolerance, options.IntegerTolerance = 1e-05 (the default value).
sol = struct with fields:
     x: [2×1 double]
    x3: 1

fval = -12
exitflag = 
OptimalSolution
output = struct with fields:
        relativegap: 0
        absolutegap: 0
      numfeaspoints: 1
           numnodes: 0
    constrviolation: 0
            message: 'Optimal solution found.↵↵Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are integer within tolerance, options.IntegerTolerance = 1e-05 (the default value).'
             solver: 'intlinprog'

For a problem without any integer constraints, you can also obtain a nonempty Lagrange multiplier structure as the fifth output.

Input Arguments

collapse all

Optimization problem, specified as an OptimizationProblem object.

Example: prob = optimproblem; prob.Objective = obj; prob.Constraints.cons1 = cons1;

Optimization solver, specified as 'intlinprog' or 'linprog'.

  • If your problem has integer constraints, the default solver is 'intlinprog'.

  • Otherwise, the default solver is 'linprog'.

Example: 'intlinprog'

Data Types: char | string

Optimization options, specified as an object created by optimoptions.

Internally, the solve function calls linprog or intlinprog. Therefore,

  • If your problem has integer constraints, or if the solver argument is 'intlinprog', use intlinprog options.

  • Otherwise, use linprog options.

For suggestions on options settings to improve an intlinprog solution or speed of solution, see Tuning Integer Linear Programming. For linprog, generally, the default 'dual-simplex' algorithm is memory efficient and speedy. Occasionally, linprog solves a large problem faster when the Algorithm option is 'interior-point'.

Example: options = optimoptions('intlinprog','Display','none')

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.

Tip

If you neglect to ask for fval, you can calculate it using

fval = evaluate(prob.Objective,sol)

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.

Algorithms

Internally, the solve function solves optimization problems by calling linprog or intlinprog. Before solve can call these functions, either it or some other associated functions or objects must convert the problems to solver form. This conversion entails, for example, linear constraints having a matrix representation rather than an optimization variable expression.

The first step in the algorithm occurs as you place optimization expressions into the problem. An OptimizationProblem object internally has an internal list of the variables used in its expressions. Each variable has a linear index in the expression, and a size. Therefore, the problem variables have an implied matrix form. The prob2struct function performs the conversion from problem form to matrix form. For an example, see Convert Problem to Structure.

If the problem has any integer variables, then by default solve calls intlinprog. Otherwise, by default solve calls linprog. You can override this default choice by using the solver input argument when calling solve.

For the algorithm that intlinprog uses to solve MILP problems, see intlinprog Algorithm. For the algorithms that linprog uses to solve linear programming problems, see Linear Programming Algorithms.

Introduced in R2017b

Was this topic helpful?