Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2090 was 1922, checked in by gkronber, 16 years ago

#650 (IAlgorithm and derived interfaces should provide properties to retrieve results):

  • Implemented properties to retrieve model quality
  • Changed CEDMA executor to retrieve results via properties
  • Removed obsolete class Execution in CEDMA (replaced by the interface IAlgorithm)
File size: 19.0 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 StandardGP()
99      : base() {
100      PopulationSize = 10000;
101      MaxGenerations = 100;
102      TournamentSize = 7;
103      MutationRate = 0.15;
104      Elites = 1;
105      MaxTreeSize = 100;
106      MaxTreeHeight = 10;
107      FullTreeShakingFactor = 0.1;
108      OnePointShakingFactor = 1.0;
109      PunishmentFactor = 10.0;
110      UseEstimatedTargetValue = false;
111      SetSeedRandomly = true;
112    }
113
114    protected internal override IOperator CreateProblemInjector() {
115      return new ProblemInjector();
116    }
117
118    protected internal override IOperator CreateSelector() {
119      TournamentSelector selector = new TournamentSelector();
120      selector.Name = "Selector";
121      selector.GetVariableInfo("Selected").ActualName = "Parents";
122      selector.GetVariableInfo("GroupSize").Local = false;
123      selector.RemoveVariable("GroupSize");
124      selector.GetVariableInfo("GroupSize").ActualName = "TournamentSize";
125      return selector;
126    }
127
128    protected internal override IOperator CreateGlobalInjector() {
129      VariableInjector globalInjector = (VariableInjector)base.CreateGlobalInjector();
130      globalInjector.AddVariable(new HeuristicLab.Core.Variable("TournamentSize", new IntData()));
131      globalInjector.AddVariable(new HeuristicLab.Core.Variable("MaxGenerations", new IntData()));
132      globalInjector.AddVariable(new HeuristicLab.Core.Variable("FullTreeShakingFactor", new DoubleData()));
133      globalInjector.AddVariable(new HeuristicLab.Core.Variable("OnePointShakingFactor", new DoubleData()));
134      globalInjector.AddVariable(new HeuristicLab.Core.Variable("MinInitialTreeSize", new IntData()));
135      return globalInjector;
136    }
137
138    protected internal override IOperator CreateCrossover() {
139      StandardCrossOver crossover = new StandardCrossOver();
140      crossover.Name = "Crossover";
141      crossover.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
142      return crossover;
143    }
144
145    protected internal override IOperator CreateTreeCreator() {
146      ProbabilisticTreeCreator treeCreator = new ProbabilisticTreeCreator();
147      treeCreator.Name = "Tree generator";
148      treeCreator.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
149      treeCreator.GetVariableInfo("MinTreeSize").ActualName = "MinInitialTreeSize";
150      return treeCreator;
151    }
152
153    protected internal override IOperator CreateFunctionLibraryInjector() {
154      FunctionLibraryInjector funLibInjector = new FunctionLibraryInjector();
155      funLibInjector.GetVariableValue<BoolData>("Xor", null, false).Data = false;
156      funLibInjector.GetVariableValue<BoolData>("Average", null, false).Data = false;
157      return funLibInjector;
158    }
159
160    protected internal override IOperator CreateManipulator() {
161      CombinedOperator manipulator = new CombinedOperator();
162      manipulator.Name = "Manipulator";
163      StochasticMultiBranch multibranch = new StochasticMultiBranch();
164      FullTreeShaker fullTreeShaker = new FullTreeShaker();
165      fullTreeShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
166      fullTreeShaker.GetVariableInfo("ShakingFactor").ActualName = "FullTreeShakingFactor";
167
168      OnePointShaker onepointShaker = new OnePointShaker();
169      onepointShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
170      onepointShaker.GetVariableInfo("ShakingFactor").ActualName = "OnePointShakingFactor";
171      ChangeNodeTypeManipulation changeNodeTypeManipulation = new ChangeNodeTypeManipulation();
172      changeNodeTypeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
173      CutOutNodeManipulation cutOutNodeManipulation = new CutOutNodeManipulation();
174      cutOutNodeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
175      DeleteSubTreeManipulation deleteSubTreeManipulation = new DeleteSubTreeManipulation();
176      deleteSubTreeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
177      SubstituteSubTreeManipulation substituteSubTreeManipulation = new SubstituteSubTreeManipulation();
178      substituteSubTreeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
179
180      IOperator[] manipulators = new IOperator[] {
181        onepointShaker, fullTreeShaker,
182        changeNodeTypeManipulation,
183        cutOutNodeManipulation,
184        deleteSubTreeManipulation,
185        substituteSubTreeManipulation};
186
187      DoubleArrayData probabilities = new DoubleArrayData(new double[manipulators.Length]);
188      for (int i = 0; i < manipulators.Length; i++) {
189        probabilities.Data[i] = 1.0;
190        multibranch.AddSubOperator(manipulators[i]);
191      }
192      multibranch.GetVariableInfo("Probabilities").Local = true;
193      multibranch.AddVariable(new HeuristicLab.Core.Variable("Probabilities", probabilities));
194
195      manipulator.OperatorGraph.AddOperator(multibranch);
196      manipulator.OperatorGraph.InitialOperator = multibranch;
197      return manipulator;
198    }
199
200    protected internal override IOperator CreateBestSolutionProcessor() {
201      SequentialProcessor bestSolutionProcessor = new SequentialProcessor();
202      MeanSquaredErrorEvaluator testMseEvaluator = new MeanSquaredErrorEvaluator();
203      testMseEvaluator.Name = "TestMeanSquaredErrorEvaluator";
204      testMseEvaluator.GetVariableInfo("MSE").ActualName = "TestQuality";
205      testMseEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
206      testMseEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
207      MeanAbsolutePercentageErrorEvaluator trainingMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
208      trainingMapeEvaluator.Name = "TrainingMapeEvaluator";
209      trainingMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TrainingMAPE";
210      trainingMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
211      trainingMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
212      MeanAbsolutePercentageErrorEvaluator validationMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
213      validationMapeEvaluator.Name = "ValidationMapeEvaluator";
214      validationMapeEvaluator.GetVariableInfo("MAPE").ActualName = "ValidationMAPE";
215      validationMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
216      validationMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
217      MeanAbsolutePercentageErrorEvaluator testMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
218      testMapeEvaluator.Name = "TestMapeEvaluator";
219      testMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TestMAPE";
220      testMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
221      testMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
222      MeanAbsolutePercentageOfRangeErrorEvaluator trainingMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator();
223      trainingMapreEvaluator.Name = "TrainingMapreEvaluator";
224      trainingMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "TrainingMAPRE";
225      trainingMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
226      trainingMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
227      MeanAbsolutePercentageOfRangeErrorEvaluator validationMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator();
228      validationMapreEvaluator.Name = "ValidationMapreEvaluator";
229      validationMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "ValidationMAPRE";
230      validationMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
231      validationMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
232      MeanAbsolutePercentageOfRangeErrorEvaluator testMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator();
233      testMapreEvaluator.Name = "TestMapreEvaluator";
234      testMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "TestMAPRE";
235      testMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
236      testMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
237      CoefficientOfDeterminationEvaluator trainingR2Evaluator = new CoefficientOfDeterminationEvaluator();
238      trainingR2Evaluator.Name = "TrainingR2Evaluator";
239      trainingR2Evaluator.GetVariableInfo("R2").ActualName = "TrainingR2";
240      trainingR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
241      trainingR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
242      CoefficientOfDeterminationEvaluator validationR2Evaluator = new CoefficientOfDeterminationEvaluator();
243      validationR2Evaluator.Name = "ValidationR2Evaluator";
244      validationR2Evaluator.GetVariableInfo("R2").ActualName = "ValidationR2";
245      validationR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
246      validationR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
247      CoefficientOfDeterminationEvaluator testR2Evaluator = new CoefficientOfDeterminationEvaluator();
248      testR2Evaluator.Name = "TestR2Evaluator";
249      testR2Evaluator.GetVariableInfo("R2").ActualName = "TestR2";
250      testR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
251      testR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
252      VarianceAccountedForEvaluator trainingVAFEvaluator = new VarianceAccountedForEvaluator();
253      trainingVAFEvaluator.Name = "TrainingVAFEvaluator";
254      trainingVAFEvaluator.GetVariableInfo("VAF").ActualName = "TrainingVAF";
255      trainingVAFEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
256      trainingVAFEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
257      VarianceAccountedForEvaluator validationVAFEvaluator = new VarianceAccountedForEvaluator();
258      validationVAFEvaluator.Name = "ValidationVAFEvaluator";
259      validationVAFEvaluator.GetVariableInfo("VAF").ActualName = "ValidationVAF";
260      validationVAFEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
261      validationVAFEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
262      VarianceAccountedForEvaluator testVAFEvaluator = new VarianceAccountedForEvaluator();
263      testVAFEvaluator.Name = "TestVAFEvaluator";
264      testVAFEvaluator.GetVariableInfo("VAF").ActualName = "TestVAF";
265      testVAFEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
266      testVAFEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
267
268      ProgrammableOperator progOperator = new ProgrammableOperator();
269      progOperator.RemoveVariableInfo("Result");
270      progOperator.AddVariableInfo(new HeuristicLab.Core.VariableInfo("EvaluatedSolutions", "", typeof(IntData), VariableKind.In));
271      progOperator.Code = @"
272int evalSolutions = EvaluatedSolutions.Data;
273scope.AddVariable(new Variable(""EvaluatedSolutions"", new IntData(evalSolutions)));
274";
275      bestSolutionProcessor.AddSubOperator(testMseEvaluator);
276      bestSolutionProcessor.AddSubOperator(trainingMapeEvaluator);
277      bestSolutionProcessor.AddSubOperator(validationMapeEvaluator);
278      bestSolutionProcessor.AddSubOperator(testMapeEvaluator);
279      bestSolutionProcessor.AddSubOperator(trainingMapreEvaluator);
280      bestSolutionProcessor.AddSubOperator(validationMapreEvaluator);
281      bestSolutionProcessor.AddSubOperator(testMapreEvaluator);
282      bestSolutionProcessor.AddSubOperator(trainingR2Evaluator);
283      bestSolutionProcessor.AddSubOperator(validationR2Evaluator);
284      bestSolutionProcessor.AddSubOperator(testR2Evaluator);
285      bestSolutionProcessor.AddSubOperator(trainingVAFEvaluator);
286      bestSolutionProcessor.AddSubOperator(validationVAFEvaluator);
287      bestSolutionProcessor.AddSubOperator(testVAFEvaluator);
288      bestSolutionProcessor.AddSubOperator(progOperator);
289      return bestSolutionProcessor;
290    }
291
292    protected internal override IOperator CreateLoggingOperator() {
293      CombinedOperator loggingOperator = new CombinedOperator();
294      loggingOperator.Name = "Logging";
295      SequentialProcessor seq = new SequentialProcessor();
296
297      DataCollector collector = new DataCollector();
298      ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
299      names.Add(new StringData("BestQuality"));
300      names.Add(new StringData("AverageQuality"));
301      names.Add(new StringData("WorstQuality"));
302      names.Add(new StringData("BestValidationQuality"));
303      names.Add(new StringData("AverageValidationQuality"));
304      names.Add(new StringData("WorstValidationQuality"));
305      LinechartInjector lineChartInjector = new LinechartInjector();
306      lineChartInjector.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
307      lineChartInjector.GetVariable("NumberOfLines").GetValue<IntData>().Data = 6;
308      QualityLogger qualityLogger = new QualityLogger();
309      QualityLogger validationQualityLogger = new QualityLogger();
310      validationQualityLogger.Name = "ValidationQualityLogger";
311      validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
312      validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
313
314      seq.AddSubOperator(collector);
315      seq.AddSubOperator(lineChartInjector);
316      seq.AddSubOperator(qualityLogger);
317      seq.AddSubOperator(validationQualityLogger);
318
319      loggingOperator.OperatorGraph.AddOperator(seq);
320      loggingOperator.OperatorGraph.InitialOperator = seq;
321      return loggingOperator;
322    }
323
324    protected internal override Model CreateGPModel(IScope bestModelScope) {
325      Model model = base.CreateGPModel(bestModelScope);
326      model.TestMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("TestQuality", false).Data;
327      model.TrainingCoefficientOfDetermination = bestModelScope.GetVariableValue<DoubleData>("TrainingR2", false).Data;
328      model.ValidationCoefficientOfDetermination = bestModelScope.GetVariableValue<DoubleData>("ValidationR2", false).Data;
329      model.TestCoefficientOfDetermination = bestModelScope.GetVariableValue<DoubleData>("TestR2", false).Data;
330      model.TrainingMeanAbsolutePercentageError = bestModelScope.GetVariableValue<DoubleData>("TrainingMAPE", false).Data;
331      model.ValidationMeanAbsolutePercentageError = bestModelScope.GetVariableValue<DoubleData>("ValidationMAPE", false).Data;
332      model.TestMeanAbsolutePercentageError = bestModelScope.GetVariableValue<DoubleData>("TestMAPE", false).Data;
333      model.TrainingMeanAbsolutePercentageOfRangeError = bestModelScope.GetVariableValue<DoubleData>("TrainingMAPRE", false).Data;
334      model.ValidationMeanAbsolutePercentageOfRangeError = bestModelScope.GetVariableValue<DoubleData>("ValidationMAPRE", false).Data;
335      model.TestMeanAbsolutePercentageOfRangeError = bestModelScope.GetVariableValue<DoubleData>("TestMAPRE", false).Data;
336      model.TrainingVarianceAccountedFor = bestModelScope.GetVariableValue<DoubleData>("TrainingVAF", false).Data;
337      model.ValidationVarianceAccountedFor = bestModelScope.GetVariableValue<DoubleData>("ValidationVAF", false).Data;
338      model.TestVarianceAccountedFor = bestModelScope.GetVariableValue<DoubleData>("TestVAF", false).Data;
339
340      return model;
341    }
342
343    public virtual IEditor CreateEditor() {
344      return new StandardGpEditor(this);
345    }
346
347    public override IView CreateView() {
348      return new StandardGpEditor(this);
349    }
350  }
351}
Note: See TracBrowser for help on using the repository browser.