private void SkrgicSelection(int size) { // new chromosomes, initially empty List <GPChromosome> newPopulation = new List <GPChromosome>(); // double k = 0.2; //additionalParameter; double fitnessMax = chromosomes.Max(x => x.fitness) * (1.0 + gpParameters.SelParam1); for (int i = 0; i < size; i++) { //Slucajni index iz populacije int randomIndex = Globals.radn.Next(0, chromosomes.Count); //Slucajni broj izmedju 0-maxFitnes ukljucujuci i maxFitness koje je prethodno vec izracunat kod evaluacije hromosoma double randomFitness = Globals.radn.NextDouble(0, fitnessMax /*, true include MaxValue*/); while (true) { //Akoje slucajno generirani broj manji ili jednak fitnesu slucajnog hromosoma selektuj hromosom if (randomFitness <= chromosomes[randomIndex].fitness * (1.0 + gpParameters.SelParam1 / fitnessMax)) { newPopulation.Add(chromosomes[randomIndex].Clone()); break; } randomIndex = Globals.radn.Next(0, chromosomes.Count); randomFitness = Globals.radn.NextDouble(0, fitnessMax /*, true include MaxValue*/); } } // survived chromosomes chromosomes.AddRange(newPopulation); // old population is goint to die GPChromosome.DestroyChromosomes(ref chromosomes); }
private void FUSSSelection(int size) { // new chromosomes, initially empty List <GPChromosome> newPopulation = new List <GPChromosome>(); //Maximum fitness double fitnessMax = chromosomes.Max(x => x.fitness); double rnd; double dif; int selIndex = 0; for (int j = 0; j < size; j++) { rnd = Globals.radn.NextDouble(0, fitnessMax); dif = Math.Abs(chromosomes[0].fitness - rnd); selIndex = 0; for (int i = 1; i < chromosomes.Count; i++) { double curDif = Math.Abs(chromosomes[i].fitness - rnd); if (dif > curDif) { dif = curDif; selIndex = i; } } newPopulation.Add(chromosomes[selIndex].Clone()); } // survived chromosomes chromosomes.AddRange(newPopulation); // old population is goint to die GPChromosome.DestroyChromosomes(ref chromosomes); }
private void UniversalStohasticSelection(int size) { // new chromosomes, initially empty List <GPChromosome> newPopulation = new List <GPChromosome>(); //chromosomes.Sort(); int currentSize = chromosomes.Count(); float fitnessSum = chromosomes.Sum(c => c.fitness); // get random distance value float randDist = (float)Globals.radn.NextDouble(0, 1.0 / (double)size); float partFitnes = 0; for (int j = 0; j < size; j++) { partFitnes = 0; for (int i = 0; i < chromosomes.Count; i++) { partFitnes += chromosomes[i].fitness / fitnessSum; if (randDist <= partFitnes) { newPopulation.Add(chromosomes[i].Clone()); break; } } randDist += 1.0F / (float)size; } // survived chromosomes chromosomes.AddRange(newPopulation); // old population is goint to die GPChromosome.DestroyChromosomes(ref chromosomes); }
private void TournamentSelection(int size) { // velicinaPopulacije of current chromosomes int currentSize = chromosomes.Count; List <GPChromosome> tourn = new List <GPChromosome>((int)gpParameters.SelParam1); // new chromosomes, initially empty List <GPChromosome> newPopulation = new List <GPChromosome>(); for (int j = 0; j < size; j++) { currentSize = chromosomes.Count; for (int i = 0; i < gpParameters.SelParam1 && i < currentSize; i++) { int ind = Globals.radn.Next(currentSize); tourn.Add(chromosomes[ind]); } tourn.Sort(); newPopulation.Add(tourn[0].Clone()); chromosomes.Remove(tourn[0]); tourn.Clear(); } // survived chromosomes chromosomes.AddRange(newPopulation); // old population is goint to die GPChromosome.DestroyChromosomes(ref chromosomes); }
/// <summary> /// Fitness šproportionate selection. /// </summary> /// <param name="size"></param> private void FitnessProportionateSelection(int size) { // new chromosomes, initially empty List <GPChromosome> newPopulation = new List <GPChromosome>(); int currentSize = chromosomes.Count; double sumOfFitness = 0; //calculate sum of fitness for (int i = 0; i < currentSize; i++) { sumOfFitness += chromosomes[i].fitness; } // create wheel ranges double[] rangeMax = new double[currentSize]; double s = 0; for (int i = 0; i < currentSize; i++) { // cumulative normalized fitness s += (chromosomes[i].fitness / sumOfFitness); rangeMax[i] = s; } // select chromosomes from old chromosomes to the new chromosomes for (int j = 0; j < size; j++) { // get wheel value double wheelValue = Globals.radn.NextDouble(); // find the chromosome for the wheel value for (int i = 0; i < currentSize; i++) { //double wheelValue = rand.NextDouble(); if (wheelValue <= rangeMax[i] && chromosomes[i].fitness != -1) { // add the chromosome to the new population var ch = chromosomes[i].Clone(); newPopulation.Add(ch); chromosomes[i].fitness = -1; break; } } } // survived chromosomes chromosomes.AddRange(newPopulation); // old population is goint to die GPChromosome.DestroyChromosomes(ref chromosomes); }
private void RankSelection(int size) { // new chromosomes, initially empty List <GPChromosome> newPopulation = new List <GPChromosome>(); // velicinaPopulacije of current chromosomes int currentSize = chromosomes.Count; // sort current chromosomes //chromosomes.Sort(); // calculate amount of ranges in the wheel double ranges = currentSize * (currentSize + 1) / 2; // create wheel ranges double[] rangeMax = new double[currentSize]; double s = 0; for (int i = 0, n = currentSize; i < currentSize; i++, n--) { s += ((double)n / ranges); rangeMax[i] = s; } // select chromosomes from old chromosomes to the new chromosomes for (int j = 0; j < size; j++) { // get wheel value double wheelValue = Globals.radn.NextDouble(); // find the chromosome for the wheel value for (int i = 0; i < currentSize; i++) { // get wheel value if (wheelValue <= rangeMax[i]) { // add the chromosome to the new chromosomes newPopulation.Add(chromosomes[i].Clone()); break; } //Debug.Assert(false); } } // survived chromosomes chromosomes.AddRange(newPopulation); // old population is goint to die GPChromosome.DestroyChromosomes(ref chromosomes); }