// // CA to be used by Gillespie // import java.util.*; import java.io.*; import java.awt.*; public class GCA { private int[][] A; private int[] neighbourhood; private int n; // n X n grid, as a torus private int m; // m species private int population; // sum of all species private double pointSize; private boolean trace; private RandomSet[] S; // one for each species private RandomSet freeSpace; private Random gen; private int events; // number of events private boolean draw; // true <-> plot private static int empty = -1; private static Color background = Color.LIGHT_GRAY; private static Color[] color = {Color.BLUE,Color.RED,Color.YELLOW,Color.GREEN,Color.CYAN,Color.MAGENTA}; public GCA(int n,int m,boolean draw){ this.n = n; this.m = m; this.draw = draw; pointSize = 0.4; gen = new Random(); A = new int [n][n]; S = new RandomSet[m]; freeSpace = new RandomSet(n*n); neighbourhood = new int [9]; // see getNeighbourhood() for explanation for (int i=0;in-1) xt = n - i; yt = j; if (j<0) yt = j + n; if (j>n-1) yt = n - j; if (A[xt][yt] == empty){ k++; neighbourhood[k] = xt*n + yt; } } neighbourhood[0] = k; } public boolean birth(int species) throws RandomSetException { events++; int x = S[species].select(); int i = x/n; int j = x%n; getToroidalNeighbourhood(i,j); if (neighbourhood[0] > 0){ int k = gen.nextInt(neighbourhood[0]) + 1; int y = neighbourhood[k]; A[y/n][y%n] = species; S[species].add(y); population++; if (draw) plot(y/n,y%n,color[species]); return true; } //System.out.println("blip"); return false; } public void plot() { StdDraw.clear(background); StdDraw.setXscale(-2,n); StdDraw.setYscale(-2,n); StdDraw.show(0); for (int i=0;i