OptimizationProblem

Optimization problem

Description

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.

Creation

Create an OptimizationProblem object by using optimproblem.

Properties

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, returned 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 or as a structure containing OptimizationConstraint 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

writeproblemSave optimization problem description
showproblemDisplay optimization problem
solveSolve optimization problem
prob2structConvert optimization problem to solver form

Examples

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.

showproblem(prob)
  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.
sol.x
ans = 2×1

   25.0000
   15.0000

Introduced in R2017b