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
For the full workflow, see Problem-Based Optimization Workflow.
OptimizationProblem object by using
The problem-based approach does not support complex values in an objective function, nonlinear equalities, or nonlinear inequalities. If a function calculation has a complex value, even as an intermediate value, the final result can be incorrect.
Description — Problem label
'' (default) | string | character vector
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
"Describes a traveling salesman problem"
ObjectiveSense — Indication to minimize or maximize
'minimize' (default) |
Indication to minimize or maximize, specified as
property affects how
You can use the short name
Variables — Optimization variables in object
This property is read-only.
Optimization variables in the object, specified as a structure of
Objective — Objective function
OptimizationExpression | structure containing scalar
Objective function, specified as a scalar
OptimizationExpression or as a structure containing a
OptimizationExpression. Incorporate an objective
function into the problem when you create the problem, or later by using dot
prob = optimproblem('Objective',5*brownies + 2*cookies) % or prob = optimproblem; prob.Objective = 5*brownies + 2*cookies
Constraints — Optimization constraints
OptimizationConstraint object |
OptimizationEquality object |
OptimizationInequality object | structure containing
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 = ;
|Create optimization options|
|Convert optimization problem or equation problem to solver form|
|Display information about optimization object|
|Solve optimization problem or equation problem|
|Map problem variables to solver-based variable index|
|Save optimization object description|
Create and Solve Maximization Problem
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: x maximize : x(1) + 2*x(2) subject to cons1: x(1) + 5*x(2) <= 100 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 25.0000 15.0000