Saturday, August 7, 2010

Optimal According to What?

Last time, I introduced the problem of finding an optimal path through Monkey IslandTM 2. If there's one thing I learned in College, it is that "optimal" can mean many things, and it is often best to specify specifically what you wish to optimize. In this case I wish to minimize the time spent traveling from screen to screen. I especially don't want to go to the larger map screens very often, and I most definitely do not want to move between islands any more than is absolutely necessary.

In order to more precisely define what I wish to optimize, consider the following simple score system:

  • It costs one point to complete any objective.
  • To travel from one location to another adjacent location costs some number of points greater than or equal to one.
Using this system, and trying to minimize score, the absolute lowest cost would be if all objectives were in the same location and I don't have to move at all. My score would then simply be the number of objectives. Any traveling you do on top of that acts as a penalty and brings your score up.

The first thing I needed is a discretization of the world to give values to the penalties you accrue for traveling. I painstakingly mapped out the world, and applied some serious guestimating to come up with this graph for the first act's locations:

This is not perfect by any means, but I feel it is a good approximation of the required times to move from place to place. It at least gives us some values to use while performing our search. Tweaking the criteria could potentially help us finish faster, but we'll just assume this is good enough for now.

The next thing I need to solve this problem is a list of the required objectives. This was created painstakingly by consulting a walkthrough and adding each step as an objective, making careful notes of all prerequisite objectives, and the locations where they occur. Once I have all this information I am ready to make a search.

I chose to implement a simple A* search because it is good for this job, and it has code readily available on wikipedia.

A search state can be identified as a location and a list of objectives that have already been completed. The cost function is calculated as described above (travel costs + objective costs), and the heuristic function is simply the number of uncompleted objectives. On each step I can either complete all available objectives at my current location, or if there are no available objectives, move to an adjacent location to my current one.

Using this method I am guaranteed optimality (according to my cost function above), and I get a fairly nice path:

  1. Get shovel
  2. Go to BarKitchen
  3. Get knife
  4. Go to Wally
  5. Get monocle
  6. Get paper
  7. Go to Cleaners
  8. Get bucket
  9. Go to Bar
  10. Talk to bartender about business
  11. Get spit with paper
  12. Go to Bridge
  13. Go to Beach
  14. Get stick
  15. Go to Swamp
  16. Get swampy water in bucket
  17. Go to Cemetery
  18. Go to CemeteryHill
  19. Dig up grave
  20. Go to Cemetery
  21. Go to Bridge
  22. Go to Hotel
  23. Use knife on rope
  24. Get cheese squiggles
  25. Go to LargosRoom
  26. Get Toupee
  27. Rig door with bucket of mud
  28. Go to Hotel
  29. Go to Cleaners
  30. Watch largo at dry cleaner
  31. Go to Hotel
  32. Go to LargosRoom
  33. Get Laundy ticket
  34. Go to Hotel
  35. Go to Cleaners
  36. Get Largo's Laundry
  37. Go to Bridge
  38. Go to Swamp
  39. Go to VoodooShack
  40. Get string
  41. Talk to voodoo lady about doll
  42. Give spit
  43. Give clothes
  44. Give toupee
  45. Give bone
  46. Go to Swamp
  47. Go to Bridge
  48. Go to Cleaners
  49. Open box
  50. Use stick and string with box
  51. Put cheese squiggle in box
  52. Pull string (wait for rat)
  53. Open box get rat
  54. Go to BarKitchen
  55. Put rat in soup
  56. Go to Bar
  57. Get a job (order stew)
  58. Go to Hotel
  59. Go to LargosRoom
  60. Stab largo
  61. Go to Swamp
  62. Go to BoatScabb
  63. Give Monocle and charter ship

This was calculated in around 15 seconds on a non-special computer, and it was completed in just over 13 minutes, with distractions. I can't see any way to improve on this path.

The next step will be to put in all the objectives for the next act (which has a considerably more complicated objectives graph) and see how it does. Wish me luck.

No comments:

Post a Comment