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