Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/StandardGP.cs @ 2210

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

GP Refactoring #713

  • introduced a plugin for GP interfaces
  • created a new interface IGeneticProgrammingModel which represents GP models in HL scopes instead of IFunctionTree
  • changed interfaces IFunction and IFunctionTree
  • moved some files to new directories (general housekeeping)
  • changed all GP operators and engines to work with IGeneticProgrammingModels
  • removed parameters TreeSize and TreeHeight in all GP operators
  • changed parameter OperatorLibrary to FunctionLibrary in all GP operators
File size: 19.4 KB
RevLine 
[1050]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;
[1052]35using HeuristicLab.Operators.Programmable;
[1856]36using HeuristicLab.Modeling;
[1050]37
38namespace HeuristicLab.GP.StructureIdentification {
[1287]39  public class StandardGP : AlgorithmBase, IEditable {
40
[1857]41    public override string Name { get { return "StandardGP"; } }
42
[1922]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
[1287]53    public virtual int MaxGenerations {
54      get { return GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data; }
55      set { GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data = value; }
[1050]56    }
[1051]57
[1287]58    public virtual int TournamentSize {
59      get { return GetVariableInjector().GetVariable("TournamentSize").GetValue<IntData>().Data; }
60      set { GetVariableInjector().GetVariable("TournamentSize").GetValue<IntData>().Data = value; }
[1050]61    }
[1287]62
63    public double FullTreeShakingFactor {
64      get { return GetVariableInjector().GetVariable("FullTreeShakingFactor").GetValue<DoubleData>().Data; }
65      set { GetVariableInjector().GetVariable("FullTreeShakingFactor").GetValue<DoubleData>().Data = value; }
[1050]66    }
67
[1287]68    public double OnePointShakingFactor {
69      get { return GetVariableInjector().GetVariable("OnePointShakingFactor").GetValue<DoubleData>().Data; }
70      set { GetVariableInjector().GetVariable("OnePointShakingFactor").GetValue<DoubleData>().Data = value; }
[1051]71    }
72
[1287]73    public int MinInitialTreeSize {
74      get { return GetVariableInjector().GetVariable("MinInitialTreeSize").GetValue<IntData>().Data; }
75      set { GetVariableInjector().GetVariable("MinInitialTreeSize").GetValue<IntData>().Data = value; }
[1051]76    }
77
[1287]78    public override int MaxTreeSize {
79      get {
80        return base.MaxTreeSize;
81      }
[1051]82      set {
[1287]83        base.MaxTreeSize = value;
84        MinInitialTreeSize = value / 2;
[1051]85      }
86    }
87
[1287]88    public override int PopulationSize {
89      get {
90        return base.PopulationSize;
91      }
92      set {
93        base.PopulationSize = value;
94        Parents = 2 * value;
95      }
[1051]96    }
97
[2161]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
[1287]107    public StandardGP()
108      : base() {
109      PopulationSize = 10000;
[2130]110      MaxGenerations = 500;
[1050]111      TournamentSize = 7;
112      MutationRate = 0.15;
[1052]113      Elites = 1;
[1287]114      MaxTreeSize = 100;
115      MaxTreeHeight = 10;
116      FullTreeShakingFactor = 0.1;
117      OnePointShakingFactor = 1.0;
118      PunishmentFactor = 10.0;
119      UseEstimatedTargetValue = false;
120      SetSeedRandomly = true;
[1050]121    }
122
[1287]123    protected internal override IOperator CreateProblemInjector() {
[2161]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;
[1287]136    }
[1050]137
[1287]138    protected internal override IOperator CreateSelector() {
[1050]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";
[1287]145      return selector;
146    }
[1050]147
[1287]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    }
[1050]157
[1287]158    protected internal override IOperator CreateCrossover() {
159      StandardCrossOver crossover = new StandardCrossOver();
160      crossover.Name = "Crossover";
161      return crossover;
162    }
[1050]163
[1287]164    protected internal override IOperator CreateTreeCreator() {
165      ProbabilisticTreeCreator treeCreator = new ProbabilisticTreeCreator();
166      treeCreator.Name = "Tree generator";
167      treeCreator.GetVariableInfo("MinTreeSize").ActualName = "MinInitialTreeSize";
168      return treeCreator;
[1050]169    }
170
[1287]171    protected internal override IOperator CreateFunctionLibraryInjector() {
[1908]172      FunctionLibraryInjector funLibInjector = new FunctionLibraryInjector();
[1873]173      funLibInjector.GetVariableValue<BoolData>("Xor", null, false).Data = false;
174      funLibInjector.GetVariableValue<BoolData>("Average", null, false).Data = false;
175      return funLibInjector;
[1050]176    }
177
[1287]178    protected internal override IOperator CreateManipulator() {
[1050]179      CombinedOperator manipulator = new CombinedOperator();
[1287]180      manipulator.Name = "Manipulator";
[1050]181      StochasticMultiBranch multibranch = new StochasticMultiBranch();
182      FullTreeShaker fullTreeShaker = new FullTreeShaker();
[1287]183      fullTreeShaker.GetVariableInfo("ShakingFactor").ActualName = "FullTreeShakingFactor";
[1050]184
185      OnePointShaker onepointShaker = new OnePointShaker();
[1287]186      onepointShaker.GetVariableInfo("ShakingFactor").ActualName = "OnePointShakingFactor";
[1050]187      ChangeNodeTypeManipulation changeNodeTypeManipulation = new ChangeNodeTypeManipulation();
188      CutOutNodeManipulation cutOutNodeManipulation = new CutOutNodeManipulation();
189      DeleteSubTreeManipulation deleteSubTreeManipulation = new DeleteSubTreeManipulation();
190      SubstituteSubTreeManipulation substituteSubTreeManipulation = new SubstituteSubTreeManipulation();
191
[1051]192      IOperator[] manipulators = new IOperator[] {
193        onepointShaker, fullTreeShaker,
194        changeNodeTypeManipulation,
195        cutOutNodeManipulation,
196        deleteSubTreeManipulation,
197        substituteSubTreeManipulation};
198
199      DoubleArrayData probabilities = new DoubleArrayData(new double[manipulators.Length]);
[1287]200      for (int i = 0; i < manipulators.Length; i++) {
[1051]201        probabilities.Data[i] = 1.0;
202        multibranch.AddSubOperator(manipulators[i]);
203      }
204      multibranch.GetVariableInfo("Probabilities").Local = true;
205      multibranch.AddVariable(new HeuristicLab.Core.Variable("Probabilities", probabilities));
206
[1050]207      manipulator.OperatorGraph.AddOperator(multibranch);
208      manipulator.OperatorGraph.InitialOperator = multibranch;
209      return manipulator;
210    }
211
[1287]212    protected internal override IOperator CreateBestSolutionProcessor() {
213      SequentialProcessor bestSolutionProcessor = new SequentialProcessor();
[2161]214      #region MSE
[1287]215      MeanSquaredErrorEvaluator testMseEvaluator = new MeanSquaredErrorEvaluator();
216      testMseEvaluator.Name = "TestMeanSquaredErrorEvaluator";
217      testMseEvaluator.GetVariableInfo("MSE").ActualName = "TestQuality";
218      testMseEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
219      testMseEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
[2161]220      #endregion
221      #region MAPE
[1287]222      MeanAbsolutePercentageErrorEvaluator trainingMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
223      trainingMapeEvaluator.Name = "TrainingMapeEvaluator";
224      trainingMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TrainingMAPE";
[2161]225      trainingMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
226      trainingMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
[1287]227      MeanAbsolutePercentageErrorEvaluator validationMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
228      validationMapeEvaluator.Name = "ValidationMapeEvaluator";
229      validationMapeEvaluator.GetVariableInfo("MAPE").ActualName = "ValidationMAPE";
230      validationMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
231      validationMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
232      MeanAbsolutePercentageErrorEvaluator testMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
233      testMapeEvaluator.Name = "TestMapeEvaluator";
234      testMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TestMAPE";
235      testMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
236      testMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
[2161]237      #endregion
238      #region MAPRE
[1287]239      MeanAbsolutePercentageOfRangeErrorEvaluator trainingMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator();
240      trainingMapreEvaluator.Name = "TrainingMapreEvaluator";
241      trainingMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "TrainingMAPRE";
[2161]242      trainingMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
243      trainingMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
[1287]244      MeanAbsolutePercentageOfRangeErrorEvaluator validationMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator();
245      validationMapreEvaluator.Name = "ValidationMapreEvaluator";
246      validationMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "ValidationMAPRE";
247      validationMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
248      validationMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
249      MeanAbsolutePercentageOfRangeErrorEvaluator testMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator();
250      testMapreEvaluator.Name = "TestMapreEvaluator";
251      testMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "TestMAPRE";
252      testMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
253      testMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
[2161]254      #endregion MAPRE
255      #region R2
[1287]256      CoefficientOfDeterminationEvaluator trainingR2Evaluator = new CoefficientOfDeterminationEvaluator();
257      trainingR2Evaluator.Name = "TrainingR2Evaluator";
258      trainingR2Evaluator.GetVariableInfo("R2").ActualName = "TrainingR2";
[2161]259      trainingR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
260      trainingR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
[1287]261      CoefficientOfDeterminationEvaluator validationR2Evaluator = new CoefficientOfDeterminationEvaluator();
262      validationR2Evaluator.Name = "ValidationR2Evaluator";
263      validationR2Evaluator.GetVariableInfo("R2").ActualName = "ValidationR2";
264      validationR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
265      validationR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
266      CoefficientOfDeterminationEvaluator testR2Evaluator = new CoefficientOfDeterminationEvaluator();
267      testR2Evaluator.Name = "TestR2Evaluator";
268      testR2Evaluator.GetVariableInfo("R2").ActualName = "TestR2";
269      testR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
270      testR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
[2161]271      #endregion
272      #region VAF
[1890]273      VarianceAccountedForEvaluator trainingVAFEvaluator = new VarianceAccountedForEvaluator();
274      trainingVAFEvaluator.Name = "TrainingVAFEvaluator";
275      trainingVAFEvaluator.GetVariableInfo("VAF").ActualName = "TrainingVAF";
[2161]276      trainingVAFEvaluator.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
277      trainingVAFEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
[1890]278      VarianceAccountedForEvaluator validationVAFEvaluator = new VarianceAccountedForEvaluator();
279      validationVAFEvaluator.Name = "ValidationVAFEvaluator";
280      validationVAFEvaluator.GetVariableInfo("VAF").ActualName = "ValidationVAF";
281      validationVAFEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
282      validationVAFEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
283      VarianceAccountedForEvaluator testVAFEvaluator = new VarianceAccountedForEvaluator();
284      testVAFEvaluator.Name = "TestVAFEvaluator";
285      testVAFEvaluator.GetVariableInfo("VAF").ActualName = "TestVAF";
286      testVAFEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
287      testVAFEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
[2161]288      #endregion
[1287]289      ProgrammableOperator progOperator = new ProgrammableOperator();
290      progOperator.RemoveVariableInfo("Result");
291      progOperator.AddVariableInfo(new HeuristicLab.Core.VariableInfo("EvaluatedSolutions", "", typeof(IntData), VariableKind.In));
292      progOperator.Code = @"
293int evalSolutions = EvaluatedSolutions.Data;
294scope.AddVariable(new Variable(""EvaluatedSolutions"", new IntData(evalSolutions)));
295";
296      bestSolutionProcessor.AddSubOperator(testMseEvaluator);
297      bestSolutionProcessor.AddSubOperator(trainingMapeEvaluator);
298      bestSolutionProcessor.AddSubOperator(validationMapeEvaluator);
299      bestSolutionProcessor.AddSubOperator(testMapeEvaluator);
300      bestSolutionProcessor.AddSubOperator(trainingMapreEvaluator);
301      bestSolutionProcessor.AddSubOperator(validationMapreEvaluator);
302      bestSolutionProcessor.AddSubOperator(testMapreEvaluator);
303      bestSolutionProcessor.AddSubOperator(trainingR2Evaluator);
304      bestSolutionProcessor.AddSubOperator(validationR2Evaluator);
305      bestSolutionProcessor.AddSubOperator(testR2Evaluator);
[1890]306      bestSolutionProcessor.AddSubOperator(trainingVAFEvaluator);
307      bestSolutionProcessor.AddSubOperator(validationVAFEvaluator);
308      bestSolutionProcessor.AddSubOperator(testVAFEvaluator);
[1287]309      bestSolutionProcessor.AddSubOperator(progOperator);
310      return bestSolutionProcessor;
[1050]311    }
312
[1287]313    protected internal override IOperator CreateLoggingOperator() {
314      CombinedOperator loggingOperator = new CombinedOperator();
315      loggingOperator.Name = "Logging";
[1050]316      SequentialProcessor seq = new SequentialProcessor();
[1287]317
[1050]318      DataCollector collector = new DataCollector();
319      ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
320      names.Add(new StringData("BestQuality"));
321      names.Add(new StringData("AverageQuality"));
322      names.Add(new StringData("WorstQuality"));
[1052]323      names.Add(new StringData("BestValidationQuality"));
324      names.Add(new StringData("AverageValidationQuality"));
325      names.Add(new StringData("WorstValidationQuality"));
[1050]326      LinechartInjector lineChartInjector = new LinechartInjector();
327      lineChartInjector.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
[1052]328      lineChartInjector.GetVariable("NumberOfLines").GetValue<IntData>().Data = 6;
[1050]329      QualityLogger qualityLogger = new QualityLogger();
[1052]330      QualityLogger validationQualityLogger = new QualityLogger();
[1287]331      validationQualityLogger.Name = "ValidationQualityLogger";
[1052]332      validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
333      validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
[1051]334
335      seq.AddSubOperator(collector);
336      seq.AddSubOperator(lineChartInjector);
337      seq.AddSubOperator(qualityLogger);
[1052]338      seq.AddSubOperator(validationQualityLogger);
[1051]339
[1287]340      loggingOperator.OperatorGraph.AddOperator(seq);
341      loggingOperator.OperatorGraph.InitialOperator = seq;
342      return loggingOperator;
[1050]343    }
344
[1906]345    protected internal override Model CreateGPModel(IScope bestModelScope) {
346      Model model = base.CreateGPModel(bestModelScope);
347      model.TestMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("TestQuality", false).Data;
[1922]348      model.TrainingCoefficientOfDetermination = bestModelScope.GetVariableValue<DoubleData>("TrainingR2", false).Data;
349      model.ValidationCoefficientOfDetermination = bestModelScope.GetVariableValue<DoubleData>("ValidationR2", false).Data;
350      model.TestCoefficientOfDetermination = bestModelScope.GetVariableValue<DoubleData>("TestR2", false).Data;
351      model.TrainingMeanAbsolutePercentageError = bestModelScope.GetVariableValue<DoubleData>("TrainingMAPE", false).Data;
352      model.ValidationMeanAbsolutePercentageError = bestModelScope.GetVariableValue<DoubleData>("ValidationMAPE", false).Data;
353      model.TestMeanAbsolutePercentageError = bestModelScope.GetVariableValue<DoubleData>("TestMAPE", false).Data;
354      model.TrainingMeanAbsolutePercentageOfRangeError = bestModelScope.GetVariableValue<DoubleData>("TrainingMAPRE", false).Data;
355      model.ValidationMeanAbsolutePercentageOfRangeError = bestModelScope.GetVariableValue<DoubleData>("ValidationMAPRE", false).Data;
356      model.TestMeanAbsolutePercentageOfRangeError = bestModelScope.GetVariableValue<DoubleData>("TestMAPRE", false).Data;
357      model.TrainingVarianceAccountedFor = bestModelScope.GetVariableValue<DoubleData>("TrainingVAF", false).Data;
358      model.ValidationVarianceAccountedFor = bestModelScope.GetVariableValue<DoubleData>("ValidationVAF", false).Data;
359      model.TestVarianceAccountedFor = bestModelScope.GetVariableValue<DoubleData>("TestVAF", false).Data;
360
[1906]361      return model;
362    }
363
[1287]364    public virtual IEditor CreateEditor() {
[1051]365      return new StandardGpEditor(this);
366    }
367
[1052]368    public override IView CreateView() {
[1051]369      return new StandardGpEditor(this);
370    }
[1050]371  }
372}
Note: See TracBrowser for help on using the repository browser.