Banner
Lab 2


Random Movement

Winter 2016

Psychology 120


In this lab, you will get your first opportunity to work with an agent-based model.  Our goal with this project is to eventually create agents that can do interesting things to investigate (i.e., we will develop a simple model of human mate choice by the end of the quarter).

Problem

One of the things real agents do is to change direction and not always move in the same direction.  One of the ways that they can move is to change direction randomly.  You will implement random movement.  If you do not have a working version of the program, ask me and we will delete your current project and replace it with this one.

Step 1

Before we implement random movement, let’s complete all four conditions (i.e., movement [a] with and [b] without collisions in [1] toroidal and [2] bounded spaces). We are missing condition [2b]. So that we have all four, we need to add the ability for particles to not collide with each other but to bounce off boundaries.

Hint 1:  Your new step method should look something like this:

    public void step(SimState state) {
	if(doCollisions){
		if(boundaries){
		     moveWithCollisionsBoundaries(state, attemptsToAvoidCollisions);
		}
		else {
		     moveWithCollisions(state, attemptsToAvoidCollisions); 
                          //if doCollisions is true
		}
	}
	else {  //particles pass through each other
		if(boundaries){ //there are boundaries
                     moveWithBoundaries(state);
                }
                else { //there are no boundaries
		     move(state); //else move as before (torus)
                }
	}
    }

You need to write the method protected void moveWithBoundaries(SimState state).

Hint 2: Modify the move method below.

    public void move (SimState state){
	x+=xdir;
	y+=ydir;
	Environment se =(Environment)state;
	x = se.particleSpace.stx(x);
        y = se.particleSpace.sty(y);
	se.particleSpace.setObjectLocation(this, x, y);
   }

Step 2

Create a random movement method.

Hint 1:  The form of the method could look like this and you will fill in the details:

     public void moveWithBoundaries(SimState state){
          Environment se = (Environment)state;
		   //convert the SimState to ourEnvironment

          if(se.random.nextBoolean(probabilityOfChange){
             //Put your code in here!
          }
    }

Hint 2: The simplest method to rewrite is move, which is done for you below.

    public void move (SimState state){
        Environment se =(Environment)state;
        if(se.random.nextBoolean(probabilityOfChange){ 
            xdir = se.random.nextInt(3)-1; 
            ydir = se.random.nextInt(3)-1; 
         }
	x+=xdir;
	y+=ydir;
	x = se.particleSpace.stx(x);
	y = se.particleSpace.sty(y);
	se.particleSpace.setObjectLocation(this, x, y);
    }

You will be rewriting each of the methods so that movement can be random to some degree.  Then write methods for a toroidal space and methods with and without boundaries. There will be a total of four methods and you can use the methods already defined.

Step 3

Make sure you have added double probabilityOfChange to the Particle and Environment classes.  Add get and set methods for probabilityOfChange  to Environment class and a set method to the Particle class.

Step 4

Run your program and debug it.

Step 5

Show us your program and that it does the steps listed above and you are done!