示例#1
0
 public void ResetThem2()
 {
     for (int i = 0; i < size; i++)
     {
         snakes2[i] = new Snake2(snakes2[i]);
     }
 }
示例#2
0
 private void PlaySecondPopulation()
 {
     firstBrain  = false;
     it          = 0;
     actualEpoch = 0;
     InitializeSecondPopulation();
     actualSnake2   = p.snakes2[it];
     p.BestOverall2 = p.snakes2[0];
 }
示例#3
0
 public Snake2(Snake2 origin)
 {
     headPosition = new Position(r.Next(5, mapSize - 5), r.Next(5, mapSize - 5));
     velocity     = new Position(1, 0);
     tailPosition = new List <Position>();
     tailPosition.Add(new Position(headPosition.X - 1, headPosition.Y));
     tailPosition.Add(new Position(headPosition.X - 2, headPosition.Y));
     tailPosition.Add(new Position(headPosition.X - 3, headPosition.Y));
     tailPosition.Add(new Position(headPosition.X - 4, headPosition.Y));
     brain           = CopyBrain(origin.brain);
     brain2          = CopyBrain(origin.brain2);
     usedSecondBrain = false;
     fitness2        = 0;
     timeOfLife      = 0;
     fitness         = origin.fitness;
     eatenFood       = 0;
     alive           = true;
     timetoDie       = 4 * mapSize;
     livesLeft       = howManyLives;
     SpawnFood();
     previousHeadPosition = new Position(-1, -1);
 }
示例#4
0
        public Snake2[] Crossover(Snake2 s)
        {
            Snake2[] tab     = new Snake2[2];
            Snake2   baby1   = new Snake2();
            Snake2   baby2   = new Snake2();
            int      warstwa = r.Next(0, brain2.Layers.Count() - 2);
            int      neuron  = r.Next(0, brain2.Layers[warstwa].Neurons.Count() - 1);
            bool     przed   = true;

            for (int i = 0; i < brain2.Layers.Count() - 1; i++)
            {
                for (int j = 0; j < brain2.Layers[i].Neurons.Count(); j++)
                {
                    if (j < neuron && warstwa == i)
                    {
                        przed = false;
                    }
                    if (przed)
                    {
                        for (int k = 0; k < brain2.Layers[i].Neurons[j].Weights.Count(); k++)
                        {
                            baby1.brain2.Layers[i].Neurons[j].Weights[k] = brain2.Layers[i].Neurons[j].Weights[k];
                            baby2.brain2.Layers[i].Neurons[j].Weights[k] = s.brain2.Layers[i].Neurons[j].Weights[k];
                        }
                    }
                    else
                    {
                        for (int k = 0; k < brain2.Layers[i].Neurons[j].Weights.Count(); k++)
                        {
                            baby2.brain2.Layers[i].Neurons[j].Weights[k] = brain2.Layers[i].Neurons[j].Weights[k];
                            baby1.brain2.Layers[i].Neurons[j].Weights[k] = s.brain2.Layers[i].Neurons[j].Weights[k];
                        }
                    }
                }
            }
            tab[0] = baby1;
            tab[1] = baby2;
            return(tab);
        }
示例#5
0
        public void Crossover2()
        {
            int           howManyBest = 5;
            List <Snake2> best        = ChooseBest2(howManyBest);

            best.Add(new Snake2(BestOverall2));
            List <Snake2> childs = new List <Snake2>();

            Snake2[] dzieci = new Snake2[2];
            for (int k = 0; k < (size - 20) / 60; k++) //180
            {
                for (int i = 0; i < howManyBest + 1; i++)
                {
                    for (int j = 0; j < howManyBest + 1; j++)
                    {
                        if (i != j)
                        {
                            dzieci = best[i].Crossover(best[j]);
                            childs.AddRange(dzieci);
                        }
                    }
                }
            }
            for (int i = 0; i < 2; i++) //10
            {
                childs.AddRange(best);
            }
            for (int i = 0; i < 10; i++) //10
            {
                childs.Add(new Snake2());
            }
            for (int i = 0; i < snakes2.Count; i++)
            {
                snakes2[i].brain2 = childs[i].CopyBrain(childs[i].brain2);
            }
        }
示例#6
0
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            if (firstBrain)
            {
                if (actualSnake.alive)
                {
                    Play();
                }
                else
                {
                    actualSnake.CalculateFitness();
                    ActBlock.Text = actualSnake.fitness.ToString();
                    if (it < p.size)
                    {
                        actualSnake = p.snakes[it++];
                    }
                    else
                    {
                        if (actualEpoch < maxEpochs)
                        {
                            double max     = p.BestSnake.fitness;
                            double maxFood = p.BestSnake.eatenFood;
                            EpochBlock.Text = (actualEpoch + 1).ToString();
                            MeanBlock.Text  = Math.Truncate(p.FitnessAVG).ToString();
                            MaxBlock.Text   = max.ToString();
                            wyniki.Add(String.Format(actualEpoch + ". AVG:" + Math.Truncate(p.FitnessAVG) + " MAX: " + max + " Zjedzone: " + maxFood));
                            if (p.BestSnake.fitness > p.BestOverall.fitness)
                            {
                                p.BestOverall = new Snake(p.BestSnake);
                            }
                            p.UseGeneticOperations();
                            it          = 0;
                            actualSnake = p.snakes[it];
                            actualEpoch++;
                        }
                        else
                        {
                            //SaveResult();
                            Draw = true;
                            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 30);
                            PlayBest();
                            //MessageBox.Show(String.Format("Zjedzone: " + p.BestOverall.eatenFood));
                            //SaveWinner();

                            //firstBrain = false;
                            //it = 0;
                            //actualEpoch = 0;
                            //InitializeSecondPopulation();
                            //actualSnake2 = p.snakes2[it];
                            //p.BestOverall2 = p.snakes2[0];
                        }
                    }
                }
            }
            else //Kiedy mamy wyuczony pierwszy mózg...
            {
                if (actualSnake2.alive)
                {
                    Play2();
                }
                else
                {
                    actualSnake2.CalculateFitness();
                    ActBlock.Text = actualSnake2.fitness.ToString();
                    if (it < p.size)
                    {
                        actualSnake2 = p.snakes2[it++];
                    }
                    else
                    {
                        if (actualEpoch < maxEpochs)
                        {
                            double max     = p.BestSnake2.fitness;
                            double maxFood = p.BestSnake2.eatenFood / Snake.howManyLives;
                            EpochBlock.Text = (actualEpoch + 1).ToString();
                            MeanBlock.Text  = Math.Truncate(p.FitnessAVG2).ToString();
                            MaxBlock.Text   = max.ToString();
                            wyniki.Add(String.Format(actualEpoch + ". AVG:" + Math.Truncate(p.FitnessAVG2) + " MAX: " + max + " Zjedzone: " + maxFood));
                            if (p.BestSnake2.fitness > p.BestOverall2.fitness)
                            {
                                p.BestOverall2 = new Snake2(p.BestSnake2);
                            }
                            p.UseGeneticOperations2();
                            it           = 0;
                            actualSnake2 = p.snakes2[it];
                            actualEpoch++;
                        }
                        else
                        {
                            //SaveResult();
                            Draw = true;
                            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 30);
                            PlayBest2();
                            MessageBox.Show(String.Format("Zjedzone: " + p.BestOverall2.eatenFood));
                            //SaveWinner();
                        }
                    }
                }
            }
        }
示例#7
0
 private void PlayBest2()
 {
     actualSnake2 = new Snake2(p.BestOverall2);
 }
示例#8
0
        private void LoadWinner2(string plik)
        {
            actualEpoch = maxEpochs;
            it          = p.size;
            string        s  = File.ReadAllText(plik);
            List <string> s2 = s.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();

            string[]          s3;
            double[]          d;
            ActivationNetwork brain = new ActivationNetwork(new BipolarSigmoidFunction(), 12, 8, 4);

            int row = 0;

            for (int i = 0; i < brain.Layers.Length; i++)
            {
                for (int j = 0; j < brain.Layers[i].Neurons.Length; j++)
                {
                    s3 = s2[row].Split(';');            //bierzemy wiersz
                    d  = new double[s3.Length];
                    for (int k = 0; k < s3.Length; k++) //robimy z niego double
                    {
                        d[k] = Convert.ToDouble(s3[k]);
                    }
                    //Wpisujemy dane do neuronu
                    for (int k = 1; k < d.Length; k++)
                    {
                        brain.Layers[i].Neurons[j].Weights[k - 1] = d[k];
                    }
                    (brain.Layers[i].Neurons[j] as ActivationNeuron).Threshold = d[0];
                    row++;

                    while (s2[row] == "")
                    {
                        row++;
                        if (row == s2.Count)
                        {
                            break;
                        }
                    }
                }
            }
            //DRUGI MÓZG
            row++;
            ActivationNetwork brain2 = new ActivationNetwork(new BipolarSigmoidFunction(), 12, 8, 4);

            for (int i = 0; i < brain2.Layers.Length; i++)
            {
                for (int j = 0; j < brain2.Layers[i].Neurons.Length; j++)
                {
                    s3 = s2[row].Split(';');            //bierzemy wiersz
                    d  = new double[s3.Length];
                    for (int k = 0; k < s3.Length; k++) //robimy z niego double
                    {
                        d[k] = Convert.ToDouble(s3[k]);
                    }
                    //Wpisujemy dane do neuronu
                    for (int k = 1; k < d.Length; k++)
                    {
                        brain2.Layers[i].Neurons[j].Weights[k - 1] = d[k];
                    }
                    (brain2.Layers[i].Neurons[j] as ActivationNeuron).Threshold = d[0];
                    row++;

                    while (s2[row] == "")
                    {
                        row++;
                        if (row == s2.Count)
                        {
                            break;
                        }
                    }
                }
            }
            actualSnake2        = new Snake2();
            actualSnake2.brain  = actualSnake2.CopyBrain(brain);
            actualSnake2.brain2 = actualSnake2.CopyBrain(brain2);
            p.BestOverall2      = new Snake2(actualSnake2);
        }