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
OptimizationProblem object by using
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
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, returned 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
OptimizationConstraintobject | structure containing
Optimization constraints, specified as an
OptimizationConstraint object or as a structure
OptimizationConstraint objects. Incorporate
constraints into the problem when you create the problem, or later by using
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 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 : max : 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);
Optimal solution found.
ans = 2×1 25.0000 15.0000