Did It Work?

Whichever function and algorithm you chose, you might end up with a turtle in your bag. If you don’t set the limits in the simulated annealing, you might end up with a turtle off the edge of the screen.

Hill Climbing

With the slightly slanting bag, and a sensible step size, around 0.1, your turtle can settle down in the bottom of the bag as shown in the figure.

images/Optimize/slanty_bag_300.png

He’s settled on the far right corner of the bag. If you make a larger step size, for example, the whole width of the bag, the turtle will get stuck quickly. You see this clearly when you try a different function, such as

 f = ​lambda​ x: math.fabs(x)

This gives you a v-shape. ...

Get Genetic Algorithms and Machine Learning for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.