public static void Initialize()
        {
            thisEvolution = new Evolution();
            thisEvolution.Start();

            Loading = true;


            Random RNG = new Random();

            EvolviMaxAmount    = 1f;
            EvolviLoadedAmount = 0;

            CurrentGeneration.Clear();

            for (int i = 0; i < evolviAmount; i++)
            {
                CurrentGeneration.Add(new Evolvi(new Vector2(RNG.Next(20, 2000 - 20), RNG.Next(20, 2000 - 20))));
            }

            CurrentFood.Clear();

            for (int i = 0; i < foodAmount; i++)
            {
                CurrentFood.Add(new Food(new Vector2(RNG.Next(20, 2000 - 20), RNG.Next(20, 2000 - 20))));

                Thread.Sleep(10);
            }

            Loading = false;
        }
Exemple #2
0
        public void NextGeneration()
        {
            SpeciesCollection.ClearSpecies();
            foreach (var genome in CurrentGeneration)
            {
                SpeciesCollection.Speciate(genome);
            }
            SpeciesCollection.ClearEmptySpecies();

            int population = Population;

            var nextGeneration = SpeciesCollection.Selection(population);

            LastGenerationWithSpecies = nextGeneration;

            // fill up when there is no species or
            var remaining = population - nextGeneration.Count;

            for (int i = 0; i < remaining; i++)
            {
                nextGeneration.Add(Genome.CreateDefault(this), null);
            }

            //Populate(population);

            LastFillUp = remaining;

            LastGeneration.Clear();
            LastGeneration.AddRange(CurrentGeneration);
            CurrentGeneration.Clear();
            CurrentGeneration.AddRange(nextGeneration.Select(x => x.Key));

            Generation++;
        }
Exemple #3
0
 public void Reset(int population, int inputs, int outputs)
 {
     Generation   = 0;
     InnovationId = 0;
     GenomeId     = 0;
     NodeId       = 0;
     InitialNodes.Clear();
     CurrentGeneration.Clear();
     LastGeneration.Clear();
     SpeciesCollection.SpeciesItems.Clear();
     Connections.Clear();
     Initialize(population, inputs, outputs);
 }
        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);
                }
            }
        }
Exemple #5
0
        public void NewGeneration()
        {
            if (CurrentGeneration.Count <= 0)
            {
                return;
            }

            newGeneration = new List <Specimen>();
            oldGeneration = new List <Specimen>();
            List <Specimen> bestOfTheBest = new List <Specimen>();

            allGenerations.Add(CurrentGeneration);

            for (int i = 0; i < CurrentGeneration.Count; i++)
            {
                for (int j = 0; j < CurrentGeneration.Count; j++)
                {
                    if (i == j)
                    {
                        j++;
                    }
                    if (j == CurrentGeneration.Count)
                    {
                        break;
                    }

                    var firstParent  = CurrentGeneration[i];
                    var secondParent = CurrentGeneration[j];

                    var child = firstParent.CrossoverWith(secondParent);
                    if (RandomForGA.Generator.NextDouble() < Parameters.MutationRate)
                    {
                        child.Mutate(Parameters.MutationAmplitude);
                    }


                    newGeneration.Add(child);
                }
            }

            for (int i = 0; i < CurrentGeneration.Count; i++)
            {
                oldGeneration.Add(CurrentGeneration[i]);
            }

            CurrentGeneration.Clear();
            for (int i = 0; i < oldGeneration.Count; i++)
            {
                CurrentGeneration.Add(oldGeneration[i]);
            }

            for (int i = 0; i < newGeneration.Count; i++)
            {
                CurrentGeneration.Add(newGeneration[i]);
            }

            CalculateFitness();

            CurrentGeneration.Sort((a, b) => population.FittingFunction(a).CompareTo(population.FittingFunction(b)));

            for (int i = 0; i < Parameters.PopulationSize; i++)
            {
                bestOfTheBest.Add(CurrentGeneration[i]);
            }
            CurrentGeneration = bestOfTheBest;
            GenerationNum++;
        }