Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.NSGA2/3.3/NSGA2MainLoop.cs @ 5313

Last change on this file since 5313 was 5208, checked in by swagner, 14 years ago

Adapted EAs to enable parallel solution evaluation (#1333)

File size: 11.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Selection;
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 {
38    #region Parameter properties
39    public ValueLookupParameter<IRandom> RandomParameter {
40      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
41    }
42    public ValueLookupParameter<BoolArray> MaximizationParameter {
43      get { return (ValueLookupParameter<BoolArray>)Parameters["Maximization"]; }
44    }
45    public ScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
46      get { return (ScopeTreeLookupParameter<DoubleArray>)Parameters["Qualities"]; }
47    }
48    public ValueLookupParameter<IntValue> PopulationSizeParameter {
49      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
50    }
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    }
78    #endregion
79
80    [StorableConstructor]
81    protected NSGA2MainLoop(bool deserializing) : base(deserializing) { }
82    protected NSGA2MainLoop(NSGA2MainLoop original, Cloner cloner) : base(original, cloner) { }
83    public NSGA2MainLoop()
84      : base() {
85      Initialize();
86    }
87
88    private void Initialize() {
89      #region Create parameters
90      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
91      Parameters.Add(new ValueLookupParameter<BoolArray>("Maximization", "True if an objective should be maximized, or false if it should be minimized."));
92      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The vector of quality values."));
93      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The population size."));
94      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
95      Parameters.Add(new ValueLookupParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on a solution."));
96      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
97      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
98      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
99      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."));
100      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
101      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
102      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
103      #endregion
104
105      #region Create operators
106      VariableCreator variableCreator = new VariableCreator();
107      ResultsCollector resultsCollector1 = new ResultsCollector();
108      Placeholder analyzer1 = new Placeholder();
109      Placeholder selector = new Placeholder();
110      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
111      ChildrenCreator childrenCreator = new ChildrenCreator();
112      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
113      StochasticBranch crossoverStochasticBranch = new StochasticBranch();
114      Placeholder crossover = new Placeholder();
115      ParentCopyCrossover noCrossover = new ParentCopyCrossover();
116      StochasticBranch mutationStochasticBranch = new StochasticBranch();
117      Placeholder mutator = new Placeholder();
118      SubScopesRemover subScopesRemover = new SubScopesRemover();
119      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
120      Placeholder evaluator = new Placeholder();
121      MergingReducer mergingReducer = new MergingReducer();
122      RankAndCrowdingSorter rankAndCrowdingSorter = new RankAndCrowdingSorter();
123      LeftSelector leftSelector = new LeftSelector();
124      RightReducer rightReducer = new RightReducer();
125      IntCounter intCounter = new IntCounter();
126      Comparator comparator = new Comparator();
127      ResultsCollector resultsCollector2 = new ResultsCollector();
128      Placeholder analyzer2 = new Placeholder();
129      ConditionalBranch conditionalBranch = new ConditionalBranch();
130
131      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
132
133      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
134      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
135
136      analyzer1.Name = "Analyzer";
137      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
138
139      selector.Name = "Selector";
140      selector.OperatorParameter.ActualName = SelectorParameter.Name;
141
142      childrenCreator.ParentsPerChild = new IntValue(2);
143
144      crossoverStochasticBranch.ProbabilityParameter.ActualName = CrossoverProbabilityParameter.Name;
145      crossoverStochasticBranch.RandomParameter.ActualName = RandomParameter.Name;
146
147      crossover.Name = "Crossover";
148      crossover.OperatorParameter.ActualName = CrossoverParameter.Name;
149
150      noCrossover.Name = "Clone parent";
151      noCrossover.RandomParameter.ActualName = RandomParameter.Name;
152
153      mutationStochasticBranch.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
154      mutationStochasticBranch.RandomParameter.ActualName = RandomParameter.Name;
155
156      mutator.Name = "Mutator";
157      mutator.OperatorParameter.ActualName = MutatorParameter.Name;
158
159      subScopesRemover.RemoveAllSubScopes = true;
160
161      uniformSubScopesProcessor2.Parallel.Value = true;
162
163      evaluator.Name = "Evaluator";
164      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
165
166      rankAndCrowdingSorter.CrowdingDistanceParameter.ActualName = "CrowdingDistance";
167      rankAndCrowdingSorter.RankParameter.ActualName = "Rank";
168
169      leftSelector.CopySelected = new BoolValue(false);
170      leftSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
171
172      intCounter.Increment = new IntValue(1);
173      intCounter.ValueParameter.ActualName = "Generations";
174
175      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
176      comparator.LeftSideParameter.ActualName = "Generations";
177      comparator.ResultParameter.ActualName = "Terminate";
178      comparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
179
180      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
181      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
182
183      analyzer2.Name = "Analyzer";
184      analyzer2.OperatorParameter.ActualName = "Analyzer";
185
186      conditionalBranch.ConditionParameter.ActualName = "Terminate";
187      #endregion
188
189      #region Create operator graph
190      OperatorGraph.InitialOperator = variableCreator;
191      variableCreator.Successor = resultsCollector1;
192      resultsCollector1.Successor = analyzer1;
193      analyzer1.Successor = selector;
194      selector.Successor = subScopesProcessor1;
195      subScopesProcessor1.Operators.Add(new EmptyOperator());
196      subScopesProcessor1.Operators.Add(childrenCreator);
197      subScopesProcessor1.Successor = mergingReducer;
198      childrenCreator.Successor = uniformSubScopesProcessor1;
199      uniformSubScopesProcessor1.Operator = crossoverStochasticBranch;
200      uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
201      crossoverStochasticBranch.FirstBranch = crossover;
202      crossoverStochasticBranch.SecondBranch = noCrossover;
203      crossoverStochasticBranch.Successor = mutationStochasticBranch;
204      crossover.Successor = null;
205      noCrossover.Successor = null;
206      mutationStochasticBranch.FirstBranch = mutator;
207      mutationStochasticBranch.SecondBranch = null;
208      mutationStochasticBranch.Successor = subScopesRemover;
209      mutator.Successor = null;
210      subScopesRemover.Successor = null;
211      uniformSubScopesProcessor2.Operator = evaluator;
212      uniformSubScopesProcessor2.Successor = null;
213      evaluator.Successor = null;
214      mergingReducer.Successor = rankAndCrowdingSorter;
215      rankAndCrowdingSorter.Successor = leftSelector;
216      leftSelector.Successor = rightReducer;
217      rightReducer.Successor = intCounter;
218      intCounter.Successor = comparator;
219      comparator.Successor = resultsCollector2;
220      resultsCollector2.Successor = analyzer2;
221      analyzer2.Successor = conditionalBranch;
222      conditionalBranch.FalseBranch = selector;
223      conditionalBranch.TrueBranch = null;
224      conditionalBranch.Successor = null;
225      #endregion
226    }
227
228    public override IDeepCloneable Clone(Cloner cloner) {
229      return new NSGA2MainLoop(this, cloner);
230    }
231  }
232}
Note: See TracBrowser for help on using the repository browser.