import java.util.*; public class ESPopulation { private static Random randg = new Random(); // Random Generator public int pop_size; public ESIndividual[] ind; public int best_index; // index of the best individual in ind array public int worst_index; // index of the worst individual in ind array public float avr_fitness; // average fitness of this population // best_fitness = ind[best_index].fitness // worst_fitness = ind[worst_index].fitness public ESPopulation(int psize,int num_obj_vars,float[] min_range,float[] max_range){ // Create a random population of size pop_size // psize: population size // gsize: genome size pop_size = psize; ind = new ESIndividual[pop_size]; for(int i=0; i < pop_size; i++) ind[i] = new ESIndividual(num_obj_vars,min_range,max_range); evaluate(); } public ESPopulation(ESIndividual[] p){ // Create a population with the same individuals in p pop_size = p.length; ind = new ESIndividual[pop_size]; for(int i=0; i < pop_size; i++) ind[i] = p[i]; evaluate(); } public static ESPopulation generate(ESPopulation p,boolean comma_strategy, int lambda,int xrate,int mrate, float[] min_range,float[] max_range){ // Generates a new population from p, p has Mu individuals (i.e. Mu = p.pop_size) // First create Lambda new individuals, xrate percent of these new induviduals // are generated by cross-over, mrate percent of them are generated by mutation, // and others by reproduction. // comma == true -> (Mu,Lambda) strategy // comma == flase -> (Mu+Lambda) strategy if( xrate < 0 || xrate > 100 || mrate < 0 || mrate > 100 || xrate + mrate > 100 ) System.err.println("error: xrate and/or mrate are not properly set"); if( lambda < p.pop_size ) System.err.println("error: lambda must be equal to or greater than mu"); ESIndividual[] newg = new ESIndividual[lambda]; // the lambda offsprings int newg_index = 0; int xn = xrate * lambda/100; // xn: number of offsprings to be produced by xover int mn = mrate * lambda/100; // mn: number of offsprings to be produced by mutation // xover: for(int i=0; i < xn; i++){ // select to parents for cross-over: int p1 = randg.nextInt(p.pop_size); int p2 = randg.nextInt(p.pop_size); newg[newg_index++] = ESIndividual.xover(p.ind[p1],p.ind[p2]); } // mutation: for(int i=0; i < mn; i++) newg[newg_index++] = p.ind[randg.nextInt(p.pop_size)].mutate(min_range,max_range); // reproduction: for(int i = newg_index; i < lambda; i++) newg[i] = p.ind[randg.nextInt(p.pop_size)]; ESIndividual[] result = new ESIndividual[p.pop_size]; if( comma_strategy ){ // use comma strategy // from newg select the first Mu (= p.pop_size) bests Arrays.sort(newg); // ascending order sort // so newg[newg.length-1] is the best individual // Now from newg, select the first lambda bests: int tmp = newg.length - 1; for(int i=0; i < result.length; i++){ result[i] = newg[tmp]; tmp--; } }else{ // use plus strategy // from p and newg select the first Mu (= p.pop_size) bests ESIndividual[] ml = new ESIndividual[p.pop_size + lambda]; // ml: comma + lambda for(int i=0; i < p.pop_size; i++) ml[i] = p.ind[i]; for(int i=p.pop_size; i < ml.length; i++) ml[i] = newg[i-p.pop_size]; Arrays.sort(ml);// ascending order sort // so ml[ml.length-1] is the best individual // Now from ml, select the first lambda bests: int tmp = ml.length - 1; for(int i=0; i < result.length; i++){ result[i] = ml[tmp]; tmp--; } } return new ESPopulation(result); } private void evaluate(){ // evaluate average fitness, best and worst individual of this population // greater fitness means better individual int best = 0; // index of the best individual float best_fitness = ind[0].fitness; int worst = 0; // index of the worst individual float worst_fitness = ind[0].fitness; float sum = ind[0].fitness; // sum of the fitness of individuals of this population for(int i=1; i < pop_size; i++){ sum += ind[i].fitness; if( ind[i].fitness > best_fitness ){ best = i; best_fitness = ind[i].fitness; }else if( ind[i].fitness < worst_fitness ){ worst = i; worst_fitness = ind[i].fitness; } } best_index = best; worst_index = worst; avr_fitness = sum / (float) pop_size; } public String toString(){ String s = "best individual = "+ind[best_index]; // s += "\nworst individual = "+ind[worst_index]; s += "\naverage fitness = "+avr_fitness; return s; } public static void main(String[] args) { } }