Main Content

Constrained Minimization Using patternsearch and Optimize Live Editor Task

This example shows how to solve a constrained minimization problem using both the Optimize Live Editor task, which offers a visual approach, and the command line.

Problem Description

The problem involves using linear and nonlinear constraints when minimizing a nonlinear function with patternsearch. The objective function is

F(x)=12xTHx+fTx,

where

H = [36 17 19 12  8 15; 
     17 33 18 11  7 14; 
     19 18 43 13  8 16;
     12 11 13 18  6 11; 
      8  7  8  6  9  8; 
     15 14 16 11  8 29];

f = [ 20 15 21 18 29 24 ]';
 
F = @(x)0.5*x'*H*x + f'*x;

This objective function is also included with your software in the file lincontest7.m.

The linear constraints are

Axb,Aeqx=beq,

where

A = [-8 7 3 -4 9 0];
b = 7;
Aeq = [7 1 8 3 3 3;
      5 0 -5 1 -5 8;
     -2 -6 7 1 1 9;
      1 -1 2 -2 3 -3];
beq = [84 62 65 1]';

Enter the preceding code sections to get the problem variables into your workspace before proceeding.

Solve Using patternsearch in Optimize Live Editor Task

  1. Create a new live script by clicking the New Live Script button in the File section on the Home tab.

    New Live Script button

  2. Insert an Optimize Live Editor task. Click the Insert tab and then, in the Code section, select Task > Optimize.

    Insert Optimize Live Editor task.

    Optimize Live Editor task initial screen

  3. Specify Problem Type

    In the Specify problem type section of the task, click the Objective > Nonlinear button.

  4. Click the Constraints > Linear inequality and Linear equality buttons.

  5. Select Solver > patternsearch - Pattern search.

  6. Select Problem Data

    Enter the problem variables in the Select problem data section of the task. To specify the objective function, select Objective function > Function handle and choose F.

  7. Set the inequality constraints to A and b. Set the equality constraints to Aeq and beq.

  8. To set the initial point, you first need to create a new section above the task. To do so, click the Section Break button on the Insert tab. In the new section above the task, enter the following code for the initial point.

    x0 = [2 1 0 9 1 0]';
  9. Run the section to place x0 into the workspace. To run the section, place the cursor in the section and press Ctrl+Enter or click the blue striped bar to the left of the line number.

  10. In the Select problem data section of the task, set x0 as the initial point.

  11. Specify Solver Options

    Because this problem is linearly constrained, specify an additional solver option. Expand the Specify solver options section of the task, and then click the Add button. Set the Poll settings > Poll method to GSSPositiveBasis2N. For more information about the efficiency of the GSS poll methods for linearly constrained problems, see Compare the Efficiency of Poll Options.

  12. Set Display Options

    In the Display progress section of the task, select the Best value and Mesh size plot functions.

    Your setup looks like this:

    patternsearch solver, function handle F, initial point x0, linear constraints A, b, Aeq, beq, poll algorithm GSSPositiveBasis2N, plots Best value and Mesh size

  13. Run Solver and Examine Results

    To run the solver, click the options button at the top right of the task window, and select Run Section.

    Run the solver; the keyboard equivalent is Ctrl+Enter.

    The plots appear in a separate figure window and in the task output area.

    Plots show the objective function value decreasing with iterations to 1919 and the mesh size eventually decreasing below 1e-6.

  14. To obtain the solution point and objective function value at the solution, look at the top of the task.

    Optimize returns "solution" and "objectiveValue" variables

    The Optimize Live Editor task returns the solution in a variable named solution and returns the objective function value in a variable named objectiveValue. View these values by entering the following code in the section below the task and then running the section, or by entering the code at the MATLAB® command line.

    disp(solution)
        8.5165
       -6.1094
        4.0989
        1.2877
       -4.2348
        2.1812
    disp(objectiveValue)
       1.9195e+03
  15. Include Nonlinear Constraints

    Add the following nonlinear constraints to the problem.

    1.5+x1x2+x1x20x1x2100.

    To include these constraints, first click the Constraints > Nonlinear button.

    Include a nonlinear constraint.

  16. In the Select problem data section, under Constraints, select Nonlinear > Local function and then click the New button. The function appears in a new section below the task. Edit the resulting code to contain the following lines.

    function [c, ceq] = double_ineq(x)
    c = [-1.5 + x(1)*x(2) + x(1) - x(2);
        -x(1)*x(2) - 10];
    ceq = [];
    end
  17. In the Nonlinear constraints section, select double_ineq.

  18. The nonlinear constraint algorithm causes patternsearch to take many function evaluations. In the Specify solver options section, click the plus sign to the right of the current options to display additional options. Then increase the maximum function evaluation limit to 5e4.

    Max function evals = 5e4

  19. Run the task again to rerun the optimization.

    The top plot shows four iterations with a best function value of 2401.77. The bottom plot shows the mesh size decreasing to 1e-9.

  20. View the solution and objective function value.

    disp(solution)
        7.2083
       -1.3873
        4.9579
       -3.1393
       -3.1843
        4.7457
    disp(objectiveValue)
       2.4018e+03

The objective function value is higher than the value in the problem without nonlinear constraints. The previous solution is not feasible with respect to the nonlinear constraints.

The plots show many fewer iterations than before because the nonlinear constraint algorithm changes the patternsearch algorithm to include another outer loop to solve a modified problem. The outer loop reduces the modification to the problem at each major iteration. In this case, the algorithm makes only four outer iterations. For algorithm details, see Nonlinear Constraint Solver Algorithm.

Solve Using patternsearch at the Command Line

To solve the original problem (only linear constraints) at the command line, execute the following code.

x0 = [2 1 0 9 1 0]';
options = optimoptions('patternsearch',...
    'PollMethod','GSSPositiveBasis2N',...
    'PlotFcn',{'psplotbestf','psplotmeshsize'});
lb = [];
ub = [];
nonlcon = [];
[x,fval] = patternsearch(F,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
Optimization terminated: mesh size less than options.MeshTolerance.

x =

    8.5165
   -6.1094
    4.0989
    1.2877
   -4.2348
    2.1812


fval =

   1.9195e+03

patternsearch generates the first pair of plots shown in the Optimize Live Editor task example.

To include the nonlinear constraints, save the following code to a file named double_ineq.m on the MATLAB path.

function [c, ceq] = double_ineq(x)
c = [-1.5 + x(1)*x(2) + x(1) - x(2);
    -x(1)*x(2) - 10];
ceq = [];
end

To allow the solver to run to completion with nonlinear constraints, increase the allowed number of function evaluations.

options.MaxFunctionEvaluations = 5e4;

Solve the problem including nonlinear constraints.

nonlcon = @double_ineq;
[x,fval] = patternsearch(F,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
Optimization terminated: mesh size less than options.MeshTolerance
 and constraint violation is less than options.ConstraintTolerance.

x =

    7.2083
   -1.3873
    4.9579
   -3.1393
   -3.1843
    4.7457


fval =

   2.4018e+03

patternsearch also generates the second pair of plots shown in the Optimize Live Editor task example.

Both the Optimize Live Editor task and the command line allow you to formulate and solve problems, and they give identical results. The command line is more streamlined, but provides less help for choosing a solver, setting up the problem, and choosing options such as plot functions. You can also start a problem using Optimize, and then generate code for command line use, as in Solve a Constrained Nonlinear Problem, Solver-Based.

See Also

Related Topics