/** Population.java - This is an array of individuals. */ public class Population { public static final int SIZE = 20; public Individual [] individual; /** default constructor */ public Population() { individual = new Individual[SIZE]; for (int i = 0; i < SIZE; ++i) individual[i] = new Individual(); } /** findAverageFitness - In order for us to observe the overall * health of the population, and thereby see how the genetic algorithm * is progressing through its generations. * I'm calling findFitness() instead of referencing the fitness attribute * just in case the attribute is not up to date. */ public double findAverageFitness() { double sum = 0.0; for (int i = 0; i < SIZE; ++i) sum += individual[i].findFitness(); return sum / SIZE; } /** toString - print each individual on its own line. * Be careful with the syntax. */ public String toString() { StringBuilder sb = new StringBuilder(); sb.append(String.format("Population's average fitness is %5.2f\n", this.findAverageFitness())); for (int i = 0; i < SIZE; ++i) sb.append(String.format("%s\n", individual[i].toString())); return sb.toString(); } }