Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4035 was 4017, checked in by abeham, 14 years ago

#1040

  • Ported CrowdingDistanceAssignment operator
  • Added parameters to the alg and the main loop
    • main loop behavior is copied from the GA
File size: 10.1 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.Core;
23using HeuristicLab.Operators;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using HeuristicLab.Parameters;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Selection;
29
30namespace HeuristicLab.Algorithms.NSGA2 {
31  /// <summary>
32  /// An operator that represents the mainloop of the NSGA-II
33  /// </summary>
34  [Item("NSGA2MainLoop", "An operator which represents the main loop of the NSGA-II algorithm.")]
35  [StorableClass]
36  public class NSGA2MainLoop : AlgorithmOperator {
37    #region Parameter properties
38    public ValueLookupParameter<IRandom> RandomParameter {
39      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
40    }
41    public ValueLookupParameter<BoolValue> MaximizationParameter {
42      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
43    }
44    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
45      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
46    }
47    public ValueLookupParameter<IOperator> SelectorParameter {
48      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
49    }
50    public ValueLookupParameter<PercentValue> CrossoverProbabilityParameter {
51      get { return (ValueLookupParameter<PercentValue>)Parameters["CrossoverProbability"]; }
52    }
53    public ValueLookupParameter<IOperator> CrossoverParameter {
54      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
55    }
56    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
57      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
58    }
59    public ValueLookupParameter<IOperator> MutatorParameter {
60      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
61    }
62    public ValueLookupParameter<IOperator> EvaluatorParameter {
63      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
64    }
65    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
66      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
67    }
68    public ValueLookupParameter<VariableCollection> ResultsParameter {
69      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
70    }
71    public ValueLookupParameter<IOperator> AnalyzerParameter {
72      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
73    }
74    #endregion
75
76    [StorableConstructor]
77    private NSGA2MainLoop(bool deserializing) : base() { }
78    public NSGA2MainLoop()
79      : base() {
80      Initialize();
81    }
82
83    private void Initialize() {
84      #region Create parameters
85      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
86      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
87      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
88      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
89      Parameters.Add(new ValueLookupParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on a solution."));
90      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
91      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
92      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
93      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
94      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
95      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
96      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
97      #endregion
98
99      #region Create operators
100      VariableCreator variableCreator = new VariableCreator();
101      ResultsCollector resultsCollector1 = new ResultsCollector();
102      Placeholder analyzer1 = new Placeholder();
103      Placeholder selector = new Placeholder();
104      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
105      ChildrenCreator childrenCreator = new ChildrenCreator();
106      UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
107      Placeholder crossover = new Placeholder();
108      StochasticBranch stochasticBranch = new StochasticBranch();
109      Placeholder mutator = new Placeholder();
110      Placeholder evaluator = new Placeholder();
111      SubScopesRemover subScopesRemover = new SubScopesRemover();
112      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
113      BestSelector bestSelector = new BestSelector();
114      RightReducer rightReducer = new RightReducer();
115      MergingReducer mergingReducer = new MergingReducer();
116      IntCounter intCounter = new IntCounter();
117      Comparator comparator = new Comparator();
118      ResultsCollector resultsCollector2 = new ResultsCollector();
119      Placeholder analyzer2 = new Placeholder();
120      ConditionalBranch conditionalBranch = new ConditionalBranch();
121
122      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
123
124      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
125      resultsCollector1.ResultsParameter.ActualName = "Results";
126
127      analyzer1.Name = "Analyzer";
128      analyzer1.OperatorParameter.ActualName = "Analyzer";
129
130      selector.Name = "Selector";
131      selector.OperatorParameter.ActualName = "Selector";
132
133      childrenCreator.ParentsPerChild = new IntValue(2);
134
135      crossover.Name = "Crossover";
136      crossover.OperatorParameter.ActualName = "Crossover";
137
138      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
139      stochasticBranch.RandomParameter.ActualName = "Random";
140
141      mutator.Name = "Mutator";
142      mutator.OperatorParameter.ActualName = "Mutator";
143
144      evaluator.Name = "Evaluator";
145      evaluator.OperatorParameter.ActualName = "Evaluator";
146
147      subScopesRemover.RemoveAllSubScopes = true;
148
149      bestSelector.CopySelected = new BoolValue(false);
150      bestSelector.MaximizationParameter.ActualName = "Maximization";
151      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
152      bestSelector.QualityParameter.ActualName = "Quality";
153
154      intCounter.Increment = new IntValue(1);
155      intCounter.ValueParameter.ActualName = "Generations";
156
157      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
158      comparator.LeftSideParameter.ActualName = "Generations";
159      comparator.ResultParameter.ActualName = "Terminate";
160      comparator.RightSideParameter.ActualName = "MaximumGenerations";
161
162      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
163      resultsCollector2.ResultsParameter.ActualName = "Results";
164
165      analyzer2.Name = "Analyzer";
166      analyzer2.OperatorParameter.ActualName = "Analyzer";
167
168      conditionalBranch.ConditionParameter.ActualName = "Terminate";
169      #endregion
170
171      #region Create operator graph
172      OperatorGraph.InitialOperator = variableCreator;
173      variableCreator.Successor = resultsCollector1;
174      resultsCollector1.Successor = analyzer1;
175      analyzer1.Successor = selector;
176      selector.Successor = subScopesProcessor1;
177      subScopesProcessor1.Operators.Add(new EmptyOperator());
178      subScopesProcessor1.Operators.Add(childrenCreator);
179      subScopesProcessor1.Successor = subScopesProcessor2;
180      childrenCreator.Successor = uniformSubScopesProcessor;
181      uniformSubScopesProcessor.Operator = crossover;
182      uniformSubScopesProcessor.Successor = null;
183      crossover.Successor = stochasticBranch;
184      stochasticBranch.FirstBranch = mutator;
185      stochasticBranch.SecondBranch = null;
186      stochasticBranch.Successor = evaluator;
187      mutator.Successor = null;
188      evaluator.Successor = subScopesRemover;
189      subScopesRemover.Successor = null;
190      subScopesProcessor2.Operators.Add(bestSelector);
191      subScopesProcessor2.Operators.Add(new EmptyOperator());
192      subScopesProcessor2.Successor = mergingReducer;
193      bestSelector.Successor = rightReducer;
194      rightReducer.Successor = null;
195      mergingReducer.Successor = intCounter;
196      intCounter.Successor = comparator;
197      comparator.Successor = resultsCollector2;
198      resultsCollector2.Successor = analyzer2;
199      analyzer2.Successor = conditionalBranch;
200      conditionalBranch.FalseBranch = selector;
201      conditionalBranch.TrueBranch = null;
202      conditionalBranch.Successor = null;
203      #endregion
204    }
205
206    public override IOperation Apply() {
207      if (CrossoverParameter.ActualValue == null)
208        return null;
209      return base.Apply();
210    }
211  }
212}
Note: See TracBrowser for help on using the repository browser.