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 |
|
---|
22 | using HeuristicLab.Core;
|
---|
23 | using HeuristicLab.DataAnalysis;
|
---|
24 | using HeuristicLab.GP.Interfaces;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Modeling;
|
---|
27 | using HeuristicLab.Logging;
|
---|
28 | using HeuristicLab.Selection;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using System.Collections.Generic;
|
---|
31 | using System;
|
---|
32 | using HeuristicLab.GP.Operators;
|
---|
33 |
|
---|
34 | namespace 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 ScalingTreeEvaluatorInjector());
|
---|
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 | ScalingTreeEvaluatorInjector evaluatorInjector = new ScalingTreeEvaluatorInjector();
|
---|
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 | LinearScalingPredictorBuilder predictorBuilder = new LinearScalingPredictorBuilder();
|
---|
163 |
|
---|
164 | solutionProc.AddSubOperator(namesExtractor);
|
---|
165 | solutionProc.AddSubOperator(predictorBuilder);
|
---|
166 | VariableQualityImpactCalculator qualityImpactCalculator = new VariableQualityImpactCalculator();
|
---|
167 | qualityImpactCalculator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
168 | qualityImpactCalculator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
169 |
|
---|
170 | solutionProc.AddSubOperator(qualityImpactCalculator);
|
---|
171 |
|
---|
172 | NodeBasedVariableImpactCalculator nodeImpactCalculator = new NodeBasedVariableImpactCalculator();
|
---|
173 | nodeImpactCalculator.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
|
---|
174 | nodeImpactCalculator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
175 | nodeImpactCalculator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
176 |
|
---|
177 | solutionProc.AddSubOperator(nodeImpactCalculator);
|
---|
178 |
|
---|
179 | VariableFrequencyBasedImpactCalculator frequencyImpactCalculator = new VariableFrequencyBasedImpactCalculator();
|
---|
180 | solutionProc.AddSubOperator(frequencyImpactCalculator);
|
---|
181 |
|
---|
182 | #endregion
|
---|
183 |
|
---|
184 | return seq;
|
---|
185 | }
|
---|
186 |
|
---|
187 | public static void PopulateAnalyzerModel(IScope bestModelScope, IAnalyzerModel model) {
|
---|
188 | model.SetMetaData("EvaluatedSolutions", bestModelScope.GetVariableValue<IntData>("EvaluatedSolutions", false).Data);
|
---|
189 | IGeneticProgrammingModel gpModel = bestModelScope.GetVariableValue<IGeneticProgrammingModel>("FunctionTree", false);
|
---|
190 | model.SetMetaData("TreeSize", gpModel.Size);
|
---|
191 | model.SetMetaData("TreeHeight", gpModel.Height);
|
---|
192 | double treeComplexity = TreeComplexityEvaluator.Calculate(gpModel.FunctionTree);
|
---|
193 | model.SetMetaData("TreeComplexity", treeComplexity);
|
---|
194 | model.SetMetaData("AverageNodeComplexity", treeComplexity / gpModel.Size);
|
---|
195 | #region variable impacts
|
---|
196 | ItemList qualityImpacts = bestModelScope.GetVariableValue<ItemList>(ModelingResult.VariableQualityImpact.ToString(), false);
|
---|
197 | foreach (ItemList row in qualityImpacts) {
|
---|
198 | string variableName = ((StringData)row[0]).Data;
|
---|
199 | double impact = ((DoubleData)row[1]).Data;
|
---|
200 | model.SetVariableResult(ModelingResult.VariableQualityImpact, variableName, impact);
|
---|
201 | model.AddInputVariable(variableName);
|
---|
202 | }
|
---|
203 | ItemList nodeImpacts = bestModelScope.GetVariableValue<ItemList>(ModelingResult.VariableNodeImpact.ToString(), false);
|
---|
204 | foreach (ItemList row in nodeImpacts) {
|
---|
205 | string variableName = ((StringData)row[0]).Data;
|
---|
206 | double impact = ((DoubleData)row[1]).Data;
|
---|
207 | model.SetVariableResult(ModelingResult.VariableNodeImpact, variableName, impact);
|
---|
208 | model.AddInputVariable(variableName);
|
---|
209 | }
|
---|
210 | ItemList frequencyImpacts = bestModelScope.GetVariableValue<ItemList>(ModelingResult.RelativeFrequencyVariableImpact.ToString(), false);
|
---|
211 | foreach (ItemList row in frequencyImpacts) {
|
---|
212 | string variableName = ((StringData)row[0]).Data;
|
---|
213 | double impact = ((DoubleData)row[1]).Data;
|
---|
214 | model.SetVariableResult(ModelingResult.RelativeFrequencyVariableImpact, variableName, impact);
|
---|
215 | model.AddInputVariable(variableName);
|
---|
216 | }
|
---|
217 |
|
---|
218 | #endregion
|
---|
219 |
|
---|
220 | }
|
---|
221 |
|
---|
222 | }
|
---|
223 | }
|
---|