There are two different Optimization Toolbox™ approaches for solving linear programming or mixed-integer linear programming problems. This section describes the problem-based approach. Optimization Problem Setup describes the solver-based approach.
To solve a linear programming problem or mixed-integer linear programming problem, perform the following steps.
Create a problem object by using optimproblem
. A problem object is a container that you fill with
objective and constraint expressions. These expressions define the problem,
along with bounds that exist in the problem variables.
For example, create a maximization problem.
prob = optimproblem('ObjectiveSense','maximize');
Create named variables by using optimvar
. An optimization variable is a symbolic variable that
you use to describe the problem objective and constraints. Include any bounds in
the variable definitions.
For example, create a 15-by-3 array of binary variables named
'x'
.
x = optimvar('x',15,3,'Type','integer','LowerBound',0,'UpperBound',1);
Define the objective function in the problem object as an expression in the named variables.
For example, assume that you have a real matrix f
of the
same size as a matrix of variables x
, and the objective is
the sum of the entries in f
times the corresponding variables
x
.
prob.Objective = sum(sum(f.*x));
Define constraints in the problem object as expressions in the named variables.
For example, assume that the sum of the variables in each row of
x
must be one, and the sum of the variables in each
column must be no more than one.
onesum = sum(x,2) == 1; vertsum = sum(x,1) <= 1; prob.Constraints.onesum = onesum; prob.Constraints.vertsum = vertsum;
Solve the optimization problem by using solve
.
sol = solve(prob);
In addition to these basic steps, you can review the problem definition before solving
the problem by using showproblem
and related functions. Set options for solve
by using optimoptions
, as explained in options
.
For a basic example, see Mixed-Integer Linear Programming Basics: Problem-Based or the video version Solve a Mixed-Integer Linear Programming Problem using Optimization Modeling. For larger, scalable examples, see Factory, Warehouse, Sales Allocation Model: Problem-Based, Traveling Salesman Problem: Problem-Based, or other examples in Problem-Based Optimization.
optimproblem
| optimvar
| solve