예제 #1
0
        public GameForm()
        {
            InitializeComponent();
            _randomPlayer = new RandomPlayer();
            _optimalPlayer = new OptimalPlayer(SquareTypes.O);
            _neatPlayer = new NeatPlayer(null, SquareTypes.O);
            _aiSquareType = SquareTypes.O;
            _humanSquareType = SquareTypes.X;
            _game = new TicTacToeGame();

            // Set the AI to the random player by default.
            _ai = _randomPlayer;

            // Experiment classes encapsulate much of the nuts and bolts of setting up a NEAT search.
            _experiment = new TicTacToeExperiment();
            _hyperNeatExperiment = new TicTacToeHyperNeatExperiment();

            // Load config XML for the NEAT experiment.
            XmlDocument xmlConfig = new XmlDocument();
            xmlConfig.Load("tictactoe.config.xml");
            _experiment.Initialize("TicTacToe", xmlConfig.DocumentElement);

            // Load config XML for the HyperNEAT experiment.
            xmlConfig = new XmlDocument();
            xmlConfig.Load("hyperneat.config.xml");
            _hyperNeatExperiment.Initialize("TicTacToe", xmlConfig.DocumentElement);
        }
        /// <summary>
        /// Evaluate the provided IBlackBox against the random tic-tac-toe player and return its fitness score.
        /// Each network plays 10 games against the random player and two games against the expert player.
        /// Half of the games are played as circle and half are played as x.
        ///
        /// A win is worth 10 points, a draw is worth 1 point, and a loss is worth 0 points.
        /// </summary>
        public FitnessInfo Evaluate(IBlackBox box)
        {
            double        fitness = 0;
            SquareTypes   winner;
            OptimalPlayer optimalPlayer = new OptimalPlayer(SquareTypes.O);
            RandomPlayer  randomPlayer  = new RandomPlayer();
            NeatPlayer    neatPlayer    = new NeatPlayer(box, SquareTypes.X);


            // Play 50 games as X against a random player
            for (int i = 0; i < 50; i++)
            {
                // Compete the two players against each other.
                winner = TicTacToeGame.PlayGameToEnd(neatPlayer, randomPlayer);

                // Update the fitness score of the network
                fitness += getScore(winner, neatPlayer.SquareType);
            }

            // Play 50 games as O against a random player
            neatPlayer.SquareType = SquareTypes.O;
            for (int i = 0; i < 50; i++)
            {
                // Compete the two players against each other.
                winner = TicTacToeGame.PlayGameToEnd(randomPlayer, neatPlayer);

                // Update the fitness score of the network
                fitness += getScore(winner, neatPlayer.SquareType);
            }

            // Play 1 game as X against an optimal player
            neatPlayer.SquareType    = SquareTypes.X;
            optimalPlayer.SquareType = SquareTypes.O;

            // Compete the two players against each other.
            winner = TicTacToeGame.PlayGameToEnd(neatPlayer, optimalPlayer);

            // Update the fitness score of the network
            fitness += getScore(winner, neatPlayer.SquareType);


            // Play 1 game as O against an optimal player
            neatPlayer.SquareType    = SquareTypes.O;
            optimalPlayer.SquareType = SquareTypes.X;

            // Compete the two players against each other.
            winner = TicTacToeGame.PlayGameToEnd(optimalPlayer, neatPlayer);

            // Update the fitness score of the network
            fitness += getScore(winner, neatPlayer.SquareType);

            // Update the evaluation counter.
            _evalCount++;

            // If the network plays perfectly, it will beat the random player
            // and draw the optimal player.
            if (fitness >= 1002)
            {
                _stopConditionSatisfied = true;
            }

            // Return the fitness score
            return(new FitnessInfo(fitness, fitness));
        }