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 | using HeuristicLab.Modeling;
|
---|
37 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
38 |
|
---|
39 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
40 |
|
---|
41 | [EmptyStorableClass]
|
---|
42 | public class StandardGP : AlgorithmBase, IEditable {
|
---|
43 |
|
---|
44 | public override string Name { get { return "StandardGP"; } }
|
---|
45 |
|
---|
46 | public virtual int MaxGenerations {
|
---|
47 | get { return GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data; }
|
---|
48 | set { GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data = value; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public virtual int TournamentSize {
|
---|
52 | get { return GetVariableInjector().GetVariable("TournamentSize").GetValue<IntData>().Data; }
|
---|
53 | set { GetVariableInjector().GetVariable("TournamentSize").GetValue<IntData>().Data = value; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public double FullTreeShakingFactor {
|
---|
57 | get { return GetVariableInjector().GetVariable("FullTreeShakingFactor").GetValue<DoubleData>().Data; }
|
---|
58 | set { GetVariableInjector().GetVariable("FullTreeShakingFactor").GetValue<DoubleData>().Data = value; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public double OnePointShakingFactor {
|
---|
62 | get { return GetVariableInjector().GetVariable("OnePointShakingFactor").GetValue<DoubleData>().Data; }
|
---|
63 | set { GetVariableInjector().GetVariable("OnePointShakingFactor").GetValue<DoubleData>().Data = value; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public int MinInitialTreeSize {
|
---|
67 | get { return GetVariableInjector().GetVariable("MinInitialTreeSize").GetValue<IntData>().Data; }
|
---|
68 | set { GetVariableInjector().GetVariable("MinInitialTreeSize").GetValue<IntData>().Data = value; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public override int MaxTreeSize {
|
---|
72 | get {
|
---|
73 | return base.MaxTreeSize;
|
---|
74 | }
|
---|
75 | set {
|
---|
76 | base.MaxTreeSize = value;
|
---|
77 | MinInitialTreeSize = value / 2;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override int PopulationSize {
|
---|
82 | get {
|
---|
83 | return base.PopulationSize;
|
---|
84 | }
|
---|
85 | set {
|
---|
86 | base.PopulationSize = value;
|
---|
87 | Parents = 2 * value;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public StandardGP()
|
---|
92 | : base() {
|
---|
93 | PopulationSize = 10000;
|
---|
94 | MaxGenerations = 100;
|
---|
95 | TournamentSize = 7;
|
---|
96 | MutationRate = 0.15;
|
---|
97 | Elites = 1;
|
---|
98 | MaxTreeSize = 100;
|
---|
99 | MaxTreeHeight = 10;
|
---|
100 | FullTreeShakingFactor = 0.1;
|
---|
101 | OnePointShakingFactor = 1.0;
|
---|
102 | PunishmentFactor = 10.0;
|
---|
103 | UseEstimatedTargetValue = false;
|
---|
104 | SetSeedRandomly = true;
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected internal override IOperator CreateProblemInjector() {
|
---|
108 | return new ProblemInjector();
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected internal override IOperator CreateSelector() {
|
---|
112 | TournamentSelector selector = new TournamentSelector();
|
---|
113 | selector.Name = "Selector";
|
---|
114 | selector.GetVariableInfo("Selected").ActualName = "Parents";
|
---|
115 | selector.GetVariableInfo("GroupSize").Local = false;
|
---|
116 | selector.RemoveVariable("GroupSize");
|
---|
117 | selector.GetVariableInfo("GroupSize").ActualName = "TournamentSize";
|
---|
118 | return selector;
|
---|
119 | }
|
---|
120 |
|
---|
121 | protected internal override IOperator CreateGlobalInjector() {
|
---|
122 | VariableInjector globalInjector = (VariableInjector)base.CreateGlobalInjector();
|
---|
123 | globalInjector.AddVariable(new HeuristicLab.Core.Variable("TournamentSize", new IntData()));
|
---|
124 | globalInjector.AddVariable(new HeuristicLab.Core.Variable("MaxGenerations", new IntData()));
|
---|
125 | globalInjector.AddVariable(new HeuristicLab.Core.Variable("FullTreeShakingFactor", new DoubleData()));
|
---|
126 | globalInjector.AddVariable(new HeuristicLab.Core.Variable("OnePointShakingFactor", new DoubleData()));
|
---|
127 | globalInjector.AddVariable(new HeuristicLab.Core.Variable("MinInitialTreeSize", new IntData()));
|
---|
128 | return globalInjector;
|
---|
129 | }
|
---|
130 |
|
---|
131 | protected internal override IOperator CreateCrossover() {
|
---|
132 | StandardCrossOver crossover = new StandardCrossOver();
|
---|
133 | crossover.Name = "Crossover";
|
---|
134 | crossover.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
135 | return crossover;
|
---|
136 | }
|
---|
137 |
|
---|
138 | protected internal override IOperator CreateTreeCreator() {
|
---|
139 | ProbabilisticTreeCreator treeCreator = new ProbabilisticTreeCreator();
|
---|
140 | treeCreator.Name = "Tree generator";
|
---|
141 | treeCreator.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
142 | treeCreator.GetVariableInfo("MinTreeSize").ActualName = "MinInitialTreeSize";
|
---|
143 | return treeCreator;
|
---|
144 | }
|
---|
145 |
|
---|
146 | protected internal override IOperator CreateFunctionLibraryInjector() {
|
---|
147 | ConfigurableFunctionLibraryInjector funLibInjector = new ConfigurableFunctionLibraryInjector();
|
---|
148 | funLibInjector.GetVariableValue<BoolData>("Xor", null, false).Data = false;
|
---|
149 | funLibInjector.GetVariableValue<BoolData>("Average", null, false).Data = false;
|
---|
150 | return funLibInjector;
|
---|
151 | }
|
---|
152 |
|
---|
153 | protected internal override IOperator CreateManipulator() {
|
---|
154 | CombinedOperator manipulator = new CombinedOperator();
|
---|
155 | manipulator.Name = "Manipulator";
|
---|
156 | StochasticMultiBranch multibranch = new StochasticMultiBranch();
|
---|
157 | FullTreeShaker fullTreeShaker = new FullTreeShaker();
|
---|
158 | fullTreeShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
159 | fullTreeShaker.GetVariableInfo("ShakingFactor").ActualName = "FullTreeShakingFactor";
|
---|
160 |
|
---|
161 | OnePointShaker onepointShaker = new OnePointShaker();
|
---|
162 | onepointShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
163 | onepointShaker.GetVariableInfo("ShakingFactor").ActualName = "OnePointShakingFactor";
|
---|
164 | ChangeNodeTypeManipulation changeNodeTypeManipulation = new ChangeNodeTypeManipulation();
|
---|
165 | changeNodeTypeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
166 | CutOutNodeManipulation cutOutNodeManipulation = new CutOutNodeManipulation();
|
---|
167 | cutOutNodeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
168 | DeleteSubTreeManipulation deleteSubTreeManipulation = new DeleteSubTreeManipulation();
|
---|
169 | deleteSubTreeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
170 | SubstituteSubTreeManipulation substituteSubTreeManipulation = new SubstituteSubTreeManipulation();
|
---|
171 | substituteSubTreeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
172 |
|
---|
173 | IOperator[] manipulators = new IOperator[] {
|
---|
174 | onepointShaker, fullTreeShaker,
|
---|
175 | changeNodeTypeManipulation,
|
---|
176 | cutOutNodeManipulation,
|
---|
177 | deleteSubTreeManipulation,
|
---|
178 | substituteSubTreeManipulation};
|
---|
179 |
|
---|
180 | DoubleArrayData probabilities = new DoubleArrayData(new double[manipulators.Length]);
|
---|
181 | for (int i = 0; i < manipulators.Length; i++) {
|
---|
182 | probabilities.Data[i] = 1.0;
|
---|
183 | multibranch.AddSubOperator(manipulators[i]);
|
---|
184 | }
|
---|
185 | multibranch.GetVariableInfo("Probabilities").Local = true;
|
---|
186 | multibranch.AddVariable(new HeuristicLab.Core.Variable("Probabilities", probabilities));
|
---|
187 |
|
---|
188 | manipulator.OperatorGraph.AddOperator(multibranch);
|
---|
189 | manipulator.OperatorGraph.InitialOperator = multibranch;
|
---|
190 | return manipulator;
|
---|
191 | }
|
---|
192 |
|
---|
193 | protected internal override IOperator CreateBestSolutionProcessor() {
|
---|
194 | SequentialProcessor bestSolutionProcessor = new SequentialProcessor();
|
---|
195 | MeanSquaredErrorEvaluator testMseEvaluator = new MeanSquaredErrorEvaluator();
|
---|
196 | testMseEvaluator.Name = "TestMeanSquaredErrorEvaluator";
|
---|
197 | testMseEvaluator.GetVariableInfo("MSE").ActualName = "TestQuality";
|
---|
198 | testMseEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
|
---|
199 | testMseEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
|
---|
200 | MeanAbsolutePercentageErrorEvaluator trainingMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
|
---|
201 | trainingMapeEvaluator.Name = "TrainingMapeEvaluator";
|
---|
202 | trainingMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TrainingMAPE";
|
---|
203 | trainingMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
|
---|
204 | trainingMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
|
---|
205 | MeanAbsolutePercentageErrorEvaluator validationMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
|
---|
206 | validationMapeEvaluator.Name = "ValidationMapeEvaluator";
|
---|
207 | validationMapeEvaluator.GetVariableInfo("MAPE").ActualName = "ValidationMAPE";
|
---|
208 | validationMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
209 | validationMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
210 | MeanAbsolutePercentageErrorEvaluator testMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
|
---|
211 | testMapeEvaluator.Name = "TestMapeEvaluator";
|
---|
212 | testMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TestMAPE";
|
---|
213 | testMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
|
---|
214 | testMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
|
---|
215 | MeanAbsolutePercentageOfRangeErrorEvaluator trainingMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator();
|
---|
216 | trainingMapreEvaluator.Name = "TrainingMapreEvaluator";
|
---|
217 | trainingMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "TrainingMAPRE";
|
---|
218 | trainingMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
|
---|
219 | trainingMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
|
---|
220 | MeanAbsolutePercentageOfRangeErrorEvaluator validationMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator();
|
---|
221 | validationMapreEvaluator.Name = "ValidationMapreEvaluator";
|
---|
222 | validationMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "ValidationMAPRE";
|
---|
223 | validationMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
224 | validationMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
225 | MeanAbsolutePercentageOfRangeErrorEvaluator testMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator();
|
---|
226 | testMapreEvaluator.Name = "TestMapreEvaluator";
|
---|
227 | testMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "TestMAPRE";
|
---|
228 | testMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
|
---|
229 | testMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
|
---|
230 | CoefficientOfDeterminationEvaluator trainingR2Evaluator = new CoefficientOfDeterminationEvaluator();
|
---|
231 | trainingR2Evaluator.Name = "TrainingR2Evaluator";
|
---|
232 | trainingR2Evaluator.GetVariableInfo("R2").ActualName = "TrainingR2";
|
---|
233 | trainingR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
|
---|
234 | trainingR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
|
---|
235 | CoefficientOfDeterminationEvaluator validationR2Evaluator = new CoefficientOfDeterminationEvaluator();
|
---|
236 | validationR2Evaluator.Name = "ValidationR2Evaluator";
|
---|
237 | validationR2Evaluator.GetVariableInfo("R2").ActualName = "ValidationR2";
|
---|
238 | validationR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
239 | validationR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
240 | CoefficientOfDeterminationEvaluator testR2Evaluator = new CoefficientOfDeterminationEvaluator();
|
---|
241 | testR2Evaluator.Name = "TestR2Evaluator";
|
---|
242 | testR2Evaluator.GetVariableInfo("R2").ActualName = "TestR2";
|
---|
243 | testR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
|
---|
244 | testR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
|
---|
245 | VarianceAccountedForEvaluator trainingVAFEvaluator = new VarianceAccountedForEvaluator();
|
---|
246 | trainingVAFEvaluator.Name = "TrainingVAFEvaluator";
|
---|
247 | trainingVAFEvaluator.GetVariableInfo("VAF").ActualName = "TrainingVAF";
|
---|
248 | trainingVAFEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
|
---|
249 | trainingVAFEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
|
---|
250 | VarianceAccountedForEvaluator validationVAFEvaluator = new VarianceAccountedForEvaluator();
|
---|
251 | validationVAFEvaluator.Name = "ValidationVAFEvaluator";
|
---|
252 | validationVAFEvaluator.GetVariableInfo("VAF").ActualName = "ValidationVAF";
|
---|
253 | validationVAFEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
254 | validationVAFEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
255 | VarianceAccountedForEvaluator testVAFEvaluator = new VarianceAccountedForEvaluator();
|
---|
256 | testVAFEvaluator.Name = "TestVAFEvaluator";
|
---|
257 | testVAFEvaluator.GetVariableInfo("VAF").ActualName = "TestVAF";
|
---|
258 | testVAFEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
|
---|
259 | testVAFEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
|
---|
260 |
|
---|
261 | ProgrammableOperator progOperator = new ProgrammableOperator();
|
---|
262 | progOperator.RemoveVariableInfo("Result");
|
---|
263 | progOperator.AddVariableInfo(new HeuristicLab.Core.VariableInfo("EvaluatedSolutions", "", typeof(IntData), VariableKind.In));
|
---|
264 | progOperator.Code = @"
|
---|
265 | int evalSolutions = EvaluatedSolutions.Data;
|
---|
266 | scope.AddVariable(new Variable(""EvaluatedSolutions"", new IntData(evalSolutions)));
|
---|
267 | ";
|
---|
268 | bestSolutionProcessor.AddSubOperator(testMseEvaluator);
|
---|
269 | bestSolutionProcessor.AddSubOperator(trainingMapeEvaluator);
|
---|
270 | bestSolutionProcessor.AddSubOperator(validationMapeEvaluator);
|
---|
271 | bestSolutionProcessor.AddSubOperator(testMapeEvaluator);
|
---|
272 | bestSolutionProcessor.AddSubOperator(trainingMapreEvaluator);
|
---|
273 | bestSolutionProcessor.AddSubOperator(validationMapreEvaluator);
|
---|
274 | bestSolutionProcessor.AddSubOperator(testMapreEvaluator);
|
---|
275 | bestSolutionProcessor.AddSubOperator(trainingR2Evaluator);
|
---|
276 | bestSolutionProcessor.AddSubOperator(validationR2Evaluator);
|
---|
277 | bestSolutionProcessor.AddSubOperator(testR2Evaluator);
|
---|
278 | bestSolutionProcessor.AddSubOperator(trainingVAFEvaluator);
|
---|
279 | bestSolutionProcessor.AddSubOperator(validationVAFEvaluator);
|
---|
280 | bestSolutionProcessor.AddSubOperator(testVAFEvaluator);
|
---|
281 | bestSolutionProcessor.AddSubOperator(progOperator);
|
---|
282 | return bestSolutionProcessor;
|
---|
283 | }
|
---|
284 |
|
---|
285 | protected internal override IOperator CreateLoggingOperator() {
|
---|
286 | CombinedOperator loggingOperator = new CombinedOperator();
|
---|
287 | loggingOperator.Name = "Logging";
|
---|
288 | SequentialProcessor seq = new SequentialProcessor();
|
---|
289 |
|
---|
290 | DataCollector collector = new DataCollector();
|
---|
291 | ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
292 | names.Add(new StringData("BestQuality"));
|
---|
293 | names.Add(new StringData("AverageQuality"));
|
---|
294 | names.Add(new StringData("WorstQuality"));
|
---|
295 | names.Add(new StringData("BestValidationQuality"));
|
---|
296 | names.Add(new StringData("AverageValidationQuality"));
|
---|
297 | names.Add(new StringData("WorstValidationQuality"));
|
---|
298 | LinechartInjector lineChartInjector = new LinechartInjector();
|
---|
299 | lineChartInjector.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
|
---|
300 | lineChartInjector.GetVariable("NumberOfLines").GetValue<IntData>().Data = 6;
|
---|
301 | QualityLogger qualityLogger = new QualityLogger();
|
---|
302 | QualityLogger validationQualityLogger = new QualityLogger();
|
---|
303 | validationQualityLogger.Name = "ValidationQualityLogger";
|
---|
304 | validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
305 | validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
|
---|
306 |
|
---|
307 | seq.AddSubOperator(collector);
|
---|
308 | seq.AddSubOperator(lineChartInjector);
|
---|
309 | seq.AddSubOperator(qualityLogger);
|
---|
310 | seq.AddSubOperator(validationQualityLogger);
|
---|
311 |
|
---|
312 | loggingOperator.OperatorGraph.AddOperator(seq);
|
---|
313 | loggingOperator.OperatorGraph.InitialOperator = seq;
|
---|
314 | return loggingOperator;
|
---|
315 | }
|
---|
316 |
|
---|
317 | protected internal override Model CreateGPModel(IScope bestModelScope) {
|
---|
318 | Model model = base.CreateGPModel(bestModelScope);
|
---|
319 | model.TestMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("TestQuality", false).Data;
|
---|
320 | return model;
|
---|
321 | }
|
---|
322 |
|
---|
323 | public virtual IEditor CreateEditor() {
|
---|
324 | return new StandardGpEditor(this);
|
---|
325 | }
|
---|
326 |
|
---|
327 | public override IView CreateView() {
|
---|
328 | return new StandardGpEditor(this);
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|