How to Get a Turtle into a Paper Bag
You now have an overview of hill climbing and simulated annealing. Next, you’ll see how to hill climb or descend a few different paper bags. Be forewarned, though, your turtles will get stuck in some of these. After that, you’ll hit the setup with a hammer to simulate annealing. From there, you’ll see how to use a temperature to model the energy of your system. This means you can probabilistically move the turtle to different spots. Ready for some hill climbing?
Climb Down a Hill
Your first recipe starts the turtle on the top-left edge of the bag. You’ll move him a step, and then stop when the next step would be up:
| pos = bag.left() |
| height = pos.y |
| while True: |
| pos.step() |
| if pos.y > height: |
| break ... |
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.