public Population Concat(Population genom2) { Population combined = new Population(this.genom); for (int i = 0; i < genom2.GetCount(); i++) { combined.Add(genom2.Get(i)); } return combined; }
public void CombinedPopulationCount() { int pop = 10; Population p1 = new Population(); p1.NewPopulation(pop); Population p2 = new Population(); p2.NewPopulation(pop); Population p3 = p1.Concat(p2); Assert.AreEqual(p3.GetCount(), p1.GetCount() + p2.GetCount()); }
public void InitialPopulationBoundaries() { Algorithm nsga = new Nsga2(); int pop = 10; TestFunctions funcs = TestFunctions.GetTestFunctions(); Population p = new Population(); p.NewPopulation(pop); int mistakes = 0; for (int i = 0; i < p.GetCount(); i++) { for (int j = 0; j < p.Get(i).DecisionVariables.Count; j++) { if (p.Get(i).DecisionVariables[0] < funcs.GetLowerThreshold() || p.Get(i).DecisionVariables[j] > funcs.GetUpperThreshold()) { mistakes++; } } } Assert.AreEqual(mistakes, 0); }
public void CrossoverTest() { GeneticOperators operators = new GeneticOperators(); int pop = 10; Population genom = new Population(); genom.NewPopulation(pop); List<Solution> newGenom = new List<Solution>(); for (int i = 0; i < genom.GetCount() - 1; i += 2) { List<Solution> list = operators.Crossover(genom.Get(i), genom.Get(i + 1)); foreach (Solution s in list) { newGenom.Add(s); } } if (newGenom.Count < genom.GetCount()) { newGenom.Add(genom.Get(genom.GetCount()-1)); } double eps = 0.001; int mistakes = 0; List<Solution>.Enumerator e = newGenom.GetEnumerator(); int j = -1; while (e.MoveNext()) { j++; for (int i = 0; i < e.Current.DecisionVariables.Count; i++) { if (Math.Abs(e.Current.DecisionVariables[i] - genom.Get(j).DecisionVariables[i]) < eps) { mistakes++; } } } Assert.AreEqual(0, mistakes); }
internal void Copy(Population list) { for (int i = 0; i < list.GetCount(); i++) { this.Add(list.Get(i)); } }
public Population Selection() { int n = genom.Count / 2; Population p = new Population(); //fitness [0,1,...,m] front index int i = 0; //int f = 0; //while (i < n) //{ // foreach (Solution s in genom) // { // if (f == s.Fitness) // { // p.Add(s); // i++; // } // } // f++; //} //while (i > n) //{ // p.Remove(p.GetCount() - 1); // i--; //} while (i < n) { p.Add(genom.ElementAt(i)); i++; } return p; }
public void SelectionCountTest() { int pop = 10; Population genom = new Population(); genom.NewPopulation(pop); Population genom2 = new Population(); genom2.NewPopulation(pop); Population combined = genom.Concat(genom2); genom = combined.Selection(); Assert.AreEqual(combined.GetCount() / 2, genom.GetCount()); }
public Population StartEvaluation(int populationCount, int generationCount) { infinite = 10000; Population genom = new Population(); genom.NewPopulation(populationCount); Console.WriteLine("Initial population"); genom.PrintToConsole(); Population newGenom = genom.CreateOffspring(); Console.WriteLine("Initial population after creating offspring pop"); genom.PrintToConsole(); Console.WriteLine("Offspring population"); newGenom.PrintToConsole(); FastNonDominatedSort(genom); RankingsPrintToConsole(); for (int i = 0; i < generationCount; i++) { Population combinedGenom = genom.Concat(newGenom); FastNonDominatedSort(combinedGenom); genom.RemoveAll(); AssignCrowdingDistance(); genom.Copy(SortPopulation(combinedGenom).Selection()); newGenom = genom.CreateOffspring(); } newGenom.PrintToConsole(); this.RankingsPrintToConsole(); //PrintToConsole(genom); // genom.PrintToConsole(); return genom; }
public void PrintToConsole(Population genom) { throw new NotImplementedException(); }
public void FastNonDominatedSort(Population genom) { genom.EvaluateObjectiveFunctions(); List<Solution> population = new List<Solution>(); for (int j = 0; j < genom.GetCount(); j++) { population.Add(genom.Get(j)); } rankings = new Ranking(); foreach (Solution p in population) { p.DominationCount = 0; foreach (Solution q in population) { if (!p.Equals(q)) { if (p.Dominates(q)) { p.DominatedSolutions.Add(q); } else { if (q.Dominates(p)) { p.DominationCount++; } } } } if (p.DominationCount == 0) { rankings.AddIndividual(0, p); } } int i = 0; List<Solution> front = rankings.GetFront(i); while (front.Count > 0) { foreach (Solution p in front) { foreach (Solution q in p.DominatedSolutions) { q.DominationCount--; if (q.DominationCount == 0) { rankings.AddIndividual(i + 1, q); } } } i++; front = rankings.GetFront(i); } for (int j = 0; j < rankings.GetFrontCount(); j++ ) { front = rankings.GetFront(j); foreach (Solution s in front) { s.Fitness = j; } if (front.Count == 0) { rankings.Remove(rankings.GetFrontCount() - 1); } } }
private Population SortPopulation(Population geom) { Population pop = new Population(); for (int i = 0; i < rankings.GetFrontCount(); i++) { var front = rankings.GetFront(i); while (front.Count > 0) { var min = front.First(); for (int j = 1; j < front.Count; j++) { if(!min.Equals(front.ElementAt(j))) { if (min.Dominates(front.ElementAt(j))) { min = front.ElementAt(j); } else { // front[j] does not dominate min either // this mean that they are kinda equal if (!front.ElementAt(j).Dominates(min)) { // crowind distance if (min.Distance < front.ElementAt(j).Distance) { min = front.ElementAt(j); } } } } } pop.Add(min); front.Remove(min); } } return pop; }