Banner
Freezing Aggregation


Freezing Aggregation


In this lab, you will build an agent-based model from the beginning to answer the problem in the next section.

Problem

Suppose agents move randomly (“Brownian motion”) and when they attempt to move to another location that contains a “frozen” agent, they also freeze in their current location (a frozen agent no longer moves).  What will the aggregate of agents look like when they all freeze assuming (1) that one agent in the middle of the space is frozen and (2) all other agents are not frozen at the start of a simulation?

Step 1

Create a new Java project called “freezingAggregation”.  Next, create a package within the src folder.  Put MASON in the project path so that you can use the MASON classes and methods.  Create three files: (1) “Agent” which implements Steppable, (2) “Environment” which extends SimState, and (2) AgentsWithGUI which uses the code below (paste it into the file AgentsWithGUI.java) or copy and paste the file out of a previous project.

package freezingAggregation; 

import sim.engine.*;
import sim.display.*;
import sim.portrayal.grid.*;
import java.awt.*;
import javax.swing.*;
import sim.portrayal.simple.OvalPortrayal2D;

public class AgentsWithGUI extends GUIState {
	public Display2D display;
	public JFrame displayFrame;
	SparseGridPortrayal2D agentsPortrayal = 
			new SparseGridPortrayal2D();

	public static void main(String[] args) {
		AgentsWithGUI ex = new AgentsWithGUI();
		Console c = new Console(ex);
		c.setVisible(true);
		System.out.println("Start Simulation");
	}

	public AgentsWithGUI() {
		super(new Environment(System.currentTimeMillis()));
	}

	public void quit() {
		super.quit(); 

		if (displayFrame!=null) displayFrame.dispose();
		displayFrame = null;
		display = null;
	}

	public void start() {
		super.start();
		setupPortrayals();
	}

	public void load(SimState state) {
		super.load(state);
		setupPortrayals();
	}

	public void setupPortrayals() {
		Environment se = (Environment)state;
		agentsPortrayal.setField(se.space);
		OvalPortrayal2D o = new OvalPortrayal2D(Color.red);
		agentsPortrayal.setPortrayalForAll(o);
		display.reset();
		display.repaint();
	}

	public void init(Controller c){
		super.init(c);
		display = new Display2D(400,400,this);
		displayFrame = display.createFrame();
		c.registerFrame(displayFrame);
		displayFrame.setVisible(true);
		display.setBackdrop(Color.WHITE);
		display.attach(agentsPortrayal,"Agents");
	}

	public Object getSimulationInspectedObject() {
		return state;
	}
}

Step 2

In the Agent class, create a method that implements the following aggregation rule in pseudocode:

If(Not in contact with a frozen agent)
    move randomly
else
   freeze forever

To implement this rule, write a method of the form:

	public void moveTillFrozen(SimState state){
		
	}

You probably want to have at least the following variables in your Agent class:

	int x;
	int y;
	int dirx;
	int diry;
	boolean frozen;

 

Step 3

Initial Conditions

(1) In the Environment class, create n Agents, place them randomly in space, and

(2) place the n + 1 Agent in the middle of the space.  Set frozen = false for the randomly placed agents (at the time they are created)

Step 4

Create getters and setters for the relevant parameters.

Run your program, debug it.