import java.util.Random; public class EPPopulation { private static Random randg = new Random(); // Random Generator public int pop_size; public EPIndividual[] 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 EPPopulation(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 EPIndividual[pop_size]; for(int i=0; i < pop_size; i++) ind[i] = new EPIndividual(num_obj_vars,min_range,max_range); evaluate(); } public EPPopulation(EPIndividual[] p){ // Create a population with the same individuals in p pop_size = p.length; ind = new EPIndividual[pop_size]; for(int i=0; i < pop_size; i++) ind[i] = p[i]; evaluate(); } public static EPPopulation generate(EPPopulation p,float[] min_range,float[] max_range){ // Generates a new population from p, p has Mu individuals (i.e. Mu = p.pop_size) // selection mechanism is (Mu + Mu) EPIndividual[] newg = new EPIndividual[p.pop_size]; // the lambda offsprings int newg_index = 0; // mutation: for(int i=0; i < p.pop_size; i++) newg[i] = p.ind[i].mutate(min_range,max_range); EPPopulation p2 = new EPPopulation(newg); EPIndividual[] result = new EPIndividual[p.pop_size]; for(int i=0; i < p.pop_size; i++){ int si = EPPopulation.tr_select(p,p2); // index of selected individual if( si < p.pop_size ) result[i] = p.ind[si]; else result[i] = p2.ind[si - p.pop_size]; } return new EPPopulation(result); } public static int tr_select(EPPopulation p1,EPPopulation p2){ // tournament selection of size 10 // it returns the index of selected individual in p1 or p2. // if index >= p1.pop_size, it means selected individual is p2[index - p1.pop_size] // otherwise the selected individual is p1[index] int s_index = randg.nextInt(p1.pop_size+p2.pop_size); // index of the selected individual float s_fitness; if( s_index < p1.pop_size ) s_fitness = p1.ind[s_index].fitness; else s_fitness = p2.ind[s_index - p1.pop_size].fitness; int tr_size = Math.min(10,p1.pop_size + p2.pop_size); for(int i=1; i < tr_size; i++){ int tmp = randg.nextInt(p1.pop_size + p2.pop_size); if( tmp < p1.pop_size ){ if( p1.ind[tmp].fitness > s_fitness ){ s_index = tmp; s_fitness = p1.ind[tmp].fitness; } }else{ if( p2.ind[tmp - p1.pop_size].fitness > s_fitness ){ s_index = tmp; s_fitness = p2.ind[tmp - p1.pop_size].fitness; } } } return s_index; } 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) { } }