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