Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed bug in regression engines. #823 (Implement tree evaluator with linear scaling to improve convergence in symbolic regression.)

File size: 12.1 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;
30using System.Collections.Generic;
31using System;
32using HeuristicLab.GP.Operators;
33
34namespace HeuristicLab.GP.StructureIdentification {
35  public static class DefaultStructureIdentificationOperators {
36    public static IOperator CreateFunctionLibraryInjector() {
37      CombinedOperator op = new CombinedOperator();
38      op.Name = "FunctionLibraryInjector";
39      SequentialProcessor seq = new SequentialProcessor();
40      seq.AddSubOperator(new DefaultFunctionLibraryInjector());
41      seq.AddSubOperator(new HL3TreeEvaluatorInjector());
42      op.OperatorGraph.AddOperator(seq);
43      op.OperatorGraph.InitialOperator = seq;
44      return op;
45    }
46
47    public static IOperator CreateInitialPopulationEvaluator() {
48      CombinedOperator combinedOp = new CombinedOperator();
49      combinedOp.Name = "Evaluator";
50      SequentialProcessor seq = new SequentialProcessor();
51
52      SimpleEvaluator eval = new SimpleEvaluator();
53      eval.GetVariableInfo("Values").ActualName = "TrainingValues";
54      eval.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
55      eval.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
56
57      LinearScaler scaler = new LinearScaler();
58      scaler.GetVariableInfo("Values").ActualName = "TrainingValues";
59
60      SimpleMSEEvaluator mseEval = new SimpleMSEEvaluator();
61      mseEval.GetVariableInfo("Values").ActualName = "TrainingValues";
62      mseEval.GetVariableInfo("MSE").ActualName = "Quality";
63
64      seq.AddSubOperator(eval);
65      seq.AddSubOperator(scaler);
66      seq.AddSubOperator(mseEval);
67      combinedOp.OperatorGraph.AddOperator(seq);
68      combinedOp.OperatorGraph.InitialOperator = seq;
69      return combinedOp;
70    }
71
72    public static IOperator CreateEvaluator() {
73      return CreateInitialPopulationEvaluator();
74    }
75
76    public static IOperator CreateGenerationStepHook() {
77      CombinedOperator op = new CombinedOperator();
78      SequentialProcessor seq = new SequentialProcessor();
79
80      SymbolFrequencyAnalyser symbolFrequencyAnalyser = new SymbolFrequencyAnalyser();
81      seq.AddSubOperator(symbolFrequencyAnalyser);
82
83      VariableFrequencyAnalyser varFrequencyAnalyser = new VariableFrequencyAnalyser();
84      seq.AddSubOperator(varFrequencyAnalyser);
85
86      UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
87      SequentialProcessor individualProc = new SequentialProcessor();
88      SimpleEvaluator validationEvaluator = new SimpleEvaluator();
89      validationEvaluator.Name = "ValidationEvaluator";
90      validationEvaluator.GetVariableInfo("Values").ActualName = "ValidationValues";
91      validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
92      validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
93      LinearScaler validationScaler = new LinearScaler();
94      validationScaler.GetVariableInfo("Values").ActualName = "ValidationValues";
95      SimpleMSEEvaluator mseEval = new SimpleMSEEvaluator();
96      mseEval.GetVariableInfo("Values").ActualName = "ValidationValues";
97      mseEval.GetVariableInfo("MSE").ActualName = "ValidationQuality";
98      individualProc.AddSubOperator(validationEvaluator);
99      individualProc.AddSubOperator(validationScaler);
100      individualProc.AddSubOperator(mseEval);
101
102      Counter bestValidationSolutionAgeCounter = new Counter();
103      bestValidationSolutionAgeCounter.Name = "BestSolutionAgeCounter";
104      bestValidationSolutionAgeCounter.GetVariableInfo("Value").ActualName = "BestValidationSolutionAge";
105
106      BestSolutionStorer solutionStorer = new BestSolutionStorer();
107      solutionStorer.GetVariableInfo("BestSolution").ActualName = "BestValidationSolution";
108      solutionStorer.GetVariableInfo("Quality").ActualName = "ValidationQuality";
109
110      OperatorExtractor bestSolutionProcessor = new OperatorExtractor();
111      bestSolutionProcessor.Name = "BestSolutionProcessor (extr.)";
112      bestSolutionProcessor.GetVariableInfo("Operator").ActualName = "BestSolutionProcessor";
113
114      solutionStorer.AddSubOperator(bestSolutionProcessor);
115
116      BestAverageWorstQualityCalculator validationQualityCalculator = new BestAverageWorstQualityCalculator();
117      validationQualityCalculator.Name = "BestAverageWorstValidationQualityCalculator";
118      validationQualityCalculator.GetVariableInfo("Quality").ActualName = "ValidationQuality";
119      validationQualityCalculator.GetVariableInfo("BestQuality").ActualName = "BestValidationQuality";
120      validationQualityCalculator.GetVariableInfo("AverageQuality").ActualName = "AverageValidationQuality";
121      validationQualityCalculator.GetVariableInfo("WorstQuality").ActualName = "WorstValidationQuality";
122
123      subScopesProc.AddSubOperator(individualProc);
124
125      seq.AddSubOperator(subScopesProc);
126      seq.AddSubOperator(bestValidationSolutionAgeCounter);
127      seq.AddSubOperator(solutionStorer);
128      seq.AddSubOperator(validationQualityCalculator);
129
130      op.OperatorGraph.AddOperator(seq);
131      op.OperatorGraph.InitialOperator = seq;
132      return op;
133    }
134
135
136    public static IOperator CreatePreparationForPostProcessingOperator() {
137      SequentialProcessor seq = new SequentialProcessor();
138      LeftReducer cleanUp = new LeftReducer();
139      cleanUp.Name = "Reset Population";
140      seq.AddSubOperator(cleanUp);
141
142      SolutionExtractor extractor = new SolutionExtractor();
143      extractor.GetVariableInfo("Scope").ActualName = "BestValidationSolution";
144      SequentialSubScopesProcessor seqSubScopeProc = new SequentialSubScopesProcessor();
145      SequentialProcessor solutionProc = new SequentialProcessor();
146
147      seq.AddSubOperator(extractor);
148      seq.AddSubOperator(seqSubScopeProc);
149      seqSubScopeProc.AddSubOperator(solutionProc);
150
151      #region simple evaluators
152      SimpleEvaluator trainingEvaluator = new SimpleEvaluator();
153      trainingEvaluator.Name = "TrainingEvaluator";
154      trainingEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
155      trainingEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
156      trainingEvaluator.GetVariableInfo("Values").ActualName = "TrainingValues";
157      SimpleEvaluator validationEvaluator = new SimpleEvaluator();
158      validationEvaluator.Name = "ValidationEvaluator";
159      validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
160      validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
161      validationEvaluator.GetVariableInfo("Values").ActualName = "ValidationValues";
162      SimpleEvaluator testEvaluator = new SimpleEvaluator();
163      testEvaluator.Name = "TestEvaluator";
164      testEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
165      testEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
166      testEvaluator.GetVariableInfo("Values").ActualName = "TestValues";
167
168      LinearScaler trainingScaler = new LinearScaler();
169      trainingScaler.GetVariableInfo("Values").ActualName = "TrainingValues";
170
171      LinearScaler validationScaler = new LinearScaler();
172      validationScaler.GetVariableInfo("Values").ActualName = "ValidationValues";
173
174      LinearScaler testScaler = new LinearScaler();
175      testScaler.GetVariableInfo("Values").ActualName = "TestValues";
176
177      solutionProc.AddSubOperator(trainingEvaluator);
178      solutionProc.AddSubOperator(validationEvaluator);
179      solutionProc.AddSubOperator(testEvaluator);
180      solutionProc.AddSubOperator(trainingScaler);
181      solutionProc.AddSubOperator(validationScaler);
182      solutionProc.AddSubOperator(testScaler);
183      #endregion
184
185      #region variable impacts
186      // calculate and set variable impacts
187      VariableNamesExtractor namesExtractor = new VariableNamesExtractor();
188      namesExtractor.GetVariableInfo("VariableNames").ActualName = "InputVariableNames";
189      LinearScalingPredictorBuilder predictorBuilder = new LinearScalingPredictorBuilder();
190
191      solutionProc.AddSubOperator(namesExtractor);
192      solutionProc.AddSubOperator(predictorBuilder);
193      VariableQualityImpactCalculator qualityImpactCalculator = new VariableQualityImpactCalculator();
194      qualityImpactCalculator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
195      qualityImpactCalculator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
196
197      solutionProc.AddSubOperator(qualityImpactCalculator);
198
199      NodeBasedVariableImpactCalculator nodeImpactCalculator = new NodeBasedVariableImpactCalculator();
200      nodeImpactCalculator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
201      nodeImpactCalculator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
202
203      solutionProc.AddSubOperator(nodeImpactCalculator);
204
205      VariableFrequencyBasedImpactCalculator frequencyImpactCalculator = new VariableFrequencyBasedImpactCalculator();
206      solutionProc.AddSubOperator(frequencyImpactCalculator);
207
208      #endregion
209
210      return seq;
211    }
212
213    public static void PopulateAnalyzerModel(IScope bestModelScope, IAnalyzerModel model) {
214      model.SetMetaData("EvaluatedSolutions", bestModelScope.GetVariableValue<IntData>("EvaluatedSolutions", false).Data);
215      IGeneticProgrammingModel gpModel = bestModelScope.GetVariableValue<IGeneticProgrammingModel>("FunctionTree", false);
216      model.SetMetaData("TreeSize", gpModel.Size);
217      model.SetMetaData("TreeHeight", gpModel.Height);
218      double treeComplexity = TreeComplexityEvaluator.Calculate(gpModel.FunctionTree);
219      model.SetMetaData("TreeComplexity", treeComplexity);
220      model.SetMetaData("AverageNodeComplexity", treeComplexity / gpModel.Size);
221      #region variable impacts
222      ItemList qualityImpacts = bestModelScope.GetVariableValue<ItemList>(ModelingResult.VariableQualityImpact.ToString(), false);
223      foreach (ItemList row in qualityImpacts) {
224        string variableName = ((StringData)row[0]).Data;
225        double impact = ((DoubleData)row[1]).Data;
226        model.SetVariableResult(ModelingResult.VariableQualityImpact, variableName, impact);
227        model.AddInputVariable(variableName);
228      }
229      ItemList nodeImpacts = bestModelScope.GetVariableValue<ItemList>(ModelingResult.VariableNodeImpact.ToString(), false);
230      foreach (ItemList row in nodeImpacts) {
231        string variableName = ((StringData)row[0]).Data;
232        double impact = ((DoubleData)row[1]).Data;
233        model.SetVariableResult(ModelingResult.VariableNodeImpact, variableName, impact);
234        model.AddInputVariable(variableName);
235      }
236      ItemList frequencyImpacts = bestModelScope.GetVariableValue<ItemList>(ModelingResult.RelativeFrequencyVariableImpact.ToString(), false);
237      foreach (ItemList row in frequencyImpacts) {
238        string variableName = ((StringData)row[0]).Data;
239        double impact = ((DoubleData)row[1]).Data;
240        model.SetVariableResult(ModelingResult.RelativeFrequencyVariableImpact, variableName, impact);
241        model.AddInputVariable(variableName);
242      }
243
244      #endregion
245
246    }
247
248  }
249}
Note: See TracBrowser for help on using the repository browser.