Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/HeuristicLab.GP.StructureIdentification/3.3/StandardGP.cs @ 3921

Last change on this file since 3921 was 2161, checked in by gkronber, 15 years ago

Implemented #704 (Shuffling of training data (only) in (non-timeseries) modeling algorithms)

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