Ejemplo n.º 1
0
        public void WhenBoardWithTwoCellsConnectedThenEmptyBoard()
        {
            var board = new Board(new[] { new Cell(0, 0), new Cell(-1, 0) });
            var generationGenerator = new GenerationGenerator();

            var result = generationGenerator.Generate(board);

            result.Should().BeEquivalentTo(new Board(new Cell[0]));
        }
Ejemplo n.º 2
0
        public void WhenBoardWithThreeCellsInALineConnectedThenBoardWithOne()
        {
            var board = new Board(new[] { new Cell(0, 0), new Cell(-1, 0), new Cell(-2, 0) });
            var generationGenerator = new GenerationGenerator();

            var result = generationGenerator.Generate(board);

            result.Should().BeEquivalentTo(new Board(new[] { new Cell(-1, 0), new Cell(-1, 1), new Cell(-1, -1) }));
        }
Ejemplo n.º 3
0
        public void WhenEmptyBoardThenNextGenerationIsEmptyNewBoard()
        {
            var board = new Board(new List <Cell>());
            var generationGenerator = new GenerationGenerator();

            var result = generationGenerator.Generate(board);

            using (new AssertionScope())
            {
                result.Should().NotBe(board, "not same board");
                result.Should().BeEquivalentTo(new Board(new List <Cell>()), "be empty board");
            }
        }
Ejemplo n.º 4
0
        public void WhenBoardWithFourCellsInALineConnectedThenBoardWithTwo()
        {
            var expectation = new Board(new[]
            {
                new Cell(-2, 0), new Cell(-1, 0), new Cell(-2, 1), new Cell(-1, 1), new Cell(-2, -1), new Cell(-1, -1)
            });
            var board = new Board(new[]
            {
                new Cell(0, 0), new Cell(-1, 0), new Cell(-2, 0), new Cell(-3, 0)
            });
            var generationGenerator = new GenerationGenerator();

            var result = generationGenerator.Generate(board);

            result.Should().BeEquivalentTo(expectation);
        }
Ejemplo n.º 5
0
        public void WhenBoardWithBlockOf9CellsConnectedThenBoardWithFour()
        {
            var expectation = new Board(new[]
            {
                new Cell(-1, 1),
                new Cell(0, 0), new Cell(0, 2),
                new Cell(1, -1), new Cell(1, 3),
                new Cell(2, 0), new Cell(2, 2),
                new Cell(3, 1),
            });
            var board = new Board(new[]
            {
                new Cell(0, 0), new Cell(0, 1), new Cell(0, 2),
                new Cell(1, 0), new Cell(1, 1), new Cell(1, 2),
                new Cell(2, 0), new Cell(2, 1), new Cell(2, 2),
                new Cell(12, 34),
            });
            var generationGenerator = new GenerationGenerator();

            var result = generationGenerator.Generate(board);

            result.Should().BeEquivalentTo(expectation);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }