[2341] | 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;
|
---|
[2568] | 30 | using System.Collections.Generic;
|
---|
| 31 | using System;
|
---|
[2683] | 32 | using HeuristicLab.GP.Operators;
|
---|
[2341] | 33 |
|
---|
| 34 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
[2356] | 35 | public static class DefaultStructureIdentificationOperators {
|
---|
[2341] | 36 | public static IOperator CreateFunctionLibraryInjector() {
|
---|
| 37 | CombinedOperator op = new CombinedOperator();
|
---|
| 38 | op.Name = "FunctionLibraryInjector";
|
---|
| 39 | SequentialProcessor seq = new SequentialProcessor();
|
---|
[2566] | 40 | seq.AddSubOperator(new DefaultFunctionLibraryInjector());
|
---|
[2977] | 41 | seq.AddSubOperator(new HL3TreeEvaluatorInjector());
|
---|
[2341] | 42 | op.OperatorGraph.AddOperator(seq);
|
---|
| 43 | op.OperatorGraph.InitialOperator = seq;
|
---|
| 44 | return op;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public static IOperator CreateInitialPopulationEvaluator() {
|
---|
[2977] | 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";
|
---|
[2341] | 54 | eval.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
|
---|
| 55 | eval.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
|
---|
[2977] | 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;
|
---|
[2341] | 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();
|
---|
[2683] | 79 |
|
---|
| 80 | SymbolFrequencyAnalyser symbolFrequencyAnalyser = new SymbolFrequencyAnalyser();
|
---|
| 81 | seq.AddSubOperator(symbolFrequencyAnalyser);
|
---|
| 82 |
|
---|
| 83 | VariableFrequencyAnalyser varFrequencyAnalyser = new VariableFrequencyAnalyser();
|
---|
| 84 | seq.AddSubOperator(varFrequencyAnalyser);
|
---|
| 85 |
|
---|
[2341] | 86 | UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
|
---|
| 87 | SequentialProcessor individualProc = new SequentialProcessor();
|
---|
[2977] | 88 | SimpleEvaluator validationEvaluator = new SimpleEvaluator();
|
---|
[2341] | 89 | validationEvaluator.Name = "ValidationEvaluator";
|
---|
[2977] | 90 | validationEvaluator.GetVariableInfo("Values").ActualName = "ValidationValues";
|
---|
[2341] | 91 | validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
| 92 | validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
[2977] | 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";
|
---|
[2341] | 98 | individualProc.AddSubOperator(validationEvaluator);
|
---|
[2977] | 99 | individualProc.AddSubOperator(validationScaler);
|
---|
| 100 | individualProc.AddSubOperator(mseEval);
|
---|
[2341] | 101 |
|
---|
[2385] | 102 | Counter bestValidationSolutionAgeCounter = new Counter();
|
---|
| 103 | bestValidationSolutionAgeCounter.Name = "BestSolutionAgeCounter";
|
---|
| 104 | bestValidationSolutionAgeCounter.GetVariableInfo("Value").ActualName = "BestValidationSolutionAge";
|
---|
| 105 |
|
---|
[2341] | 106 | BestSolutionStorer solutionStorer = new BestSolutionStorer();
|
---|
| 107 | solutionStorer.GetVariableInfo("BestSolution").ActualName = "BestValidationSolution";
|
---|
| 108 | solutionStorer.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
| 109 |
|
---|
[2362] | 110 | OperatorExtractor bestSolutionProcessor = new OperatorExtractor();
|
---|
| 111 | bestSolutionProcessor.Name = "BestSolutionProcessor (extr.)";
|
---|
| 112 | bestSolutionProcessor.GetVariableInfo("Operator").ActualName = "BestSolutionProcessor";
|
---|
| 113 |
|
---|
| 114 | solutionStorer.AddSubOperator(bestSolutionProcessor);
|
---|
| 115 |
|
---|
[2341] | 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);
|
---|
[2385] | 126 | seq.AddSubOperator(bestValidationSolutionAgeCounter);
|
---|
[2341] | 127 | seq.AddSubOperator(solutionStorer);
|
---|
| 128 | seq.AddSubOperator(validationQualityCalculator);
|
---|
| 129 |
|
---|
| 130 | op.OperatorGraph.AddOperator(seq);
|
---|
| 131 | op.OperatorGraph.InitialOperator = seq;
|
---|
| 132 | return op;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[2356] | 135 |
|
---|
| 136 | public static IOperator CreatePreparationForPostProcessingOperator() {
|
---|
[2341] | 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 |
|
---|
[2351] | 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";
|
---|
[2977] | 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();
|
---|
[2979] | 175 | testScaler.GetVariableInfo("Values").ActualName = "TestValues";
|
---|
[2977] | 176 |
|
---|
[2351] | 177 | solutionProc.AddSubOperator(trainingEvaluator);
|
---|
| 178 | solutionProc.AddSubOperator(validationEvaluator);
|
---|
| 179 | solutionProc.AddSubOperator(testEvaluator);
|
---|
[2977] | 180 | solutionProc.AddSubOperator(trainingScaler);
|
---|
| 181 | solutionProc.AddSubOperator(validationScaler);
|
---|
| 182 | solutionProc.AddSubOperator(testScaler);
|
---|
[2356] | 183 | #endregion
|
---|
[2341] | 184 |
|
---|
| 185 | #region variable impacts
|
---|
| 186 | // calculate and set variable impacts
|
---|
| 187 | VariableNamesExtractor namesExtractor = new VariableNamesExtractor();
|
---|
| 188 | namesExtractor.GetVariableInfo("VariableNames").ActualName = "InputVariableNames";
|
---|
[2722] | 189 | LinearScalingPredictorBuilder predictorBuilder = new LinearScalingPredictorBuilder();
|
---|
[2341] | 190 |
|
---|
| 191 | solutionProc.AddSubOperator(namesExtractor);
|
---|
| 192 | solutionProc.AddSubOperator(predictorBuilder);
|
---|
[2454] | 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 |
|
---|
[2683] | 205 | VariableFrequencyBasedImpactCalculator frequencyImpactCalculator = new VariableFrequencyBasedImpactCalculator();
|
---|
| 206 | solutionProc.AddSubOperator(frequencyImpactCalculator);
|
---|
| 207 |
|
---|
[2341] | 208 | #endregion
|
---|
| 209 |
|
---|
[2356] | 210 | return seq;
|
---|
[2341] | 211 | }
|
---|
| 212 |
|
---|
[2356] | 213 | public static void PopulateAnalyzerModel(IScope bestModelScope, IAnalyzerModel model) {
|
---|
[2363] | 214 | model.SetMetaData("EvaluatedSolutions", bestModelScope.GetVariableValue<IntData>("EvaluatedSolutions", false).Data);
|
---|
[2356] | 215 | IGeneticProgrammingModel gpModel = bestModelScope.GetVariableValue<IGeneticProgrammingModel>("FunctionTree", false);
|
---|
[2344] | 216 | model.SetMetaData("TreeSize", gpModel.Size);
|
---|
| 217 | model.SetMetaData("TreeHeight", gpModel.Height);
|
---|
[2568] | 218 | double treeComplexity = TreeComplexityEvaluator.Calculate(gpModel.FunctionTree);
|
---|
| 219 | model.SetMetaData("TreeComplexity", treeComplexity);
|
---|
| 220 | model.SetMetaData("AverageNodeComplexity", treeComplexity / gpModel.Size);
|
---|
[2454] | 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 | }
|
---|
[2683] | 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 |
|
---|
[2454] | 244 | #endregion
|
---|
| 245 |
|
---|
[2341] | 246 | }
|
---|
[2568] | 247 |
|
---|
[2341] | 248 | }
|
---|
| 249 | }
|
---|