This programming assignment continues the theme from Assignment 1.
Essentially, you will "objectify" the code from Assignment 1, while
also adding new features, and estimating new probabilities.
In this assignment, just like Assignment 1, agents are placed
in a grid at starting locations, and are given destination locations.
The agents move as before trying to reach their destinations.
Again, the simulation stops when all agents have reached their
destination or some agents remain irretrievably stuck.
This particular assignment will differ from Assignment 1
in the following ways:
- You will use dynamic objects in many ways for various
parts of the simulation. In particular:
- Define and use an Agent class to hold information
about agents.
- Define and use a Grid class to hold information
about the current status of the grid.
- Define and use a Cell class to hold information
about an individual cell. Make sure Grid
is a 2D array of Cell's.
- For each object created, ensure that appropriate initialization
is performed in the object's constructor, and that you use
appropriate mutators and accessors.
- Each object should have a toString() method that you
will use in the printGrid() method, and in other places
that you perform debugging. Indeed, printGrid()
should have only one line of code:
void printGrid (Grid g)
{
System.out.println (g);
}
Since you will write a toString() method for Grid,
the toString() method should
really write out the grid status (a snapshot of the grid).
- Implement a class called
RandomList
(So, don't use the RandomList
from Assignment 1) with the following features:
- A method called add_data (Object obj) that adds
an object to the list (or whatever data structure you use inside).
- A method called randomize that rearranges the
list randomly inside.
- A method called recycleFront() that returns the
front of the list and then moves the element at the front to the rear
(the recycling).
- Use this class in place of the RandomList class in
Assignment 1.
Here's the idea behind RandomList and why it's useful for
the simulation. Essentially, we want to pick random locations for
the agents, both as starting positions and destinations, right? One
important requirement is that we
don't want locations to repeat (this would potentially place two
agents on the same cell). So, what we really want is to to have
all the cells randomly
ordered. In this case, what we do is simply put all the cells in
a list and (randomly) re-order the list when desired.
For example, here cells of a 2-by-2 grid may be ordered
as follows:
(0,1) (0,0) (1,0) (1,1)
Here's another ordering of the same list:
(1,1) (1,0) (0,1) (0,0)
Now, everytime we want to "randomize" the list, we simply pick
a random rearrangement of this list, e.g.,
(1,0) (1,1) (0,0) (0,1)
Once we randomize, we can traverse the list left to right and
obtain a single random ordering. This ordering can then be used
to assign a start location to the first agent
(the location (1,0)), the second agent
(the location (1,1)), and so on.
We are going to implement traversing by
repeatedly calling recycleFront().
Here's the status
of the list when I call recycleFront() once (using
the above list):
(1,1) (0,0) (0,1) (1,0)
Notice that (1,0) went to the back of the list.
Here's what I get when I call
recycleFront()
again:
(0,0) (0,1) (1,0) (1,1)
Now go back to the code for Assignment 1 and observe how
recycleFront()
is used - it should make sense.
- You can actually implement the list in many ways. One of
the simplest is an array - this makes "recycling" easy.
In this assignment, unlike the first assignment, we will use
agents with different capabilities:
- In your previous assignment, an agent could only move a
distance of one cell (X or Y direction). In this assignment some
agents will be allowed to move more than one cell. Specifically,
each agent will be given a "horsepower" rating between 1
and H, where H is an integer.
A k-horsepower agent will be able to move
a total of k (or less, if
needed or allowed)
cells in either the X or Y direction. Note: in a single turn an
agent cannot move
some cells in the X direction, followed by some in the
Y-direction; thus, in any particular turn, an agent can move in only one direction.
- We will also introduce a "sleep" or "rest" time for each
agent. An agent, after it moves, will be required to sleep (miss a
few turns) for a number of time-steps. Specifically, a
k-horsepower agent will need to sleep for
k time
steps after each move. The case
k=1 corresponds to Assignment 1.
- There is one minor detail regarding "rest" time that's
important. Consider an agent with enough horsepower to reach the
destination in one move. Should this agent be removed or should it
wait to "rest" at the destination? For this assignment, we will
remove the agent without requiring it to "rest" at the destination.
Thus, agents do NOT rest at their destinations.
However, we'll still wait
T steps before removing it,
for consistency with Assignment 1.
- Also, it's possible that
all agents are "sleeping" in a particular timestep. Do not end
the simulation just because all agents are sleeping. The simulation
ends only if none remaining can move due to cell availability, or
all have reached.
- Next, it is possible for a k-horsepower agent to move less
than k steps. In this case, should it sleep less than k timesteps?
For simplicity, we'll let the agent sleep k timesteps.
- Finally, for simplicity, whenever there's a choice between
X and Y directions, an agent will prefer X to Y, even if the Y
direction is shorter.
Your overall goal in this assignment is see whether "horsepower"
makes a significant difference in the ability of agents to get to
their destinations.
To determine the "horsepower" of each agent, select this number
randomly between 1 and
H.
You will need to read in the value of
H
from the screen.
Deliverables:
- The source code for program. NOTE: It is no longer necessary
to write all your all your code in the
same file. In fact it is better to place each class in a separate file.
Just make sure your main method is in
Assign2.java.
- You will estimate the following:
- What is the fraction of agents that reach their destination?
Obtain this number for each simulation run and then take the
average across the 10,000 runs (simulations).
- Obtain the above fraction for each value of horsepower. That
is, what is the fraction (that reach) for horsepower 1, what is the
fraction for horsepower 2, etc. Is added horsepower an advantage?
- Also include results for the case
H=1
where all agents have the same horsepower, a horsepower of
1. You should get the same
results as in the first assignment.
Estimate each probability using an average of 10,000 simulations.
- You are to report the above numbers for the following
parameters:
- M=4,
and each value of
N
in the range [2,16].
Take T=1
and H=1.
This should confirm results from the previous assignment.
- M=4,
and each value of
N
in the range [2,16].
Take T=1
and H=4.
-
M=4, and each value of
N in the range
[2,16].
Take T
to be infinity and H=4.
Write these numbers programmatically to a plain text
file called results.txt in
your directory. For a few extra points, you might plot a graph
with N on the X-axis, and each
estimate on the Y-axis, for different values of
H.
- What did you do to ensure that your code is working correctly?
You will also need to provide some evidence, test cases (and output)
to validate your program.
- Submit evidence that you used the stepwise refinement approach.
- Your code will be graded
on both correctness and documentation.
Submission:
- The name of subdirectory for this exercise should be: your username
followed by _a2.
Example: if your username is beavis
your subdirectory will be called
beavis_a2.
- Follow the
submission instructions carefully as usual.
- Submit a printout (hardcopy) of your code before
the due date in my mailbox.