public void WriteMethod(HillClimbMode temp = HillClimbMode.None) { Directory.CreateDirectory("report"); var file = File.OpenWrite($"report\\{ProblemObj.Title}_method.out"); StreamWriter writer = new StreamWriter(file); writer.WriteLine($"seed: {RandomizationObj.seed}"); writer.WriteLine($"pop.: {ProblemObj.PopulationSize}"); writer.WriteLine($"gen.: {ProblemObj.MaxGeneration}"); switch (temp) { case HillClimbMode.None: writer.WriteLine("Method: Basic NSGAII"); break; case HillClimbMode.ChildOnly: writer.WriteLine("Method: NSGAII with hillclimb on child pop"); break; case HillClimbMode.MixedOnly: writer.WriteLine("Method: NSGAII with hillclimb on mixed pop"); break; case HillClimbMode.ParentOnly: writer.WriteLine("Method: NSGAII with hillclimb on parent pop"); break; case HillClimbMode.All: writer.WriteLine("Method: NSGAII with hillclimb on all pops"); break; case HillClimbMode.BestOfParent: writer.WriteLine("Method: NSGAII with hillclimb on best of parent pop"); break; case HillClimbMode.AllBestOfParent: writer.WriteLine("Method: NSGAII with hillclimb on all best of parent pop"); break; case HillClimbMode.AdaptiveParent: writer.WriteLine("Method: NSGAII with adaptive hillclimb on parent pop"); break; case HillClimbMode.Rank1Best: writer.WriteLine("Method: NSGAII with hillclimb on best rank 1 parent"); break; case HillClimbMode.Rank1All: writer.WriteLine("Method: NSGAII with hillclimb on rank 1 parents"); break; case HillClimbMode.AdaptiveRank1All: writer.WriteLine("Method: NSGAII with adaptive hillclimb on rank 1 parents"); break; } writer.Flush(); writer.Close(); }
public void NextGeneration(HillClimbMode mode = HillClimbMode.None) { if (CurrentGeneration >= ProblemObj.MaxGeneration) return; CurrentGeneration++; Selection(ParentPopulation, ChildPopulation, ProblemObj, RandomizationObj); MutatePopulation(ChildPopulation, ProblemObj, RandomizationObj); ChildPopulation.Decode(ProblemObj); ChildPopulation.Evaluate(ProblemObj); if (mode == HillClimbMode.All || mode == HillClimbMode.ChildOnly) { ChildPopulation.HillClimb(ProblemObj); } MixedPopulation.Merge(ParentPopulation, ChildPopulation, ProblemObj); //mixedPopulation.Decode(ProblemObj); //mixedPopulation.Evaluate(ProblemObj); fill_nondominated_sort(MixedPopulation, ParentPopulation, ProblemObj, RandomizationObj); ParentPopulation.Decode(ProblemObj); ParentPopulation.Evaluate(ProblemObj); if (mode == HillClimbMode.All || mode == HillClimbMode.ParentOnly) { ParentPopulation.HillClimb(ProblemObj); } double minimumResult = ParentPopulation.IndList.Min(x => x.TotalResult); if (minimumResult < Best) { Best = minimumResult; AdaptiveClimb = 0; } else { AdaptiveClimb++; } if (mode == HillClimbMode.AdaptiveParent && AdaptiveClimb >= 25) { ParentPopulation.HillClimb(ProblemObj); minimumResult = ParentPopulation.IndList.Min(x => x.TotalResult); Best = minimumResult; AdaptiveClimb = 0; } else if (mode == HillClimbMode.AdaptiveRank1All && AdaptiveClimb >= 25) { var rank1All = ParentPopulation.IndList.Where(x => x.Rank == 1); foreach (var child in rank1All) { child.HillClimb(ProblemObj); } minimumResult = ParentPopulation.IndList.Min(x => x.TotalResult); Best = minimumResult; AdaptiveClimb = 0; } var result = minimumResult; var bestChild = ParentPopulation.IndList.Where(x => x.TotalResult <= result).ToList(); if (mode == HillClimbMode.BestOfParent) { bestChild.First().HillClimb(ProblemObj); } else if (mode == HillClimbMode.AllBestOfParent) { foreach (var child in bestChild) { child.HillClimb(ProblemObj); } } else if (mode == HillClimbMode.Rank1Best) { var rank1Best = ParentPopulation.IndList.First(x => x.Rank == 1 && x.TotalResult <= result); rank1Best.HillClimb(ProblemObj); } else if (mode == HillClimbMode.Rank1All) { var rank1All = ParentPopulation.IndList.Where(x => x.Rank == 1); foreach (var child in rank1All) { child.HillClimb(ProblemObj); } } // Comment following lines if information for all //parent_pop.ReportPopulation(fpt4,ProblemObj); if (UsePlot) { _displayObj.PlotPopulation(ParentPopulation, ProblemObj, CurrentGeneration, bestChild.ToList()); } Console.WriteLine(GenerationReport()); Console.WriteLine(BestReport()); }