Tino Intro To Java

Last modified: March 08, 2023

Lab A4.9 Creature AI - Custom Strategy

If you have not done Lab A4.6 Creature AI - Direct Path, Lab A4.7 Creature AI - Scouting Line of Movement and Lab A4.8 Creature AI - Wander, you should go do those first as this lab will build upon those labs.

Mission

You are writing the AI for a creature that will wander the world finding and collecting food while avoiding walls. The creature must follow these rules while collecting treats:

  1. The creature can never end a frame even partially inside a wall. No going through walls.
  2. The creature can never end a frame more than 2 pixels from where they started (i.e. no moving faster than 2).
  3. You cannot remove or change any properties of any walls.
  4. You cannot move any treats or change the properties of any treats.
  5. You cannot remove any treats the creature is not touching.
  6. You cannot change the size or image of the creature.

The Current State

At this point the creature moves towards any treats it can directly reach and wanders around otherwise. This is pretty effective at eventually finding all the treats in many worlds, though it still has scenarios where it has a lot of difficulty. Here are some examples:

  1. When there are side passages, it often fails to go down any passages where no treats are in sight.
  2. If it is in a room with only a small exit, it has difficulty finding the exit.
  3. It often wanders around in the same area, repeatedly visiting the same places.

Define A Problem To Solve

In this lab, you will attempt to improve your AI so it can reach all the treats faster. In order to do this, you should choose a behavior that is currently not optimal and try to improve it. You don't need to make it perfect, just make it better than the original behavior. Here are some ideas for what to look for when coming up with ideas:

  1. Define how the creature will identify a special scenario where it often performs the wrong action and make the creature take a more helpful action in that scenario.
  2. Define a new behavior for the creature that will make it more likely for the creature to find treats. The behavior doesn't have to be helpful in every situation, but if it helps more than it hurts, it could be worth it.
  3. Improve an existing behavior so it can accomplish its purpose more effectively.

Develop New Strategies

You will develop and implement at least one significant new strategy. Create a google doc and make it shared with anyone with the link so a Teacher or TA can access it to give feedback. In the google doc you will write out a plan for your strategy, discuss the development process including challenges you encountered, and report on the results.

You can use the following prompts to help you develop and explain your strategy:

  1. What is the problem you are trying to address with this strategy? Be as specific as possible.
  2. What new behavior will you implement?
  3. Predict what effect the new behavior will have? What makes you think it will solve the problem?

Report Results

After attempting to implement the strategy, report on the results. Not every strategy will pan out, but if you find that a strategy didn't work, you should still explain what you tried in the description so you will have a log of ideas you attempted to implement and a detailed explanation of how you tried to implement it. Even a great idea may not seem to work initially due to ineffective implementation details. To make a strategy work optimally, you may need to go through numerous iterations and experiment with multiple tweaks before it is actually implemented effectively.

Example Plan

Here is an example showing how to plan out a strategy:

What is the problem you are trying to address with this strategy? Be as specific as possible.

I noticed that the Creature was sometimes blocked from reaching a treat by a wall that could be skirted past, if only it would slide off the wall instead of sticking.

What new behavior will you implement?

When finding a direct path to a treat, if the creature encounters a wall, the creature will try to move closer to the target either horizontally or vertically along the wall, rather than giving up. See the diagram below for an example showing how the sliding would work.

Predict what effect the new behavior will have? What makes you think it will solve the problem?

Since the creature will be able to move directly to treats that were previously not considered reachable, the creature will spend less time wandering randomly and will therefore find treats much faster. The creature will now be able to reach any treat that can be reached by continuously stepping closer to the target, even if it is not directly moving toward the target. It will still fail to find a direct path to targets in scenarios where the creature has to move away from the target to get around a wall.

Results

When I wrote the code, it didn't work correctly at first because the code got stuck in an infinite loop while trying to find the path to the creature. After adding a breakpoint, and observing what was happening, I could see that the creature kept moving back and forth past the x value of the treat. I resolved this issue by making the creature only move one cell at a time. This causes the creature to go slower, so it might be worth revisiting this to come up with a solution that still allows the creature to move at speed 2 without getting in an infinite loop.

Submission

  • Submit your PX_LastName_FirstName_Creature.java file. For example if you were in 3rd period and named Michael Wang, then you would submit P3_Wang_Michael_Creature.java.
  • Make sure your Google Doc has Editing permissions for "anyone with the link." This is for your teacher and TAs.
  • Paste the link to your Google Doc in the Submission Comments section on this submission form (which becomes visible after you enter a file to submit)

You must Sign In to submit to this assignment

Dark Mode

Outline