Main Content


Optimization problem


An OptimizationProblem object describes an optimization problem, including variables for the optimization, constraints, the objective function, and whether the objective is to be maximized or minimized. Solve a complete problem using solve.


For the full workflow, see Problem-Based Optimization Workflow.


Create an OptimizationProblem object by using optimproblem.




expand all

Problem label, specified as a string or character vector. The software does not use Description. It is an arbitrary label that you can use for any reason. For example, you can share, archive, or present a model or problem, and store descriptive information about the model or problem in the Description property.

Example: "Describes a traveling salesman problem"

Data Types: char | string

Indication to minimize or maximize, specified as 'minimize' or 'maximize'. This property affects how solve works.

You can use the short name 'min' for 'minimize' or 'max' for 'maximize'.

Example: 'maximize'

Data Types: char | string

This property is read-only.

Optimization variables in the object, specified as a structure of OptimizationVariable objects.

Data Types: struct

Objective function, specified as a scalar OptimizationExpression or as a structure containing a scalar OptimizationExpression. Incorporate an objective function into the problem when you create the problem, or later by using dot notation.

prob = optimproblem('Objective',5*brownies + 2*cookies)
% or
prob = optimproblem;
prob.Objective = 5*brownies + 2*cookies

Optimization constraints, specified as an OptimizationConstraint object, an OptimizationEquality object, an OptimizationInequality object, or as a structure containing one of these objects. Incorporate constraints into the problem when you create the problem, or later by using dot notation:

constrs = struct('TrayArea',10*brownies + 20*cookies <= traysize,...
    'TrayWeight',12*brownies + 18*cookies <= maxweight);
prob = optimproblem('Constraints',constrs)
% or
prob.Constraints.TrayArea = 10*brownies + 20*cookies <= traysize
prob.Constraints.TrayWeight = 12*brownies + 18*cookies <= maxweight

Remove a constraint by setting it to [].

prob.Constraints.TrayArea = [];

Object Functions

optimoptionsCreate optimization options
prob2structConvert optimization problem or equation problem to solver form
showDisplay information about optimization object
solveSolve optimization problem or equation problem
varindexMap problem variables to solver-based variable index
writeSave optimization object description


collapse all

Create a linear programming problem for maximization. The problem has two positive variables and three linear inequality constraints.

prob = optimproblem('ObjectiveSense','max');

Create positive variables. Include an objective function in the problem.

x = optimvar('x',2,1,'LowerBound',0);
prob.Objective = x(1) + 2*x(2);

Create linear inequality constraints in the problem.

cons1 = x(1) + 5*x(2) <= 100;
cons2 = x(1) + x(2) <= 40;
cons3 = 2*x(1) + x(2)/2 <= 60;
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
prob.Constraints.cons3 = cons3;

Review the problem.

  OptimizationProblem : 

	Solve for:

	maximize :
       x(1) + 2*x(2)

	subject to cons1:

	subject to cons2:
       x(1) + x(2) <= 40

	subject to cons3:
       2*x(1) + 0.5*x(2) <= 60

	variable bounds:
       0 <= x(1)
       0 <= x(2)

Solve the problem.

sol = solve(prob);
Solving problem using linprog.

Optimal solution found.
ans = 2×1


Introduced in R2017b