예제 #1
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
        }
예제 #2
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;
            //}
        }