Example #1
0
        private List <Gene> FindBestGenes(TowerState state, int pathLength, int geneLength, int maximumTurns)
        {
            var gg         = new GenerationGenerator(generationCount, geneLength);
            var generation = gg.RandomGeneration();

            var simulator = new Simulator(pathLength, maximumTurns, state.Cannons, state.Archers);

            var bestScores = new List <double>();

            while (!EnoughGenerations(bestScores, state))
            {
                Parallel.ForEach(generation, (gene) =>
                {
                    var result = simulator.Simulate(new MyGeneParser(gene));
                    gene.Score = scoringPolicy.CalculateTotalScore(result);
                });

                bestScores.Add((double)generation.Select(x => x.Score).Max());

                generation = gg.Genetic(generation);
            }

            return(generation);
        }
Example #2
0
        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);
        }