Create optimization problem
prob = optimproblem
prob = optimproblem(Name,Value)
uses additional options specified by one or more prob
= optimproblem(Name,Value
)Name,Value
pair
arguments. For example, to specify a maximization problem instead of a minimization
problem, use prob =
optimproblem('ObjectiveSense','maximize')
.
Create an optimization problem with default properties.
prob = optimproblem
prob = OptimizationProblem with properties: Description: '' ObjectiveSense: 'minimize' Variables: [0x0 struct] containing 0 OptimizationVariables Objective: [0x0 OptimizationExpression] Constraints: [0x0 struct] containing 0 OptimizationConstraints No problem defined.
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.
showproblem(prob)
OptimizationProblem : max : x(1, 1) + 2*x(2, 1) subject to cons1: x(1, 1) + 5*x(2, 1) <= 100 subject to cons2: x(1, 1) + x(2, 1) <= 40 subject to cons3: 2*x(1, 1) + 0.5*x(2, 1) <= 60 variable bounds: 0 <= x(1, 1) 0 <= x(2, 1)
Solve the problem.
sol = solve(prob); sol.x
Optimal solution found. ans = 25.0000 15.0000
Specify optional
comma-separated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside single quotes (' '
). You can
specify several name and value pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
prob =
optimproblem('ObjectiveSense','maximize')
.'Constraints'
— Problem constraintsOptimizationConstraint
array | structure with OptimizationConstraint
arrays as
fieldsProblem constraints, specified as an OptimizationConstraint
array or a structure with
OptimizationConstraint
arrays as fields.
Example: prob = optimproblem('Constraints',sum(x,2) ==
1)
'Description'
— Problem label''
(default) | string | character vectorProblem label, specified as a string or character vector. The software
does not use Description
for computation.
Description
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: prob = optimproblem('Description',"An iterative
approach to the Traveling Salesman
problem")
Data Types: char
| string
'Objective'
— Objective functionOptimizationExpression
Objective function, specified as a scalar OptimizationExpression
object.
Example: prob =
optimproblem('Objective',sum(sum(x)))
for a 2-D variable
x
'ObjectiveSense'
— Sense of optimization'minimize'
(default) | 'min'
| 'maximize'
| 'max'
Sense of optimization, specified as 'minimize'
or
'maximize'
. You can also specify
'min'
to obtain 'minimize'
or
'max'
to obtain 'maximize'
.
The solve
function minimizes the objective when
ObjectiveSense
is 'minimize'
and maximizes the objective when ObjectiveSense
is
'maximize'
.
Example: prob =
optimproblem('ObjectiveSense','max')
Data Types: char
| string
prob
— Optimization problemOptimizationProblem
objectOptimization problem, returned as an OptimizationProblem
object. Typically, to complete the problem
description, you specify an objective function and linear constraints.
However, you can have a feasibility problem, which has no objective
function, or you can have a problem with no linear constraints. Solve a
complete problem by calling solve
.