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.Analysis;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Selection;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.GeneticAlgorithm {
|
---|
33 | /// <summary>
|
---|
34 | /// An operator which represents the main loop of a genetic algorithm.
|
---|
35 | /// </summary>
|
---|
36 | [Item("GeneticAlgorithmMainLoop", "An operator which represents the main loop of a genetic algorithm.")]
|
---|
37 | [StorableClass]
|
---|
38 | public sealed class GeneticAlgorithmMainLoop : AlgorithmOperator {
|
---|
39 | #region Parameter properties
|
---|
40 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
41 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
42 | }
|
---|
43 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
44 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
45 | }
|
---|
46 | public SubScopesLookupParameter<DoubleValue> QualityParameter {
|
---|
47 | get { return (SubScopesLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
48 | }
|
---|
49 | public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
50 | get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
51 | }
|
---|
52 | public ValueLookupParameter<IOperator> SelectorParameter {
|
---|
53 | get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
|
---|
54 | }
|
---|
55 | public ValueLookupParameter<IOperator> CrossoverParameter {
|
---|
56 | get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
|
---|
57 | }
|
---|
58 | public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
|
---|
59 | get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
60 | }
|
---|
61 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
62 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
63 | }
|
---|
64 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
65 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
66 | }
|
---|
67 | public ValueLookupParameter<IntValue> ElitesParameter {
|
---|
68 | get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
|
---|
69 | }
|
---|
70 | public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
71 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
72 | }
|
---|
73 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
74 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
75 | }
|
---|
76 | public ValueLookupParameter<IOperator> VisualizerParameter {
|
---|
77 | get { return (ValueLookupParameter<IOperator>)Parameters["Visualizer"]; }
|
---|
78 | }
|
---|
79 | public LookupParameter<IItem> VisualizationParameter {
|
---|
80 | get { return (LookupParameter<IItem>)Parameters["Visualization"]; }
|
---|
81 | }
|
---|
82 | private ScopeParameter CurrentScopeParameter {
|
---|
83 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public IScope CurrentScope {
|
---|
87 | get { return CurrentScopeParameter.ActualValue; }
|
---|
88 | }
|
---|
89 | #endregion
|
---|
90 |
|
---|
91 | [StorableConstructor]
|
---|
92 | private GeneticAlgorithmMainLoop(bool deserializing) : base() { }
|
---|
93 | public GeneticAlgorithmMainLoop()
|
---|
94 | : base() {
|
---|
95 | Initialize();
|
---|
96 | }
|
---|
97 |
|
---|
98 | private void Initialize() {
|
---|
99 | #region Create parameters
|
---|
100 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
101 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
102 | Parameters.Add(new SubScopesLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
103 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
104 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
|
---|
105 | Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
|
---|
106 | Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
107 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
108 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
|
---|
109 | Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
|
---|
110 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
111 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
112 | Parameters.Add(new ValueLookupParameter<IOperator>("Visualizer", "The operator used to visualize solutions."));
|
---|
113 | Parameters.Add(new LookupParameter<IItem>("Visualization", "The item which represents the visualization of solutions."));
|
---|
114 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
|
---|
115 | #endregion
|
---|
116 |
|
---|
117 | #region Create operators
|
---|
118 | VariableCreator variableCreator = new VariableCreator();
|
---|
119 | BestQualityMemorizer bestQualityMemorizer1 = new BestQualityMemorizer();
|
---|
120 | BestQualityMemorizer bestQualityMemorizer2 = new BestQualityMemorizer();
|
---|
121 | BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator1 = new BestAverageWorstQualityCalculator();
|
---|
122 | DataTableValuesCollector dataTableValuesCollector1 = new DataTableValuesCollector();
|
---|
123 | QualityDifferenceCalculator qualityDifferenceCalculator1 = new QualityDifferenceCalculator();
|
---|
124 | Placeholder visualizer1 = new Placeholder();
|
---|
125 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
126 | Placeholder selector = new Placeholder();
|
---|
127 | SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
|
---|
128 | ChildrenCreator childrenCreator = new ChildrenCreator();
|
---|
129 | UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
|
---|
130 | Placeholder crossover = new Placeholder();
|
---|
131 | StochasticBranch stochasticBranch = new StochasticBranch();
|
---|
132 | Placeholder mutator = new Placeholder();
|
---|
133 | Placeholder evaluator = new Placeholder();
|
---|
134 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
135 | SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
|
---|
136 | BestSelector bestSelector = new BestSelector();
|
---|
137 | RightReducer rightReducer = new RightReducer();
|
---|
138 | MergingReducer mergingReducer = new MergingReducer();
|
---|
139 | IntCounter intCounter = new IntCounter();
|
---|
140 | Comparator comparator = new Comparator();
|
---|
141 | BestQualityMemorizer bestQualityMemorizer3 = new BestQualityMemorizer();
|
---|
142 | BestQualityMemorizer bestQualityMemorizer4 = new BestQualityMemorizer();
|
---|
143 | BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator2 = new BestAverageWorstQualityCalculator();
|
---|
144 | DataTableValuesCollector dataTableValuesCollector2 = new DataTableValuesCollector();
|
---|
145 | QualityDifferenceCalculator qualityDifferenceCalculator2 = new QualityDifferenceCalculator();
|
---|
146 | Placeholder visualizer2 = new Placeholder();
|
---|
147 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
148 |
|
---|
149 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
|
---|
150 |
|
---|
151 | bestQualityMemorizer1.BestQualityParameter.ActualName = "BestQuality";
|
---|
152 | bestQualityMemorizer1.MaximizationParameter.ActualName = "Maximization";
|
---|
153 | bestQualityMemorizer1.QualityParameter.ActualName = "Quality";
|
---|
154 |
|
---|
155 | bestQualityMemorizer2.BestQualityParameter.ActualName = "BestKnownQuality";
|
---|
156 | bestQualityMemorizer2.MaximizationParameter.ActualName = "Maximization";
|
---|
157 | bestQualityMemorizer2.QualityParameter.ActualName = "Quality";
|
---|
158 |
|
---|
159 | bestAverageWorstQualityCalculator1.AverageQualityParameter.ActualName = "CurrentAverageQuality";
|
---|
160 | bestAverageWorstQualityCalculator1.BestQualityParameter.ActualName = "CurrentBestQuality";
|
---|
161 | bestAverageWorstQualityCalculator1.MaximizationParameter.ActualName = "Maximization";
|
---|
162 | bestAverageWorstQualityCalculator1.QualityParameter.ActualName = "Quality";
|
---|
163 | bestAverageWorstQualityCalculator1.WorstQualityParameter.ActualName = "CurrentWorstQuality";
|
---|
164 |
|
---|
165 | dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Best Quality", null, "CurrentBestQuality"));
|
---|
166 | dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Average Quality", null, "CurrentAverageQuality"));
|
---|
167 | dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Worst Quality", null, "CurrentWorstQuality"));
|
---|
168 | dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
|
---|
169 | dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, "BestKnownQuality"));
|
---|
170 | dataTableValuesCollector1.DataTableParameter.ActualName = "Qualities";
|
---|
171 |
|
---|
172 | qualityDifferenceCalculator1.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
|
---|
173 | qualityDifferenceCalculator1.FirstQualityParameter.ActualName = "BestKnownQuality";
|
---|
174 | qualityDifferenceCalculator1.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
|
---|
175 | qualityDifferenceCalculator1.SecondQualityParameter.ActualName = "BestQuality";
|
---|
176 |
|
---|
177 | visualizer1.Name = "Visualizer";
|
---|
178 | visualizer1.OperatorParameter.ActualName = "Visualizer";
|
---|
179 |
|
---|
180 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
181 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Best Quality", null, "CurrentBestQuality"));
|
---|
182 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Average Quality", null, "CurrentAverageQuality"));
|
---|
183 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Worst Quality", null, "CurrentWorstQuality"));
|
---|
184 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
|
---|
185 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, "BestKnownQuality"));
|
---|
186 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Absolute Difference of Best Known Quality to Best Quality", null, "AbsoluteDifferenceBestKnownToBest"));
|
---|
187 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Relative Difference of Best Known Quality to Best Quality", null, "RelativeDifferenceBestKnownToBest"));
|
---|
188 | resultsCollector.CollectedValues.Add(new LookupParameter<IItem>("Solution Visualization", null, "Visualization"));
|
---|
189 | resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Qualities"));
|
---|
190 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
191 |
|
---|
192 | selector.Name = "Selector";
|
---|
193 | selector.OperatorParameter.ActualName = "Selector";
|
---|
194 |
|
---|
195 | childrenCreator.ParentsPerChild = new IntValue(2);
|
---|
196 |
|
---|
197 | crossover.Name = "Crossover";
|
---|
198 | crossover.OperatorParameter.ActualName = "Crossover";
|
---|
199 |
|
---|
200 | stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
|
---|
201 | stochasticBranch.RandomParameter.ActualName = "Random";
|
---|
202 |
|
---|
203 | mutator.Name = "Mutator";
|
---|
204 | mutator.OperatorParameter.ActualName = "Mutator";
|
---|
205 |
|
---|
206 | evaluator.Name = "Evaluator";
|
---|
207 | evaluator.OperatorParameter.ActualName = "Evaluator";
|
---|
208 |
|
---|
209 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
210 |
|
---|
211 | bestSelector.CopySelected = new BoolValue(false);
|
---|
212 | bestSelector.MaximizationParameter.ActualName = "Maximization";
|
---|
213 | bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
|
---|
214 | bestSelector.QualityParameter.ActualName = "Quality";
|
---|
215 |
|
---|
216 | intCounter.Increment = new IntValue(1);
|
---|
217 | intCounter.ValueParameter.ActualName = "Generations";
|
---|
218 |
|
---|
219 | comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
220 | comparator.LeftSideParameter.ActualName = "Generations";
|
---|
221 | comparator.ResultParameter.ActualName = "Terminate";
|
---|
222 | comparator.RightSideParameter.ActualName = "MaximumGenerations";
|
---|
223 |
|
---|
224 | bestQualityMemorizer3.BestQualityParameter.ActualName = "BestQuality";
|
---|
225 | bestQualityMemorizer3.MaximizationParameter.ActualName = "Maximization";
|
---|
226 | bestQualityMemorizer3.QualityParameter.ActualName = "Quality";
|
---|
227 |
|
---|
228 | bestQualityMemorizer4.BestQualityParameter.ActualName = "BestKnownQuality";
|
---|
229 | bestQualityMemorizer4.MaximizationParameter.ActualName = "Maximization";
|
---|
230 | bestQualityMemorizer4.QualityParameter.ActualName = "Quality";
|
---|
231 |
|
---|
232 | bestAverageWorstQualityCalculator2.AverageQualityParameter.ActualName = "CurrentAverageQuality";
|
---|
233 | bestAverageWorstQualityCalculator2.BestQualityParameter.ActualName = "CurrentBestQuality";
|
---|
234 | bestAverageWorstQualityCalculator2.MaximizationParameter.ActualName = "Maximization";
|
---|
235 | bestAverageWorstQualityCalculator2.QualityParameter.ActualName = "Quality";
|
---|
236 | bestAverageWorstQualityCalculator2.WorstQualityParameter.ActualName = "CurrentWorstQuality";
|
---|
237 |
|
---|
238 | dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Best Quality", null, "CurrentBestQuality"));
|
---|
239 | dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Average Quality", null, "CurrentAverageQuality"));
|
---|
240 | dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Worst Quality", null, "CurrentWorstQuality"));
|
---|
241 | dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
|
---|
242 | dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, "BestKnownQuality"));
|
---|
243 | dataTableValuesCollector2.DataTableParameter.ActualName = "Qualities";
|
---|
244 |
|
---|
245 | qualityDifferenceCalculator2.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
|
---|
246 | qualityDifferenceCalculator2.FirstQualityParameter.ActualName = "BestKnownQuality";
|
---|
247 | qualityDifferenceCalculator2.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
|
---|
248 | qualityDifferenceCalculator2.SecondQualityParameter.ActualName = "BestQuality";
|
---|
249 |
|
---|
250 | visualizer2.Name = "Visualizer";
|
---|
251 | visualizer2.OperatorParameter.ActualName = "Visualizer";
|
---|
252 |
|
---|
253 | conditionalBranch.ConditionParameter.ActualName = "Terminate";
|
---|
254 | #endregion
|
---|
255 |
|
---|
256 | #region Create operator graph
|
---|
257 | OperatorGraph.InitialOperator = variableCreator;
|
---|
258 | variableCreator.Successor = bestQualityMemorizer1;
|
---|
259 | bestQualityMemorizer1.Successor = bestQualityMemorizer2;
|
---|
260 | bestQualityMemorizer2.Successor = bestAverageWorstQualityCalculator1;
|
---|
261 | bestAverageWorstQualityCalculator1.Successor = dataTableValuesCollector1;
|
---|
262 | dataTableValuesCollector1.Successor = qualityDifferenceCalculator1;
|
---|
263 | qualityDifferenceCalculator1.Successor = visualizer1;
|
---|
264 | visualizer1.Successor = resultsCollector;
|
---|
265 | resultsCollector.Successor = selector;
|
---|
266 | selector.Successor = subScopesProcessor1;
|
---|
267 | subScopesProcessor1.Operators.Add(new EmptyOperator());
|
---|
268 | subScopesProcessor1.Operators.Add(childrenCreator);
|
---|
269 | subScopesProcessor1.Successor = subScopesProcessor2;
|
---|
270 | childrenCreator.Successor = uniformSubScopesProcessor;
|
---|
271 | uniformSubScopesProcessor.Operator = crossover;
|
---|
272 | uniformSubScopesProcessor.Successor = null;
|
---|
273 | crossover.Successor = stochasticBranch;
|
---|
274 | stochasticBranch.FirstBranch = mutator;
|
---|
275 | stochasticBranch.SecondBranch = null;
|
---|
276 | stochasticBranch.Successor = evaluator;
|
---|
277 | mutator.Successor = null;
|
---|
278 | evaluator.Successor = subScopesRemover;
|
---|
279 | subScopesRemover.Successor = null;
|
---|
280 | subScopesProcessor2.Operators.Add(bestSelector);
|
---|
281 | subScopesProcessor2.Operators.Add(new EmptyOperator());
|
---|
282 | subScopesProcessor2.Successor = mergingReducer;
|
---|
283 | bestSelector.Successor = rightReducer;
|
---|
284 | rightReducer.Successor = null;
|
---|
285 | mergingReducer.Successor = intCounter;
|
---|
286 | intCounter.Successor = comparator;
|
---|
287 | comparator.Successor = bestQualityMemorizer3;
|
---|
288 | bestQualityMemorizer3.Successor = bestQualityMemorizer4;
|
---|
289 | bestQualityMemorizer4.Successor = bestAverageWorstQualityCalculator2;
|
---|
290 | bestAverageWorstQualityCalculator2.Successor = dataTableValuesCollector2;
|
---|
291 | dataTableValuesCollector2.Successor = qualityDifferenceCalculator2;
|
---|
292 | qualityDifferenceCalculator2.Successor = visualizer2;
|
---|
293 | visualizer2.Successor = conditionalBranch;
|
---|
294 | conditionalBranch.FalseBranch = selector;
|
---|
295 | conditionalBranch.TrueBranch = null;
|
---|
296 | conditionalBranch.Successor = null;
|
---|
297 | #endregion
|
---|
298 | }
|
---|
299 | }
|
---|
300 | }
|
---|