private static void gasolver_GenerationCreated(Flowerz.Solver.GA.GenerationCreatedEventArgs args) { if (args.GenerationNumber % 10 == 0) { Console.WriteLine("".PadLeft(_nestLevel * 2, ' ') + "Current generation = " + args.GenerationNumber + ". Score = " + args.HighestPreviousScore + " out of " + args.MaximumNumberOfFreeSpaces); } }
void childSolver_GenerationCreated(GenerationCreatedEventArgs args) { if (GenerationCreated != null) { GenerationCreated(args); } }
private MoveList Solve() { var randomizer = RandomizerFactory.CreateRandomizer(); CreateInitialPopulation(randomizer); var numGenerationsCreated = 0; foreach (var t in _currentGeneration.Where(t => t.Score == Board.MaximumNumberOfFreeSpaces)) { //No need to loop! return(t); } while (numGenerationsCreated <= GeneticAlgorithmOptions.NumberOfGenerationsToCreate) { while (_nextGeneration.Count < GeneticAlgorithmOptions.PopulationSize) { var operationToPerform = GeneticOperatorSelector.DetermineGeneticOperation(GeneticAlgorithmOptions.CrossOverChance, GeneticAlgorithmOptions.MutationChance, randomizer); if (operationToPerform == GeneticOperation.Crossover) { PerformCrossover(randomizer); } if (operationToPerform == GeneticOperation.Mutation) { PerformMutation(randomizer); } if (operationToPerform == GeneticOperation.Reproduction) { PerformReproduction(randomizer); } } //Check if child has maximum score var childIndexWithMaximumScore = new int?(); for (var i = 0; i < _nextGeneration.Count; i++) { if (_nextGeneration[i].Score == Board.MaximumNumberOfFreeSpaces) { childIndexWithMaximumScore = i; } } if (childIndexWithMaximumScore.HasValue) { _currentGeneration.Clear(); foreach (var child in _nextGeneration) { _currentGeneration.Add(child); } break; } //Highest score parent generation var highestScorePreviousGeneration = _currentGeneration.Select(parent => parent.Score).Concat(new[] { Int32.MinValue }).Max(); //Highest score next genetation var highestScoreNextGeneration = Int32.MinValue; var indexHighestScoreNextGen = -1; for (var i = 0; i < _nextGeneration.Count; i++) { var child = _nextGeneration[i]; if (child.Score > highestScoreNextGeneration) { highestScoreNextGeneration = child.Score; indexHighestScoreNextGen = i; } } var moves = _nextGeneration[indexHighestScoreNextGen].ToString(); if (highestScoreNextGeneration < highestScorePreviousGeneration) { //Preserving parents that are better than their children foreach (var parent in _currentGeneration) { if (parent.Score == highestScorePreviousGeneration) { _nextGeneration.Add(parent); } } } _currentGeneration.Clear(); foreach (var child in _nextGeneration) { _currentGeneration.Add(child); } _nextGeneration.Clear(); numGenerationsCreated++; if (GenerationCreated != null) { var generationArgs = new GenerationCreatedEventArgs { GenerationNumber = numGenerationsCreated, HighestScoreThisGeneration = highestScoreNextGeneration, HighestPreviousScore = highestScorePreviousGeneration, HighestScoringMoves = moves, MaximumNumberOfFreeSpaces = Board.MaximumNumberOfFreeSpaces }; GenerationCreated(generationArgs); } } var indexWithHighestScore = -1; var highestScore = Int32.MinValue; for (var i = 0; i < _currentGeneration.Count; i++) { var scoreCurrentMovelist = _currentGeneration[i].Score; if (scoreCurrentMovelist > highestScore) { highestScore = scoreCurrentMovelist; indexWithHighestScore = i; } } return(_currentGeneration[indexWithHighestScore]); }