Print

Print


Here is Agent6, it is a work in progress.
regards.

package sim.algs.alg6;

import sim.engine.SimState;
import sim.engine.Steppable;
import sim.field.grid.Grid2D;
import sim.field.grid.ObjectGrid2D;
import sim.util.Bag;
import sim.util.IntBag;
import ec.util.*;

/**
 * Created by Your PC on 14/05/2015.
 */
public class Agent6 implements Steppable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public MersenneTwisterFast random;
	public static final int gridWidth = 45;
    public static final int gridHeight = 20;
    
    /*
     * Grid sizes under test are Square
     * 900, 1600, 2500, 3600.
     * Then for each of the squares a further four
     * rectangular grids with the same total cells 
     * will be tested.
     */

    private int myX, myY, nextX, nextY;
    private double age;
    private int counter;

    // This constructor for offspring
    public Agent6(){
        age = 0.0;
    }

    public Agent6(SimEnviro6 theModel) {
        placeFirstAgent(theModel);
        age = 1.0;
    }

    public void placeFirstAgent(SimEnviro6 theModel) {
        ObjectGrid2D mySpace = theModel.getMySpace();

        // 2 options either direct placement.
/*        do {
            myX = 3;
            myY = 3;
        }
			or randomly place agent into grid
*/		do {
			myX = theModel.random.nextInt(gridWidth-1);
			myY = theModel.random.nextInt(gridHeight-1);
		}
		
        while (mySpace.field[myX][myY] != null);
        mySpace.field[myX][myY] = this;
    }
    
    public void finish() {
    	
    }
    
    public void step(SimState state) {
/*		replicate();  
*/    }
    // Don't need this method as there is not a requirement
    // of random movement except for the first placement.
    // each subsequent placement is from the von Neumann neighbours
    // found by the built-in method.

    public void replicate(SimState state) {
        SimEnviro6 theModel = (SimEnviro6) state;
        Bag theAgents = theModel.getAgents();

        ObjectGrid2D mySpace = theModel.getMySpace();

        int hop = 1;
        IntBag xNeighbors = new IntBag(8);
        IntBag yNeighbors = new IntBag(8);

   		mySpace.getMooreNeighbors(myX, myY, hop, Grid2D.BOUNDED, false, null, xNeighbors, yNeighbors);
// null

        for (int i = 0; i < xNeighbors.size(); i++) {
/*
            System.out.print(" x = " + xNeighbors.get(i));  
            System.out.println(" : y = " + yNeighbors.get(i));
*/         	}
          		
        int len = xNeighbors.size();

        for (int i=0; i<len; i++) {
            Agent6 child = new Agent6();

                nextX = mySpace.tx(xNeighbors.get(i));
                nextY = mySpace.tx(yNeighbors.get(i));
                if (mySpace.field[nextX][nextY]== null) {
                	System.out.print(" nextX = " + nextX);
                	System.out.print(" : NextY = " + nextY);
                    System.out.println(" : counter = " + counter);
                    mySpace.field[nextX][nextY] = child;
                    child.myX = nextX;
                    child.myY = nextY;
                    theAgents.add(child);
                    counter++;
                    break;
                }
            } 
        
        		myX = nextX; myY = nextY;
        		if (counter == gridWidth * gridHeight)  {
        			state.finish();
        		} /* else 
        			if (mySpace.field[nextX][nextY] != null) {
        				state.finish();
        			} Does not work as there are !null cells in each
        			  neighbourhood.
        			
        			 */
    }
}