applying optimization tools to nd the inputs that maximize ...math272/notes/week10/notes10.pdfl = 5...

30
. Week #10 : Optimization Goals: Applying optimization tools to find the inputs that maximize or minimize a function. Applying tweaks to both the search tool and our own code to track progress and detect search problems.

Upload: others

Post on 22-Jan-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

.

Week #10 : Optimization

Goals:

• Applying optimization tools to find the inputs that maximize or minimize afunction.

• Applying tweaks to both the search tool and our own code to track progressand detect search problems.

Page 2: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Intro - 1

Optimization Intro

Many design problems and applications involve the search for the best parametersto achieve a goal. Examples include finding

Page 3: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Intro - 2

In all these cases, we assume that

• there is a single variable of interest (distance, temperature, external load); and

• we can select settings for one or more parameters than influence this value.

For example,

Page 4: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Intro - 3

The function that maps from the input/design parameters to the output is calledthe objective function:

f (~x)

Our goal is to find the specific values of the input parameters that maximizeor minimize the output of the objective function.

Page 5: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Intro - 4

Analytic Solutions

Some optimization problems can be solved analytically through calculus:

Local maxes and mins of f (~x) occur at critical points of the objectivefunction,

or where ∇f (~x) = ~0If an analytic optimization is possible, it should be used!

Unfortunately, in most interesting applications, we won’t be able to compute ananalytic derivative of the objective function, so simply solving for the optimal ~xusing ∇f (~x) = ~0 won’t be an option. In other cases, we might be able to findderivatives necessary to identify the critical points, but solving the set of resultingequations might be impossible analytically.

Page 6: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Intro - 5

Numeric Optimization

When analytic methods aren’t available, we must resort to numerical optimizationmethods. A good metaphor for most of these methods is blind hill-climbing:

Given an objective function f (~x), and current estimate of the optimuminput, ~xi, we can only

• pick a new point, ~xi+1,

• find the height exactly there.

With only these tools, we try to find a way to get up the mountain (maximizef (~x)) by choosing a sequence of points ~x0, ~x1, ~x2, . . . values in some systematicway.

Algorithms are compared by their ability to

• find maxes in fewer steps or fewer function calls,

• find global max instead of local maxes, and

• their general robustness on a variety of objective functions f (~x)

Page 7: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Intro - 6

Historic (and Practical) Note

When users think of optimization, they usually think of “maximization”. However,for historic reasons, optimization routines (almost) always search for local minima.If you want to find a local max of f (~x), how can you do this using a tool thatsearches only for local minima?

Page 8: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1- Basic fminunc search - 1

Optimization Example 1 - Basic fminunc search

• f (x, y) = (x4 − 16x2 + 5x) + (y4 − 30y2 + 5y)

• Optima can be found by setting∂f

∂x,∂f

∂y= 0

• Local minima at:

x y f (x, y)-2.9035 -3.9140 77.22.7468 -3.9140 105.5-2.9035 3.8306 115.92.7468 3.8306 144.2

•Global minimum at (-2.9035, -3.910), with the f value 77.2.

Page 9: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1- Basic fminunc search - 2

Displaying the 2D surface shows the 4 minima arranged in a rectangle.

Our numerical optimization algorithms must blindly search through this surface:they do not have a graph to work with.The algorithms test heights and have some strategy to estimates where the minimaare positioned, or at least where a better point is locally.

Page 10: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1- Basic fminunc search - 3

Gradient-Based Search - fminunc

If we’re close to a minimum, and just want to find a local minimum, we shouldjust move downwards. If we had derivative information, we could guarantee thatevery step would go downhill.

For f (x1, . . . , xn), the gradient of f is defined

∇f =

(∂f

∂x1, . . . ,

∂f

∂xn

)At all points, the gradient vector points in the direction of maximum increase off :

Gradient-based Strategy: Follow gradient backwards to find local minima

Page 11: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1- Basic fminunc search - 4

fminunc

fminunc (find minimum of f , unconstrained search) implements families of gradient-based searches for minima.In using this command, you can either

• just provide the function to MATLAB, and the algorithm will sample extra

points to estimate∂f

∂x,∂f

∂y, etc.

• define the function and the gradient to MATLAB, so the gradient is exactinstead of estimated. (Advanced - see the Help menu)

Page 12: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1- Basic fminunc search - 5

Exercise: Download W10 1.m, which performs a search for a local minimum.It also displays the 2D function as a contour diagram, showing the starting pointand the ending point of the optimization search.

Question: How do we convert our original function (with inputs x and y) foruse in MATLAB with fminunc?

Question: What does the notice at the end of the MATLAB calculation mean?Is there any reason for concern?

Page 13: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1- Basic fminunc search - 6

Finding other Minima

Question: What change would you make to the script to look for a differentlocal minima?

Question: How could we look for the single global minimum?

Page 14: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1 - Tracking Progress - 1

Optimization Example 1 - Tracking Progress

One limitation of the basic optimization search is that we get no feedback aboutthe search process as it is running. This is acceptable during a search on a simplefunction like our example function, but can be a problem when working on morecomplex optimization tasks as in the design projects.We will introduce two ways to gain insight into the optimization search as it isrunning.

1) Using an optimset option for displaying the search status every iteration.

2) Modifying our objective function so output is displayed each iteration.

Page 15: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1 - Tracking Progress - 2

1) optimset with Display - Iter

Exercise: In W10 1.m, use the optimset command to display the progress ofthe search every iteration.

Question: How many iterations, and how many function calls does it take tofind one of the local minima?

Question: What are the limitations of this default ’Display’, ’iter’ out-put?

Page 16: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1 - Tracking Progress - 3

2) Updating in the Objective Function

Exercise: Copy the mathematical function

f (x, y) = (x4 − 16x2 + 5x) + (y4 − 30y2 + 5y)

from the main script into a new MATLAB function file, f1_plot_points.mExtend that function so that it also prints out the current search location, and plotthe current location on a contour diagram.

Copy the W10_1.m script to W10_2.m, and modify the search so it uses the newfunction.

Question: Experiment with different starting points, to search for different localminima. Can you identify the underlying gradient-based calculations by followingthe progress of the search positiion?

Page 17: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1 - Tracking Progress - 4

Question: Compare the ease of coding and the additional insight gained byusing either the ’Display’, ’iter’ option, vs. expanding the code in yourMATLAB objective function.

Page 18: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1 - Non-Gradient Search Methods - 1

Non-Gradient Search Methods

Gradient-based optimization methods are not the only numerical search strategy.Many optimization problems do not have smooth objective functions (e.g.the maxforce in a truss, as geometry is modified.)This means that derivatives of the objective function aren’t defined, or that theyhave odd discontinuities in them, so following the gradient may not be a reliablestrategy.Non-gradient optimization methods include:

• Simplex algorithm

• Genetic algorithms

• Sampling algorithms

• Simulated annealing

• and many more.

Engineers have developed all of these methods because general optimization is ahard problem!

Page 19: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1 - Non-Gradient Search Methods - 2

Simplex Method

MATLAB has a built-in function fminsearch which implements a simplex algo-rithm. The simplex algorithms start at a user-specified initial point, and samplesf (x) at nearby points to create a simplex, or n-D pyramid. Once the simplex isconstructed, there are rules that transform the simplex at each iteration to get itto contract into a local minimum.

1

1Lagarias et al., SIAM Journal of Optimization, 199, 9:1 pg 112-147

Page 20: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1 - Non-Gradient Search Methods - 3

MATLAB - Switching Optimizers

You’ll notice that most search algorithms have the same input structure i.e. MAT-LAB syntax.

x = fminunc(fun,x0,options)

x = fminsearch(fun,x0,options)

This is a good thing, because it means you can swap different searches strate-gies/algorithms in and out relatively easily when you want to experiment.

Page 21: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Optimization Example 1 - Non-Gradient Search Methods - 4

Exercise: Look at fminsearch in MATLAB help.

Exercise: Modify W10 2.m to use fminsearch to search for local minima ofour function.

Question: Compare the search patterns of fminunc and fminsearch.

Page 22: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Spring Example - Complicated Objective Functions - 1

Spring Example - Complicated Objective Functions

More typical optimization tasks involve finding optimal values of more than twovariables. These also frequently involve an objective function that cannot be easilyexpressed as a formula, but instead is the result of a step-wise calculation.

Consider the 5 spring/5 node arrangement below.

b

b

b

bb

L = 2

L = 3

L = 4

L = 5

L = 7

P1

P2

P3

P4P5

Page 23: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Spring Example - Complicated Objective Functions - 2

b

b

b

bb

L = 2

L = 3

L = 4

L = 5

L = 7

P1

P2

P3

P4P5

Exercise: If all the springs shown have spring constant k = 1, find a formula forthe total potential energy in the system, based on the five note locations, P1, . . . P5.

Page 24: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Spring Example - Complicated Objective Functions - 3

Exercise: On paper, write out the pro-cess for computing the total potential energyin the springs based on the five (x, y) nodelocations, P1, . . . P5 or (x1, y1), . . . (x5, y5).

b

b

b

bb

L = 2

L = 3

L = 4

L = 5

L = 7

P1

P2

P3

P4P5

Page 25: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Spring Example - In MATLAB - 1

Spring Example - In MATLAB

b

b

b

bb

L = 2

L = 3

L = 4

L = 5

L = 7

P1

P2

P3

P4P5

Exercise: Translate the on-paper work into an objective function in MATLAB.

Question: How did we choose to represent the node locations defined in MAT-LAB? Are other options possible?

Page 26: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Spring Example - In MATLAB - 2

b

b

b

bb

L = 2

L = 3

L = 4

L = 5

L = 7

P1

P2

P3

P4P5

Exercise: Write a main script W10_3.m that will use fminsearch to search fora minimum energy configuration of the nodes.

Exercise: Extend your objective function so that it displays the current config-uration, and current potential energy, as the search is occurring.

Page 27: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Spring Example - Impact of Initial Values - 1

Spring Example - Impact of Initial Values

Exercise: Copy W10_3.m to W10_4.m. Up-date the initial positions to all be the sameand re-run the search for the minimum en-ergy configuration.

b

b

b

bb

L = 2

L = 3

L = 4

L = 5

L = 7

P1

P2

P3

P4P5

Exercise: Repeat the W10_4.m search, but now using fminunc. What changesin the results?

Page 28: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Spring Example - Impact of Initial Values - 2

Importance of the Initial Values for Optimization

There is interaction between the initial conditions and the search type. Generallyspeaking:

• The closer your initial values are to a reasonable design, the more reliable yoursearch will be.

• If you start with impossible or very poor configurations, the search can fail tomake any improvement at all.

• Displaying the current configuration as you search can help uncover failures inthe search process.

Page 29: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Practical Optimization Tips - Limiting Search Time - 1

Practical Optimization Tips - Limiting Search Time

Running the optimization routines like fminsearch for a long time can be prob-lematic

• poor starting point leads to slow improvements,

• batteries run out,

• systems crash, etc.

It can be helpful to have the optimization routine

• show the output each iteration, and

• stop and produce its ‘best-so-far’ result, even if it hasn’t found a local mini-mum/maximum.

options = optimset(’Display’, ’iter’, ’MaxIter’, 500);

[vmin, fmin] = fminsearch(fv, v0, options)

Page 30: Applying optimization tools to nd the inputs that maximize ...math272/Notes/Week10/notes10.pdfL = 5 L = 7 P 1 P 2 P 3 P 5 P 4 Exercise: Write a main script W10_3.m that will use fminsearch

Practical Optimization Tips - Limiting Search Time - 2

Exercise: Return to the spring system example in W10_4.m. Use fminsearch,but use the optimset options to stop the search after 50 iterations.