Esempio n. 1
0
        public Evolvi CloneAll()
        {
            Random RNG = new Random();

            Evolvi clone = new Evolvi(new Vector2(RNG.Next(0, 2000), RNG.Next(0, 2000)), false);

            clone._angle        = _angle;
            clone.AreasOfVision = AreasOfVision;
            clone.FOV           = FOV;
            clone.VisionRange   = VisionRange;
            clone.CollisionBox  = new Rectangle(CollisionBox.Location, CollisionBox.Size);
            clone.Color         = Color;
            clone.FoodEaten     = FoodEaten;
            clone.FOV           = FOV;
            clone.Inputs        = new Input[AreasOfVision + 1];
            clone.TextureName   = "Evolvi_Body";
            clone.Energy        = Energy;

            for (int i = 0; i < AreasOfVision + 1; i++)
            {
                clone.Inputs[i] = new Input();
            }

            clone.Network = Network.Clone();

            return(clone);
        }
Esempio n. 2
0
        private void Btn_start_Clicked(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "Neural Network Json | *.json";
            DialogResult dr = ofd.ShowDialog();

            if (dr == DialogResult.OK)
            {
                Network n = Network.LoadFromFile(ofd.FileName);

                GeneticAlgorithm.Initialize(false);

                for (int i = 0; i < GeneticAlgorithm.evolviAmount; i++)
                {
                    Random RNG = new Random();

                    Evolvi j = new Evolvi(new Vector2(RNG.Next(100, 1900), RNG.Next(100, 1900)), false);
                    j.Network = n.Clone();

                    GeneticAlgorithm.CurrentGeneration.Add(j);

                    Thread.Sleep(50);
                }

                ScreenManager.SetScreen("SC_EVO");
            }
            else
            {
                ScreenModal.ShowDialog("You did not choose a file, try again.", ModalButtons.OK);
            }
        }
Esempio n. 3
0
        public static void DrawToolTipIndividual(Vector2 position, Evolvi e)
        {
            Vector2 size = new Vector2(650, 650);

            GameHelper.SpriteBatch.Draw(GameHelper.TextureManager["PIXEL"], position, null, new Color(80, 80, 80, 200), 0f, Vector2.Zero, size, SpriteEffects.None, 0f);
            GameHelper.SpriteBatch.DrawString(GameHelper.Font, "Fitness: " + e.Energy.ToString(), position + new Vector2(10), Color.White);
            DrawNetwork(position + new Vector2(10), e.Network, (int)(size.X - 40) / e.Network.LayerCount, (int)(size.Y - 40) / e.Network.Layers[0].NeuronCount, size.Y / 2f);
        }
Esempio n. 4
0
        public ScreenEvo()
        {
            g          = new Graph(GameHelper.GraphicsDevice, new Point(600, 500));
            g.Position = new Vector2(150, 150 + 500);
            g.MaxValue = 1;

            selectedAngle   = 0;
            SELECTED_EVOLVI = null;

            pausedSpeed = 1;

            GameHelper.DEBUG_MODE = false;

            camPos = GameHelper.GraphicsDevice.Viewport.Bounds.Size.ToVector2() / 2f;

            Name = "SC_EVO";

            backColor = new Color(200, 200, 200);

            base.Entered += this.ScreenEvo_Entered;

            btn_exit        = new Button(new Vector2(10, 10), new Point(100, 50), "Exit", ButtonReleased);
            btn_debug       = new Button(new Vector2(120, 10), new Point(120, 20), "Debug", ButtonReleased);
            btn_showNetwork = new Button(new Vector2(120, 40), new Point(120, 20), "Show Network", ButtonReleased);
            btn_evoSpeed_1  = new Button(new Vector2(360, 10), new Point(100, 50), "1x", ButtonReleased);
            btn_evoSpeed_5  = new Button(new Vector2(470, 10), new Point(100, 50), "5x", ButtonReleased);
            btn_evoSpeed_10 = new Button(new Vector2(580, 10), new Point(100, 50), "10x", ButtonReleased);
            btn_pauseEvo    = new Button(new Vector2(250, 10), new Point(100, 50), "Paused", ButtonClicked);
            btn_autoSelectOnNewGeneration = new Button(new Vector2(690, 10), new Point(100, 20), "ASNG", ButtonReleased);
            btn_autoSelectBestPerformer   = new Button(new Vector2(690, 40), new Point(100, 20), "ASBP", ButtonReleased);
            btn_openConsole     = new Button(new Vector2(800, 10), new Point(100, 50), "Console", ButtonReleased);
            btn_openGrapher     = new Button(new Vector2(910, 10), new Point(100, 50), "Grapher", ButtonReleased);
            btn_resumeEvolution = new Button(new Vector2(1020, 10), new Point(100, 50), "Return", ButtonReleased, false);

            btn_exit.Clicked        += this.Btn_exit_Clicked;
            btn_debug.Clicked       += this.Btn_debug_Clicked;
            btn_showNetwork.Clicked += Btn_showNetwork_Clicked;
            btn_evoSpeed_1.Clicked  += this.Btn_evoSpeed_1_Clicked;
            btn_evoSpeed_5.Clicked  += Btn_evoSpeed_5_Clicked;
            btn_evoSpeed_10.Clicked += Btn_evoSpeed_10_Clicked;
            btn_pauseEvo.Clicked    += Btn_pauseEvo_Clicked;
            btn_autoSelectOnNewGeneration.Clicked += Btn_autoSelectOnNewGeneration_Clicked;
            btn_autoSelectBestPerformer.Clicked   += Btn_autoSelectBestPerformer_Clicked;
            btn_openConsole.Clicked     += Btn_openConsole_Clicked;
            btn_openConsole.Released    += Btn_openConsole_Released;
            btn_openGrapher.Clicked     += Btn_openGrapher_Clicked;
            btn_resumeEvolution.Clicked += Btn_resumeEvolution_Clicked;

            GeneticAlgorithm.NewGeneration += GeneticAlgorithm_NewGeneration;

            ScreenModal         = new Modal();
            ScreenModal.Closed += ScreenModal_Closed;
        }
Esempio n. 5
0
        private void GeneticAlgorithm_NewGeneration(object sender, EventArgs e)
        {
            MyConsole.SendMessage(new Message("New Gen: " + GeneticAlgorithm.Generation, Message.MessageType.Information));

            SELECTED_EVOLVI = null;
            FOLLOW_SELECTED = false;

            if (AUTOSELECT_ON_NEWGEN)
            {
                SELECTED_EVOLVI = GeneticAlgorithm.SortFittest().Last();
            }
        }
Esempio n. 6
0
        public Evolvi Crossbreed(Evolvi e)
        {
            Random RNG = new Random();

            Vector2 pos = Position;

            pos = new Vector2(RNG.Next(0, 2000), RNG.Next(0, 2000));

            Evolvi newE = new Evolvi(pos + new Vector2(RNG.Next(-30, 30), RNG.Next(-30, 30)), false);

            //newE.color = (new Color((float)RNG.NextDouble(), (float)RNG.NextDouble(), ((float)RNG.NextDouble() * 2f) - 1f));
            newE.Network = this.Network.Crossbreed(e.Network);

            newE._angle = (float)(RNG.NextDouble() * MathHelper.TwoPi);

            return(newE);
        }
Esempio n. 7
0
        public static void DoUpdate(GameTime gameTime)
        {
            if ((ElapsedGenerationTime > GenerationTime && GenerationTime != 0) || !AnyoneAlive())
            {
                // GENETIC ALGORITHM THIS SHIT

                thisEvolution.AddGeneration(new Generation(CurrentGeneration));


                if (CurrentGeneration.Count > 0)
                {
                    List <Evolvi> sorted = SortFittest();

                    int root = (int)Math.Sqrt(sorted.Count);

                    sorted = sorted.OrderByDescending(x => x.Energy).ToList();

                    List <Evolvi> newGen = new List <Evolvi>();

                    while (newGen.Count < evolviAmount)
                    {
                        Evolvi e = sorted.First().Crossbreed(sorted.First());
                        e.Mutate(mutationRate);

                        newGen.Add(e);

                        Thread.Sleep(10);
                    }

                    CurrentGeneration.Clear();

                    CurrentGeneration = newGen;
                }
                else
                {
                    Random RNG = new Random();

                    Evolvi e = new Evolvi(new Vector2(RNG.Next(0, 2000), RNG.Next(0, 2000)));

                    for (int i = 0; i < evolviAmount; i++)
                    {
                        CurrentGeneration.Add(e.Clone());

                        Thread.Sleep(10);
                    }
                }


                ElapsedGenerationTime = 0;
                Generation++;


                OnNewGeneration(null, EventArgs.Empty);
            }

            ElapsedGenerationTime += (float)gameTime.ElapsedGameTime.TotalSeconds;

            List <Evolvi> deadEvolvis = new List <Evolvi>();

            foreach (Evolvi evolvi in CurrentGeneration)
            {
                if (!evolvi.Dead)
                {
                    if (evolvi.Energy <= 0)
                    {
                        evolvi.Dead = true;
                    }

                    Input[] inpts   = new Input[evolvi.AreasOfVision + 1];
                    int     indexer = 0;

                    if (Math.Abs(evolvi.AngleDegrees) > 360)
                    {
                        evolvi.AngleDegrees = 0;
                    }

                    float anglePerAreaOfVision = (float)evolvi.FOV / (float)(evolvi.AreasOfVision + 1);

                    for (float i = -evolvi.FOV / 2f; i < (evolvi.FOV / 2f); i += anglePerAreaOfVision)
                    {
                        inpts[indexer] = new Input();

                        Vector2 start = evolvi.Position;
                        Vector2 end;

                        float xDiff = (float)Math.Cos(evolvi.AngleRadians + MathHelper.ToRadians(i)) * evolvi.VisionRange;
                        float yDiff = (float)Math.Sin(evolvi.AngleRadians + MathHelper.ToRadians(i)) * evolvi.VisionRange;

                        end = start + new Vector2(xDiff, yDiff);
                        inpts[indexer].value = 0;

                        Vector2 dir = new Vector2(xDiff, yDiff);
                        dir.Normalize();

                        Ray ray = new Ray(new Vector3(start, 0), new Vector3(dir, 0));

                        float angle = (float)Math.Atan2(dir.Y, dir.X);

                        List <DistanceEvaluator> ends = new List <DistanceEvaluator>();

                        float intersectionToWall = RayIntersectsAWall(ray);

                        foreach (Food food in CurrentFood)
                        {
                            if (evolvi.CollisionBox.Intersects(food.CollisionBox))
                            {
                                evolvi.Energy += food.Nutrition;
                                evolvi.FoodEaten++;

                                Random RNG = new Random();

                                food.Position = new Vector2(RNG.Next(40, 2000 - 80), RNG.Next(40, 2000 - 80));
                                //food.CollisionBox = new Rectangle(food.Position.ToPoint(), food.TextureFood.Bounds.Size);
                                //food.BoundingBox = new BoundingBox(new Vector3(food.Position, 0), new Vector3(food.Position + food.CollisionBox.Size.ToVector2(), 0));

                                bool done = false;

                                while (!done)
                                {
                                    bool intersects = false;

                                    foreach (Food secondFood in CurrentFood)
                                    {
                                        if (food != secondFood)
                                        {
                                            if (food.CollisionBox.Intersects(secondFood.CollisionBox))
                                            {
                                                intersects = true;
                                            }
                                        }
                                    }

                                    if (!intersects)
                                    {
                                        done = true;
                                    }
                                    else
                                    {
                                        food.Position = new Vector2(RNG.Next(20, 2000 - 20), RNG.Next(20, 2000 - 20));
                                        //food.CollisionBox = new Rectangle(food.Position.ToPoint(), food.TextureFood.Bounds.Size);
                                        //food.BoundingBox = new BoundingBox(new Vector3(food.Position, 0), new Vector3(food.Position + food.CollisionBox.Size.ToVector2(), 0));
                                    }
                                }
                            }

                            if (ray.Intersects(food.BoundingBox) != 0)
                            {
                                if (ray.Intersects(food.BoundingBox) < intersectionToWall)
                                {
                                    if (ray.Intersects(food.BoundingBox) < evolvi.VisionRange)
                                    {
                                        float?dist = ray.Intersects(food.BoundingBox);

                                        ends.Add(new DistanceEvaluator((1f - (dist.Value / evolvi.VisionRange)) * (food.Nutrition / 50f), new Vector2((float)(Math.Cos(angle) * dist), (float)(Math.Sin(angle) * dist)) + start));
                                    }
                                }
                                else
                                {
                                    if (intersectionToWall < evolvi.VisionRange)
                                    {
                                        float?dist = intersectionToWall;

                                        ends.Add(new DistanceEvaluator(-(1 - (dist.Value / evolvi.VisionRange)), new Vector2((float)(Math.Cos(angle) * dist), (float)(Math.Sin(angle) * dist)) + start));
                                    }
                                }
                            }
                        }

                        if (ends.Count > 0)
                        {
                            List <DistanceEvaluator> sortedByDist = ends.OrderBy(x => x.Distance).ToList();

                            end = sortedByDist.Last().End;
                            inpts[indexer].value = sortedByDist.Last().Distance;
                        }

                        inpts[indexer].start = start;
                        inpts[indexer].end   = end;

                        indexer++;
                    }

                    evolvi.Update(inpts);
                }
            }
        }
Esempio n. 8
0
        public override void Update()
        {
            if (ScreenModal?.isActive == false)
            {
                GameHelper.Window.Title = "Time: " + Math.Round(GeneticAlgorithm.ElapsedGenerationTime) + " / " + GeneticAlgorithm.GenerationTime + " Generation: " + GeneticAlgorithm.Generation;

                btn_exit.Update();
                btn_debug.Update();
                btn_showNetwork.Update();
                btn_evoSpeed_1.Update();
                btn_evoSpeed_5.Update();
                btn_evoSpeed_10.Update();
                btn_pauseEvo.Update();
                btn_autoSelectOnNewGeneration.Update();
                btn_autoSelectBestPerformer.Update();
                btn_openConsole.Update();
                btn_openGrapher.Update();
                btn_resumeEvolution.Update();

                if (!USING_CONSOLE)
                {
                    #region Camera Handling
                    Camera.Instance.SetFocalPoint(camPos, false);
                    Camera.Instance.Update();

                    if (InputManager.ScrolledUp())
                    {
                        Camera.Instance.Zoom *= 1.1f;
                    }
                    if (InputManager.ScrolledDown())
                    {
                        Camera.Instance.Zoom /= 1.1f;
                    }

                    if (InputManager.KeyPressing(Microsoft.Xna.Framework.Input.Keys.W))
                    {
                        FOLLOW_SELECTED = false;
                        camPos         += new Vector2(0, -cameraSpeed / Camera.Instance.Zoom);
                    }
                    if (InputManager.KeyPressing(Microsoft.Xna.Framework.Input.Keys.S))
                    {
                        FOLLOW_SELECTED = false;
                        camPos         += new Vector2(0, cameraSpeed / Camera.Instance.Zoom);
                    }
                    if (InputManager.KeyPressing(Microsoft.Xna.Framework.Input.Keys.A))
                    {
                        FOLLOW_SELECTED = false;
                        camPos         += new Vector2(-cameraSpeed / Camera.Instance.Zoom, 0);
                    }
                    if (InputManager.KeyPressing(Microsoft.Xna.Framework.Input.Keys.D))
                    {
                        FOLLOW_SELECTED = false;
                        camPos         += new Vector2(cameraSpeed / Camera.Instance.Zoom, 0);
                    }
                    #endregion

                    if (InputManager.PressedMouseLeft())
                    {
                        foreach (Evolvi e in GeneticAlgorithm.CurrentGeneration)
                        {
                            if (InputManager.MouseBoxWorld.Intersects(e.CollisionBox))
                            {
                                SELECTED_EVOLVI = e;
                                break;
                            }
                        }
                    }

                    if (InputManager.KeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                    {
                        FOLLOW_SELECTED = true;
                    }
                }

                if (USING_CONSOLE && InputManager.KeyPressed(Microsoft.Xna.Framework.Input.Keys.Escape))
                {
                    USING_CONSOLE = false;
                }

                if (AUTOSELECT_BEST_PERFORMER && GeneticAlgorithm.CurrentGeneration.Count > 0)
                {
                    SELECTED_EVOLVI = GeneticAlgorithm.SortFittest().Last();
                }

                if (FOLLOW_SELECTED && SELECTED_EVOLVI != null)
                {
                    camPos = SELECTED_EVOLVI.Position;
                }

                if (USING_CONSOLE)
                {
                    MyConsole.Update();
                }

                if (MyGrapher.IsActive)
                {
                    MyGrapher.Update();
                }

                if (MyIndividualViewer.IsActive)
                {
                    MyIndividualViewer.Update();
                }

                GeneticAlgorithm.Update(GameHelper.GameTime);

                selectedAngle += 0.05f;


                base.Update();
            }
            else
            {
                ScreenModal.Update();
            }
        }
Esempio n. 9
0
 public Generation(List <Evolvi> evolvis)
 {
     Evolvis = new Evolvi[evolvis.Count];
     evolvis.CopyTo(Evolvis);
 }