Beispiel #1
0
        private static NeuralPlayer MakeNeuralPlayer(int inputCount, int outputCount, int depth, int hiddenNeuronsPerLayer, DNA individual)
        {
            NeuralNetwork.NeuralNetwork network = new NeuralNetwork.NeuralNetwork();

            network.BuildFromDNA($"{inputCount}||{outputCount}||{depth}||{hiddenNeuronsPerLayer}||{individual.ToString()}");

            NeuralPlayer p2 = new NeuralPlayer(network, SquareTypes.O);

            return(p2);
        }
Beispiel #2
0
        private static void MatchReplay()
        {
            int inputCount            = 9;
            int outputCount           = 9;
            int depth                 = 3;
            int hiddenNeuronsPerLayer = 5;

            NeuralPlayer  champion = MakeNeuralPlayer(inputCount, outputCount, depth, hiddenNeuronsPerLayer, new DNA(bestDNA));
            OptimalPlayer p2       = new OptimalPlayer(SquareTypes.O);

            champion.SquareType = SquareTypes.X;
            TicTacToeGame.PlayGameToEnd(champion, p2);
        }
Beispiel #3
0
        private static void PlayVSHuman()
        {
            int           inputCount            = 9;
            int           outputCount           = 9;
            int           depth                 = 3;
            int           hiddenNeuronsPerLayer = 5;
            TicTacToeGame game = new TicTacToeGame();

            NeuralPlayer champion = MakeNeuralPlayer(inputCount, outputCount, depth, hiddenNeuronsPerLayer, new DNA(bestDNA));

            champion.SquareType = SquareTypes.X;
            SquareTypes winner = SquareTypes.N;

            for (int moveNum = 0; moveNum < 9 && winner == SquareTypes.N; moveNum++)
            {
                Console.WriteLine($"+- Turn {moveNum + 1} -+");
                TicTacToeGame.DisplayBoard(game.Board);

                SquareTypes curSquareType;
                Move        move;

                if (moveNum % 2 == 1)
                {
                    curSquareType = SquareTypes.O;
                    move          = AskHumanForMove();
                }
                else
                {
                    curSquareType = SquareTypes.X;
                    move          = champion.GetMove(game.Board);
                }

                if (game.IsEmpty(move.X, move.Y))
                {
                    game.Board[move.X, move.Y] = curSquareType;
                }
                else
                {
                    Console.WriteLine($"Player {curSquareType.ToString()} tried to use non-empty space [{move.X}, {move.Y}]");
                }

                if (moveNum > 3)
                {
                    winner = game.GetWinner();
                }
            }

            Console.WriteLine($"The winner is {winner.ToString()}");
        }
Beispiel #4
0
        private static void RunExperiment(StreamWriter saveFileWriter)
        {
            int inputCount            = 9;
            int outputCount           = 9;
            int depth                 = 3;
            int hiddenNeuronsPerLayer = 5;
            PopulationManager pop     = GeneratePopulation(inputCount, outputCount, depth, hiddenNeuronsPerLayer);
            Dictionary <int, List <double> > fitnesses = new Dictionary <int, List <double> >();
            DNA bestIndividual = new DNA(bestDNA);

            const int genCount = 2000;

            using (ProgressBar progress = new ProgressBar())
            {
                Console.WriteLine("Running genetics...");
                for (int gen = 0; gen < genCount; gen++)
                {
                    progress.SetLabel($"gen {gen+GenToLoad}/{genCount+GenToLoad}");
                    progress.Report((double)gen / genCount);
                    foreach (DNA individual in pop.Population)
                    {
                        NeuralPlayer p1 = MakeNeuralPlayer(inputCount, outputCount, depth, hiddenNeuronsPerLayer, individual);
                        NeuralPlayer p2 = MakeNeuralPlayer(inputCount, outputCount, depth, hiddenNeuronsPerLayer, bestIndividual);

                        p1.SquareType = SquareTypes.X;
                        p2.SquareType = SquareTypes.O;
                        for (int g = 0; g < 50; g++)
                        {
                            (SquareTypes winner, int moveNum) = TicTacToeGame.PlayGameToEnd(p1, p2);
                            if (winner == SquareTypes.X)
                            {
                                individual.Fitness += 50f / moveNum;
                            }
                            else if (winner == SquareTypes.N)
                            {
                                individual.Fitness += 10f / moveNum;
                            }
                        }
                        p2.SquareType = SquareTypes.X;
                        p1.SquareType = SquareTypes.O;
                        for (int g = 0; g < 50; g++)
                        {
                            (SquareTypes winner, int moveNum) = TicTacToeGame.PlayGameToEnd(p2, p1);
                            if (winner == SquareTypes.O)
                            {
                                individual.Fitness += 50f / moveNum;
                            }
                            else if (winner == SquareTypes.N)
                            {
                                individual.Fitness += 10f / moveNum;
                            }
                        }
                    }
                    if (gen % 50 == 0)
                    {
                        SaveGeneration(gen + 1 + GenToLoad, pop);
                    }
                    int dnaCounter = 0;
                    pop.Population.OrderByDescending(i => i.Fitness).ToList().ForEach(dna =>
                    {
                        if (!fitnesses.ContainsKey(dnaCounter))
                        {
                            fitnesses[dnaCounter] = new List <double>();
                        }
                        fitnesses[dnaCounter].Add(dna.Fitness);
                        dnaCounter++;
                    });
                    var thisBest = pop.Population.OrderByDescending(i => i.Fitness).First();
                    if (thisBest.Fitness > bestIndividual.Fitness)
                    {
                        bestIndividual = thisBest;
                    }
                    pop.NextGeneration();
                }
            }
            Console.WriteLine(" Done.");

            SaveGeneration(genCount + GenToLoad, pop);

            foreach (KeyValuePair <int, List <double> > fitnessList in fitnesses)
            {
                saveFileWriter.WriteLine($"{fitnessList.Key},{String.Join(',', fitnessList.Value)}");
            }

            saveFileWriter.WriteLine("");
            saveFileWriter.WriteLine("Best final dna:");
            saveFileWriter.WriteLine(String.Join("||", pop.Population.OrderByDescending(i => i.Fitness).First().Genes));
        }