Beispiel #1
0
        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;
        }
Beispiel #2
0
 public void Populate(int population)
 {
     for (var i = 0; i < population; i++)
     {
         var genome = Genome.CreateDefault(this);
         CurrentGeneration.Add(genome);
     }
 }
 private void InitializePopulation()
 {
     for (int i = 0; i < PopulationSize; i++)
     {
         var candidate = new Candidate();
         CurrentGeneration.Add(candidate);
     }
 }
        public void ApplyFitnessJudgement(FitnessJudgement judgement)
        {
            int weight = _settingsGa.GetWeightOfFitnessJudgement(judgement);

            if (weight > 0)
            {
                _currentCa.Fitness = weight;
                CurrentGeneration.Add(_currentCa);
            }
            GenerateNewCa();
        }
Beispiel #5
0
 public void CreateEmpty(int universeSize)
 {
     Size = universeSize;
     for (int column = 0; column < universeSize; column++)
     {
         for (int raw = 0; raw < universeSize; raw++)
         {
             CurrentGeneration.Add(new Point(raw, column), new Cell());
             NextGeneration.Add(new Point(raw, column), new Cell());
         }
     }
 }
Beispiel #6
0
        private void InitializePopulation()
        {
            for (var i = 0; i < PopulationSize; i++)
            {
                CurrentGeneration.Add(new Knapsack(_knapsackCapacity, _maxKnapsackWeight));
            }

            BestOfRun = CurrentGeneration[0];

            foreach (var knapsack in CurrentGeneration)
            {
                var knapsackFitness     = GetKnapsackFitness(knapsack);
                var bestKnapsackFitness = GetKnapsackFitness(BestOfRun);
                if (knapsackFitness > bestKnapsackFitness)
                {
                    BestOfRun        = knapsack;
                    BestOfRunFitness = knapsackFitness;
                }
            }

            Generations.Add(CurrentGeneration);
        }
Beispiel #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);
                }
            }
        }
Beispiel #8
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++;
        }