PackageChapter2;Importjava.util.Arrays;ImportJava.util.Comparator;ImportJava.util.Random;/*** A population is a abstraction of a collection of individuals. The population * class is generally used to perform group-level operations on its individuals, * such as finding the Stron Gest individuals, collecting stats on the population * as a whole, and selecting individuals to mutate or crossover. * * @authorBkanber **/ Public classPopulation {Privateindividual population[]; Private Doublepopulationfitness =-1; /*** Initializes blank population of individuals * *@paramPopulationsize * The number of individuals in the population*/ PublicPopulation (intpopulationsize) { //Initial Population This. Population =NewIndividual[populationsize]; } /*** Initializes population of individuals * *@paramPopulationsize * The number of individuals in the population *@paramChromosomelength * The size of each individual ' s chromosome*/ PublicPopulation (intPopulationsize,intchromosomelength) { //Initialize The population as an array of individuals This. Population =NewIndividual[populationsize]; //Create Each individual in turn for(intIndividualcount = 0; Individualcount < populationsize; individualcount++) { //Create An individual, initializing it chromosome to the given//lengthIndividual individual =Newindividual (chromosomelength); //Add individual to population This. population[individualcount] =individual; } } /*** Get individuals from the population * *@returnindividuals individuals in population*/ Publicindividual[] Getindividuals () {return This. Population; } /*** Find A individual in the population by it fitness * * This method lets you select a individual in O Rder of its fitness. This * can is used to find the single strongest individual (eg, if you ' re * testing for a solution), but it can al So is used to find weak individuals * (if you ' re looking to cull the population) or some of the strongest * indivi Duals (If you ' re using "elitism"). * * @paramOffset * The offset of the individual want, sorted by fitness. 0 is * the strongest , Population.length-1 is the weakest. * @returnindividual individual at offset*/ PublicIndividual Getfittest (intoffset) { //Order population by fitnessArrays.sort ( This. Population,NewComparator<individual>() {@Override Public intCompare (individual O1, individual O2) {if(O1.getfitness () >o2.getfitness ()) { return-1; } Else if(O1.getfitness () <o2.getfitness ()) { return1; } return0; } }); //Return the fittest individual return This. Population[offset]; } /*** Set population ' s Group Fitness * *@paramFitness * The population ' s total Fitness*/ Public voidSetpopulationfitness (DoubleFitness) { This. populationfitness =Fitness; } /*** Get population ' s Group Fitness * *@returnpopulationfitness The population ' s total Fitness*/ Public Doublegetpopulationfitness () {return This. populationfitness; } /*** Get Population ' s size * *@returnsize The population ' s size*/ Public intsize () {return This. Population.length; } /*** Set Individual at offset * *@paramIndividual *@paramOffset *@returnindividual*/ PublicIndividual setindividual (intoffset, individual individual) { returnPopulation[offset] =individual; } /*** Get Individual at offset * *@paramOffset *@returnindividual*/ PublicIndividual getindividual (intoffset) { returnPopulation[offset]; } /*** Shuffles The population in-place * *@paramvoid *@returnvoid*/ Public voidShuffle () {Random rnd=NewRandom (); for(inti = population.length-1; i > 0; i--) { intindex = rnd.nextint (i + 1); Individual A=Population[index]; Population[index]=Population[i]; Population[i]=A; } }}
Genetic algorithm population