Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/DefaultStructureIdentificationOperators.cs @ 2354

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

Worked on SVM classification engine. #625

File size: 18.8 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.GP.Interfaces;
25using HeuristicLab.Operators;
26using HeuristicLab.Modeling;
27using HeuristicLab.Logging;
28using HeuristicLab.Selection;
29using HeuristicLab.Data;
30
31namespace HeuristicLab.GP.StructureIdentification {
32  public static class DefaultStructureIdentificationAlgorithmOperators {
33    public static IOperator CreateFunctionLibraryInjector() {
34      CombinedOperator op = new CombinedOperator();
35      op.Name = "FunctionLibraryInjector";
36      SequentialProcessor seq = new SequentialProcessor();
37      seq.AddSubOperator(new FunctionLibraryInjector());
38      seq.AddSubOperator(new HL3TreeEvaluatorInjector());
39      op.OperatorGraph.AddOperator(seq);
40      op.OperatorGraph.InitialOperator = seq;
41      return op;
42    }
43
44    public static IOperator CreateProblemInjector() {
45      CombinedOperator op = new CombinedOperator();
46      op.Name = "ProblemInjector";
47      SequentialProcessor seq = new SequentialProcessor();
48
49      seq.AddSubOperator(new ProblemInjector());
50      DatasetShuffler shuffler = new DatasetShuffler();
51      shuffler.GetVariableInfo("ShuffleStart").ActualName = "TrainingSamplesStart";
52      shuffler.GetVariableInfo("ShuffleEnd").ActualName = "TrainingSamplesEnd";
53      seq.AddSubOperator(shuffler);
54      op.OperatorGraph.AddOperator(seq);
55      op.OperatorGraph.InitialOperator = seq;
56      return op;
57    }
58
59    public static IOperator CreateInitialPopulationEvaluator() {
60      MeanSquaredErrorEvaluator eval = new MeanSquaredErrorEvaluator();
61      eval.Name = "Evaluator";
62      eval.GetVariableInfo("MSE").ActualName = "Quality";
63      eval.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
64      eval.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
65      return eval;
66    }
67
68    public static IOperator CreateEvaluator() {
69      return CreateInitialPopulationEvaluator();
70    }
71
72    public static IOperator CreateGenerationStepHook() {
73      CombinedOperator op = new CombinedOperator();
74      SequentialProcessor seq = new SequentialProcessor();
75      UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
76      SequentialProcessor individualProc = new SequentialProcessor();
77      MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
78      validationEvaluator.Name = "ValidationEvaluator";
79      validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
80      validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
81      validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
82
83      individualProc.AddSubOperator(validationEvaluator);
84
85      BestSolutionStorer solutionStorer = new BestSolutionStorer();
86      solutionStorer.GetVariableInfo("BestSolution").ActualName = "BestValidationSolution";
87      solutionStorer.GetVariableInfo("Quality").ActualName = "ValidationQuality";
88
89      BestAverageWorstQualityCalculator validationQualityCalculator = new BestAverageWorstQualityCalculator();
90      validationQualityCalculator.Name = "BestAverageWorstValidationQualityCalculator";
91      validationQualityCalculator.GetVariableInfo("Quality").ActualName = "ValidationQuality";
92      validationQualityCalculator.GetVariableInfo("BestQuality").ActualName = "BestValidationQuality";
93      validationQualityCalculator.GetVariableInfo("AverageQuality").ActualName = "AverageValidationQuality";
94      validationQualityCalculator.GetVariableInfo("WorstQuality").ActualName = "WorstValidationQuality";
95
96      subScopesProc.AddSubOperator(individualProc);
97
98      seq.AddSubOperator(subScopesProc);
99      seq.AddSubOperator(solutionStorer);
100      seq.AddSubOperator(validationQualityCalculator);
101
102      op.OperatorGraph.AddOperator(seq);
103      op.OperatorGraph.InitialOperator = seq;
104      return op;
105    }
106
107    public static IOperator CreatePostProcessingOperator() {
108      CombinedOperator op = new CombinedOperator();
109      op.Name = "ModelAnalyser";
110      SequentialProcessor seq = new SequentialProcessor();
111      LeftReducer cleanUp = new LeftReducer();
112      cleanUp.Name = "Reset Population";
113      seq.AddSubOperator(cleanUp);
114
115      SolutionExtractor extractor = new SolutionExtractor();
116      extractor.GetVariableInfo("Scope").ActualName = "BestValidationSolution";
117      SequentialSubScopesProcessor seqSubScopeProc = new SequentialSubScopesProcessor();
118      SequentialProcessor solutionProc = new SequentialProcessor();
119
120      seq.AddSubOperator(extractor);
121      seq.AddSubOperator(seqSubScopeProc);
122      seqSubScopeProc.AddSubOperator(solutionProc);
123
124      HL3TreeEvaluatorInjector evaluatorInjector = new HL3TreeEvaluatorInjector();
125      evaluatorInjector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData(1000.0)));
126      evaluatorInjector.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
127
128      #region simple evaluators
129      SimpleEvaluator trainingEvaluator = new SimpleEvaluator();
130      trainingEvaluator.Name = "TrainingEvaluator";
131      trainingEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
132      trainingEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
133      trainingEvaluator.GetVariableInfo("Values").ActualName = "TrainingValues";
134      trainingEvaluator.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
135      SimpleEvaluator validationEvaluator = new SimpleEvaluator();
136      validationEvaluator.Name = "ValidationEvaluator";
137      validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
138      validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
139      validationEvaluator.GetVariableInfo("Values").ActualName = "ValidationValues";
140      validationEvaluator.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
141      SimpleEvaluator testEvaluator = new SimpleEvaluator();
142      testEvaluator.Name = "TestEvaluator";
143      testEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
144      testEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
145      testEvaluator.GetVariableInfo("Values").ActualName = "TestValues";
146      testEvaluator.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
147      #endregion
148      #region MSE
149      SimpleMSEEvaluator trainingMseEvaluator = new SimpleMSEEvaluator();
150      trainingMseEvaluator.Name = "TrainingMseEvaluator";
151      trainingMseEvaluator.GetVariableInfo("MSE").ActualName = "TrainingMSE";
152      trainingMseEvaluator.GetVariableInfo("Values").ActualName = "TrainingValues";
153      SimpleMSEEvaluator validationMseEvaluator = new SimpleMSEEvaluator();
154      validationMseEvaluator.Name = "ValidationMseEvaluator";
155      validationMseEvaluator.GetVariableInfo("MSE").ActualName = "ValidationMSE";
156      validationMseEvaluator.GetVariableInfo("Values").ActualName = "ValidationValues";
157      SimpleMSEEvaluator testMseEvaluator = new SimpleMSEEvaluator();
158      testMseEvaluator.Name = "TestMseEvaluator";
159      testMseEvaluator.GetVariableInfo("MSE").ActualName = "TestMSE";
160      testMseEvaluator.GetVariableInfo("Values").ActualName = "TestValues";
161      #endregion
162      #region MAPE
163      SimpleMeanAbsolutePercentageErrorEvaluator trainingMapeEvaluator = new SimpleMeanAbsolutePercentageErrorEvaluator();
164      trainingMapeEvaluator.Name = "TrainingMapeEvaluator";
165      trainingMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TrainingMAPE";
166      trainingMapeEvaluator.GetVariableInfo("Values").ActualName = "TrainingValues";
167      SimpleMeanAbsolutePercentageErrorEvaluator validationMapeEvaluator = new SimpleMeanAbsolutePercentageErrorEvaluator();
168      validationMapeEvaluator.Name = "ValidationMapeEvaluator";
169      validationMapeEvaluator.GetVariableInfo("MAPE").ActualName = "ValidationMAPE";
170      validationMapeEvaluator.GetVariableInfo("Values").ActualName = "ValidationValues";
171      SimpleMeanAbsolutePercentageErrorEvaluator testMapeEvaluator = new SimpleMeanAbsolutePercentageErrorEvaluator();
172      testMapeEvaluator.Name = "TestMapeEvaluator";
173      testMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TestMAPE";
174      testMapeEvaluator.GetVariableInfo("Values").ActualName = "TestValues";
175      #endregion
176      #region MAPRE
177      SimpleMeanAbsolutePercentageOfRangeErrorEvaluator trainingMapreEvaluator = new SimpleMeanAbsolutePercentageOfRangeErrorEvaluator();
178      trainingMapreEvaluator.Name = "TrainingMapreEvaluator";
179      trainingMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "TrainingMAPRE";
180      trainingMapreEvaluator.GetVariableInfo("Values").ActualName = "TrainingValues";
181      SimpleMeanAbsolutePercentageOfRangeErrorEvaluator validationMapreEvaluator = new SimpleMeanAbsolutePercentageOfRangeErrorEvaluator();
182      validationMapreEvaluator.Name = "ValidationMapreEvaluator";
183      validationMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "ValidationMAPRE";
184      validationMapreEvaluator.GetVariableInfo("Values").ActualName = "ValidationValues";
185      SimpleMeanAbsolutePercentageOfRangeErrorEvaluator testMapreEvaluator = new SimpleMeanAbsolutePercentageOfRangeErrorEvaluator();
186      testMapreEvaluator.Name = "TestMapreEvaluator";
187      testMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "TestMAPRE";
188      testMapreEvaluator.GetVariableInfo("Values").ActualName = "TestValues";
189      #endregion MAPRE
190      #region R2
191      SimpleR2Evaluator trainingR2Evaluator = new SimpleR2Evaluator();
192      trainingR2Evaluator.Name = "TrainingR2Evaluator";
193      trainingR2Evaluator.GetVariableInfo("R2").ActualName = "TrainingR2";
194      trainingR2Evaluator.GetVariableInfo("Values").ActualName = "TrainingValues";
195      SimpleR2Evaluator validationR2Evaluator = new SimpleR2Evaluator();
196      validationR2Evaluator.Name = "ValidationR2Evaluator";
197      validationR2Evaluator.GetVariableInfo("R2").ActualName = "ValidationR2";
198      validationR2Evaluator.GetVariableInfo("Values").ActualName = "ValidationValues";
199      SimpleR2Evaluator testR2Evaluator = new SimpleR2Evaluator();
200      testR2Evaluator.Name = "TestR2Evaluator";
201      testR2Evaluator.GetVariableInfo("R2").ActualName = "TestR2";
202      testR2Evaluator.GetVariableInfo("Values").ActualName = "TestValues";
203      #endregion
204      #region VAF
205      SimpleVarianceAccountedForEvaluator trainingVAFEvaluator = new SimpleVarianceAccountedForEvaluator();
206      trainingVAFEvaluator.Name = "TrainingVAFEvaluator";
207      trainingVAFEvaluator.GetVariableInfo("VAF").ActualName = "TrainingVAF";
208      trainingVAFEvaluator.GetVariableInfo("Values").ActualName = "TrainingValues";
209      SimpleVarianceAccountedForEvaluator validationVAFEvaluator = new SimpleVarianceAccountedForEvaluator();
210      validationVAFEvaluator.Name = "ValidationVAFEvaluator";
211      validationVAFEvaluator.GetVariableInfo("VAF").ActualName = "ValidationVAF";
212      validationVAFEvaluator.GetVariableInfo("Values").ActualName = "ValidationValues";
213      SimpleVarianceAccountedForEvaluator testVAFEvaluator = new SimpleVarianceAccountedForEvaluator();
214      testVAFEvaluator.Name = "TestVAFEvaluator";
215      testVAFEvaluator.GetVariableInfo("VAF").ActualName = "TestVAF";
216      testVAFEvaluator.GetVariableInfo("Values").ActualName = "TestValues";
217      #endregion
218
219      solutionProc.AddSubOperator(evaluatorInjector);
220      solutionProc.AddSubOperator(trainingEvaluator);
221      solutionProc.AddSubOperator(validationEvaluator);
222      solutionProc.AddSubOperator(testEvaluator);
223      solutionProc.AddSubOperator(trainingMseEvaluator);
224      solutionProc.AddSubOperator(validationMseEvaluator);
225      solutionProc.AddSubOperator(testMseEvaluator);
226      solutionProc.AddSubOperator(trainingMapeEvaluator);
227      solutionProc.AddSubOperator(validationMapeEvaluator);
228      solutionProc.AddSubOperator(testMapeEvaluator);
229      solutionProc.AddSubOperator(trainingMapreEvaluator);
230      solutionProc.AddSubOperator(validationMapreEvaluator);
231      solutionProc.AddSubOperator(testMapreEvaluator);
232      solutionProc.AddSubOperator(trainingR2Evaluator);
233      solutionProc.AddSubOperator(validationR2Evaluator);
234      solutionProc.AddSubOperator(testR2Evaluator);
235      solutionProc.AddSubOperator(trainingVAFEvaluator);
236      solutionProc.AddSubOperator(validationVAFEvaluator);
237      solutionProc.AddSubOperator(testVAFEvaluator);
238
239      #region variable impacts
240      // calculate and set variable impacts
241      VariableNamesExtractor namesExtractor = new VariableNamesExtractor();
242      namesExtractor.GetVariableInfo("VariableNames").ActualName = "InputVariableNames";
243      PredictorBuilder predictorBuilder = new PredictorBuilder();
244      predictorBuilder.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
245
246      VariableEvaluationImpactCalculator evaluationImpactCalculator = new VariableEvaluationImpactCalculator();
247      evaluationImpactCalculator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
248      evaluationImpactCalculator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
249      VariableQualityImpactCalculator qualityImpactCalculator = new VariableQualityImpactCalculator();
250      qualityImpactCalculator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
251      qualityImpactCalculator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
252
253      solutionProc.AddSubOperator(namesExtractor);
254      solutionProc.AddSubOperator(predictorBuilder);
255      solutionProc.AddSubOperator(evaluationImpactCalculator);
256      solutionProc.AddSubOperator(qualityImpactCalculator);
257      #endregion
258
259      op.OperatorGraph.AddOperator(seq);
260      op.OperatorGraph.InitialOperator = seq;
261      return op;
262    }
263
264    public static IAnalyzerModel CreateGPModel(IScope bestModelScope) {
265      IAnalyzerModel model = new AnalyzerModel();
266      model.Predictor = bestModelScope.GetVariableValue<IPredictor>("Predictor", true);
267      Dataset ds = bestModelScope.GetVariableValue<Dataset>("Dataset", true);
268      model.Dataset = ds;
269      model.TargetVariable = ds.GetVariableName(bestModelScope.GetVariableValue<IntData>("TargetVariable", true).Data);
270      model.TrainingSamplesStart = bestModelScope.GetVariableValue<IntData>("TrainingSamplesStart", true).Data;
271      model.TrainingSamplesEnd = bestModelScope.GetVariableValue<IntData>("TrainingSamplesEnd", true).Data;
272      model.ValidationSamplesStart = bestModelScope.GetVariableValue<IntData>("ValidationSamplesStart", true).Data;
273      model.ValidationSamplesEnd = bestModelScope.GetVariableValue<IntData>("ValidationSamplesEnd", true).Data;
274      model.TestSamplesStart = bestModelScope.GetVariableValue<IntData>("TestSamplesStart", true).Data;
275      model.TestSamplesEnd = bestModelScope.GetVariableValue<IntData>("TestSamplesEnd", true).Data;
276
277      model.SetResult("TrainingMeanSquaredError", bestModelScope.GetVariableValue<DoubleData>("TrainingMSE", false).Data);
278      model.SetResult("ValidationMeanSquaredError", bestModelScope.GetVariableValue<DoubleData>("ValidationMSE", false).Data);
279      model.SetResult("TestMeanSquaredError", bestModelScope.GetVariableValue<DoubleData>("TestMSE", false).Data);
280      model.SetResult("TrainingCoefficientOfDetermination", bestModelScope.GetVariableValue<DoubleData>("TrainingR2", false).Data);
281      model.SetResult("ValidationCoefficientOfDetermination", bestModelScope.GetVariableValue<DoubleData>("ValidationR2", false).Data);
282      model.SetResult("TestCoefficientOfDetermination", bestModelScope.GetVariableValue<DoubleData>("TestR2", false).Data);
283      model.SetResult("TrainingMeanAbsolutePercentageError", bestModelScope.GetVariableValue<DoubleData>("TrainingMAPE", false).Data);
284      model.SetResult("ValidationMeanAbsolutePercentageError", bestModelScope.GetVariableValue<DoubleData>("ValidationMAPE", false).Data);
285      model.SetResult("TestMeanAbsolutePercentageError", bestModelScope.GetVariableValue<DoubleData>("TestMAPE", false).Data);
286      model.SetResult("TrainingMeanAbsolutePercentageOfRangeError", bestModelScope.GetVariableValue<DoubleData>("TrainingMAPRE", false).Data);
287      model.SetResult("ValidationMeanAbsolutePercentageOfRangeError", bestModelScope.GetVariableValue<DoubleData>("ValidationMAPRE", false).Data);
288      model.SetResult("TestMeanAbsolutePercentageOfRangeError", bestModelScope.GetVariableValue<DoubleData>("TestMAPRE", false).Data);
289      model.SetResult("TrainingVarianceAccountedFor", bestModelScope.GetVariableValue<DoubleData>("TrainingVAF", false).Data);
290      model.SetResult("ValidationVarianceAccountedFor", bestModelScope.GetVariableValue<DoubleData>("ValidationVAF", false).Data);
291      model.SetResult("TestVarianceAccountedFor", bestModelScope.GetVariableValue<DoubleData>("TestVAF", false).Data);
292
293      model.SetMetaData("EvaluatedSolutions", bestModelScope.GetVariableValue<DoubleData>("EvaluatedSolutions", true).Data);
294      IGeneticProgrammingModel gpModel = bestModelScope.GetVariableValue<IGeneticProgrammingModel>("FunctionTree", true);
295      model.SetMetaData("TreeSize", gpModel.Size);
296      model.SetMetaData("TreeHeight", gpModel.Height);
297
298      ItemList evaluationImpacts = bestModelScope.GetVariableValue<ItemList>("VariableEvaluationImpacts", false);
299      ItemList qualityImpacts = bestModelScope.GetVariableValue<ItemList>("VariableQualityImpacts", false);
300      foreach (ItemList row in evaluationImpacts) {
301        string variableName = ((StringData)row[0]).Data;
302        double impact = ((DoubleData)row[1]).Data;
303        model.SetVariableEvaluationImpact(variableName, impact);
304        model.AddInputVariable(variableName);
305      }
306      foreach (ItemList row in qualityImpacts) {
307        string variableName = ((StringData)row[0]).Data;
308        double impact = ((DoubleData)row[1]).Data;
309        model.SetVariableQualityImpact(variableName, impact);
310        model.AddInputVariable(variableName);
311      }
312
313      return model;
314    }
315  }
316}
Note: See TracBrowser for help on using the repository browser.