public AuthorController(ILogger <AuthorController> nLogger, IAuthorService nAuthorService) { sup = new SupportingFunctions(); _logger = nLogger; authorService = nAuthorService; }
/// <summary> /// First, we'll copy the elites over. Then we'll perform merger (every hunter in the breeding population has a chance of performing /// merger. These are added to a list, and then executed without replacement. /// </summary> /// <param name="nextGen">Filling this</param> /// <param name="BreedingPop">by using this</param> private void fillListFromBreedingPop(List <Hunter> nextGen, List <Hunter> BreedingPop) { int elitismNum = (int)Math.Ceiling(OptoGlobals.ElitismPercent * PopSize); Console.WriteLine("Elitism..."); for (int e = 0; e < elitismNum; ++e) { Console.WriteLine("Elitism: Adding hunter with fitness " + population[e].Fitness); if (population[e].Fitness == 0) { nextGen.Add(new Hunter(population[e].NumChromosomes, 2)); } nextGen.Add(population[e].EliteCopy()); } Console.WriteLine("Merge..."); List <int> mergeList = new List <int>(15), used; int i = -1; Console.WriteLine("Before merge: best fitness is " + nextGen[0].Fitness); while (++i < BreedingPop.Count) { if (OptoGlobals.RNG.NextDouble() < OptoGlobals.MergerPercent) { mergeList.Add(i); } } used = new List <int>(mergeList); foreach (int target in mergeList) { used.Add(target); int k = SupportingFunctions.GetUnpickedInt(BreedingPop.Count, used); used.Add(k); //Console.WriteLine("Merging Hunters " + k + " and " + target); nextGen.Add(Hunter.Merger(BreedingPop[target], BreedingPop[k])); } //Now, we will use Crossover for the remaining slots while (nextGen.Count < PopSize) { int j = OptoGlobals.RNG.Next(0, elitismNum), k = OptoGlobals.RNG.Next(0, BreedingPop.Count); while (k == j) { k = OptoGlobals.RNG.Next(0, BreedingPop.Count); } foreach (Hunter newGuy in Hunter.Crossover(nextGen[j], BreedingPop[k])) { nextGen.Add(newGuy.EliteCopy()); } } //Since Crossover returns 2 at a time, we will strip off any extras while (nextGen.Count > PopSize) { nextGen.RemoveAt(nextGen.Count - 1); } }
public BookController(ILogger <BookController> nLogger, IBookService nBookService, IAuthorService nAuthorService, IReaderService nReaderService) { sup = new SupportingFunctions(); _logger = nLogger; bookService = nBookService; authorService = nAuthorService; readerService = nReaderService; }
private void generateNextGeneration() { Console.WriteLine("Before generation: best fitness is " + population[0].Fitness); List <Hunter> nextGen = new List <Hunter>(PopSize), breedingPop = new List <Hunter>(PopSize / 2); breedingPop = SupportingFunctions.StochasticUniformSample(population); for (int i = 0; i < breedingPop.Count; ++i) { if (breedingPop[i].Fitness == 0) { breedingPop[i] = new Hunter(breedingPop[i].NumChromosomes, 1); } } Console.WriteLine("Filling breeding pool"); fillListFromBreedingPop(nextGen, breedingPop); mutatePopulation(nextGen, (int)Math.Ceiling(OptoGlobals.ElitismPercent * PopSize)); population = nextGen; }
public static Hunter[] Crossover(Hunter a, Hunter b) { Hunter[] ret = new Hunter[2]; string bDebugStr = b.HumanReadableHunter, aDebugStr = a.HumanReadableHunter; ret[0] = new Hunter().StripChromosomes(); ret[1] = new Hunter().StripChromosomes(); Hunter target = ret[0], notTarget = ret[1], mostChromosomes; Chromosome[] temp = new Chromosome[2];//If a has 7 Chromosomes, and b has 3, //we want to end as soon as either fails //So if a and b both have count 3, a is true, but b is false, so the comparison fails List <int> aCrossed = new List <int>(a.myChromosomes.Count), bCrossed = new List <int>(b.myChromosomes.Count); List <int> mostCrossed; int max = Math.Min(a.myChromosomes.Count, b.myChromosomes.Count), point1 = OptoGlobals.RNG.Next(0, max), point2 = OptoGlobals.RNG.Next(point1, max); Hunter maxHunter = a.myChromosomes.Count == max?b:a; #region CrossoverModeBlock switch (OptoGlobals.CrossoverMode) { case OptoGlobals.CrossoverModes.Uniform: for (int i = 0; i < max; ++i) { //Trying a form of uniform crossover, 2 point //just swapping chromosomes, not crossing over at that level temp = Chromosome.CrossOver(a[i].deepCopy(), b[i].deepCopy()); target.AddChromosome(temp[0]); notTarget.AddChromosome(temp[1]); aCrossed.Add(i); bCrossed.Add(i); if (OptoGlobals.RNG.NextDouble() <= OptoGlobals.CrossoverChance) { switchTargets(ret[0], ret[1], ref target, ref notTarget); } } for (int i = max; i < maxHunter.myChromosomes.Count; ++i) { target.AddChromosome(maxHunter[i]); if (OptoGlobals.RNG.NextDouble() <= OptoGlobals.CrossoverChance) { switchTargets(ret[0], ret[1], ref target, ref notTarget); } } break; case OptoGlobals.CrossoverModes.TwoPointHunter: for (int i = 0; i < max; ++i) { //Trying a form of uniform crossover, 2 point //just swapping chromosomes, not crossing over at that level //temp = Chromosome.CrossOver(a[i], b[i]); target.AddChromosome(a[i]); notTarget.AddChromosome(b[i]); aCrossed.Add(i); bCrossed.Add(i); if (i == point1 || i == point2) { switchTargets(ret[0], ret[1], ref target, ref notTarget); } } break; case OptoGlobals.CrossoverModes.SinglePointHunter: for (int i = 0; i < max; ++i) { //Trying a form of uniform crossover, 2 point //just swapping chromosomes, not crossing over at that level //temp = Chromosome.CrossOver(a[i], b[i]); target.AddChromosome(a[i]); notTarget.AddChromosome(b[i]); aCrossed.Add(i); bCrossed.Add(i); if (i == point1) { switchTargets(ret[0], ret[1], ref target, ref notTarget); } } break; case OptoGlobals.CrossoverModes.TwoPointChromosome: for (int i = 0; i < max; ++i) { //Trying a form of uniform crossover, 2 point //just swapping chromosomes, not crossing over at that level temp = Chromosome.CrossOver(a[i], b[i]); target.AddChromosome(temp[0]); notTarget.AddChromosome(temp[1]); aCrossed.Add(i); bCrossed.Add(i); if (i == point1 || i == point2) { switchTargets(ret[0], ret[1], ref target, ref notTarget); } } break; case OptoGlobals.CrossoverModes.SinglePointChromosome: for (int i = 0; i < max; ++i) { //Trying a form of uniform crossover, 2 point //just swapping chromosomes, not crossing over at that level temp = Chromosome.CrossOver(a[i], b[i]); target.AddChromosome(temp[0]); notTarget.AddChromosome(temp[1]); aCrossed.Add(i); bCrossed.Add(i); if (i == point1) { switchTargets(ret[0], ret[1], ref target, ref notTarget); } } break; case OptoGlobals.CrossoverModes.RandomHunter: while (aCrossed.Count != a.myChromosomes.Count && bCrossed.Count != b.myChromosomes.Count) { int i = SupportingFunctions.GetUnpickedInt(a.myChromosomes.Count, aCrossed); int j = SupportingFunctions.GetUnpickedInt(b.myChromosomes.Count, bCrossed); if (OptoGlobals.RNG.NextDouble() <= OptoGlobals.CrossoverChance) { switchTargets(ret[0], ret[1], ref target, ref notTarget); } //temp = Chromosome.CrossOver(a[i], b[j]); target.AddChromosome(a[i]); notTarget.AddChromosome(b[j]); aCrossed.Add(i); bCrossed.Add(j); } break; case OptoGlobals.CrossoverModes.RandomChromosome: while (aCrossed.Count != a.myChromosomes.Count && bCrossed.Count != b.myChromosomes.Count) { int i = SupportingFunctions.GetUnpickedInt(a.myChromosomes.Count, aCrossed); int j = SupportingFunctions.GetUnpickedInt(b.myChromosomes.Count, bCrossed); if (OptoGlobals.RNG.NextDouble() <= OptoGlobals.CrossoverChance) { switchTargets(ret[0], ret[1], ref target, ref notTarget); } temp = Chromosome.CrossOver(a[i], b[j]); target.AddChromosome(temp[0]); notTarget.AddChromosome(temp[1]); aCrossed.Add(i); bCrossed.Add(j); } break; } #endregion if (aCrossed.Count == a.myChromosomes.Count) { mostChromosomes = b; mostCrossed = bCrossed; } else { mostChromosomes = a; mostCrossed = aCrossed; } //Randomly distribute the remaining Chromosomes while (mostCrossed.Count != mostChromosomes.myChromosomes.Count) { if (OptoGlobals.RNG.NextDouble() <= OptoGlobals.CrossoverChance) { switchTargets(ret[0], ret[1], ref target, ref notTarget); } int i = SupportingFunctions.GetUnpickedInt(mostChromosomes.myChromosomes.Count, mostCrossed); target.AddChromosome(mostChromosomes[i]); mostCrossed.Add(i); } string aOutStr = a.HumanReadableHunter, bOutStr = b.HumanReadableHunter; Debug.Assert(aOutStr == aDebugStr && bOutStr == bDebugStr); ret[0].updateCellNum(); ret[1].updateCellNum(); return(ret); /* Hunter[] ret = new Hunter[2]; * ret[0] = new Hunter().StripChromosomes(); * ret[1] = new Hunter().StripChromosomes(); * Hunter target = ret[0], notTarget = ret[1], mostChromosomes; * Chromosome[] temp = new Chromosome[2]; * int min = Math.Min(a.myChromosomes.Count, b.myChromosomes.Count); * for (int i = 0; i < min; ++i) * { * if (OptoGlobals.RNG.NextDouble() <= OptoGlobals.CrossoverChance) Hunter.switchTargets(ret[0], ret[1], ref target, ref notTarget); * temp = Chromosome.CrossOver(a[i], b[i]); * target[i] = temp[0]; * notTarget[i] = temp[1]; * } * * int max = Math.Max(a.myChromosomes.Count, b.myChromosomes.Count); * if (max == a.myChromosomes.Count) mostChromosomes = a; * else mostChromosomes = b; * * for (int i = min; i < max; i++) * { * if (OptoGlobals.RNG.NextDouble() <= OptoGlobals.CrossoverChance) switchTargets(ret[0], ret[1], ref target, ref notTarget); * if (mostChromosomes[i] != null) * target[i] = mostChromosomes[i]; * } * ret[0].updateCellNum(); * ret[1].updateCellNum(); * return ret; */ }