Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in...

19
Outline Overview Optimization Toolbox Genetic Algorithm and Direct Search Toolbox Function handles GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization in Matlab

Transcript of Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in...

Page 1: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Optimization in Matlab

Kevin Carlberg

Stanford University

July 28, 2009

Kevin Carlberg Optimization in Matlab

Page 2: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

1 Overview

2 Optimization Toolbox

3 Genetic Algorithm and Direct Search Toolbox

4 Function handles

5 GUI

6 Homework

Kevin Carlberg Optimization in Matlab

Page 3: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

OverviewMatlab has two toolboxes that contain optimization algorithmsdiscussed in this class

Optimization ToolboxUnconstrained nonlinearConstrained nonlinearSimple convex: LP, QPLeast SquaresBinary Integer ProgrammingMultiobjective

Genetic Algorithm and Direct Search Toolbox: generaloptimization problems

Direct search algorithms (directional): generalized patternsearch and mesh adaptive searchGenetic algorithmSimulated annealing and Threshold acceptance

Kevin Carlberg Optimization in Matlab

Page 4: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Problem types and algorithms

ContinuousConvex, constrained (simple)

LP: linprogQP: quadprog

Nonlinear

Unconstrained: fminunc, fminsearchConstrained: fmincon, fminbnd, fseminf

Least-squares (specialized problem type): minx ‖F (x)‖2F (x) linear, constrained: lsqnonneg, lsqlinF (x) nonlinear: lsqnonlin, lsqcurvefit

Multiobjective: fgoalattain, fminimax

Discrete

Linear, Binary Integer Programming: bintprog

Kevin Carlberg Optimization in Matlab

Page 5: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Continuous, nonlinear algorithms

We will focus only on the following algorithms for continuous,nonlinear problems

Unconstrained: fminunc, fminsearch

Constrained: fmincon

Kevin Carlberg Optimization in Matlab

Page 6: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Nonlinear, unconstrained algorithms

fminunc: a gradient-based algorithm with two modes

Large-scale: This is a subspace trust-region method (see p.76–77 of Nocedal and Wright). It can take a user-suppliedHessian or approximate it using finite differences (with aspecified sparsity pattern)Medium-scale: This is a cubic line-search method. It usesQuasi-Newton updates of the Hessian (recall thatQuasi-Newton updates give dense matrices, which areimpractical for large-scale problems)

fminsearch: a derivative-free method based on Nelder-Meadsimplex

Kevin Carlberg Optimization in Matlab

Page 7: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Nonlinear constrained algorithm: fmincon

fmincon: a gradient-based framework with three algorithms

Trust-region reflective: a subspace trust-region methodActive Set: a sequential quadratic programming (SQP)method. The Hessian of the Lagrangian is updated usingBFGS.Interior Point: a log-barrier penalty term is used for theinequality constraints, and the problem is reduced to havingonly equality constraints

Kevin Carlberg Optimization in Matlab

Page 8: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Algorithms

Algorithms in this toolbox can be used to solve generalproblems

All algorithms are derivative-free methods

Direct search: patternsearch

Genetic algorithm: ga

Simulated annealing/threshold acceptance: simulannealbnd,threshacceptbnd

Genetic Algorithm for multiobjective optimization:gamultiobj

Kevin Carlberg Optimization in Matlab

Page 9: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Function handlesFunction handle: a MATLAB value that provides a means ofcalling a function indirectly

Function handles can be passed in calls to other functionsFunction handles can be stored in data structures for later useThe optimization and genetic algorithm toolboxes makeextensive use of function handles

Example: Creating a handle to an anonymous functionbowl = @(x,y)x^2+(y-2)^2;ezsurf(bowl)

!6!4

!20

24

6

!5

0

5

0

20

40

60

80

100

x

x2+(y!2)

2

y

Kevin Carlberg Optimization in Matlab

Page 10: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Example: creating a handle to a named functionAt the command line, type

edit bowlNamed;

In an editor, create an m-file containing

function f = bowlNamed(x,y)f = x^2+(y-2)^2;

At the command line, type

bowlhandle = @bowlNamed;ezsurf(bowlhandle)

!6!4

!20

24

6

!5

0

5

0

20

40

60

80

100

x

x2+(y!2)

2

y

Kevin Carlberg Optimization in Matlab

Page 11: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Function handles for optimization

For the optimization toolbox, only one vector-valued inputargument should be used

Example: creating a handle to an anonymous function withone vector-valued input variable

bowlVec = @(x)x(1)^2+(x(2)-2)^2;

Example: creating a handle to a named function with twoscalar-valued input variables

bowlVecNamed = @(x)bowlNamed(x(1),x(2));

ezsurf cannot accept handles with vector-valued arguments(stick with examples on previous pages)

Kevin Carlberg Optimization in Matlab

Page 12: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Supplying gradients

It may be desirable to analytically specify the gradient of thefunction

To do this, the named function must return two outputs: thefunction value and the gradient

Kevin Carlberg Optimization in Matlab

Page 13: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Complete example: creating a handle to a named function,plotting it, and specifying the handle for optimization

At the command line, type

edit bowlNamed;

In the editor, create an m-file containing

function [f,g] = bowlNamed(x,y)f = x^2+(y-2)^2;g(1) = 2*x;g(2) = 2*(y-2);

At the command line, type

bowlhandle = @bowlNamed;ezsurf(bowlhandle);bowlhandleOpt = @(x) bowlNamed(x(1),x(2))

bowlhandleOpt can now be used as the argument to aMatlab optimization function with supplied gradients

Kevin Carlberg Optimization in Matlab

Page 14: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

GUIThe optimization toolbox includes a graphical user interface(GUI) that is easy to useTo activate, simply type optimtool at the command line

Kevin Carlberg Optimization in Matlab

Page 15: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

GUI options

We would like to “track” the progress of the optimizer

Under options, set Level of display: iterative

Under plot functions, check: function value

When ga is used, check “Best fitness,” and “Expectation” totrack the fitness of the best member of the population andthe average fitness of the population

Kevin Carlberg Optimization in Matlab

Page 16: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

For the functions on the following pages, do the following:

1 Create a function that takes in two scalar-valued argumentsand outputs both the function and gradient

2 Create a handle for this function and use ezsurf to plot thefunction

3 Create an optimization-ready handle for this function andsolve using different starting points using:

fminunc, medium scale, derivatives approximated by solverfminunc, medium scale, gradient suppliedfminsearchga

4 Compare the algorithms on the following measures:

1 Robustness: ability to find a global optimum and dependenceof performance on initial guess

2 Efficiency: how many function evaluations were required?

Kevin Carlberg Optimization in Matlab

Page 17: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Problem 1Consider a convex function with constant Hessian

f (x1, x2) = 4x21 + 7(x2 − 4)2 − 4x1 + 3x2

! 6! 4

! 20

2

4

6

! 6! 4

! 20

24

6

0

200

400

600

800

y

prob1

x

x

y

prob1

!6 !4 !2 0 2 4 6

!6

!4

!2

0

2

4

6

Figure: Surface and contour plot

Also, find the analytical solution to this problemKevin Carlberg Optimization in Matlab

Page 18: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Problem 2Consider the Rosenbrock function, a non-convex problem thatis difficult to minimize. The global minimum is located at(x1, x2) = (0, 0)

f (x1, x2) = (1− x1)2 + 100(x2 − x21 )2

! 1

! 0.5

0

0.5

1

! 1! 0.500.510

100

200

300

400

500

x

rosen

y x

y

rosen

! 1 ! 0.8 ! 0.6 ! 0.4 ! 0.2 0 0.2 0.4 0.6 0.8 1! 1

! 0.8

! 0.6

! 0.4

! 0.2

0

0.2

0.4

0.6

0.8

1

Figure: Surface and contour plot

Kevin Carlberg Optimization in Matlab

Page 19: Optimization in Matlab - sandia.govktcarlb/opt_class/OPT_Matlab.pdf · GUI Homework Optimization in Matlab Kevin Carlberg Stanford University July 28, 2009 Kevin Carlberg Optimization

OutlineOverview

Optimization ToolboxGenetic Algorithm and Direct Search Toolbox

Function handlesGUI

Homework

Problem 3

Consider the Rastragin’s function, an all-around nastyfunction. The global minimum is located at (x1, x2) = (0, 0)

f (x1, x2) = 20 + x21 + x2

2 − 10(cos 2πx1 + cos 2πx2)

! 6! 4

! 20

24

6

! 5

0

50

20

40

60

80

100

x

ras

y x

y

ras

! 6 ! 4 ! 2 0 2 4 6! 6

! 4

! 2

0

2

4

6

Figure: Surface and contour plot

Kevin Carlberg Optimization in Matlab