public void initPool() { int genomeI; for( genomeI = 0; genomeI < 10; genomeI++ ) { Genome genomeWithRating; int bitI; genomeWithRating = new Genome(genomeBits); for( bitI = 0; bitI < genomeBits; bitI++ ) { genomeWithRating.genome[bitI] = random.Next(1) == 0; } genomesWithRating.Add(genomeWithRating); } }
public void run() { List <Genome> newRound, crossOverGenoms; float fitnessSum; float currentThreshold; int eliteI; int genomeI; newRound = new List <Genome>(); crossOverGenoms = new List <Genome>(); rating(); // elitism selection algorithm // we choose the top 3 genoms and transfer them into the winner currentThreshold = float.PositiveInfinity; for (eliteI = 0; eliteI < eliteCount; eliteI++) { int highestIndex; bool resultValid; highestIndex = getHighestGenomWithRatingBelow(out resultValid, currentThreshold); newRound.Add(genomesWithRating[highestIndex]); currentThreshold = genomesWithRating[highestIndex].rating; // update the highest rating if (eliteI == 0) { highestRating = currentThreshold; } } newRound.AddRange(getAllGenomsWithRatingBelow(currentThreshold)); // selection // we use the Roulette Wheel Selection algorithm fitnessSum = 0.0f; foreach (Genome iterationGenome in newRound) { fitnessSum += iterationGenome.rating; } for (;;) { float currentSum; int mateI; int[] mateIndices; // indices of genoms which need to be combined float[] roulettValues; bool[] crossoverResult; mateIndices = new int[2]; roulettValues = new float[2]; roulettValues = GeneticAlgorithm.getRoulettPair(random, fitnessSum, newRound.Count); currentSum = 0.0f; mateI = 0; for (genomeI = 0; genomeI < newRound.Count; genomeI++) { currentSum += newRound[genomeI].rating; if (currentSum > roulettValues[mateI]) { mateIndices[mateI] = genomeI; mateI++; if (mateI == 2) { break; } } } // sanity check if (mateI == 0) { System.Diagnostics.Debug.Assert(false, "Should not happen!"); } else if (mateI == 1) { // if not all mates have been chosen System.Diagnostics.Debug.Assert(false, "TODO???"); } crossoverResult = crossover(newRound[mateIndices[0]].genome, newRound[mateIndices[1]].genome); // create the new crossover genom Genome crossOverGenom; crossOverGenom = new Genome(genomeBits); crossOverGenom.genome = crossoverResult; crossOverGenom.rating = float.PositiveInfinity; crossOverGenoms.Add(crossOverGenom); break; } System.Diagnostics.Debug.Assert(crossOverGenoms.Count == 1); newRound.AddRange(crossOverGenoms); genomesWithRating = newRound; while (genomesWithRating.Count > 10) { // remove the genom of the lowest rating int lowestFittnessI; lowestFittnessI = GeneticAlgorithm.getIndexOfLowestFittness(genomesWithRating); genomesWithRating.RemoveAt(lowestFittnessI); } // mutate // we mutate all but the elites not for (genomeI = 2; genomeI < genomesWithRating.Count; genomeI++) { Genome iterationGenome; iterationGenome = genomesWithRating[genomeI]; mutate(ref iterationGenome.genome); } }
public void run() { List<Genome> newRound, crossOverGenoms; float fitnessSum; float currentThreshold; int eliteI; int genomeI; newRound = new List<Genome>(); crossOverGenoms = new List<Genome>(); rating(); // elitism selection algorithm // we choose the top 3 genoms and transfer them into the winner currentThreshold = float.PositiveInfinity; for( eliteI = 0; eliteI < eliteCount; eliteI++ ) { int highestIndex; bool resultValid; highestIndex = getHighestGenomWithRatingBelow(out resultValid, currentThreshold); newRound.Add(genomesWithRating[highestIndex]); currentThreshold = genomesWithRating[highestIndex].rating; // update the highest rating if( eliteI == 0 ) { highestRating = currentThreshold; } } newRound.AddRange(getAllGenomsWithRatingBelow(currentThreshold)); // selection // we use the Roulette Wheel Selection algorithm fitnessSum = 0.0f; foreach( Genome iterationGenome in newRound ) { fitnessSum += iterationGenome.rating; } for(;;) { float currentSum; int mateI; int[] mateIndices; // indices of genoms which need to be combined float[] roulettValues; bool[] crossoverResult; mateIndices = new int[2]; roulettValues = new float[2]; roulettValues = GeneticAlgorithm.getRoulettPair(random, fitnessSum, newRound.Count); currentSum = 0.0f; mateI = 0; for( genomeI = 0; genomeI < newRound.Count; genomeI++ ) { currentSum += newRound[genomeI].rating; if( currentSum > roulettValues[mateI] ) { mateIndices[mateI] = genomeI; mateI++; if( mateI == 2 ) { break; } } } // sanity check if( mateI == 0 ) { System.Diagnostics.Debug.Assert(false, "Should not happen!"); } else if( mateI == 1 ) { // if not all mates have been chosen System.Diagnostics.Debug.Assert(false, "TODO???"); } crossoverResult = crossover(newRound[mateIndices[0]].genome, newRound[mateIndices[1]].genome); // create the new crossover genom Genome crossOverGenom; crossOverGenom = new Genome(genomeBits); crossOverGenom.genome = crossoverResult; crossOverGenom.rating = float.PositiveInfinity; crossOverGenoms.Add(crossOverGenom); break; } System.Diagnostics.Debug.Assert(crossOverGenoms.Count == 1); newRound.AddRange(crossOverGenoms); genomesWithRating = newRound; while( genomesWithRating.Count > 10 ) { // remove the genom of the lowest rating int lowestFittnessI; lowestFittnessI = GeneticAlgorithm.getIndexOfLowestFittness(genomesWithRating); genomesWithRating.RemoveAt(lowestFittnessI); } // mutate // we mutate all but the elites not for( genomeI = 2; genomeI < genomesWithRating.Count; genomeI++ ) { Genome iterationGenome; iterationGenome = genomesWithRating[genomeI]; mutate(ref iterationGenome.genome); } }