Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed #848 (NodeBasedVariableQualityImpactCalucator uses incorrect tree evaluator to calculate variable impacts in hard coded GP algorithms for structure identification).

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