Difference between revisions of "CISC181 F2017 Lab4"

From class_wiki
Jump to: navigation, search
(Created page with "===Preliminaries=== * Make a new project with ''n'' = 4 (following these instructions) * Name your main class "Lab4" (when cre...")
 
(Instructions)
 
(19 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
In this lab you will write a simple AI (artificial intelligence) for a "Creature" that exists in a grid-like world.  Similar to the turtle in the last assignment, your Creature lives in a 2-D grid with rectangular borders.  However, this time if the Creature tries to cross one of the borders it is not stopped, but rather crosses over to the opposite side.  Thus, the world may be thought of as a torus.
 
In this lab you will write a simple AI (artificial intelligence) for a "Creature" that exists in a grid-like world.  Similar to the turtle in the last assignment, your Creature lives in a 2-D grid with rectangular borders.  However, this time if the Creature tries to cross one of the borders it is not stopped, but rather crosses over to the opposite side.  Thus, the world may be thought of as a torus.
  
[[Image:Bt screenshot.png|300px]]<br> ‎
+
Also, in this world each grid square has a different ''terrain type:'' DESERT, FOREST, FIELD, WATER, or MOUNTAIN. These are represented as shown below with different colors: yellow, dark green, light green, blue, and red, respectively.  The Creature can move through any terrain type, but DESERT and MOUNTAIN grid cells are difficult and thus deplete one ''energy unit'' for each turn you spend in them.  FOREST cells have shade and berries, and thus regenerate one energy unit for each turn you spend in them.  If you get down to an energy level of 0, you can no longer move and are effectively "dead."
  
<!--[[Image:300px-Turtle_grid.png|300px]]<br>-->
+
Finally, there are a number of ''food'' caches scattered around the world (shown as red circles) that will completely replenish your energy if your Creature visits them.  However, once you have used a food caches, it disappears forever and cannot be used again.
  
There are several facts about a turtle:
+
[[Image:Bt screenshot.png|800px]]<br>
* It has an integer grid position <tt>(row, column)</tt> (where (0, 0) is the upper-left corner of the rectangle). The turtle above is at (2, 3)
 
* It faces in a compass direction: <tt>NORTH</tt>, <tt>SOUTH</tt>, <tt>EAST</tt>, or <tt>WEST</tt>.  The turtle above is facing <tt>NORTH</tt>.
 
* It can turn, but only to the left (counter-clockwise), and 90 degrees at a time.  If the turtle above turned once, it would be facing <tt>WEST</tt>. 
 
* It can change its position according to a ''movement pattern'' <tt>(forward, lateral)</tt>.  These must be integers, but each of them can be positive, negative, or zero.  <tt>forward</tt> determines how many squares ahead the turtle would move ''in the direction it is currently facing'', and <tt>lateral</tt> determines how many squares to the left (a negative value) or right (positive) the turtle would move ''relative to the direction it is facing''.  NO TURNING OCCURS.  Also, the turtle cannot make any movement that would land '''outside''' the grid boundaries .  Both turtles below have a movement pattern of <tt>(2, -1)</tt>; the ''blue'' turtle has moved once from its initial position, and the ''red'' turtle has turned once before moving once.
 
  
[[Image:300px-Turtle_grid_after_move.png|300px]]<br>
+
The object of this assignment is to write an AI for your Creature that will guide it to get every food cache without dying along the way.  This will require you to:
  
Write a class <tt>Turtle</tt> which stores all relevant information in appropriately named and typed fields, and implements the above behaviors.  To that end, define an enumerated type <tt>Direction</tt> for the compass directions '''outside of the Turtle and Lab3 classes''', and set the grid dimensions with static constants <tt>NUM_ROWS</tt>, <tt>NUM_COLS</tt> (both equal to 10) '''inside the Turtle class'''.  All fields should be <tt>private</tt>.  Your class does not need to be <tt>public</tt>, so please keep it inside <tt>Lab3.java</tt> file, but define it ''above'' the <tt>Lab3</tt> class.
+
* Select one of the remaining food caches and move toward it, until there are none left
 +
* Monitor your current energy level and make sure it does not dwindle to 0 during your quest
  
Your class '''must''' have the following public methods that set or change the state of the turtle:
+
Note that every time you run the program, the world is regenerated randomly and the Creature is placed in it randomly (but NOT in DESERT or MOUNTAIN areas).
  
* The default (no-argument) constructor should place the turtle initially at <tt>(NUM_ROWS / 2, NUM_COLS / 2)</tt>, facing <tt>NORTH</tt>, with movement pattern <tt>(1, 0)</tt>
+
We are providing you with several files that implement this simulator, draw the Creature, furnish you with information about the world, and allow you to choose an "action" to move the Creature aroundUnlike the previous lab, actual movement is handled by the simulator rather than you.  Here are the files you should add to your project (note that you probably need to change the package name to match your own):
* A 3-argument constructor which sets the turtle's initial position and directionThe default movement pattern is used
 
* A 5-argument constructor which is like the 3-argument constructor, but adds the ability to set the movement pattern
 
* <tt>void turn(int n)</tt>: The turtle executes <tt>n</tt> counter-clockwise 90 degree turns.  A negative number of turns is ignored.
 
* <tt>void move(int n)</tt>: The turtle executes its current movement pattern <tt>n</tt> times.  Any attempted movement that would land outside the grid is ignored (but all moves before it happen).  A negative number of moves is also ignored.
 
* <tt>void flip(boolean doForward, boolean doLateral)</tt>: Negate forward and/or lateral component of movement pattern.  This changes the movement pattern for all future moves until <tt>flip()</tt> is called again
 
  
The class must also have the following public accessor and informational methods:
+
* [https://drive.google.com/open?id=0B0dKPju-EmtQQmx5a2ViYjVzVU0 BioTorus.java]
 +
* [https://drive.google.com/open?id=0B0dKPju-EmtQZDkta2xzOTFBUXc Creature.java]
 +
* [https://drive.google.com/open?id=0B0dKPju-EmtQQ0dkbFFKYUVUUGs MyClass.java] (this is my name for your Lab4.java -- probably it is easiest to just copy the contents into your own pre-made file)
  
* <tt>int getRow()</tt>, <tt>int getColumn()</tt>, <tt>Direction getDirection()</tt>
+
This code will not compile as is, because it expects a class called <tt>MyCreatureBrain</tt> that does not exist.  You should create this class and change the name to something unique, like your last name instead of "My".  This class should extend the abstract class <tt>CreatureBrain</tt> (defined in Creature.java)<tt>CreatureBrain</tt> has a number of utility functions that you will inherit and can use, but one ''abstract'' method that you must define: <tt>void setAction()</tt>.  This function will be called automatically by the simulator, once per turn, and is the place where your Creature "thinks" about its goals and situation, and makes a decision about which of the following actions to take:
* <tt>int squaresTraveled()</tt>: How many squares has the turtle moved ''forward and laterally'' since it was constructed? 
 
* <tt>double distanceToHome()</tt>: Euclidean distance from the turtle's current position to its initial position
 
  
You may add any "helper" private methods that you need. 
+
* Move forward one space
 +
* Turn left 90 degrees (without moving forward)
 +
* Turn right 90 degrees
 +
* Stay in place and do not turn
  
'''Just to be clear, there is no drawing by you in this lab, and no need for arrays.  The pictures above are just for illustration'''
+
==Actions==
  
===Testing===
+
The way that you say what action you choose is to call one of the following methods inherited from <tt>CreatureBrain</tt>:
  
Test the correctness of your implementation in <tt>main()</tt> by constructing <tt>Turtle</tt> objects with different constructors and arguments, calling their methods in different sequences, then checking whether the final position, orientation, distance traveled, and distance home are correct. 
+
* <tt>setForwardSpeed(int speed)</tt>, where speed is either 0 (don't move) or 1 (move forward)
 +
* <tt>setTurningSpeed(int turnspeed)</tt>, where turnspeed is either -1 (turn left), 0 (don't turn), or +1 (turn right)
  
I have created a helper class for testing called [http://nameless.cis.udel.edu/class_data/181_s2017/TurtleTester.java TurtleTester.java]Download it and add it to your project by putting it in the same folder as <tt>Lab3.java</tt>.  '''Instructions for and examples of using it are [[CISC181_F2017_TurtleTester | here]].''' The TA will grade your submission using the examples on that page.
+
"Illegal" values will be ignored, and if you try to turn AND move forward at the same time, turning will take precedence.
 +
 
 +
==Sensing==
 +
 
 +
You can learn about the state of your Creature, where you are in the world, and where the food is with the following functions:
 +
 
 +
* <tt>int getRow()</tt>: What row of the grid your Creature is currently on
 +
* <tt>int getCol()</tt>: What column of the grid your Creature is currently on
 +
* <tt>Direction getDir()</tt>: What compass direction your Creature is currently facing
 +
* <tt>int getEnergyLevel()</tt>: How many energy units your creature has left
 +
* <tt>BioTorus.TerrainType getCurrentTerrain()</tt>: The type of terrain your Creature is currently standing on
 +
* <tt>BioTorus.TerrainType getForwardTerrain()</tt>: The type of terrain your Creature *will* be standing on if it moves forward
 +
* <tt>ArrayList<Food> getFoodLocations()</tt>: A list of the remaining food cache locations.  Each <tt>Food</tt> object has a row and col field giving its coordinates.
 +
 
 +
Remember that getting to the nearest food might require crossing one of the grid boundaries to "loop" back to the other side. You can access the grid dimensions with <tt>getNumRows()</tt> and <tt>getNumCols()</tt>.
 +
 
 +
==Debugging==
 +
 
 +
Finally, for help with debugging, you can display a String over your Creature's current location by setting <tt>thoughtBubbleString</tt> to a non-null value in <tt>setAction()</tt>.
 +
Some examples:
 +
 
 +
* <tt>thoughtBubbleString = getForwardTerrain().name();</tt>
 +
* <tt>thoughtBubbleString = Integer.toString(getFoodLocations().size());</tt>
 +
* <tt>thoughtBubbleString = Integer.toString(getEnergyLevel());</tt>
 +
 
 +
===Submission===
 +
 
 +
Randomness in decision-making is allowed, and it could be very helpful in making sure you never get "stuck." However, you will be evaluated on two criteria: (1) Do you reliably get all of the food without dying?, and (2) How many turns does it take you to get all of the food?  Random wandering is permissible, but the more "direct" you are, the better.  You may add any extra fields and methods to your brain class that you want -- up to and including an array to "memorize" what you have seen, if you think that could be useful.
 +
 
 +
When we test for grading, we will use the same world and Creature start locations for fairness.
 +
 
 +
You should only submit your <tt>MyCreatureBrain.java</tt>.  Remember, it should be renamed to something unique like <tt>JSmithCreatureBrain.java</tt> or <tt>StudentXCreatureBrain.java</tt>Just make sure your full name and section number are in a comment in the file.

Latest revision as of 09:21, 3 October 2017

Preliminaries

  • Make a new project with n = 4 (following these instructions)
  • Name your main class "Lab4" (when creating a new module in the instructions above, in the Java class name field)
  • Modify Lab4.java by adding your name in a comment before the Lab4 class body.

Instructions

In this lab you will write a simple AI (artificial intelligence) for a "Creature" that exists in a grid-like world. Similar to the turtle in the last assignment, your Creature lives in a 2-D grid with rectangular borders. However, this time if the Creature tries to cross one of the borders it is not stopped, but rather crosses over to the opposite side. Thus, the world may be thought of as a torus.

Also, in this world each grid square has a different terrain type: DESERT, FOREST, FIELD, WATER, or MOUNTAIN. These are represented as shown below with different colors: yellow, dark green, light green, blue, and red, respectively. The Creature can move through any terrain type, but DESERT and MOUNTAIN grid cells are difficult and thus deplete one energy unit for each turn you spend in them. FOREST cells have shade and berries, and thus regenerate one energy unit for each turn you spend in them. If you get down to an energy level of 0, you can no longer move and are effectively "dead."

Finally, there are a number of food caches scattered around the world (shown as red circles) that will completely replenish your energy if your Creature visits them. However, once you have used a food caches, it disappears forever and cannot be used again.

Bt screenshot.png

The object of this assignment is to write an AI for your Creature that will guide it to get every food cache without dying along the way. This will require you to:

  • Select one of the remaining food caches and move toward it, until there are none left
  • Monitor your current energy level and make sure it does not dwindle to 0 during your quest

Note that every time you run the program, the world is regenerated randomly and the Creature is placed in it randomly (but NOT in DESERT or MOUNTAIN areas).

We are providing you with several files that implement this simulator, draw the Creature, furnish you with information about the world, and allow you to choose an "action" to move the Creature around. Unlike the previous lab, actual movement is handled by the simulator rather than you. Here are the files you should add to your project (note that you probably need to change the package name to match your own):

This code will not compile as is, because it expects a class called MyCreatureBrain that does not exist. You should create this class and change the name to something unique, like your last name instead of "My". This class should extend the abstract class CreatureBrain (defined in Creature.java). CreatureBrain has a number of utility functions that you will inherit and can use, but one abstract method that you must define: void setAction(). This function will be called automatically by the simulator, once per turn, and is the place where your Creature "thinks" about its goals and situation, and makes a decision about which of the following actions to take:

  • Move forward one space
  • Turn left 90 degrees (without moving forward)
  • Turn right 90 degrees
  • Stay in place and do not turn

Actions

The way that you say what action you choose is to call one of the following methods inherited from CreatureBrain:

  • setForwardSpeed(int speed), where speed is either 0 (don't move) or 1 (move forward)
  • setTurningSpeed(int turnspeed), where turnspeed is either -1 (turn left), 0 (don't turn), or +1 (turn right)

"Illegal" values will be ignored, and if you try to turn AND move forward at the same time, turning will take precedence.

Sensing

You can learn about the state of your Creature, where you are in the world, and where the food is with the following functions:

  • int getRow(): What row of the grid your Creature is currently on
  • int getCol(): What column of the grid your Creature is currently on
  • Direction getDir(): What compass direction your Creature is currently facing
  • int getEnergyLevel(): How many energy units your creature has left
  • BioTorus.TerrainType getCurrentTerrain(): The type of terrain your Creature is currently standing on
  • BioTorus.TerrainType getForwardTerrain(): The type of terrain your Creature *will* be standing on if it moves forward
  • ArrayList<Food> getFoodLocations(): A list of the remaining food cache locations. Each Food object has a row and col field giving its coordinates.

Remember that getting to the nearest food might require crossing one of the grid boundaries to "loop" back to the other side. You can access the grid dimensions with getNumRows() and getNumCols().

Debugging

Finally, for help with debugging, you can display a String over your Creature's current location by setting thoughtBubbleString to a non-null value in setAction(). Some examples:

  • thoughtBubbleString = getForwardTerrain().name();
  • thoughtBubbleString = Integer.toString(getFoodLocations().size());
  • thoughtBubbleString = Integer.toString(getEnergyLevel());

Submission

Randomness in decision-making is allowed, and it could be very helpful in making sure you never get "stuck." However, you will be evaluated on two criteria: (1) Do you reliably get all of the food without dying?, and (2) How many turns does it take you to get all of the food? Random wandering is permissible, but the more "direct" you are, the better. You may add any extra fields and methods to your brain class that you want -- up to and including an array to "memorize" what you have seen, if you think that could be useful.

When we test for grading, we will use the same world and Creature start locations for fairness.

You should only submit your MyCreatureBrain.java. Remember, it should be renamed to something unique like JSmithCreatureBrain.java or StudentXCreatureBrain.java. Just make sure your full name and section number are in a comment in the file.