コード例 #1
0
        internal override Agent CrossOver(Agent _partner)
        {
            NeatAgent offspring = new NeatAgent();
            NeatAgent partner   = (NeatAgent)_partner;
            NeatAgent fittestAgent;
            NeatAgent lessFitAgent;

            fittestAgent = this.fitness > partner.fitness ? this : partner;
            lessFitAgent = this.fitness >= partner.fitness ? partner : this;

            fittestAgent.Connections.Sort();
            lessFitAgent.Connections.Sort();

            int maxInnovationNum = Math.Max(fittestAgent.GetMaxInnovationNum(), lessFitAgent.GetMaxInnovationNum());

            if (maxInnovationNum != -1)
            {
                int innovationIndex = 0;
                do
                {
                    while (!(fittestAgent.LocalInnovationSet.Contains(innovationIndex)) &&
                           !(lessFitAgent.LocalInnovationSet.Contains(innovationIndex)))
                    {
                        innovationIndex++;
                        continue;
                    }

                    if (fittestAgent.LocalInnovationSet.Contains(innovationIndex) &&
                        !(lessFitAgent.LocalInnovationSet.Contains(innovationIndex)))
                    {
                        offspring.AddConnection(fittestAgent.GetConnectionByIN(innovationIndex));
                    }
                    else if (fittestAgent.LocalInnovationSet.Contains(innovationIndex) &&
                             lessFitAgent.LocalInnovationSet.Contains(innovationIndex))
                    {
                        if (fittestAgent.fitness / (fittestAgent.fitness + lessFitAgent.fitness) < RandomDouble())
                        {
                            offspring.AddConnection(fittestAgent.GetConnectionByIN(innovationIndex));
                        }
                        else
                        {
                            offspring.AddConnection(lessFitAgent.GetConnectionByIN(innovationIndex));
                        }
                    }
                    //disjoint or excess lessFitAgent gene, ignore
                    else
                    {
                        innovationIndex++;
                        continue;
                    }

                    offspring.AddNodesFromConnectionIN(innovationIndex);

                    innovationIndex++;
                }while (innovationIndex < maxInnovationNum);
            }

            offspring.Mutate();
            return(offspring);
        }
コード例 #2
0
        private void DebugSingleCrossover()
        {
            partner1 = new NeatAgent();
            partner2 = new NeatAgent();

            for (int i = 0; i < 15; i++)
            {
                partner1.ForceMutate();
                partner2.ForceMutate();
            }
            partner1.fitness = 10;
            partner2.fitness = 11;

            offspring    = (NeatAgent)partner1.CrossOver(partner2);
            currentAgent = partner1;
        }
コード例 #3
0
        private void OnKeyPress(object sender, KeyReleaseEventArgs args)
        {
            switch (args.Event.Key)
            {
            case Gdk.Key.Escape:
                Application.Quit();
                break;

            case Gdk.Key.Key_1:
                currentAgent = partner1;
                drawingArea.QueueDraw();
                break;

            case Gdk.Key.Key_2:
                currentAgent = partner2;
                drawingArea.QueueDraw();
                break;

            case Gdk.Key.Key_3:
                currentAgent = offspring;
                drawingArea.QueueDraw();
                break;

            case Gdk.Key.R:
            case Gdk.Key.r:
                DebugSingleCrossover();
                drawingArea.QueueDraw();
                break;

            case Gdk.Key.space:
                currentAgent.ActivateWithRandomInputs();
                drawingArea.QueueDraw();
                break;

            default:
                Here(args.Event.Key);
                break;
            }
        }
コード例 #4
0
        public TopographyViewerApp() : base("TopographyViewer")
        {
            SetSizeRequest(720, 450);
            SetPosition(WindowPosition.Center);
            Resizable    = false;
            DeleteEvent += delegate { Application.Quit(); };
            //ButtonPressEvent += ButtonPress;
            //KeyPressEvent += KeyPress;
            KeyReleaseEvent += OnKeyPress;
            AddEvents((int)Gdk.EventMask.ButtonPressMask);

            drawingArea = new DrawingArea();
            drawingArea.WidthRequest  = 720;
            drawingArea.HeightRequest = 450;
            drawingArea.ModifyBg(Gtk.StateType.Normal, new Gdk.Color(125, 125, 125));
            drawingArea.ExposeEvent += OnExpose;

            Add(drawingArea);
            currentAgent = new NeatAgent();

            ShowAll();

            XorAgents();
        }
コード例 #5
0
        private void XorAgents()
        {
            List <NeatAgent> population     = new List <NeatAgent>();
            List <NeatAgent> nextPopulation = new List <NeatAgent>();

            int popSize = 300;
            int gens    = 10;

            for (int i = 0; i < popSize; i++)
            {
                population.Add(new NeatAgent());
            }
            for (int j = 0; j < gens; j++)
            {
                for (int i = 0; i < population.Count; i++)
                {
                    currentAgent = population[i];
                    drawingArea.QueueDraw();
                    double error = 0;
                    error += -1 - population[i].Activate(new double[] { -1, -1 })[0];
                    error += -1 + population[i].Activate(new double[] { 1, -1 })[0];
                    error += -1 + population[i].Activate(new double[] { -1, 1 })[0];
                    error += -1 - population[i].Activate(new double[] { -1, -1 })[0];
                    population[i].fitness = error;
                }

                population.Sort();
                population.Reverse();
                nextPopulation.Clear();

                for (int i = 0; i < popSize; i++)
                {
                    int mate1 = RandomInt(popSize);
                    int mate2 = RandomInt(popSize);
                    while (mate1 == mate2)
                    {
                        mate2 = RandomInt(popSize);
                    }
                    nextPopulation.Add((NeatAgent)population[mate1].CrossOver(population[mate2]));
                }

                population.Clear();
                for (int i = 0; i < nextPopulation.Count; i++)
                {
                    population.Add(nextPopulation[i]);
                }
                Here((String.Format("pop {0}", j)));
            }

            population.Sort();
            population.Reverse();

            Console.WriteLine(population[0].Activate(new double[] { -1, -1 })[0]);
            Console.WriteLine(population[0].Activate(new double[] { 1, -1 })[0]);
            Console.WriteLine(population[0].Activate(new double[] { -1, 1 })[0]);
            Console.WriteLine(population[0].Activate(new double[] { 1, 1 })[0]);

            partner1     = population[0];
            partner2     = population[1];
            offspring    = population[2];
            currentAgent = partner1;
        }