Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/StandardGP.cs @ 2285

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

Worked on #722 (IModel should provide a Predict() method to get predicted values for an input vector).
At the same time removed parameter PunishmentFactor from GP algorithms (this parameter is internal to TreeEvaluators now).

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