O'Reilly logo

Genetic Algorithms and Machine Learning for Programmers by Frances Buontempo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required