1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using System.Xml;
|
---|
28 | using System.Diagnostics;
|
---|
29 | using HeuristicLab.DataAnalysis;
|
---|
30 | using HeuristicLab.Operators;
|
---|
31 | using HeuristicLab.Random;
|
---|
32 | using HeuristicLab.Selection;
|
---|
33 | using HeuristicLab.Logging;
|
---|
34 | using HeuristicLab.Data;
|
---|
35 | using HeuristicLab.Operators.Programmable;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
38 | public class StandardGP : ItemBase, IEditable {
|
---|
39 | private IntData maxGenerations = new IntData();
|
---|
40 | public int MaxGenerations {
|
---|
41 | get { return maxGenerations.Data; }
|
---|
42 | set { maxGenerations.Data = value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | private IntData tournamentSize = new IntData();
|
---|
46 | public int TournamentSize {
|
---|
47 | get { return tournamentSize.Data; }
|
---|
48 | set { tournamentSize.Data = value; }
|
---|
49 | }
|
---|
50 | private DoubleData mutationRate = new DoubleData();
|
---|
51 | public double MutationRate {
|
---|
52 | get { return mutationRate.Data; }
|
---|
53 | set { mutationRate.Data = value; }
|
---|
54 | }
|
---|
55 | private IntData parents = new IntData();
|
---|
56 | private IntData populationSize = new IntData();
|
---|
57 | public int PopulationSize {
|
---|
58 | get { return populationSize.Data; }
|
---|
59 | set {
|
---|
60 | populationSize.Data = value;
|
---|
61 | parents.Data = value * 2;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | private BoolData setSeedRandomly = new BoolData();
|
---|
66 | public bool SetSeedRandomly {
|
---|
67 | get { return setSeedRandomly.Data; }
|
---|
68 | set { setSeedRandomly.Data = value; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | private IntData seed = new IntData();
|
---|
72 | public int Seed {
|
---|
73 | get { return seed.Data; }
|
---|
74 | set { seed.Data = value; }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public IOperator ProblemInjector {
|
---|
78 | get { return algorithm.SubOperators[0]; }
|
---|
79 | set {
|
---|
80 | value.Name = "ProblemInjector";
|
---|
81 | algorithm.RemoveSubOperator(0);
|
---|
82 | algorithm.AddSubOperator(value, 0);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | private IntData elites = new IntData();
|
---|
87 | public int Elites {
|
---|
88 | get { return elites.Data; }
|
---|
89 | set { elites.Data = value; }
|
---|
90 | }
|
---|
91 |
|
---|
92 | private int maxTreeSize = 50;
|
---|
93 | public int MaxTreeSize {
|
---|
94 | get { return maxTreeSize; }
|
---|
95 | set { maxTreeSize = value; }
|
---|
96 | }
|
---|
97 |
|
---|
98 | private int maxTreeHeight = 8;
|
---|
99 | public int MaxTreeHeight {
|
---|
100 | get { return maxTreeHeight; }
|
---|
101 | set { maxTreeHeight = value; }
|
---|
102 | }
|
---|
103 | private double punishmentFactor = 10.0;
|
---|
104 | private bool useEstimatedTargetValue = false;
|
---|
105 | private double fullTreeShakingFactor = 0.1;
|
---|
106 | private double onepointShakingFactor = 1.0;
|
---|
107 | private IOperator algorithm;
|
---|
108 |
|
---|
109 | private SequentialEngine.SequentialEngine engine;
|
---|
110 | public IEngine Engine {
|
---|
111 | get { return engine; }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public StandardGP() {
|
---|
115 | PopulationSize = 10000;
|
---|
116 | MaxGenerations = 100;
|
---|
117 | TournamentSize = 7;
|
---|
118 | MutationRate = 0.15;
|
---|
119 | Elites = 1;
|
---|
120 | MaxTreeSize = 100;
|
---|
121 | MaxTreeHeight = 10;
|
---|
122 | engine = new SequentialEngine.SequentialEngine();
|
---|
123 | CombinedOperator algo = CreateAlgorithm();
|
---|
124 | engine.OperatorGraph.AddOperator(algo);
|
---|
125 | engine.OperatorGraph.InitialOperator = algo;
|
---|
126 | }
|
---|
127 |
|
---|
128 | private CombinedOperator CreateAlgorithm() {
|
---|
129 | CombinedOperator algo = new CombinedOperator();
|
---|
130 | algo.Name = "StandardGP";
|
---|
131 | SequentialProcessor seq = new SequentialProcessor();
|
---|
132 | EmptyOperator problemInjectorPlaceholder = new EmptyOperator();
|
---|
133 | RandomInjector randomInjector = new RandomInjector();
|
---|
134 | randomInjector.GetVariable("SetSeedRandomly").Value = setSeedRandomly;
|
---|
135 | randomInjector.GetVariable("Seed").Value = seed;
|
---|
136 | randomInjector.Name = "Random Injector";
|
---|
137 | VariableInjector globalInjector = CreateGlobalInjector();
|
---|
138 | CombinedOperator initialization = CreateInitialization();
|
---|
139 | initialization.Name = "Initialization";
|
---|
140 | FunctionLibraryInjector funLibInjector = new FunctionLibraryInjector();
|
---|
141 | CombinedOperator mainLoop = CreateMainLoop();
|
---|
142 | mainLoop.Name = "Main loop";
|
---|
143 |
|
---|
144 | ProbabilisticTreeCreator treeCreator = new ProbabilisticTreeCreator();
|
---|
145 | treeCreator.Name = "Tree generator";
|
---|
146 | treeCreator.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
147 | treeCreator.GetVariableInfo("MinTreeSize").Local = true;
|
---|
148 | treeCreator.AddVariable(new HeuristicLab.Core.Variable("MinTreeSize", new IntData(3)));
|
---|
149 | MeanSquaredErrorEvaluator evaluator = new MeanSquaredErrorEvaluator();
|
---|
150 | evaluator.GetVariableInfo("MSE").ActualName = "Quality";
|
---|
151 | evaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
|
---|
152 | evaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
|
---|
153 | evaluator.Name = "Evaluator";
|
---|
154 | StandardCrossOver crossover = new StandardCrossOver();
|
---|
155 | crossover.Name = "Crossover";
|
---|
156 | crossover.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
157 | CombinedOperator manipulator = CreateManipulator();
|
---|
158 | manipulator.Name = "Manipulator";
|
---|
159 | TournamentSelector selector = new TournamentSelector();
|
---|
160 | selector.Name = "Selector";
|
---|
161 | selector.GetVariableInfo("Selected").ActualName = "Parents";
|
---|
162 | selector.GetVariableInfo("GroupSize").Local = false;
|
---|
163 | selector.RemoveVariable("GroupSize");
|
---|
164 | selector.GetVariableInfo("GroupSize").ActualName = "TournamentSize";
|
---|
165 | LeftReducer cleanUp = new LeftReducer();
|
---|
166 |
|
---|
167 | seq.AddSubOperator(problemInjectorPlaceholder);
|
---|
168 | seq.AddSubOperator(randomInjector);
|
---|
169 | seq.AddSubOperator(globalInjector);
|
---|
170 | seq.AddSubOperator(funLibInjector);
|
---|
171 | seq.AddSubOperator(initialization);
|
---|
172 | seq.AddSubOperator(mainLoop);
|
---|
173 | seq.AddSubOperator(cleanUp);
|
---|
174 |
|
---|
175 | initialization.AddSubOperator(treeCreator);
|
---|
176 | initialization.AddSubOperator(evaluator);
|
---|
177 |
|
---|
178 | mainLoop.AddSubOperator(selector);
|
---|
179 | mainLoop.AddSubOperator(crossover);
|
---|
180 | mainLoop.AddSubOperator(manipulator);
|
---|
181 | mainLoop.AddSubOperator(evaluator);
|
---|
182 | algo.OperatorGraph.AddOperator(seq);
|
---|
183 | algo.OperatorGraph.InitialOperator = seq;
|
---|
184 | this.algorithm = seq;
|
---|
185 | return algo;
|
---|
186 | }
|
---|
187 |
|
---|
188 | private VariableInjector CreateGlobalInjector() {
|
---|
189 | VariableInjector injector = new VariableInjector();
|
---|
190 | injector.Name = "Global Injector";
|
---|
191 | injector.AddVariable(new HeuristicLab.Core.Variable("Generations", new IntData(0)));
|
---|
192 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxGenerations", maxGenerations));
|
---|
193 | injector.AddVariable(new HeuristicLab.Core.Variable("MutationRate", mutationRate));
|
---|
194 | injector.AddVariable(new HeuristicLab.Core.Variable("PopulationSize", populationSize));
|
---|
195 | injector.AddVariable(new HeuristicLab.Core.Variable("Parents", parents));
|
---|
196 | injector.AddVariable(new HeuristicLab.Core.Variable("Elites", elites));
|
---|
197 | injector.AddVariable(new HeuristicLab.Core.Variable("TournamentSize", tournamentSize));
|
---|
198 | injector.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false)));
|
---|
199 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new IntData(maxTreeHeight)));
|
---|
200 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new IntData(maxTreeSize)));
|
---|
201 | injector.AddVariable(new HeuristicLab.Core.Variable("EvaluatedSolutions", new IntData(0)));
|
---|
202 | injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0)));
|
---|
203 | injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData(punishmentFactor)));
|
---|
204 | injector.AddVariable(new HeuristicLab.Core.Variable("UseEstimatedTargetValue", new BoolData(useEstimatedTargetValue)));
|
---|
205 | return injector;
|
---|
206 | }
|
---|
207 |
|
---|
208 | private CombinedOperator CreateManipulator() {
|
---|
209 | CombinedOperator manipulator = new CombinedOperator();
|
---|
210 | StochasticMultiBranch multibranch = new StochasticMultiBranch();
|
---|
211 | FullTreeShaker fullTreeShaker = new FullTreeShaker();
|
---|
212 | fullTreeShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
213 | fullTreeShaker.GetVariableInfo("ShakingFactor").Local = true;
|
---|
214 | fullTreeShaker.AddVariable(new HeuristicLab.Core.Variable("ShakingFactor", new DoubleData(fullTreeShakingFactor)));
|
---|
215 |
|
---|
216 | OnePointShaker onepointShaker = new OnePointShaker();
|
---|
217 | onepointShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
218 | onepointShaker.GetVariableInfo("ShakingFactor").Local = true;
|
---|
219 | onepointShaker.AddVariable(new HeuristicLab.Core.Variable("ShakingFactor", new DoubleData(onepointShakingFactor)));
|
---|
220 | ChangeNodeTypeManipulation changeNodeTypeManipulation = new ChangeNodeTypeManipulation();
|
---|
221 | changeNodeTypeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
222 | CutOutNodeManipulation cutOutNodeManipulation = new CutOutNodeManipulation();
|
---|
223 | cutOutNodeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
224 | DeleteSubTreeManipulation deleteSubTreeManipulation = new DeleteSubTreeManipulation();
|
---|
225 | deleteSubTreeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
226 | SubstituteSubTreeManipulation substituteSubTreeManipulation = new SubstituteSubTreeManipulation();
|
---|
227 | substituteSubTreeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
228 |
|
---|
229 | IOperator[] manipulators = new IOperator[] {
|
---|
230 | onepointShaker, fullTreeShaker,
|
---|
231 | changeNodeTypeManipulation,
|
---|
232 | cutOutNodeManipulation,
|
---|
233 | deleteSubTreeManipulation,
|
---|
234 | substituteSubTreeManipulation};
|
---|
235 |
|
---|
236 | DoubleArrayData probabilities = new DoubleArrayData(new double[manipulators.Length]);
|
---|
237 | for (int i = 0; i < manipulators.Length; i++) {
|
---|
238 | probabilities.Data[i] = 1.0;
|
---|
239 | multibranch.AddSubOperator(manipulators[i]);
|
---|
240 | }
|
---|
241 | multibranch.GetVariableInfo("Probabilities").Local = true;
|
---|
242 | multibranch.AddVariable(new HeuristicLab.Core.Variable("Probabilities", probabilities));
|
---|
243 |
|
---|
244 | manipulator.OperatorGraph.AddOperator(multibranch);
|
---|
245 | manipulator.OperatorGraph.InitialOperator = multibranch;
|
---|
246 | return manipulator;
|
---|
247 | }
|
---|
248 |
|
---|
249 | private CombinedOperator CreateInitialization() {
|
---|
250 | CombinedOperator init = new CombinedOperator();
|
---|
251 | SequentialProcessor seq = new SequentialProcessor();
|
---|
252 | SubScopesCreater subScopesCreater = new SubScopesCreater();
|
---|
253 | subScopesCreater.GetVariableInfo("SubScopes").ActualName = "PopulationSize";
|
---|
254 | UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
|
---|
255 | SequentialProcessor individualSeq = new SequentialProcessor();
|
---|
256 | OperatorExtractor treeCreater = new OperatorExtractor();
|
---|
257 | treeCreater.Name = "Tree generator (extr.)";
|
---|
258 | treeCreater.GetVariableInfo("Operator").ActualName = "Tree generator";
|
---|
259 | OperatorExtractor evaluator = new OperatorExtractor();
|
---|
260 | evaluator.Name = "Evaluator (extr.)";
|
---|
261 | evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
|
---|
262 | MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
|
---|
263 | validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
|
---|
264 | validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
265 | validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
266 | Counter evalCounter = new Counter();
|
---|
267 | evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
268 | Sorter sorter = new Sorter();
|
---|
269 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
270 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
271 |
|
---|
272 | seq.AddSubOperator(subScopesCreater);
|
---|
273 | seq.AddSubOperator(subScopesProc);
|
---|
274 | seq.AddSubOperator(sorter);
|
---|
275 |
|
---|
276 | subScopesProc.AddSubOperator(individualSeq);
|
---|
277 | individualSeq.AddSubOperator(treeCreater);
|
---|
278 | individualSeq.AddSubOperator(evaluator);
|
---|
279 | individualSeq.AddSubOperator(validationEvaluator);
|
---|
280 | individualSeq.AddSubOperator(evalCounter);
|
---|
281 |
|
---|
282 | init.OperatorGraph.AddOperator(seq);
|
---|
283 | init.OperatorGraph.InitialOperator = seq;
|
---|
284 | return init;
|
---|
285 | }
|
---|
286 |
|
---|
287 | private CombinedOperator CreateMainLoop() {
|
---|
288 | CombinedOperator main = new CombinedOperator();
|
---|
289 | SequentialProcessor seq = new SequentialProcessor();
|
---|
290 | CombinedOperator childCreater = CreateChildCreater();
|
---|
291 | childCreater.Name = "Create children";
|
---|
292 | CombinedOperator replacement = CreateReplacement();
|
---|
293 | replacement.Name = "Replacement";
|
---|
294 | BestSolutionStorer solutionStorer = CreateBestSolutionStorer();
|
---|
295 | BestAverageWorstQualityCalculator qualityCalculator = new BestAverageWorstQualityCalculator();
|
---|
296 | BestAverageWorstQualityCalculator validationQualityCalculator = new BestAverageWorstQualityCalculator();
|
---|
297 | validationQualityCalculator.Name = "ValidationQualityCalculator";
|
---|
298 | validationQualityCalculator.GetVariableInfo("BestQuality").ActualName = "BestValidationQuality";
|
---|
299 | validationQualityCalculator.GetVariableInfo("AverageQuality").ActualName = "AverageValidationQuality";
|
---|
300 | validationQualityCalculator.GetVariableInfo("WorstQuality").ActualName = "WorstValidationQuality";
|
---|
301 | DataCollector collector = new DataCollector();
|
---|
302 | ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
303 | names.Add(new StringData("BestQuality"));
|
---|
304 | names.Add(new StringData("AverageQuality"));
|
---|
305 | names.Add(new StringData("WorstQuality"));
|
---|
306 | names.Add(new StringData("BestValidationQuality"));
|
---|
307 | names.Add(new StringData("AverageValidationQuality"));
|
---|
308 | names.Add(new StringData("WorstValidationQuality"));
|
---|
309 | LinechartInjector lineChartInjector = new LinechartInjector();
|
---|
310 | lineChartInjector.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
|
---|
311 | lineChartInjector.GetVariable("NumberOfLines").GetValue<IntData>().Data = 6;
|
---|
312 | QualityLogger qualityLogger = new QualityLogger();
|
---|
313 | QualityLogger validationQualityLogger = new QualityLogger();
|
---|
314 | validationQualityCalculator.Name = "ValidationQualityLogger";
|
---|
315 | validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
316 | validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
|
---|
317 | Counter counter = new Counter();
|
---|
318 | counter.GetVariableInfo("Value").ActualName = "Generations";
|
---|
319 | LessThanComparator comparator = new LessThanComparator();
|
---|
320 | comparator.GetVariableInfo("LeftSide").ActualName = "Generations";
|
---|
321 | comparator.GetVariableInfo("RightSide").ActualName = "MaxGenerations";
|
---|
322 | comparator.GetVariableInfo("Result").ActualName = "GenerationsCondition";
|
---|
323 | ConditionalBranch cond = new ConditionalBranch();
|
---|
324 | cond.GetVariableInfo("Condition").ActualName = "GenerationsCondition";
|
---|
325 |
|
---|
326 | seq.AddSubOperator(childCreater);
|
---|
327 | seq.AddSubOperator(replacement);
|
---|
328 | seq.AddSubOperator(solutionStorer);
|
---|
329 | seq.AddSubOperator(qualityCalculator);
|
---|
330 | seq.AddSubOperator(validationQualityCalculator);
|
---|
331 | seq.AddSubOperator(collector);
|
---|
332 | seq.AddSubOperator(lineChartInjector);
|
---|
333 | seq.AddSubOperator(qualityLogger);
|
---|
334 | seq.AddSubOperator(validationQualityLogger);
|
---|
335 | seq.AddSubOperator(counter);
|
---|
336 | seq.AddSubOperator(comparator);
|
---|
337 | seq.AddSubOperator(cond);
|
---|
338 | cond.AddSubOperator(seq);
|
---|
339 |
|
---|
340 | main.OperatorGraph.AddOperator(seq);
|
---|
341 | main.OperatorGraph.InitialOperator = seq;
|
---|
342 | return main;
|
---|
343 | }
|
---|
344 |
|
---|
345 | private BestSolutionStorer CreateBestSolutionStorer() {
|
---|
346 | BestSolutionStorer solutionStorer = new BestSolutionStorer();
|
---|
347 | solutionStorer.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
348 | solutionStorer.GetVariableInfo("BestSolution").ActualName = "BestValidationSolution";
|
---|
349 | SequentialProcessor bestSolutionProcessor = new SequentialProcessor();
|
---|
350 | MeanAbsolutePercentageErrorEvaluator trainingMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
|
---|
351 | trainingMapeEvaluator.Name = "TrainingMapeEvaluator";
|
---|
352 | trainingMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TrainingMAPE";
|
---|
353 | trainingMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
|
---|
354 | trainingMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
|
---|
355 | MeanAbsolutePercentageErrorEvaluator validationMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
|
---|
356 | validationMapeEvaluator.Name = "ValidationMapeEvaluator";
|
---|
357 | validationMapeEvaluator.GetVariableInfo("MAPE").ActualName = "ValidationMAPE";
|
---|
358 | validationMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
359 | validationMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
360 | ProgrammableOperator progOperator = new ProgrammableOperator();
|
---|
361 | progOperator.RemoveVariableInfo("Result");
|
---|
362 | progOperator.AddVariableInfo(new HeuristicLab.Core.VariableInfo("EvaluatedSolutions", "", typeof(IntData), VariableKind.In));
|
---|
363 | progOperator.Code = @"
|
---|
364 | int evalSolutions = EvaluatedSolutions.Data;
|
---|
365 | scope.AddVariable(new Variable(""EvaluatedSolutions"", new IntData(evalSolutions)));
|
---|
366 | ";
|
---|
367 | solutionStorer.AddSubOperator(bestSolutionProcessor);
|
---|
368 | bestSolutionProcessor.AddSubOperator(trainingMapeEvaluator);
|
---|
369 | bestSolutionProcessor.AddSubOperator(validationMapeEvaluator);
|
---|
370 | bestSolutionProcessor.AddSubOperator(progOperator);
|
---|
371 | return solutionStorer;
|
---|
372 | }
|
---|
373 |
|
---|
374 | private CombinedOperator CreateReplacement() {
|
---|
375 | CombinedOperator replacement = new CombinedOperator();
|
---|
376 | SequentialProcessor seq = new SequentialProcessor();
|
---|
377 | SequentialSubScopesProcessor seqScopeProc = new SequentialSubScopesProcessor();
|
---|
378 | SequentialProcessor selectedProc = new SequentialProcessor();
|
---|
379 | LeftSelector leftSelector = new LeftSelector();
|
---|
380 | leftSelector.GetVariableInfo("Selected").ActualName = "Elites";
|
---|
381 | RightReducer rightReducer = new RightReducer();
|
---|
382 |
|
---|
383 | SequentialProcessor remainingProc = new SequentialProcessor();
|
---|
384 | RightSelector rightSelector = new RightSelector();
|
---|
385 | rightSelector.GetVariableInfo("Selected").ActualName = "Elites";
|
---|
386 | LeftReducer leftReducer = new LeftReducer();
|
---|
387 | MergingReducer mergingReducer = new MergingReducer();
|
---|
388 | Sorter sorter = new Sorter();
|
---|
389 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
390 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
391 |
|
---|
392 | seq.AddSubOperator(seqScopeProc);
|
---|
393 | seqScopeProc.AddSubOperator(selectedProc);
|
---|
394 | selectedProc.AddSubOperator(leftSelector);
|
---|
395 | selectedProc.AddSubOperator(rightReducer);
|
---|
396 |
|
---|
397 | seqScopeProc.AddSubOperator(remainingProc);
|
---|
398 | remainingProc.AddSubOperator(rightSelector);
|
---|
399 | remainingProc.AddSubOperator(leftReducer);
|
---|
400 | seq.AddSubOperator(mergingReducer);
|
---|
401 | seq.AddSubOperator(sorter);
|
---|
402 | replacement.OperatorGraph.AddOperator(seq);
|
---|
403 | replacement.OperatorGraph.InitialOperator = seq;
|
---|
404 | return replacement;
|
---|
405 | }
|
---|
406 |
|
---|
407 | private CombinedOperator CreateChildCreater() {
|
---|
408 | CombinedOperator childCreater = new CombinedOperator();
|
---|
409 | SequentialProcessor seq = new SequentialProcessor();
|
---|
410 | OperatorExtractor selector = new OperatorExtractor();
|
---|
411 | selector.Name = "Selector (extr.)";
|
---|
412 | selector.GetVariableInfo("Operator").ActualName = "Selector";
|
---|
413 | SequentialSubScopesProcessor seqScopesProc = new SequentialSubScopesProcessor();
|
---|
414 | EmptyOperator emptyOpt = new EmptyOperator();
|
---|
415 | SequentialProcessor selectedProc = new SequentialProcessor();
|
---|
416 | OperatorExtractor crossover = new OperatorExtractor();
|
---|
417 | crossover.Name = "Crossover (extr.)";
|
---|
418 | crossover.GetVariableInfo("Operator").ActualName = "Crossover";
|
---|
419 | UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
|
---|
420 | SequentialProcessor individualSeqProc = new SequentialProcessor();
|
---|
421 | StochasticBranch cond = new StochasticBranch();
|
---|
422 | cond.GetVariableInfo("Probability").ActualName = "MutationRate";
|
---|
423 | OperatorExtractor manipulator = new OperatorExtractor();
|
---|
424 | manipulator.Name = "Manipulator (extr.)";
|
---|
425 | manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
|
---|
426 | OperatorExtractor evaluator = new OperatorExtractor();
|
---|
427 | evaluator.Name = "Evaluator (extr.)";
|
---|
428 | evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
|
---|
429 | MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
|
---|
430 | validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
|
---|
431 | validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
432 | validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
433 | Counter evalCounter = new Counter();
|
---|
434 | evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
435 |
|
---|
436 | Sorter sorter = new Sorter();
|
---|
437 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
438 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
439 |
|
---|
440 |
|
---|
441 | seq.AddSubOperator(selector);
|
---|
442 | seq.AddSubOperator(seqScopesProc);
|
---|
443 | seqScopesProc.AddSubOperator(emptyOpt);
|
---|
444 | seqScopesProc.AddSubOperator(selectedProc);
|
---|
445 | selectedProc.AddSubOperator(crossover);
|
---|
446 | selectedProc.AddSubOperator(individualProc);
|
---|
447 | individualProc.AddSubOperator(individualSeqProc);
|
---|
448 | individualSeqProc.AddSubOperator(cond);
|
---|
449 | cond.AddSubOperator(manipulator);
|
---|
450 | individualSeqProc.AddSubOperator(evaluator);
|
---|
451 | individualSeqProc.AddSubOperator(validationEvaluator);
|
---|
452 | individualSeqProc.AddSubOperator(evalCounter);
|
---|
453 | selectedProc.AddSubOperator(sorter);
|
---|
454 |
|
---|
455 | childCreater.OperatorGraph.AddOperator(seq);
|
---|
456 | childCreater.OperatorGraph.InitialOperator = seq;
|
---|
457 | return childCreater;
|
---|
458 | }
|
---|
459 |
|
---|
460 | public IEditor CreateEditor() {
|
---|
461 | return new StandardGpEditor(this);
|
---|
462 | }
|
---|
463 |
|
---|
464 | public override IView CreateView() {
|
---|
465 | return new StandardGpEditor(this);
|
---|
466 | }
|
---|
467 |
|
---|
468 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
469 | StandardGP clone = new StandardGP();
|
---|
470 | clonedObjects.Add(Guid, clone);
|
---|
471 | clone.engine = (SequentialEngine.SequentialEngine)Auxiliary.Clone(Engine, clonedObjects);
|
---|
472 | return clone;
|
---|
473 | }
|
---|
474 | #region SetReferences Method
|
---|
475 | private void SetReferences() {
|
---|
476 | // SGA
|
---|
477 | CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
|
---|
478 | // SequentialProcessor in SGA
|
---|
479 | algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
|
---|
480 | // RandomInjector
|
---|
481 | RandomInjector ri = (RandomInjector)algorithm.SubOperators[1];
|
---|
482 | setSeedRandomly = ri.GetVariable("SetSeedRandomly").GetValue<BoolData>();
|
---|
483 | seed = ri.GetVariable("Seed").GetValue<IntData>();
|
---|
484 | // VariableInjector
|
---|
485 | VariableInjector vi = (VariableInjector)algorithm.SubOperators[2];
|
---|
486 | populationSize = vi.GetVariable("PopulationSize").GetValue<IntData>();
|
---|
487 | parents = vi.GetVariable("Parents").GetValue<IntData>();
|
---|
488 | maxGenerations = vi.GetVariable("MaxGenerations").GetValue<IntData>();
|
---|
489 | mutationRate = vi.GetVariable("MutationRate").GetValue<DoubleData>();
|
---|
490 | elites = vi.GetVariable("Elites").GetValue<IntData>();
|
---|
491 | }
|
---|
492 | #endregion
|
---|
493 |
|
---|
494 | #region Persistence Methods
|
---|
495 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
496 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
497 | node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
|
---|
498 | return node;
|
---|
499 | }
|
---|
500 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
501 | base.Populate(node, restoredObjects);
|
---|
502 | engine = (SequentialEngine.SequentialEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
|
---|
503 | SetReferences();
|
---|
504 | }
|
---|
505 | #endregion
|
---|
506 | }
|
---|
507 | }
|
---|