Main Content

Problem-Based Optimization Workflow

Note

Optimization Toolbox™ provides two approaches for solving single-objective optimization problems. This topic describes the problem-based approach. Solver-Based Optimization Problem Setup describes the solver-based approach.

To solve an optimization problem, perform the following steps.

  • Create an optimization problem object by using optimproblem. A problem object is a container in which you define an objective expression and constraints. The optimization problem object defines the problem and any 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.

    Note

    If you have a nonlinear function that is not composed of polynomials, rational expressions, and elementary functions such as exp, then convert the function to an optimization expression by using fcn2optimexpr. See Convert Nonlinear Function to Optimization Expression and Supported Operations for Optimization Variables and Expressions.

    If necessary, include extra parameters in your expression as workspace variables; see Pass Extra Parameters in Problem-Based Approach.

    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 for optimization problems as either comparisons in the named variables or as comparisons of expressions.

    Note

    If you have a nonlinear function that is not composed of polynomials, rational expressions, and elementary functions such as exp, then convert the function to an optimization expression by using fcn2optimexpr. See Convert Nonlinear Function to Optimization Expression and Supported Operations for Optimization Variables and Expressions.

    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;
  • For nonlinear problems, set an initial point as a structure whose fields are the optimization variable names. For example:

    x0.x = randn(size(x));
    x0.y = eye(4); % Assumes y is a 4-by-4 variable
  • Solve the problem by using solve.

    sol = solve(prob);
    % Or, for nonlinear problems,
    sol = solve(prob,x0)

In addition to these basic steps, you can review the problem definition before solving the problem by using show or write. Set options for solve by using optimoptions, as explained in Change Default Solver or Options.

Warning

The problem-based approach does not support complex values in the following: an objective function, nonlinear equalities, and nonlinear inequalities. If a function calculation has a complex value, even as an intermediate value, the final result might be incorrect.

Note

All names in an optimization problem must be unique. Specifically, all variable names, objective function names, and constraint function names must be different.

For a basic mixed-integer linear programming example, see Mixed-Integer Linear Programming Basics: Problem-Based or the video version Solve a Mixed-Integer Linear Programming Problem Using Optimization Modeling. For a nonlinear example, see Solve a Constrained Nonlinear Problem, Problem-Based. For more extensive examples, see Problem-Based Nonlinear Optimization, Linear Programming and Mixed-Integer Linear Programming, or Quadratic Programming and Cone Programming.

See Also

| | | | | |

Related Topics