[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;
|
---|
[2341] | 32 |
|
---|
| 33 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
[2356] | 34 | public static class DefaultStructureIdentificationOperators {
|
---|
[2341] | 35 | public static IOperator CreateFunctionLibraryInjector() {
|
---|
| 36 | CombinedOperator op = new CombinedOperator();
|
---|
| 37 | op.Name = "FunctionLibraryInjector";
|
---|
| 38 | SequentialProcessor seq = new SequentialProcessor();
|
---|
[2566] | 39 | seq.AddSubOperator(new DefaultFunctionLibraryInjector());
|
---|
[2341] | 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 |
|
---|
[2385] | 72 | Counter bestValidationSolutionAgeCounter = new Counter();
|
---|
| 73 | bestValidationSolutionAgeCounter.Name = "BestSolutionAgeCounter";
|
---|
| 74 | bestValidationSolutionAgeCounter.GetVariableInfo("Value").ActualName = "BestValidationSolutionAge";
|
---|
| 75 |
|
---|
[2341] | 76 | BestSolutionStorer solutionStorer = new BestSolutionStorer();
|
---|
| 77 | solutionStorer.GetVariableInfo("BestSolution").ActualName = "BestValidationSolution";
|
---|
| 78 | solutionStorer.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
| 79 |
|
---|
[2362] | 80 | OperatorExtractor bestSolutionProcessor = new OperatorExtractor();
|
---|
| 81 | bestSolutionProcessor.Name = "BestSolutionProcessor (extr.)";
|
---|
| 82 | bestSolutionProcessor.GetVariableInfo("Operator").ActualName = "BestSolutionProcessor";
|
---|
| 83 |
|
---|
| 84 | solutionStorer.AddSubOperator(bestSolutionProcessor);
|
---|
| 85 |
|
---|
[2341] | 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);
|
---|
[2385] | 96 | seq.AddSubOperator(bestValidationSolutionAgeCounter);
|
---|
[2341] | 97 | seq.AddSubOperator(solutionStorer);
|
---|
| 98 | seq.AddSubOperator(validationQualityCalculator);
|
---|
| 99 |
|
---|
| 100 | op.OperatorGraph.AddOperator(seq);
|
---|
| 101 | op.OperatorGraph.InitialOperator = seq;
|
---|
| 102 | return op;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[2356] | 105 |
|
---|
| 106 | public static IOperator CreatePreparationForPostProcessingOperator() {
|
---|
[2341] | 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 |
|
---|
[2351] | 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";
|
---|
[2341] | 144 | solutionProc.AddSubOperator(evaluatorInjector);
|
---|
[2351] | 145 | solutionProc.AddSubOperator(trainingEvaluator);
|
---|
| 146 | solutionProc.AddSubOperator(validationEvaluator);
|
---|
| 147 | solutionProc.AddSubOperator(testEvaluator);
|
---|
[2356] | 148 | #endregion
|
---|
[2341] | 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);
|
---|
[2454] | 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();
|
---|
[2632] | 166 | nodeImpactCalculator.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
|
---|
[2454] | 167 | nodeImpactCalculator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
| 168 | nodeImpactCalculator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
| 169 |
|
---|
| 170 | solutionProc.AddSubOperator(nodeImpactCalculator);
|
---|
| 171 |
|
---|
[2341] | 172 | #endregion
|
---|
| 173 |
|
---|
[2356] | 174 | return seq;
|
---|
[2341] | 175 | }
|
---|
| 176 |
|
---|
[2356] | 177 | public static void PopulateAnalyzerModel(IScope bestModelScope, IAnalyzerModel model) {
|
---|
[2363] | 178 | model.SetMetaData("EvaluatedSolutions", bestModelScope.GetVariableValue<IntData>("EvaluatedSolutions", false).Data);
|
---|
[2356] | 179 | IGeneticProgrammingModel gpModel = bestModelScope.GetVariableValue<IGeneticProgrammingModel>("FunctionTree", false);
|
---|
[2344] | 180 | model.SetMetaData("TreeSize", gpModel.Size);
|
---|
| 181 | model.SetMetaData("TreeHeight", gpModel.Height);
|
---|
[2568] | 182 | double treeComplexity = TreeComplexityEvaluator.Calculate(gpModel.FunctionTree);
|
---|
| 183 | model.SetMetaData("TreeComplexity", treeComplexity);
|
---|
| 184 | model.SetMetaData("AverageNodeComplexity", treeComplexity / gpModel.Size);
|
---|
[2454] | 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 |
|
---|
[2341] | 202 | }
|
---|
[2568] | 203 |
|
---|
[2341] | 204 | }
|
---|
| 205 | }
|
---|