[4012] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4012] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[4017] | 23 | using System.Linq;
|
---|
[4068] | 24 | using HeuristicLab.Analysis;
|
---|
[4012] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[5356] | 28 | using HeuristicLab.Operators;
|
---|
[4012] | 29 | using HeuristicLab.Optimization;
|
---|
[4068] | 30 | using HeuristicLab.Optimization.Operators;
|
---|
[4012] | 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[4068] | 33 | using HeuristicLab.PluginInfrastructure;
|
---|
[4017] | 34 | using HeuristicLab.Random;
|
---|
[4012] | 35 |
|
---|
| 36 | namespace HeuristicLab.Algorithms.NSGA2 {
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// The Nondominated Sorting Genetic Algorithm II was introduced in Deb et al. 2002. A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation, 6(2), pp. 182-197.
|
---|
| 39 | /// </summary>
|
---|
[4017] | 40 | [Item("NSGA-II", "The Nondominated Sorting Genetic Algorithm II was introduced in Deb et al. 2002. A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation, 6(2), pp. 182-197.")]
|
---|
[15252] | 41 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 135)]
|
---|
[4012] | 42 | [StorableClass]
|
---|
[5809] | 43 | public class NSGA2 : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
[5366] | 44 | public string Filename { get; set; }
|
---|
| 45 |
|
---|
[4012] | 46 | #region Problem Properties
|
---|
| 47 | public override Type ProblemType {
|
---|
[5809] | 48 | get { return typeof(IMultiObjectiveHeuristicOptimizationProblem); }
|
---|
[4012] | 49 | }
|
---|
[5809] | 50 | public new IMultiObjectiveHeuristicOptimizationProblem Problem {
|
---|
| 51 | get { return (IMultiObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
[4012] | 52 | set { base.Problem = value; }
|
---|
| 53 | }
|
---|
| 54 | #endregion
|
---|
| 55 |
|
---|
| 56 | #region Parameter Properties
|
---|
[4017] | 57 | private ValueParameter<IntValue> SeedParameter {
|
---|
| 58 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
[4012] | 59 | }
|
---|
[4017] | 60 | private ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
| 61 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
| 62 | }
|
---|
| 63 | private ValueParameter<IntValue> PopulationSizeParameter {
|
---|
| 64 | get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
| 65 | }
|
---|
[8121] | 66 | public IConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
| 67 | get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
[4017] | 68 | }
|
---|
| 69 | private ValueParameter<PercentValue> CrossoverProbabilityParameter {
|
---|
| 70 | get { return (ValueParameter<PercentValue>)Parameters["CrossoverProbability"]; }
|
---|
| 71 | }
|
---|
[8121] | 72 | public IConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
| 73 | get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
[4017] | 74 | }
|
---|
| 75 | private ValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 76 | get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 77 | }
|
---|
[8121] | 78 | public IConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
| 79 | get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
[4017] | 80 | }
|
---|
| 81 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 82 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
| 83 | }
|
---|
| 84 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 85 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 86 | }
|
---|
[4514] | 87 | private ValueParameter<IntValue> SelectedParentsParameter {
|
---|
| 88 | get { return (ValueParameter<IntValue>)Parameters["SelectedParents"]; }
|
---|
| 89 | }
|
---|
[12276] | 90 |
|
---|
| 91 | private IFixedValueParameter<BoolValue> DominateOnEqualQualitiesParameter {
|
---|
| 92 | get { return (IFixedValueParameter<BoolValue>)Parameters["DominateOnEqualQualities"]; }
|
---|
| 93 | }
|
---|
[4012] | 94 | #endregion
|
---|
| 95 |
|
---|
| 96 | #region Properties
|
---|
[4017] | 97 | public IntValue Seed {
|
---|
| 98 | get { return SeedParameter.Value; }
|
---|
| 99 | set { SeedParameter.Value = value; }
|
---|
| 100 | }
|
---|
| 101 | public BoolValue SetSeedRandomly {
|
---|
| 102 | get { return SetSeedRandomlyParameter.Value; }
|
---|
| 103 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
| 104 | }
|
---|
| 105 | public IntValue PopulationSize {
|
---|
| 106 | get { return PopulationSizeParameter.Value; }
|
---|
| 107 | set { PopulationSizeParameter.Value = value; }
|
---|
| 108 | }
|
---|
| 109 | public ISelector Selector {
|
---|
| 110 | get { return SelectorParameter.Value; }
|
---|
| 111 | set { SelectorParameter.Value = value; }
|
---|
| 112 | }
|
---|
| 113 | public PercentValue CrossoverProbability {
|
---|
| 114 | get { return CrossoverProbabilityParameter.Value; }
|
---|
| 115 | set { CrossoverProbabilityParameter.Value = value; }
|
---|
| 116 | }
|
---|
| 117 | public ICrossover Crossover {
|
---|
| 118 | get { return CrossoverParameter.Value; }
|
---|
| 119 | set { CrossoverParameter.Value = value; }
|
---|
| 120 | }
|
---|
| 121 | public PercentValue MutationProbability {
|
---|
| 122 | get { return MutationProbabilityParameter.Value; }
|
---|
| 123 | set { MutationProbabilityParameter.Value = value; }
|
---|
| 124 | }
|
---|
| 125 | public IManipulator Mutator {
|
---|
| 126 | get { return MutatorParameter.Value; }
|
---|
| 127 | set { MutatorParameter.Value = value; }
|
---|
| 128 | }
|
---|
| 129 | public MultiAnalyzer Analyzer {
|
---|
| 130 | get { return AnalyzerParameter.Value; }
|
---|
| 131 | set { AnalyzerParameter.Value = value; }
|
---|
| 132 | }
|
---|
| 133 | public IntValue MaximumGenerations {
|
---|
| 134 | get { return MaximumGenerationsParameter.Value; }
|
---|
| 135 | set { MaximumGenerationsParameter.Value = value; }
|
---|
| 136 | }
|
---|
[4514] | 137 | public IntValue SelectedParents {
|
---|
| 138 | get { return SelectedParentsParameter.Value; }
|
---|
| 139 | set { SelectedParentsParameter.Value = value; }
|
---|
| 140 | }
|
---|
[12276] | 141 | public bool DominateOnEqualQualities {
|
---|
| 142 | get { return DominateOnEqualQualitiesParameter.Value.Value; }
|
---|
| 143 | set { DominateOnEqualQualitiesParameter.Value.Value = value; }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[4017] | 146 | private RandomCreator RandomCreator {
|
---|
| 147 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 148 | }
|
---|
| 149 | private SolutionsCreator SolutionsCreator {
|
---|
| 150 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
| 151 | }
|
---|
[4045] | 152 | private RankAndCrowdingSorter RankAndCrowdingSorter {
|
---|
[5356] | 153 | get { return (RankAndCrowdingSorter)((SubScopesCounter)SolutionsCreator.Successor).Successor; }
|
---|
[4045] | 154 | }
|
---|
[4017] | 155 | private NSGA2MainLoop MainLoop {
|
---|
[5366] | 156 | get { return FindMainLoop(RankAndCrowdingSorter.Successor); }
|
---|
[4017] | 157 | }
|
---|
[4012] | 158 | #endregion
|
---|
| 159 |
|
---|
[4086] | 160 | [Storable]
|
---|
[5143] | 161 | private RankBasedParetoFrontAnalyzer paretoFrontAnalyzer;
|
---|
[4086] | 162 |
|
---|
[4012] | 163 | [StorableConstructor]
|
---|
[4902] | 164 | protected NSGA2(bool deserializing) : base(deserializing) { }
|
---|
[5356] | 165 | protected NSGA2(NSGA2 original, Cloner cloner)
|
---|
| 166 | : base(original, cloner) {
|
---|
[5143] | 167 | paretoFrontAnalyzer = (RankBasedParetoFrontAnalyzer)cloner.Clone(original.paretoFrontAnalyzer);
|
---|
[16078] | 168 | RegisterEventhandlers();
|
---|
[4902] | 169 | }
|
---|
[4012] | 170 | public NSGA2() {
|
---|
[4017] | 171 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 172 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
| 173 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
| 174 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
[4045] | 175 | Parameters.Add(new ValueParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on two parents.", new PercentValue(0.9)));
|
---|
[4017] | 176 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
| 177 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
[15238] | 178 | Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
[4017] | 179 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
| 180 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
[4514] | 181 | Parameters.Add(new ValueParameter<IntValue>("SelectedParents", "Each two parents form a new child, typically this value should be twice the population size, but because the NSGA-II is maximally elitist it can be any multiple of 2 greater than 0.", new IntValue(200)));
|
---|
[12276] | 182 | Parameters.Add(new FixedValueParameter<BoolValue>("DominateOnEqualQualities", "Flag which determines wether solutions with equal quality values should be treated as dominated.", new BoolValue(false)));
|
---|
[4017] | 183 |
|
---|
| 184 | RandomCreator randomCreator = new RandomCreator();
|
---|
| 185 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
[5356] | 186 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
[4045] | 187 | RankAndCrowdingSorter rankAndCrowdingSorter = new RankAndCrowdingSorter();
|
---|
[5356] | 188 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
[4017] | 189 | NSGA2MainLoop mainLoop = new NSGA2MainLoop();
|
---|
[5356] | 190 |
|
---|
[4017] | 191 | OperatorGraph.InitialOperator = randomCreator;
|
---|
| 192 |
|
---|
| 193 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
| 194 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
| 195 | randomCreator.SeedParameter.Value = null;
|
---|
| 196 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
| 197 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
| 198 | randomCreator.Successor = solutionsCreator;
|
---|
| 199 |
|
---|
| 200 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
[5356] | 201 | solutionsCreator.Successor = subScopesCounter;
|
---|
[4017] | 202 |
|
---|
[5356] | 203 | subScopesCounter.Name = "Initialize EvaluatedSolutions";
|
---|
| 204 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
| 205 | subScopesCounter.Successor = rankAndCrowdingSorter;
|
---|
| 206 |
|
---|
[12276] | 207 | rankAndCrowdingSorter.DominateOnEqualQualitiesParameter.ActualName = DominateOnEqualQualitiesParameter.Name;
|
---|
[4045] | 208 | rankAndCrowdingSorter.CrowdingDistanceParameter.ActualName = "CrowdingDistance";
|
---|
| 209 | rankAndCrowdingSorter.RankParameter.ActualName = "Rank";
|
---|
[5356] | 210 | rankAndCrowdingSorter.Successor = resultsCollector;
|
---|
[4045] | 211 |
|
---|
[5356] | 212 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
| 213 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
| 214 | resultsCollector.Successor = mainLoop;
|
---|
| 215 |
|
---|
[4045] | 216 | mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
[4017] | 217 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
| 218 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
| 219 | mainLoop.CrossoverProbabilityParameter.ActualName = CrossoverProbabilityParameter.Name;
|
---|
| 220 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
| 221 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 222 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
| 223 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 224 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 225 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
[5356] | 226 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
[4017] | 227 |
|
---|
[4045] | 228 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is ISingleObjectiveSelector)).OrderBy(x => x.Name))
|
---|
[4017] | 229 | SelectorParameter.ValidValues.Add(selector);
|
---|
[4045] | 230 | ISelector tournamentSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("CrowdedTournamentSelector"));
|
---|
| 231 | if (tournamentSelector != null) SelectorParameter.Value = tournamentSelector;
|
---|
[4017] | 232 |
|
---|
[4045] | 233 | ParameterizeSelectors();
|
---|
| 234 |
|
---|
[5143] | 235 | paretoFrontAnalyzer = new RankBasedParetoFrontAnalyzer();
|
---|
| 236 | paretoFrontAnalyzer.RankParameter.ActualName = "Rank";
|
---|
| 237 | paretoFrontAnalyzer.RankParameter.Depth = 1;
|
---|
| 238 | paretoFrontAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
[4086] | 239 | ParameterizeAnalyzers();
|
---|
| 240 | UpdateAnalyzers();
|
---|
| 241 |
|
---|
[16078] | 242 | RegisterEventhandlers();
|
---|
[4012] | 243 | }
|
---|
| 244 |
|
---|
| 245 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[5356] | 246 | return new NSGA2(this, cloner);
|
---|
[4012] | 247 | }
|
---|
[4017] | 248 |
|
---|
[7209] | 249 | public override void Prepare() {
|
---|
| 250 | if (Problem != null) base.Prepare();
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[4017] | 253 | #region Events
|
---|
| 254 | protected override void OnProblemChanged() {
|
---|
[4045] | 255 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 256 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
[7999] | 257 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
[4045] | 258 | ParameterizeSolutionsCreator();
|
---|
[4067] | 259 | ParameterizeRankAndCrowdingSorter();
|
---|
[4045] | 260 | ParameterizeMainLoop();
|
---|
| 261 | ParameterizeSelectors();
|
---|
| 262 | ParameterizeAnalyzers();
|
---|
| 263 | ParameterizeIterationBasedOperators();
|
---|
| 264 | UpdateCrossovers();
|
---|
| 265 | UpdateMutators();
|
---|
| 266 | UpdateAnalyzers();
|
---|
| 267 | Problem.Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(Evaluator_QualitiesParameter_ActualNameChanged);
|
---|
[4017] | 268 | base.OnProblemChanged();
|
---|
| 269 | }
|
---|
| 270 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
[4045] | 271 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 272 | ParameterizeSolutionsCreator();
|
---|
[4017] | 273 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
| 274 | }
|
---|
| 275 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
[4045] | 276 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 277 | ParameterizeSolutionsCreator();
|
---|
[4067] | 278 | ParameterizeRankAndCrowdingSorter();
|
---|
[4045] | 279 | ParameterizeMainLoop();
|
---|
| 280 | ParameterizeSelectors();
|
---|
| 281 | ParameterizeAnalyzers();
|
---|
[4017] | 282 | Problem.Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(Evaluator_QualitiesParameter_ActualNameChanged);
|
---|
| 283 | base.Problem_EvaluatorChanged(sender, e);
|
---|
| 284 | }
|
---|
| 285 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
[7999] | 286 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
[4045] | 287 | ParameterizeIterationBasedOperators();
|
---|
| 288 | UpdateCrossovers();
|
---|
| 289 | UpdateMutators();
|
---|
| 290 | UpdateAnalyzers();
|
---|
[4017] | 291 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 292 | }
|
---|
| 293 | protected override void Problem_Reset(object sender, EventArgs e) {
|
---|
| 294 | base.Problem_Reset(sender, e);
|
---|
| 295 | }
|
---|
| 296 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 297 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
[4045] | 298 | ParameterizeSelectors();
|
---|
[4017] | 299 | }
|
---|
| 300 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
[4045] | 301 | ParameterizeSelectors();
|
---|
[4017] | 302 | }
|
---|
| 303 | private void Evaluator_QualitiesParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[4067] | 304 | ParameterizeRankAndCrowdingSorter();
|
---|
[4045] | 305 | ParameterizeMainLoop();
|
---|
| 306 | ParameterizeSelectors();
|
---|
| 307 | ParameterizeAnalyzers();
|
---|
[4017] | 308 | }
|
---|
[4514] | 309 | private void SelectedParentsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 310 | SelectedParents.ValueChanged += new EventHandler(SelectedParents_ValueChanged);
|
---|
| 311 | SelectedParents_ValueChanged(null, EventArgs.Empty);
|
---|
| 312 | }
|
---|
| 313 | private void SelectedParents_ValueChanged(object sender, EventArgs e) {
|
---|
| 314 | if (SelectedParents.Value < 2) SelectedParents.Value = 2;
|
---|
| 315 | else if (SelectedParents.Value % 2 != 0) {
|
---|
| 316 | SelectedParents.Value = SelectedParents.Value + 1;
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
[4017] | 319 | #endregion
|
---|
| 320 |
|
---|
| 321 | #region Helpers
|
---|
| 322 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[7351] | 323 | private void AfterDeserialization() {
|
---|
[12276] | 324 | // BackwardsCompatibility3.3
|
---|
| 325 | #region Backwards compatible code, remove with 3.4
|
---|
| 326 | if (!Parameters.ContainsKey("DominateOnEqualQualities"))
|
---|
| 327 | Parameters.Add(new FixedValueParameter<BoolValue>("DominateOnEqualQualities", "Flag which determines wether solutions with equal quality values should be treated as dominated.", new BoolValue(false)));
|
---|
[15238] | 328 | var optionalMutatorParameter = MutatorParameter as OptionalConstrainedValueParameter<IManipulator>;
|
---|
| 329 | if (optionalMutatorParameter != null) {
|
---|
| 330 | Parameters.Remove(optionalMutatorParameter);
|
---|
| 331 | Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 332 | foreach (var m in optionalMutatorParameter.ValidValues)
|
---|
| 333 | MutatorParameter.ValidValues.Add(m);
|
---|
| 334 | if (optionalMutatorParameter.Value == null) MutationProbability.Value = 0; // to guarantee that the old configuration results in the same behavior
|
---|
| 335 | else Mutator = optionalMutatorParameter.Value;
|
---|
| 336 | optionalMutatorParameter.ValidValues.Clear(); // to avoid dangling references to the old parameter its valid values are cleared
|
---|
| 337 | }
|
---|
[12276] | 338 | #endregion
|
---|
| 339 |
|
---|
[16078] | 340 | RegisterEventhandlers();
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | private void RegisterEventhandlers() {
|
---|
[4017] | 344 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
| 345 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
[4514] | 346 | SelectedParentsParameter.ValueChanged += new EventHandler(SelectedParentsParameter_ValueChanged);
|
---|
| 347 | SelectedParents.ValueChanged += new EventHandler(SelectedParents_ValueChanged);
|
---|
[4017] | 348 | if (Problem != null) {
|
---|
| 349 | Problem.Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(Evaluator_QualitiesParameter_ActualNameChanged);
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
[16078] | 352 |
|
---|
[4045] | 353 | private void ParameterizeSolutionsCreator() {
|
---|
| 354 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 355 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
| 356 | }
|
---|
[4067] | 357 | private void ParameterizeRankAndCrowdingSorter() {
|
---|
| 358 | RankAndCrowdingSorter.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 359 | RankAndCrowdingSorter.QualitiesParameter.ActualName = Problem.Evaluator.QualitiesParameter.ActualName;
|
---|
| 360 | }
|
---|
[4045] | 361 | private void ParameterizeMainLoop() {
|
---|
| 362 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 363 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 364 | MainLoop.QualitiesParameter.ActualName = Problem.Evaluator.QualitiesParameter.ActualName;
|
---|
| 365 | }
|
---|
| 366 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
| 367 | if (op is IStochasticOperator)
|
---|
| 368 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 369 | }
|
---|
| 370 | private void ParameterizeSelectors() {
|
---|
| 371 | foreach (ISelector selector in SelectorParameter.ValidValues) {
|
---|
| 372 | selector.CopySelected = new BoolValue(true);
|
---|
[4514] | 373 | selector.NumberOfSelectedSubScopesParameter.ActualName = SelectedParentsParameter.Name;
|
---|
[4045] | 374 | ParameterizeStochasticOperator(selector);
|
---|
| 375 | }
|
---|
| 376 | if (Problem != null) {
|
---|
| 377 | foreach (IMultiObjectiveSelector selector in SelectorParameter.ValidValues.OfType<IMultiObjectiveSelector>()) {
|
---|
| 378 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 379 | selector.QualitiesParameter.ActualName = Problem.Evaluator.QualitiesParameter.ActualName;
|
---|
| 380 | }
|
---|
| 381 | }
|
---|
| 382 | }
|
---|
| 383 | private void ParameterizeAnalyzers() {
|
---|
[4086] | 384 | if (Problem != null) {
|
---|
[5143] | 385 | paretoFrontAnalyzer.QualitiesParameter.ActualName = Problem.Evaluator.QualitiesParameter.ActualName;
|
---|
| 386 | paretoFrontAnalyzer.QualitiesParameter.Depth = 1;
|
---|
[4086] | 387 | }
|
---|
[4045] | 388 | }
|
---|
| 389 | private void ParameterizeIterationBasedOperators() {
|
---|
| 390 | if (Problem != null) {
|
---|
| 391 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 392 | op.IterationsParameter.ActualName = "Generations";
|
---|
| 393 | op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
| 394 | }
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 | private void UpdateCrossovers() {
|
---|
| 398 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
[7511] | 399 | ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
[4045] | 400 | CrossoverParameter.ValidValues.Clear();
|
---|
| 401 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
| 402 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
| 403 | if (oldCrossover != null) {
|
---|
| 404 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
| 405 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
[7511] | 406 | else oldCrossover = null;
|
---|
[4045] | 407 | }
|
---|
[7511] | 408 | if (oldCrossover == null && defaultCrossover != null)
|
---|
| 409 | CrossoverParameter.Value = defaultCrossover;
|
---|
[4045] | 410 | }
|
---|
| 411 | private void UpdateMutators() {
|
---|
| 412 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
| 413 | MutatorParameter.ValidValues.Clear();
|
---|
[15238] | 414 | IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
|
---|
[4045] | 415 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
| 416 | MutatorParameter.ValidValues.Add(mutator);
|
---|
| 417 | if (oldMutator != null) {
|
---|
| 418 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
| 419 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
[15238] | 420 | else oldMutator = null;
|
---|
[4045] | 421 | }
|
---|
[15238] | 422 | if (oldMutator == null && defaultMutator != null)
|
---|
| 423 | MutatorParameter.Value = defaultMutator;
|
---|
[4045] | 424 | }
|
---|
| 425 | private void UpdateAnalyzers() {
|
---|
| 426 | Analyzer.Operators.Clear();
|
---|
| 427 | if (Problem != null) {
|
---|
| 428 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
| 429 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 430 | param.Depth = 1;
|
---|
[7172] | 431 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
[4045] | 432 | }
|
---|
| 433 | }
|
---|
[7172] | 434 | Analyzer.Operators.Add(paretoFrontAnalyzer, paretoFrontAnalyzer.EnabledByDefault);
|
---|
[4045] | 435 | }
|
---|
[5366] | 436 | private NSGA2MainLoop FindMainLoop(IOperator start) {
|
---|
| 437 | IOperator mainLoop = start;
|
---|
| 438 | while (mainLoop != null && !(mainLoop is NSGA2MainLoop))
|
---|
| 439 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
| 440 | if (mainLoop == null) return null;
|
---|
| 441 | else return (NSGA2MainLoop)mainLoop;
|
---|
| 442 | }
|
---|
[4017] | 443 | #endregion
|
---|
[4012] | 444 | }
|
---|
| 445 | }
|
---|