private static Gene findBestGeneForTowerPattern( int[] cannons, int[] archers, int length, int preferredMoneyToSpend, int generationPopulation, int maximumCountOfGenerations, int geneToTroopMean, int lengthOfGene, IScoringPolicy scoringPolicy, bool printEvaluationLog) { Gene bestGene = null; var simulator = new Simulator(length, turns, cannons, archers); var gg = new GenerationGenerator(generationPopulation, lengthOfGene); var generation = gg.RandomGeneration(); var archerString = towerLocationsToString(archers, length, "a"); var cannonString = towerLocationsToString(cannons, length, "c"); double lastBestScore = default; var bestScoreCount = 0; var bestScoreCountLimit = 30; var st = new Stopwatch(); st.Start(); for (var i = 0; i < maximumCountOfGenerations; i++) { foreach (var gene in generation) { var result = simulator.Simulate(new MyGeneParser(gene)); gene.Score = scoringPolicy.CalculateTotalScore(result); } bestGene = Program.bestGene(generation); if (printEvaluationLog) { var sortedGeneration = generation.OrderBy(x => x.Score).ToList(); logGenerationStatistics(length, generation, i, sortedGeneration, bestGene, archerString, cannonString, geneToTroopMean); if (Console.KeyAvailable) { Console.ReadKey(); Console.ReadKey(); logGeneSimulationResult(simulator, bestGene, scoringPolicy, preferredMoneyToSpend, archerString, cannonString, geneToTroopMean, length); Console.ReadKey(); } } if (bestGene.Score != null) { if (i == 0) { lastBestScore = bestGene.Score.Value; bestScoreCount++; } else { if (bestGene.Score.Value.Equals(lastBestScore) && !bestGene.Score.Value.Equals(0)) { bestScoreCount++; } else { lastBestScore = bestGene.Score.Value; bestScoreCount = 1; } } } if (bestScoreCount >= bestScoreCountLimit) { if (printEvaluationLog) { logGeneSimulationResult(simulator, bestGene, scoringPolicy, preferredMoneyToSpend, archerString, cannonString, geneToTroopMean, length); } break; } if (i != maximumCountOfGenerations - 1) { generation = gg.Genetic(generation); } else { if (printEvaluationLog) { logGeneSimulationResult(simulator, bestGene, scoringPolicy, preferredMoneyToSpend, archerString, cannonString, geneToTroopMean, length); } } } st.Stop(); if (printEvaluationLog) { Console.WriteLine(st.Elapsed); } return(bestGene); }
private static void logGeneSimulationResult(Simulator simulator, Gene bestGene, IScoringPolicy scoringPolicy, int preferredMoneyToSpend, string archersString, string cannonsString, double geneToTroopMean, int length) { var result = simulator.Simulate(new MyGeneParser(bestGene), true, archersString, cannonsString); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Results: "); Console.WriteLine($"Damages to base: {result.DamagesToEnemyBase}"); Console.WriteLine($"Total money spent: {result.TotalPrice} with {preferredMoneyToSpend} prefered"); Console.WriteLine($"Score: {scoringPolicy.CalculateTotalScore(result)}"); Console.WriteLine($"Turns: {result.Turns}"); Console.WriteLine(); }
public SolutionMaker(ITowerStateMaker towerStateMaker, IScoringPolicy scoringPolicy) { this.towerStateMaker = towerStateMaker; this.scoringPolicy = scoringPolicy; }