![Page 1: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/1.jpg)
L2 Computational Physics
1
![Page 2: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/2.jpg)
Optimisation Techniques
Function minimisation
2
![Page 3: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/3.jpg)
Optimisation Techniques � Background
� Aside – visualising 2D data as images
� Brute force methods
� Deterministic Methods
� Gradient Descent
� Nelder-Mead Simplex
� Stochastic Methods
� Genetic Algorithms
3
![Page 4: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/4.jpg)
Background
4
![Page 5: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/5.jpg)
Function minimisation � Given f(x,y,….) find the coordinates and value at the
minima of the function
� Analytical techniques – only useful for some functions
� Numerical techniques � This is what we will look at today
5
![Page 6: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/6.jpg)
Applications of minimisation
� Science � Fitting a model function to experimental data � Orbital mechanics � Optical design
� Maximise image quality � Minimise cost
� Adaptive Optics � Protein folding � Optimising control system parameters
� An example in your pocket: � Auto-focus cameras (smart phone)
6
![Page 7: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/7.jpg)
Aside
Visualising 2D functions
7
![Page 8: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/8.jpg)
• It’s going to be really useful to be able to visualise these functions
• Let’s look at how we do that with Python, numpy and matplotlib
8
![Page 9: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/9.jpg)
2D plotting • We want to visualise f(x, y)
• Evaluate f(x, y) over a range of evenly spaced x and y values
• Store the results in a 2D numpy array
• Display this with matplotlib
9
![Page 10: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/10.jpg)
2D plotting
10
from __future__ import division import numpy import matplotlib.pyplot as pyplot import matplotlib.colors as colors import matplotlib.cm def f(x, y): a=numpy.cos(0.2*x**2-0.3*y**2+3) b=numpy.sin(2*y-1+numpy.e**x) return a*b
![Page 11: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/11.jpg)
2D plotting
11
# Define bounds x0, x1 = -2.5, 2 y0, y1 = -2, 2 #explore 1000 points in x and y N_POINTS=1000 dx=(x1-x0)/N_POINTS dy=(y1-y0)/N_POINTS #generate x and y values xs=numpy.arange(x0,x1,dx) ys=numpy.arange(y0,y1,dy) #array to hold function values dat=numpy.zeros((len(xs), len(ys)))
![Page 12: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/12.jpg)
2D plotting
12
for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure() # Show a greyscale colourmap of the data im = pyplot.imshow(dat, extent=(x0, x1,y0, y1), origin='lower', cmap=matplotlib.cm.gray) pyplot.xlabel('x') !pyplot.ylabel('y') ! pyplot.colorbar(im, orientation='vertical', label='$f(x,y)$') pyplot.show()
![Page 13: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/13.jpg)
13
![Page 14: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/14.jpg)
Methods • Brute Force and Ignorance
– Exhaustive Search
• Deterministic searches – Nelder-Mead Simplex – Gradient Descent – Hill Climbing
• Stochastic Searches – Genetic Algorithm – Stochastic Gradient Descent – Simulated Annealing
14
![Page 15: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/15.jpg)
Brute Force and Ignorance
Fast to code Slow to run
15
![Page 16: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/16.jpg)
Exhaustive Search � For every x
� For every y
� Is this the smallest f(x, y)?
� Benefits
� Trivial to code
� Drawbacks
� Slow
� Not very accurate – it must operate on some finite, quantised grid
16
![Page 17: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/17.jpg)
Deterministic Methods
17
![Page 18: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/18.jpg)
Deterministic Methods • These methods all start from some initial position
• If you run the same method from the same position multiple times, you get the same result
18
![Page 19: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/19.jpg)
Gradient Descent
19
![Page 20: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/20.jpg)
Gradient Descent: Algorithm
� Walk downhill
20
![Page 21: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/21.jpg)
Gradient Descent: Algorithm Maths notation Quantity Python r! Position vector r = numpy.array(x, y) ( )rf ! Function at r! def f((x, y)):
return ??? ( )rf !∇ Vector differential of ( )rf ! def f((x, y)):
df_dx = ??? df_dy = ??? grad = numpy.array((df_dx, dy_dy) return grad
0r! Initial position r0 = numpy.array((x0, y0))
γ Step size gamma = 0.something
21
( )nnn rfrr !!!∇−=+ γ1
![Page 22: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/22.jpg)
22 Coloured lines are contours - see http://matplotlib.org/examples/pylab_examples/contour_demo.html
![Page 23: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/23.jpg)
23
![Page 24: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/24.jpg)
24
![Page 25: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/25.jpg)
25
![Page 26: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/26.jpg)
26
![Page 27: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/27.jpg)
27
![Page 28: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/28.jpg)
28
![Page 29: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/29.jpg)
29
![Page 30: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/30.jpg)
Step Size • Gradient Descent is highly sensitive to the step size,
gamma
• Too small a step and convergence is very slow
• Too large a step and it may overshoot and the method becomes unstable
• Audience Question: What causes this to happen?
30
![Page 31: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/31.jpg)
Step Size • Gradient Descent is highly sensitive to the step size,
gamma
• Too small a step and convergence is very slow
• Too large a step and it may overshoot and the method becomes unstable
• Curvature and higher order terms mean the gradient is only locally constant – adaptive step size can choose a gamma based on curvature measurements etc.
31
![Page 32: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/32.jpg)
γ to small – slow convergence
32
![Page 33: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/33.jpg)
γ larger – faster convergence
33
![Page 34: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/34.jpg)
γ about right
34
![Page 35: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/35.jpg)
γ to big – oscillatory convergence
35
![Page 36: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/36.jpg)
γ – perfectly wrong
36
![Page 37: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/37.jpg)
γ far to big - divergence
37
![Page 38: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/38.jpg)
GD Example 2 • Rosenbrock’s Banana Function
• A tough test case for minima finding
• Steep cliffs
• Very shallow valley
38
f (x, y) = (1− x)2 +100(y− x2 )2
![Page 39: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/39.jpg)
39
GD Example 2
![Page 40: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/40.jpg)
40
GD Example 2
Trajectories rapidly enter the valley then crawl along the shallow gradient to the minima at (1,1)
Most trajectories run out before then as we have not run for enough itterations
![Page 41: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/41.jpg)
41
The method can ‘zig-zag’ in an unstable manner if the step size is to large, constantly overshooting the lowest local value
![Page 42: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/42.jpg)
42
GD in the valley - slow
![Page 43: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/43.jpg)
43
GD in the valley - slow
![Page 44: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/44.jpg)
44
GD in the valley - slow
![Page 45: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/45.jpg)
45
GD in the valley - slow
![Page 46: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/46.jpg)
GD – when to stop? � It’s common to have a maximum number of
iterations
� Another common pattern is to terminate early upon reaching some convergence criteria
46
![Page 47: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/47.jpg)
Hill Climbing
47
![Page 48: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/48.jpg)
Hill Climbing � Hill climbing is similar to gradient descent but
simpler
� Hill climbing tries moving a small distance in one dimension only at a time
� When this no longer works, another dimension is explored
� Very simple to code
48
![Page 49: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/49.jpg)
Local Minima
49
![Page 50: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/50.jpg)
50
GD & Local Minima
![Page 51: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/51.jpg)
Pathological example
51
![Page 52: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/52.jpg)
No gradient into the minima
52
![Page 53: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/53.jpg)
Stochastic Methods Often, a little bit of randomness goes a long way
53
![Page 54: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/54.jpg)
54
Genetic Algorithms
![Page 55: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/55.jpg)
55
Genetic Algorithms • Random solutions
• Survival of the fittest
• Sexual Reproduction
• Mutation
![Page 56: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/56.jpg)
56
![Page 57: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/57.jpg)
57
![Page 58: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/58.jpg)
58
![Page 59: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/59.jpg)
59
![Page 60: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/60.jpg)
60
![Page 61: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/61.jpg)
61
![Page 62: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/62.jpg)
62
![Page 63: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/63.jpg)
63
![Page 64: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/64.jpg)
64
![Page 65: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/65.jpg)
65
![Page 66: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/66.jpg)
66
![Page 67: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/67.jpg)
67
![Page 68: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/68.jpg)
68
![Page 69: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/69.jpg)
69
![Page 70: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/70.jpg)
70
GA & Local Minima • The wide sampling of the initial ‘scattergun’
approach of the GA means that some points should fall near the global maxima
• Due to ‘survival of the fittest’ these rapidly form the basis of the population
![Page 71: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/71.jpg)
71
GA & Local Minima
![Page 72: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/72.jpg)
72
![Page 73: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/73.jpg)
73
![Page 74: L2 Computational Physicsastro.dur.ac.uk/~done/l2comp/CP_week6.pdf · 2018. 7. 3. · 2D plotting 12 for ix, x in enumerate(xs): for iy, y in enumerate(ys): dat[ix,iy]=f(x,y) pyplot.figure()](https://reader035.vdocuments.us/reader035/viewer/2022071213/6040c2823abefd3fd7292fc6/html5/thumbnails/74.jpg)
Weekly Assessment � You are going to implement a 2D Gradient Descent
and plot the results
� A gradient descent solver starts from a vector point � Just like your Euler solver for the spring
� The solver makes a series of steps that update the vector position with some function of the vector
� Just like your Euler solver for the spring
74