class Garden { int MAX; float mutationRate; Pixorg[] population; ArrayList darwin; int generations; Garden(float m, int num) { mutationRate = m; MAX = num; population = new Pixorg[MAX]; darwin = new ArrayList(); generations = 0; for (int i = 0; i < population.length; i++) { population[i] = new Pixorg(96 + 64 * i, 2*height/3, random(PI)); } } Pixorg get(int index) { return population[index]; } void naturalSelection() { darwin.clear(); float totalFitness = getTotalFitness(); for (int i = 0; i < population.length; i++) { float fitnessNormal = population[i].getFitness() / totalFitness; int n = (int)(fitnessNormal * 1000f); for (int j = 0; j < n; j++) { darwin.add(population[i]); } } } void generate() { for (int i = 0; i < population.length; i++) { int m = int(random(darwin.size())); int d = int(random(darwin.size())); Pixorg mom = (Pixorg)darwin.get(m); Pixorg dad = (Pixorg)darwin.get(d); CrossOver c; float r = random(1); if (r > 0.75) { c = new CrossOver(); } else if (r > 0.5) { c = new Stripes(); } else if (r > 0.25) { c = new PixelByPixel(); } else { c = new Spot(); } population[i] = new Pixorg(c.crossover(mom.getFlower(), dad.getFlower()), 96 + 64 * i, 2*height/3, (mom.theta + dad.theta) / 2f); population[i].mutate(mutationRate); } generations++; } int getGenerations() { return generations; } float getTotalFitness() { float total = 0; for (int i = 0; i < population.length; i++) { total += population[i].getFitness(); } return total; } int size() { return MAX; } }