private ApplicationEngine(Size worldCanvasSize, Size brainCanvasSize, Size graphCanvasSize) { _geneticEvolution = new GeneticEvolution(ApplicationSettings.Random, ApplicationSettings.MutationRate, ApplicationSettings.CrossOverRate); _agentsColor = new List <Color>(); agents = new List <Agent>(); food = new List <Food>(); badFood = new List <Food>(); bitmapWorld = new Bitmap(worldCanvasSize.Width, worldCanvasSize.Height); graphicsWorld = Graphics.FromImage(bitmapWorld); bitmapBrain = new Bitmap(brainCanvasSize.Width, brainCanvasSize.Height); graphicsBrain = Graphics.FromImage(bitmapBrain); bitmapGraph = new Bitmap(graphCanvasSize.Width, graphCanvasSize.Height); graphicsGraph = Graphics.FromImage(bitmapGraph); typeOneEats.Add(SpecieType.Plant); typeTwoEats.Add(SpecieType.Herbivore); _agentsColor.Add(Color.Green); _agentsColor.Add(Color.Red); _agentsColor.Add(Color.Blue); for (int i = 0; i < ApplicationSettings.NumberOfAgentsTypeOne; i++) { Agent a = GenerateAgent(SpecieType.Herbivore, false, null); if (a != null) { agents.Add(a); } } for (int i = 0; i < ApplicationSettings.NumberOfAgentsTypeTwo; i++) { Agent a = GenerateAgent(SpecieType.Carnivore, false, null); if (a != null) { agents.Add(a); } } for (int i = 0; i < ApplicationSettings.FoodOnScreen; i++) { food.Add(CreateNewFood(colorFood, 50)); } for (int i = 0; i < ApplicationSettings.BadFoodOnScreen; i++) { badFood.Add(CreateNewFood(colorBadFood, -50)); } evolutionGraph = new Graph(200, 500, 3, _agentsColor); evolutionGraph.AddPoint(0, 0); evolutionGraph.AddPoint(1, 0); evolutionGraph.AddPoint(2, 0); }
public Evolution(int maxGenerations = 100) { _maxGenerations = maxGenerations; var options = new EvolutionOptions { PopulationSize = 100, MaxNumberValue = 100, CollectionSize = 4, Crossover = CrossoverTypes.Slice, Mutation = MutationTypes.Addition, }; _geneticEvolution = new GeneticEvolution <SnakeGA>(options); }
private ApplicationEngine(Size worldCanvasSize, Size brainCanvasSize, Size graphCanvasSize) { ticksIntoGeneration = 0; ticksForGeneration = 10; timeWhenApplicationStarted = DateTime.Now; _geneticEvolution = new GeneticEvolution(ApplicationSettings.Random, ApplicationSettings.MutationRate, ApplicationSettings.CrossOverRate); _agentsColor = new List <Color>(); agents = new List <Agent>(); food = new List <Food>(); badFood = new List <Food>(); bitmapWorld = new Bitmap(worldCanvasSize.Width, worldCanvasSize.Height); graphicsWorld = Graphics.FromImage(bitmapWorld); bitmapBrain = new Bitmap(brainCanvasSize.Width, brainCanvasSize.Height); graphicsBrain = Graphics.FromImage(bitmapBrain); bitmapGraph = new Bitmap(graphCanvasSize.Width, graphCanvasSize.Height); graphicsGraph = Graphics.FromImage(bitmapGraph); typeOneEats.Add(SpecieType.Plant); typeTwoEats.Add(SpecieType.Herbivore); _agentsColor.Add(Color.Red); _agentsColor.Add(Color.Blue); for (int i = 0; i < ApplicationSettings.NumberOfAgentsTypeOne; i++) { GenerateAgent(SpecieType.Herbivore, false); } for (int i = 0; i < ApplicationSettings.NumberOfAgentsTypeTwo; i++) { GenerateAgent(SpecieType.Carnivore, false); } for (int i = 0; i < ApplicationSettings.FoodOnScreen; i++) { GenerateFood(); } for (int i = 0; i < ApplicationSettings.BadFoodOnScreen; i++) { GenerateBadFood(); } evolutionGraph = new Graph(200, 500, 2, _agentsColor); map = new Map(ApplicationSettings.MapSize, ApplicationSettings.CellSize, ApplicationSettings.Random); }
protected void Evolve <T>(int generationsCount = 50, EvolutionOptions options = null, Func <EvolutionResult <T>, bool> stopCondition = null) where T : class, IEvolutionaryIndividual, new() { var geneticEvolution = new GeneticEvolution <T>(options ?? EvolutionOptions.Default); geneticEvolution.EvolveUntil(stopCondition ?? (r => r.Generation.Number == generationsCount), result => { _output.WriteLine($"Gen. : #{geneticEvolution.CurrentGeneration.Number}"); _output.WriteLine($"Best : {result.BestIndividual}"); _output.WriteLine($"Avg : {result.AverageIndividual}"); _output.WriteLine($"Worst: {result.WorstIndividual}"); _output.WriteLine($"------------------------------"); _output.WriteLine($"Avg.Fitness: {result.AverageFitness}"); _output.WriteLine(""); return(result.Generation.Number < 500); // safe stop }); }
//Subscribed to PlotStart, which is queued in UIReflect after UI updates (which is queued StartEvolve) private void EvolvePopulation(object sender, EventArgs e) { TimeLogger.LogStart(); float averageFitness = 0.0f; List <Bed> evolvedBeds = new List <Bed>(); //Actual Evolve process for (int g = 0; g < givenIterations; g++) { //New beds get inited from rand conditions for the first gen //Should ideally ahve already been done from Start -> ResetPopulation, just a safety measure if (!initialised) { newBeds = CreateBeds(); } //The previous List gets emptied. This data is now stored in newBeds after the last generation if (initialised) { } /*for (int i = 0; i < beds.Count; i++) * { * beds[i].ComputeFitness(); * }*/ if (evolvedBeds == null) { //Debug.Log("Evolved null"); } if (newBeds == null) { //Debug.Log("new beds null"); } //Use the GA to evolve our data evolvedBeds = GeneticEvolution.Evolve(newBeds, newBeds.Count, testbench.GetUsedMatingPoolSelector(), testbench.GetUsedBreedingPairSelector(), testbench.GetUsedCrossoverOperator(), testbench.GetUsedMutator()); for (int i = 0; i < evolvedBeds.Count; i++) { evolvedBeds[i].LoadGardenAndPatches(this, patches); } generationNumber++; //Debug.Log("Unsorted List " + beds[0].Fitness); Bed[] bedArray = evolvedBeds.ToArray(); TimSort <Bed> .sort(bedArray, new GeneticEntityComparable()); /* for (int i = 0; i < bedArray.Length; i++) * { * Debug.Log("Sorted post-gen i: " + i + " Fitness: " + bedArray[i].Fitness); * }*/ //Debug.Log("Top of Array: " + bedArray[0].Fitness); evolvedBeds = new List <Bed>(bedArray); //Trimming the solutions //Debug.Log("Evolved Beds pre-trim - size: " + evolvedBeds.Count); //Certain Pair Selectors create larger numbers of solutions than the intial set of parents. //In this case, we require as many solutions (beds) as we put in and therefore remove the excess //after they've been fitness sorted if (evolvedBeds.Count > numberOfBeds) { evolvedBeds.RemoveRange(numberOfBeds - 1, (evolvedBeds.Count - numberOfBeds)); } //Debug.Log("Evolved Beds post-trim - size: " + evolvedBeds.Count); averageFitness = 0.0f; for (int i = 0; i < evolvedBeds.Count; i++) { averageFitness += evolvedBeds[i].Fitness; // Debug.Log("Beds [" + i + "]:" + beds[i].Fitness); } averageFitness /= evolvedBeds.Count; //Clear out the starting beds newBeds.Clear(); //Save the evolved beds into a new starting List to be used for the next generation for (int i = 0; i < evolvedBeds.Count; i++) { newBeds.Add(evolvedBeds[i]); } //Clear out the evolved one evolvedBeds.Clear(); //Debug.Log("End of gen loop - eb size: " + evolvedBeds.Count + " | nb: " + newBeds.Count); } TimeLogger.LogEnd(); EventQueue.QueueEvent(EventQueue.EventType.Plot_End, this, new TimeArgs(Time.realtimeSinceStartup)); //We use newBeds not evolvedBeds here since at the end of the gen, new holds the next (or last) generation's data //and evolved is already cleared for the next gen EventQueue.QueueEvent(EventQueue.EventType.BroadcastGeneration, this, new GenerationArgs <Bed>(newBeds, newBeds[0].Fitness, averageFitness, generationNumber, testingBase)); }