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); }
private void initializeNetworks() { //UpdateMessage("Loading neural networks."); // initialize modules const string CHAMPION_FILE = @"host_parasite_champion_to_load.xml"; List <NeatGenome> anns; XmlConfigurator.Configure(new System.IO.FileInfo("log4net.properties")); // Load config XML. XmlDocument xmlConfig = new XmlDocument(); //load genomes // Save the best genome to file XmlReaderSettings xwSettings = new XmlReaderSettings(); using (XmlReader xw = XmlReader.Create(CHAMPION_FILE, xwSettings)) { anns = NeatGenomeXmlIO.ReadCompleteGenomeList(xw, false); } IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = new NeatGenomeDecoder(new SharpNeat.Decoders.NetworkActivationScheme(2)); rhythm1 = new NeatPlayer(genomeDecoder.Decode(anns[0])); rhythm2 = new NeatPlayer(genomeDecoder.Decode(anns[1])); pitch1 = new NeatPlayer(genomeDecoder.Decode(anns[2])); pitch2 = new NeatPlayer(genomeDecoder.Decode(anns[3])); }
/// <summary> /// Evaluate the two black boxes by playing them against each other in a /// game of Tic-Tac-Toe. All permutations of size 2 are going to be /// evaluated, so we only play one game: box1 is X, box2 is O. /// </summary> public void Evaluate(IBlackBox box1, IBlackBox box2, out FitnessInfo fitness1, out FitnessInfo fitness2) { // box1 plays as X. NeatPlayer player1 = new NeatPlayer(box1, SquareTypes.X); // box2 plays as O. NeatPlayer player2 = new NeatPlayer(box2, SquareTypes.O); // Play the two boxes against each other. var winner = TicTacToeGame.PlayGameToEnd(player1, player2); // Score box1 double score1 = getScore(winner, SquareTypes.X); fitness1 = new FitnessInfo(score1, score1); // Score box2 double score2 = getScore(winner, SquareTypes.O); fitness2 = new FitnessInfo(score2, score2); // Update the evaluation counter. _evalCount++; }
private void evaluate(IBlackBox box1, IBlackBox box2, out double score1, out double score2) { // box1 plays as X. NeatPlayer player1 = new NeatPlayer(box1, SquareTypes.X); // box2 plays as O. NeatPlayer player2 = new NeatPlayer(box2, SquareTypes.O); // Play the two boxes against each other. var winner = TicTacToeGame.PlayGameToEnd(player1, player2); // Score box1 score1 = getScore(winner, SquareTypes.X); // Score box2 score2 = getScore(winner, SquareTypes.O); }
/// <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)); }