Free cookie consent management tool by TermsFeed Policy Generator

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

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

GP Refactoring: #713

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