Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented operator to calculate variable impacts based on the log of variable frequencies over the whole GP run and integrated operators into default SGP and OSGP algorithms. #853 (Operator to calculate variable impacts as integral over the relative frequencies of variable references over the whole GP run and the whole population)

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