示例#1
0
        public Pig.Player GetRandomMember(double totalSqrtFitness, Pig.Player exclusion = null)
        {
            if (exclusion != null)
            {
                totalSqrtFitness -= Math.Pow(exclusion.averageFitness, genPlayerFitScale);
            }

            Random memberGen = new Random(Guid.NewGuid().GetHashCode());
            //Random memberGen = new Random(1);
            double randomVal = memberGen.NextDouble() * totalSqrtFitness;

            foreach (Pig.Player player in Players)
            {
                double currentFit = Math.Pow(player.averageFitness, genPlayerFitScale);

                if (player == exclusion)
                {
                    continue;
                }

                if (randomVal < currentFit)
                {
                    return(player);
                }

                randomVal -= currentFit;
            }

            return(Players[-1]); //this will crash and mean that something went wrong- however it should never in theory
        }
        private void injectPlayer(string file, NN.Generation injectGen)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(Pig.Player));

            Pig.Player aiPlayer = new Pig.Player();


            FileStream openFile = new FileStream("basePlayer.dat", FileMode.Open);

            Pig.Player tempPlayer;
            try
            {
                tempPlayer = (Pig.Player)deserializer.Deserialize(openFile);
            }
            catch (Exception err)
            {
                System.Windows.Forms.MessageBox.Show(err.Message);
                throw;
            }

            openFile.Close();

            aiPlayer             = new Pig.Player(tempPlayer);
            injectGen.Players[0] = aiPlayer;
        }
示例#3
0
        public void CreateGen()
        {
            List <Pig.Player> newPlayers = new List <Pig.Player>();

            foreach (Pig.Player player in Players)
            {
                player.averageFitness = player.totalFitness / player.gameCount;
            }
            Players.Sort((a, b) => b.averageFitness.CompareTo(a.averageFitness));

            double totalSqrtFitness = 0;

            totalSqrtFitness = Players.Sum(x => Math.Pow(x.averageFitness, genPlayerFitScale));

            maxFitness = Players[0].averageFitness;

            newPlayers.Add(new Pig.Player(Players[0]));
            newPlayers.Add(new Pig.Player(Players[1]));
            newPlayers.Add(new Pig.Player(Players[2]));

            for (int i = 3; i < Players.Count; i++)
            {
                Pig.Player cross1 = GetRandomMember(totalSqrtFitness);
                //Pig.Player cross2 = GetRandomMember(totalSqrtFitness - Math.Pow(cross1.averageFitness, genPlayerFitScale), cross1);
                Pig.Player cross2 = GetRandomMember(totalSqrtFitness);

                Pig.Player newPlayer = new Pig.Player(cross1, cross2);
                newPlayer.Mutate();
                newPlayers.Add(newPlayer);
            }

            Players = newPlayers; //copy over new players
        }
示例#4
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            FileStream openFile = new FileStream("bestPlayer.dat", FileMode.Open);

            Pig.Player tempPlayer = (Pig.Player)deserializer.Deserialize(openFile);
            aiPlayer = new Pig.Player(tempPlayer.net);

            aiTimer.Tick    += aiTimer_Tick;
            aiTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
        }
示例#5
0
        private void BaselineBtn_Click(object sender, RoutedEventArgs e)
        {
            Pig.Player    humanPlayer  = new Pig.Player();
            XmlSerializer deserializer = new XmlSerializer(typeof(Pig.Player));

            Pig.Player aiPlayer = new Pig.Player();


            FileStream openFile = new FileStream("basePlayer.dat", FileMode.Open);

            Pig.Player tempPlayer;
            try
            {
                tempPlayer = (Pig.Player)deserializer.Deserialize(openFile);
            }
            catch (Exception err)
            {
                System.Windows.Forms.MessageBox.Show(err.Message);
                throw;
            }
            //creation of two players

            openFile.Close();

            aiPlayer = new Pig.Player(tempPlayer);
            Pig.Player bestCurrentPlayer = new Pig.Player(aiGeneration.Players[0]);

            for (int i = 0; i < 10000; i++)
            {
                //get two players
                Pig.Pig game;
                if (rndGen.NextDouble() > 0.5) // to change who is starting player
                {
                    game = new Pig.Pig(aiPlayer, bestCurrentPlayer);
                }
                else
                {
                    game = new Pig.Pig(bestCurrentPlayer, aiPlayer);
                }

                while (!game.hasEnded && game.turnCount < game.maxTurns)
                {
                    //breaks here
                    game.PlayRound();
                }

                aiPlayer.gameCount++;
                bestCurrentPlayer.gameCount++;
                game.CalculateFitness();
            }

            //ratio of fitness of bestplayer to otherplayer
            baseLbl.Content = bestCurrentPlayer.totalFitness / aiPlayer.totalFitness;
        }
示例#6
0
        public void CreateGen()
        {
            List <Pig.Player> newPlayers = new List <Pig.Player>();

            foreach (Pig.Player player in Players)
            {
                player.averageFitness = player.totalFitness / player.gameCount;
            }
            Players.Sort((a, b) => b.averageFitness.CompareTo(a.averageFitness));

            double totalSqrtFitness = 0;

            totalSqrtFitness = Players.Sum(x => Math.Pow(x.averageFitness, genPlayerFitScale));

            maxFitness = Players[0].averageFitness;

            for (int i = 0; i < 6; i++)
            {
                newPlayers.Add(new Pig.Player(Players[i]));
            }



            //using (var stringwriter = new System.IO.StringWriter())
            //{
            //    Type[] types = new Type[3];
            //    types[0] = typeof(NN.NeuralNet);
            //    types[1] = typeof(NN.Neuron);
            //    types[2] = typeof(NN.Layer);
            //    var serializer = new XmlSerializer(Players[0].GetType(), types);
            //    serializer.Serialize(stringwriter, Players[0]);

            //    File.WriteAllText("bestPlayer.dat", stringwriter.ToString());
            //}

            for (int i = 6; i < Players.Count; i++)
            {
                Pig.Player cross1 = GetRandomMember(totalSqrtFitness);
                //Pig.Player cross2 = GetRandomMember(totalSqrtFitness - Math.Pow(cross1.averageFitness, genPlayerFitScale), cross1);
                Pig.Player cross2 = GetRandomMember(totalSqrtFitness);

                Pig.Player newPlayer = new Pig.Player(cross1, cross2);
                newPlayer.Mutate(Constants.mutationRate, Constants.percentMutate);
                newPlayers.Add(newPlayer);
            }

            Players = newPlayers; //copy over new players

            //foreach (Pig.Player player in Players)
            //{
            //    player.net._outputLayer._neurons[0]._weights[0] = 0;
            //    player.net._outputLayer._neurons[0]._weights[1] = 1;
            //}
        }
示例#7
0
        public void PlayGame()
        {
            if (versusIndex == playerIndex)
            {
                versusIndex++;
            }

            if (versusIndex >= Players.Count)
            {
                playerIndex++;
                versusIndex = 0;
            }

            if (playerIndex >= Players.Count)
            {
                if (_currentGenIteration < _iterationsPerGeneration)
                {
                    //resets player
                    versusIndex = 0;
                    playerIndex = 0;
                    _currentGenIteration++;
                }
                else
                {
                    //reset generation
                    _currentGenIteration = 0;
                    versusIndex          = 0;
                    playerIndex          = 0;
                    CreateGen();
                    currentGen++;
                }
            }
            else
            {
                Pig.Player p1 = Players[playerIndex];
                Pig.Player p2 = Players[versusIndex];
                versusIndex++;

                //get two players
                Pig.Pig game = new Pig.Pig(p1, p2);
                game.trainType = trainType;

                while (!game.hasEnded && game.turnCount < game.maxTurns)
                {
                    //breaks here
                    game.PlayRound();
                    Constants.debugGen++;
                }

                p1.gameCount++;
                p2.gameCount++;
                game.CalculateFitness();
            }
        }
        public void PlayGenIteration()
        {
            int playerIndex = 0;
            int versusIndex = 0;

            while (true)
            {
                if (versusIndex == playerIndex)
                {
                    versusIndex++;
                }

                if (versusIndex >= Players.Count)
                {
                    playerIndex++;
                    versusIndex = 0;
                }

                if (playerIndex >= Players.Count)
                {
                    Thread.CurrentThread.Abort();

                    //done the work end thread
                }
                else
                {
                    Pig.Player p1 = Players[playerIndex];
                    Pig.Player p2 = Players[versusIndex];
                    versusIndex++;

                    //get two players
                    Pig.Pig game = new Pig.Pig(p1, p2);

                    while (!game.hasEnded && game.turnCount < game.maxTurns)
                    {
                        //breaks here
                        game.PlayRound();
                        Constants.debugGen++;
                    }

                    p1.gameCount++;
                    p2.gameCount++;
                    game.CalculateFitness();
                }
            }
        }
示例#9
0
        public void PlayGame()
        {
            if (versusIndex == playerIndex)
            {
                versusIndex++;
            }

            if (versusIndex >= Players.Count)
            {
                playerIndex++;
                versusIndex = 0;
            }

            if (playerIndex >= Players.Count)
            {
                CreateGen();
                currentGen++;
                versusIndex = 0;
                playerIndex = 0;
            }
            else
            {
                Pig.Player p1 = Players[playerIndex];
                Pig.Player p2 = Players[versusIndex];
                versusIndex++;

                //get two players
                Pig.Pig game = new Pig.Pig(p1, p2);

                while (!game.hasEnded && game.turnCount < game.maxTurns)
                {
                    //breaks here
                    game.PlayRound();
                    Constants.debugGen++;
                }

                p1.gameCount++;
                p2.gameCount++;
                game.CalculateFitness();
            }
        }
        private void CalcBaseline()
        {
            Pig.Player    humanPlayer  = new Pig.Player();
            XmlSerializer deserializer = new XmlSerializer(typeof(Pig.Player));

            Pig.Player aiPlayer       = new Pig.Player();
            int        wins           = 0;
            int        gameIterations = 10000;


            FileStream openFile = new FileStream("basePlayer.dat", FileMode.Open);

            Pig.Player tempPlayer;
            try
            {
                tempPlayer = (Pig.Player)deserializer.Deserialize(openFile);
            }
            catch (Exception err)
            {
                System.Windows.Forms.MessageBox.Show(err.Message);
                throw;
            }
            //creation of two players

            openFile.Close();

            aiPlayer = new Pig.Player(tempPlayer);
            Pig.Player bestCurrentPlayer = new Pig.Player(aiGeneration.Players[0]);

            for (int i = 0; i < gameIterations; i++)
            {
                //get two players
                Pig.Pig game;
                if (rndGen.NextDouble() > 0.5) // to change who is starting player
                {
                    game = new Pig.Pig(aiPlayer, bestCurrentPlayer);
                }
                else
                {
                    game = new Pig.Pig(bestCurrentPlayer, aiPlayer);
                }

                game.trainType = aiGeneration.trainType;

                while (!game.hasEnded && game.turnCount < game.maxTurns)
                {
                    //breaks here
                    game.PlayRound();
                }

                if (game.hasEnded && game._winnerPlayer == bestCurrentPlayer)
                {
                    wins++;
                }

                aiPlayer.gameCount++;
                bestCurrentPlayer.gameCount++;
                game.CalculateFitness();
            }

            //ratio of fitness of bestplayer to otherplayer
            //baseLbl.Content = bestCurrentPlayer.totalFitness / aiPlayer.totalFitness;
            baseLbl.Content = ((double)wins / (double)gameIterations * 100) + "%";
        }