l2 computational physicsastro.dur.ac.uk/~done/l2comp/cp_week6.pdf · 2018. 7. 3. · 2d plotting 12...

74
L2 Computational Physics 1

Upload: others

Post on 10-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

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()

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()

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()

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()

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()

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()

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()

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()

•  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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

γ 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()

γ 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()

γ 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()

γ 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()

γ – 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()

γ 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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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