Banner
KH-Model: Movement

KH-Model: Movement

Spring 2017

PSC/ANB 290

 


Introduction

The KH-Model is not a spatially explicit agent-based model even though we placed agents into space. For it to be a spatially explicit model, being located in space must matter for the outcome of virtual experiments. Today, we will make a few changes in the code and at functionality in MASONex that will allow us to implement random movement and aggregation.

PowerPoint: Random Movement

Todays Project: KH-Model-PramSweep-Movement


Environment

First we will create an Environment Class that extends SimStateSweep. We then add a new variable with getters and setters for agents to find other agents in their surrounding neighborhoods.


 

	public int mateSearchRadius = 2;

	public int getMateSearchRadius() {
		return mateSearchRadius;
	}

	public void setMateSearchRadius(int mateSearchRadius) {
		this.mateSearchRadius = mateSearchRadius;
	}

 


Agents

Next, instead of implementing Steppable for Agents, we will change Agents to Agent extends RandomWalker, which is a class that implements Steppable.  We will also need to add the variable mateSearchRadius, update the constructor method, step method, and findDate method.


 

	int mateSearchRadius;
	
	public Agent(Environment state, int x, int y,int walkRuleNumber, 
                    boolean female, double attractivenessA) {
		super(state,x,y,walkRuleNumber);
		this.x = x;
		this.y = y;
		this.female = female;
		this.attractiveness = attractivenessA;
		this.maxAttractivenss = state.maxAttractiveness;
		float value =(float)(this.attractiveness/this.maxAttractivenss);
		if(female)
			setColor(state, (float)1, (float)0, (float)0, value);
		else
			setColor(state, (float)0, (float)0, (float)1, value);
		this.similar = state.similar;
		this.choosiness = state.choosiness;
		this.maxDates = state.maxDates;
		this.probe = state.probe;
		this.mateSearchRadius = state.mateSearchRadius;
	}

 


Next, we update the step method allow our agents to move by using super.


	public void step(SimState state) {
		super.step(state);
		findDate((Environment) state);

	}

 


Finally, we update the findDate with one line of code.


 

        public void findDate(Environment state){
		Bag all = state.sparseSpace.getMooreNeighbors(x, y, mateSearchRadius, 
                     state.sparseSpace.TOROIDAL, false);
		all.shuffle(state.random); //randomly shuffle them
		Agent other = null;
		for(int i = 0;i< all.numObjs;i++){
			other = (Agent)all.objs[i];
			if(other.female != this.female){
				break;
			}
			else{
				other = null;
			}
		}
	
		double p1 = 0; //choosing agent
		double p2 = 0; //selected agent
		if(other != null){
			if(similar){
				p1 = chooseSimilar(other);
				p2 = other.chooseSimilar(this);
			}
			else{
				p1 = chooseTheBest(other);
				p2 = other.chooseTheBest(this);
			}
			p1 = closingTimeRule(p1); //correct for closing time rule
			p2 = other.closingTimeRule(p2);
			d++;// increment d
			other.d++;// increment d
			
			//make joint decision
			double p = p1 * p2; //joint probability
			if(state.random.nextBoolean(p)){
				if(this.female){ //(female, male)
					probe.getData(this.attractiveness, other.attractiveness);
				}
				else{
					probe.getData(other.attractiveness, this.attractiveness);
				}
				this.removeSelf(state);
				other.removeSelf(state);
			}
		}
	}

 


Environment Again

Finally, we need to update the agent make method to include a random walk rule.  The “classic” rules are:

Name                      Number
Speedster                  193
Zigzag                     65
Forward-Left-Right         162
Brownian                   255
Sidestep                   34
von Neuman                 170
Close-to-Home              20
Cyclone                    96
Tail Chaser                24

 


 

	public void makeAgents(){
		for(int i= 0;i<females;i++){
			Int2D location = findUniqueLocation();
			double attractiveness = (int)(random.nextDouble() * maxAttractiveness) + 1;
			Agent a = new Agent(this,location.x, location.y,
                           RandomWalkMechanics.classicRules[rule],true, attractiveness);
			a.stop = schedule.scheduleRepeating(a);
			sparseSpace.setObjectLocation(a, location);
		}
		
		for(int i= 0;i<males;i++){
			Int2D location = findUniqueLocation();
			double attractiveness = (int)(random.nextDouble() * maxAttractiveness) + 1;
			Agent a = new Agent(this,location.x, location.y,
                           RandomWalkMechanics.classicRules[rule],false, attractiveness);
			a.stop = schedule.scheduleRepeating(a);
			sparseSpace.setObjectLocation(a, location);
		}
	}