Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.NSGA2/3.3/NSGA2MainLoop.cs @ 12328

Last change on this file since 12328 was 12276, checked in by mkommend, 10 years ago

#2321: Merged r12123, r12124 and r12144 into stable.

File size: 12.7 KB
RevLine 
[4012]1#region License Information
2/* HeuristicLab
[12009]3 * Copyright (C) 2002-2015 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
[5208]22using HeuristicLab.Common;
[4012]23using HeuristicLab.Core;
[4068]24using HeuristicLab.Data;
[4012]25using HeuristicLab.Operators;
[4068]26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Parameters;
[4012]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[4017]29using HeuristicLab.Selection;
[4012]30
31namespace HeuristicLab.Algorithms.NSGA2 {
32  /// <summary>
33  /// An operator that represents the mainloop of the NSGA-II
34  /// </summary>
35  [Item("NSGA2MainLoop", "An operator which represents the main loop of the NSGA-II algorithm.")]
36  [StorableClass]
37  public class NSGA2MainLoop : AlgorithmOperator {
[4017]38    #region Parameter properties
39    public ValueLookupParameter<IRandom> RandomParameter {
40      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
41    }
[4045]42    public ValueLookupParameter<BoolArray> MaximizationParameter {
43      get { return (ValueLookupParameter<BoolArray>)Parameters["Maximization"]; }
[4017]44    }
[4045]45    public ScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
46      get { return (ScopeTreeLookupParameter<DoubleArray>)Parameters["Qualities"]; }
[4017]47    }
[4045]48    public ValueLookupParameter<IntValue> PopulationSizeParameter {
49      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
50    }
[4017]51    public ValueLookupParameter<IOperator> SelectorParameter {
52      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
53    }
54    public ValueLookupParameter<PercentValue> CrossoverProbabilityParameter {
55      get { return (ValueLookupParameter<PercentValue>)Parameters["CrossoverProbability"]; }
56    }
57    public ValueLookupParameter<IOperator> CrossoverParameter {
58      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
59    }
60    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
61      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
62    }
63    public ValueLookupParameter<IOperator> MutatorParameter {
64      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
65    }
66    public ValueLookupParameter<IOperator> EvaluatorParameter {
67      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
68    }
69    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
70      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
71    }
72    public ValueLookupParameter<VariableCollection> ResultsParameter {
73      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
74    }
75    public ValueLookupParameter<IOperator> AnalyzerParameter {
76      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
77    }
[5356]78    public LookupParameter<IntValue> EvaluatedSolutionsParameter {
79      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
80    }
[12276]81    public IValueLookupParameter<BoolValue> DominateOnEqualQualitiesParameter {
82      get { return (ValueLookupParameter<BoolValue>)Parameters["DominateOnEqualQualities"]; }
83    }
[4017]84    #endregion
85
[4012]86    [StorableConstructor]
[4902]87    protected NSGA2MainLoop(bool deserializing) : base(deserializing) { }
[12276]88    [StorableHook(HookType.AfterDeserialization)]
89    private void AfterDeserialization() {
90      // BackwardsCompatibility3.3
91      #region Backwards compatible code, remove with 3.4
92      if (!Parameters.ContainsKey("DominateOnEqualQualities"))
93        Parameters.Add(new ValueLookupParameter<BoolValue>("DominateOnEqualQualities", "Flag which determines wether solutions with equal quality values should be treated as dominated."));
94      #endregion
95    }
96
[4902]97    protected NSGA2MainLoop(NSGA2MainLoop original, Cloner cloner) : base(original, cloner) { }
[4012]98    public NSGA2MainLoop()
99      : base() {
100      Initialize();
101    }
102
103    private void Initialize() {
[4017]104      #region Create parameters
105      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
[4045]106      Parameters.Add(new ValueLookupParameter<BoolArray>("Maximization", "True if an objective should be maximized, or false if it should be minimized."));
107      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The vector of quality values."));
108      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The population size."));
[4017]109      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
110      Parameters.Add(new ValueLookupParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on a solution."));
111      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
112      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
113      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
[5208]114      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
[4017]115      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
116      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
117      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
[5356]118      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
[12276]119      Parameters.Add(new ValueLookupParameter<BoolValue>("DominateOnEqualQualities", "Flag which determines wether solutions with equal quality values should be treated as dominated."));
[4012]120      #endregion
121
[4017]122      #region Create operators
123      VariableCreator variableCreator = new VariableCreator();
124      ResultsCollector resultsCollector1 = new ResultsCollector();
125      Placeholder analyzer1 = new Placeholder();
126      Placeholder selector = new Placeholder();
127      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
128      ChildrenCreator childrenCreator = new ChildrenCreator();
[5208]129      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
[4045]130      StochasticBranch crossoverStochasticBranch = new StochasticBranch();
[4017]131      Placeholder crossover = new Placeholder();
[5143]132      ParentCopyCrossover noCrossover = new ParentCopyCrossover();
[4045]133      StochasticBranch mutationStochasticBranch = new StochasticBranch();
[4017]134      Placeholder mutator = new Placeholder();
[5208]135      SubScopesRemover subScopesRemover = new SubScopesRemover();
136      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
[4017]137      Placeholder evaluator = new Placeholder();
[5356]138      SubScopesCounter subScopesCounter = new SubScopesCounter();
[4045]139      MergingReducer mergingReducer = new MergingReducer();
140      RankAndCrowdingSorter rankAndCrowdingSorter = new RankAndCrowdingSorter();
141      LeftSelector leftSelector = new LeftSelector();
[4017]142      RightReducer rightReducer = new RightReducer();
143      IntCounter intCounter = new IntCounter();
144      Comparator comparator = new Comparator();
145      Placeholder analyzer2 = new Placeholder();
146      ConditionalBranch conditionalBranch = new ConditionalBranch();
147
148      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
149
150      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
[4045]151      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
[4017]152
153      analyzer1.Name = "Analyzer";
[4045]154      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
[4017]155
156      selector.Name = "Selector";
[4045]157      selector.OperatorParameter.ActualName = SelectorParameter.Name;
[4017]158
159      childrenCreator.ParentsPerChild = new IntValue(2);
160
[4045]161      crossoverStochasticBranch.ProbabilityParameter.ActualName = CrossoverProbabilityParameter.Name;
162      crossoverStochasticBranch.RandomParameter.ActualName = RandomParameter.Name;
163
[4017]164      crossover.Name = "Crossover";
[4045]165      crossover.OperatorParameter.ActualName = CrossoverParameter.Name;
[4017]166
[4045]167      noCrossover.Name = "Clone parent";
168      noCrossover.RandomParameter.ActualName = RandomParameter.Name;
[4017]169
[4045]170      mutationStochasticBranch.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
171      mutationStochasticBranch.RandomParameter.ActualName = RandomParameter.Name;
172
[4017]173      mutator.Name = "Mutator";
[4045]174      mutator.OperatorParameter.ActualName = MutatorParameter.Name;
[4017]175
[5208]176      subScopesRemover.RemoveAllSubScopes = true;
177
178      uniformSubScopesProcessor2.Parallel.Value = true;
179
[4017]180      evaluator.Name = "Evaluator";
[4045]181      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
[4017]182
[5356]183      subScopesCounter.Name = "Increment EvaluatedSolutions";
184      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
185
[12276]186      rankAndCrowdingSorter.DominateOnEqualQualitiesParameter.ActualName = DominateOnEqualQualitiesParameter.Name;
[4045]187      rankAndCrowdingSorter.CrowdingDistanceParameter.ActualName = "CrowdingDistance";
188      rankAndCrowdingSorter.RankParameter.ActualName = "Rank";
[4017]189
[4045]190      leftSelector.CopySelected = new BoolValue(false);
191      leftSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
192
[4017]193      intCounter.Increment = new IntValue(1);
194      intCounter.ValueParameter.ActualName = "Generations";
195
196      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
197      comparator.LeftSideParameter.ActualName = "Generations";
198      comparator.ResultParameter.ActualName = "Terminate";
[4045]199      comparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
[4017]200
201      analyzer2.Name = "Analyzer";
202      analyzer2.OperatorParameter.ActualName = "Analyzer";
203
204      conditionalBranch.ConditionParameter.ActualName = "Terminate";
[4012]205      #endregion
206
[4017]207      #region Create operator graph
208      OperatorGraph.InitialOperator = variableCreator;
209      variableCreator.Successor = resultsCollector1;
210      resultsCollector1.Successor = analyzer1;
211      analyzer1.Successor = selector;
212      selector.Successor = subScopesProcessor1;
213      subScopesProcessor1.Operators.Add(new EmptyOperator());
214      subScopesProcessor1.Operators.Add(childrenCreator);
[4045]215      subScopesProcessor1.Successor = mergingReducer;
[5208]216      childrenCreator.Successor = uniformSubScopesProcessor1;
217      uniformSubScopesProcessor1.Operator = crossoverStochasticBranch;
218      uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
[4045]219      crossoverStochasticBranch.FirstBranch = crossover;
220      crossoverStochasticBranch.SecondBranch = noCrossover;
221      crossoverStochasticBranch.Successor = mutationStochasticBranch;
222      crossover.Successor = null;
223      noCrossover.Successor = null;
224      mutationStochasticBranch.FirstBranch = mutator;
225      mutationStochasticBranch.SecondBranch = null;
[5208]226      mutationStochasticBranch.Successor = subScopesRemover;
[4017]227      mutator.Successor = null;
228      subScopesRemover.Successor = null;
[5208]229      uniformSubScopesProcessor2.Operator = evaluator;
[5356]230      uniformSubScopesProcessor2.Successor = subScopesCounter;
[5208]231      evaluator.Successor = null;
[5356]232      subScopesCounter.Successor = null;
[4045]233      mergingReducer.Successor = rankAndCrowdingSorter;
234      rankAndCrowdingSorter.Successor = leftSelector;
235      leftSelector.Successor = rightReducer;
236      rightReducer.Successor = intCounter;
[4017]237      intCounter.Successor = comparator;
[5356]238      comparator.Successor = analyzer2;
[4017]239      analyzer2.Successor = conditionalBranch;
240      conditionalBranch.FalseBranch = selector;
241      conditionalBranch.TrueBranch = null;
242      conditionalBranch.Successor = null;
[4012]243      #endregion
244    }
[4902]245
246    public override IDeepCloneable Clone(Cloner cloner) {
247      return new NSGA2MainLoop(this, cloner);
248    }
[4012]249  }
250}
Note: See TracBrowser for help on using the repository browser.