CS 231

Assignment 1: Fields of Agents

Due 24 September 2007

Overview

For this assignment we will be generating very simple agent-based simulations of various scenarios. The inspiration for this project comes from work at the Brookings Institute by Epstein and Axtell on modeling human social dynamics. There are a number of newer publications and white papers on their web site with more recent results, including significant models of the spread of disease.

If you are interested in the subject, the book Growing Artificial Societies provides a nice introduction.

Setup

Find a partner, if you wish. I recommend it.

Create a new project in BlueJ, or whatever your preferred IDE is. Call the project something informative. You will want to copy your RandomInt class over to the directory so that it is included in the project.

Procedure

The project is broken down in a number of small steps. If any step takes you longer than 30-60min, stop by and ask some questions.

Please test as you go. For each class, write a main function that tests all of the methods you've written to ensure that they do the right thing. Print statements are extremely useful, because they let you see what your program is doing, so put in lots of them.

Comments in your code are also extremely important. Please put function descriptions (brief) before each method. You should also have short comments before any signficant block of code, like a for loop. See Maxwell's style guide for some general guidelines and examples on code writing.

  1. Entity class: create an Entity class that will represent individual agents in the simulation. The Entity class should have integer fields for row, column, and id. Provide a constructor that takes all three arguments. The class should also have accessors for each field (functions that return the value of the field), and a set function that takes a new row and column as ints, as well as a set function that takes all three fields. The legalMove function should just return true for now, allowing any move. The toString method ought to output a string representation of the Entity. For now, just output a string version of the id value.
    • public Entity(int rows, int cols, int id);
    • public int getRow()
    • public int getCol()
    • public int getID()
    • public void set(int row, int col)
    • public void set(int row, int col, int id)
    • public boolean legalMove(int row, int col)
    • public String toString()
  2. Greenfield class: create a Greenfield class that represents the terrain on which the agents exist. The Greenfield class should have row and column fields as well as a 2D array of type Entity. Make a constructor that takes the number of rows and columns as arguments and allocates the 2D array of references. It should not allocate the individual Entity elements. In addition, create accessor methods for the rows and cols fields, and methods that can get and set locations in the grid. The toString method ought to convert the 2D map into a String object that is useful for visualization. It will be a big String object.
    • public Greenfield(int rows, int cols)
    • public int getRows()
    • public int getCols()
    • public Entity get(int row, int col)
    • public void set(int row, int col, Entity e)
    • public String toString()
  3. RandomInt class: make sure your RandomInt works properly. As specified in the homework, it ought to return a value in the range [0, N-1] inclusive.
  4. SimulationA class: the simulation class runs the actual simulation using the other three classes. It needs a Greenfield, an array of Entity objects, a row RandomInt, a column RandomInt, and an identity RandomInt. The SimulationA class should have a constructor with at least row, column, and number of agents parameters. It should also have an initialize function that sets up the simulation and puts it in a random initial state. The iterate function should execute one iteration of the simulation, going through each Entity. Finally, the function should have a write function that prints a visualization of the map to the system output.
    • public SimulationA(int rows, int cols, int agents)
    • public void initialize()
    • public void iterate()
    • public void write()

    The iterate function is the most complex part of the class. Follow the algorithm below for your first implementation.

    • For each entity in the array
      • Test if the entity is happy by counting the number of neighbors that are like and unlike. Start by just looking in the row above, the row below, the column left and the column right of the entity's current location. You can expand the area in which it looks to an arbitrary size as an extension.
      • If the entity has more like neighbors than unlike it's happy and nothing happens.
      • If the Entity is not happy, pick a new random location for it, test to see if the new location is free, and swap its position if the location is empty and the move is a legalMove (see function under Entity above).

  5. Run the simulation for different numbers of iterations, field sizes, and entities. A good size to start with is 20x60 with 500 agents and run for 50 iterations.
  6. Implement a second type of Entity by extending the Entity class to make a child class EntityLimit. EntityLimit should add a limit field, provide a useful constructor, and override the legalMove function so that any move where the distance is greater than the limit is prohibited. Then implement a second simulation class, SimulationB, that initialzes the board to half Entity and half EntityLimit. You can also choose to extend SimulationA and just override the initialize function. You might want the EntityLimit class to provide a different string representation than the Entity class (add 2 to the id number, for example).
  7. Run various simulations and see what difference the limits make. For your writeup, you'll want a few (at least 3) different simulation results showing different parameter settings and initial conditions.

Extensions

Writeup

Follow the writeup instructions to create a web page for your assignment. Send the instructor an email with the code in a zip or tar file as well as a pointer to the URL for the writeup.