[2335] | 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.Operators;
|
---|
| 24 | using HeuristicLab.Selection;
|
---|
| 25 | using HeuristicLab.Logging;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.GP.Operators;
|
---|
| 28 | using HeuristicLab.Modeling;
|
---|
| 29 | using System.Collections.Generic;
|
---|
| 30 | using System;
|
---|
[2375] | 31 | using System.Linq;
|
---|
[2340] | 32 | using HeuristicLab.DataAnalysis;
|
---|
[2362] | 33 | using HeuristicLab.Operators.Programmable;
|
---|
[2385] | 34 | using HeuristicLab.GP.Algorithms;
|
---|
[2335] | 35 |
|
---|
| 36 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
[2363] | 37 | public class StandardGPRegression : HeuristicLab.GP.Algorithms.StandardGP, IStochasticAlgorithm {
|
---|
[2335] | 38 |
|
---|
| 39 | public override string Name { get { return "StandardGP - StructureIdentification"; } }
|
---|
[2337] | 40 |
|
---|
[2440] | 41 | public virtual string TargetVariable {
|
---|
| 42 | get { return ProblemInjector.GetVariableValue<StringData>("TargetVariable", null, false).Data; }
|
---|
[2977] | 43 | set {
|
---|
| 44 | ProblemInjector.GetVariableValue<StringData>("TargetVariable", null, false).Data = value;
|
---|
| 45 | }
|
---|
[2337] | 46 | }
|
---|
| 47 |
|
---|
[2340] | 48 | public virtual Dataset Dataset {
|
---|
| 49 | get { return ProblemInjector.GetVariableValue<Dataset>("Dataset", null, false); }
|
---|
[2977] | 50 | set {
|
---|
| 51 | ProblemInjector.GetVariable("Dataset").Value = value;
|
---|
| 52 | }
|
---|
[2340] | 53 | }
|
---|
| 54 |
|
---|
| 55 | public virtual IAnalyzerModel Model {
|
---|
[2337] | 56 | get {
|
---|
[2340] | 57 | if (!Engine.Terminated)
|
---|
| 58 | throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
|
---|
| 59 | else
|
---|
| 60 | return CreateGPModel();
|
---|
[2337] | 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[2440] | 64 | public IEnumerable<string> AllowedVariables {
|
---|
[2375] | 65 | get {
|
---|
[2440] | 66 | ItemList<StringData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<StringData>>("AllowedFeatures", null, false);
|
---|
[2375] | 67 | return allowedVariables.Select(x => x.Data);
|
---|
| 68 | }
|
---|
| 69 | set {
|
---|
[2440] | 70 | ItemList<StringData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<StringData>>("AllowedFeatures", null, false);
|
---|
| 71 | foreach (string x in value) allowedVariables.Add(new StringData(x));
|
---|
[2375] | 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public int TrainingSamplesStart {
|
---|
| 76 | get { return ProblemInjector.GetVariableValue<IntData>("TrainingSamplesStart", null, false).Data; }
|
---|
| 77 | set { ProblemInjector.GetVariableValue<IntData>("TrainingSamplesStart", null, false).Data = value; }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | public int TrainingSamplesEnd {
|
---|
| 81 | get { return ProblemInjector.GetVariableValue<IntData>("TrainingSamplesEnd", null, false).Data; }
|
---|
| 82 | set { ProblemInjector.GetVariableValue<IntData>("TrainingSamplesEnd", null, false).Data = value; }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public int ValidationSamplesStart {
|
---|
| 86 | get { return ProblemInjector.GetVariableValue<IntData>("ValidationSamplesStart", null, false).Data; }
|
---|
| 87 | set { ProblemInjector.GetVariableValue<IntData>("ValidationSamplesStart", null, false).Data = value; }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public int ValidationSamplesEnd {
|
---|
| 91 | get { return ProblemInjector.GetVariableValue<IntData>("ValidationSamplesEnd", null, false).Data; }
|
---|
| 92 | set { ProblemInjector.GetVariableValue<IntData>("ValidationSamplesEnd", null, false).Data = value; }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | public int TestSamplesStart {
|
---|
| 96 | get { return ProblemInjector.GetVariableValue<IntData>("TestSamplesStart", null, false).Data; }
|
---|
| 97 | set { ProblemInjector.GetVariableValue<IntData>("TestSamplesStart", null, false).Data = value; }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public int TestSamplesEnd {
|
---|
| 101 | get { return ProblemInjector.GetVariableValue<IntData>("TestSamplesEnd", null, false).Data; }
|
---|
| 102 | set { ProblemInjector.GetVariableValue<IntData>("TestSamplesEnd", null, false).Data = value; }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[2335] | 105 | public virtual double PunishmentFactor {
|
---|
| 106 | get { return GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data; }
|
---|
[2977] | 107 | set {
|
---|
| 108 | GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data = value;
|
---|
| 109 | }
|
---|
[2335] | 110 | }
|
---|
| 111 |
|
---|
[2977] | 112 | public virtual double UpperEstimationLimit {
|
---|
| 113 | get { return GetVariableInjector().GetVariable("UpperEstimationLimit").GetValue<DoubleData>().Data; }
|
---|
| 114 | set { GetVariableInjector().GetVariable("UpperEstimationLimit").GetValue<DoubleData>().Data = value; }
|
---|
| 115 | }
|
---|
| 116 | public virtual double LowerEstimationLimit {
|
---|
| 117 | get { return GetVariableInjector().GetVariable("LowerEstimationLimit").GetValue<DoubleData>().Data; }
|
---|
| 118 | set { GetVariableInjector().GetVariable("LowerEstimationLimit").GetValue<DoubleData>().Data = value; }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[2385] | 121 | public virtual int MaxBestValidationSolutionAge {
|
---|
| 122 | get { return GetVariableInjector().GetVariable("MaxBestValidationSolutionAge").GetValue<IntData>().Data; }
|
---|
| 123 | set { GetVariableInjector().GetVariable("MaxBestValidationSolutionAge").GetValue<IntData>().Data = value; }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[2340] | 126 | public override IOperator ProblemInjector {
|
---|
| 127 | get { return GetProblemInjector().OperatorGraph.InitialOperator.SubOperators[0]; }
|
---|
| 128 | set {
|
---|
| 129 | value.Name = "ProblemInjector";
|
---|
| 130 | CombinedOperator problemInjector = GetProblemInjector();
|
---|
| 131 | problemInjector.OperatorGraph.RemoveOperator(ProblemInjector.Guid);
|
---|
| 132 | problemInjector.OperatorGraph.AddOperator(value);
|
---|
| 133 | problemInjector.OperatorGraph.InitialOperator.AddSubOperator(value, 0);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[2566] | 137 | public override IOperator FunctionLibraryInjector {
|
---|
| 138 | get {
|
---|
| 139 | CombinedOperator funLibInjector = (CombinedOperator)GetInitializationOperator().SubOperators[1];
|
---|
| 140 | return funLibInjector.OperatorGraph.InitialOperator.SubOperators[0];
|
---|
| 141 | }
|
---|
| 142 | set {
|
---|
| 143 | CombinedOperator funLibInjector = (CombinedOperator)GetInitializationOperator().SubOperators[1];
|
---|
| 144 | IOperator seq = funLibInjector.OperatorGraph.InitialOperator;
|
---|
| 145 | seq.RemoveSubOperator(0);
|
---|
| 146 | seq.AddSubOperator(value, 0);
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[2361] | 150 | public StandardGPRegression()
|
---|
[2335] | 151 | : base() {
|
---|
| 152 | PunishmentFactor = 10.0;
|
---|
[2395] | 153 | MaxBestValidationSolutionAge = 30;
|
---|
[2335] | 154 | }
|
---|
| 155 |
|
---|
| 156 | protected override IOperator CreateFunctionLibraryInjector() {
|
---|
[2356] | 157 | return DefaultStructureIdentificationOperators.CreateFunctionLibraryInjector();
|
---|
[2335] | 158 | }
|
---|
| 159 |
|
---|
| 160 | protected override IOperator CreateProblemInjector() {
|
---|
[2356] | 161 | return DefaultRegressionOperators.CreateProblemInjector();
|
---|
[2335] | 162 | }
|
---|
| 163 |
|
---|
| 164 | protected override IOperator CreateInitialPopulationEvaluator() {
|
---|
[2356] | 165 | return DefaultStructureIdentificationOperators.CreateInitialPopulationEvaluator();
|
---|
[2335] | 166 | }
|
---|
| 167 |
|
---|
| 168 | protected override IOperator CreateEvaluationOperator() {
|
---|
[2356] | 169 | return DefaultStructureIdentificationOperators.CreateEvaluator();
|
---|
[2335] | 170 | }
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | protected override IOperator CreateGenerationStepHook() {
|
---|
[2363] | 174 | IOperator op = DefaultStructureIdentificationOperators.CreateGenerationStepHook();
|
---|
| 175 | op.AddSubOperator(CreateBestSolutionProcessor());
|
---|
| 176 | return op;
|
---|
[2335] | 177 | }
|
---|
| 178 |
|
---|
[2363] | 179 | private IOperator CreateBestSolutionProcessor() {
|
---|
| 180 | CombinedOperator op = new CombinedOperator();
|
---|
| 181 | op.Name = "BestSolutionProcessor";
|
---|
| 182 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 183 |
|
---|
| 184 | ProgrammableOperator evaluatedSolutionsStorer = new ProgrammableOperator();
|
---|
| 185 | evaluatedSolutionsStorer.RemoveVariableInfo("Result");
|
---|
| 186 | evaluatedSolutionsStorer.AddVariableInfo(new VariableInfo("Input", "Value to copy", typeof(IntData), VariableKind.In));
|
---|
| 187 | evaluatedSolutionsStorer.AddVariableInfo(new VariableInfo("Output", "Value to write", typeof(IntData), VariableKind.New));
|
---|
| 188 | evaluatedSolutionsStorer.GetVariableInfo("Input").ActualName = "EvaluatedSolutions";
|
---|
| 189 | evaluatedSolutionsStorer.GetVariableInfo("Output").ActualName = "EvaluatedSolutions";
|
---|
| 190 | evaluatedSolutionsStorer.Code = "Output.Data = Input.Data;";
|
---|
| 191 |
|
---|
[2385] | 192 | ProgrammableOperator resetBestSolutionAge = new ProgrammableOperator();
|
---|
| 193 | resetBestSolutionAge.RemoveVariable("Result");
|
---|
| 194 | resetBestSolutionAge.AddVariableInfo(
|
---|
| 195 | new VariableInfo("BestValidationSolutionAge", "Age of best validation solution", typeof(IntData), VariableKind.In | VariableKind.Out));
|
---|
| 196 | resetBestSolutionAge.Code = "BestValidationSolutionAge.Data = 0;";
|
---|
| 197 |
|
---|
[2363] | 198 | seq.AddSubOperator(evaluatedSolutionsStorer);
|
---|
[2385] | 199 | seq.AddSubOperator(resetBestSolutionAge);
|
---|
[2363] | 200 |
|
---|
| 201 | op.OperatorGraph.AddOperator(seq);
|
---|
| 202 | op.OperatorGraph.InitialOperator = seq;
|
---|
| 203 | return op;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 |
|
---|
[2335] | 207 | protected override VariableInjector CreateGlobalInjector() {
|
---|
| 208 | VariableInjector injector = base.CreateGlobalInjector();
|
---|
| 209 | injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
|
---|
[2977] | 210 | // NB: LowerEstimationLimit and UpperEstimationLimit should replace the direct use of PunishmentFactor in the algorithm (gkronber 9 March, 2010)
|
---|
| 211 | injector.AddVariable(new HeuristicLab.Core.Variable("LowerEstimationLimit", new DoubleData(double.NegativeInfinity)));
|
---|
| 212 | injector.AddVariable(new HeuristicLab.Core.Variable("UpperEstimationLimit", new DoubleData(double.PositiveInfinity)));
|
---|
[2385] | 213 | injector.AddVariable(new HeuristicLab.Core.Variable("BestValidationSolutionAge", new IntData()));
|
---|
| 214 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxBestValidationSolutionAge", new IntData()));
|
---|
[2419] | 215 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxNumberOfTrainingSamples", new IntData(4000)));
|
---|
[2335] | 216 | return injector;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | protected override IOperator CreateLoggingOperator() {
|
---|
| 220 | CombinedOperator loggingOperator = new CombinedOperator();
|
---|
| 221 | loggingOperator.Name = "Logging";
|
---|
| 222 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 223 |
|
---|
| 224 | DataCollector collector = new DataCollector();
|
---|
| 225 | ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
| 226 | names.Add(new StringData("BestQuality"));
|
---|
| 227 | names.Add(new StringData("AverageQuality"));
|
---|
| 228 | names.Add(new StringData("WorstQuality"));
|
---|
| 229 | names.Add(new StringData("BestValidationQuality"));
|
---|
| 230 | names.Add(new StringData("AverageValidationQuality"));
|
---|
| 231 | names.Add(new StringData("WorstValidationQuality"));
|
---|
| 232 | names.Add(new StringData("EvaluatedSolutions"));
|
---|
| 233 | QualityLogger qualityLogger = new QualityLogger();
|
---|
| 234 | QualityLogger validationQualityLogger = new QualityLogger();
|
---|
| 235 | validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
| 236 | validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
|
---|
| 237 | seq.AddSubOperator(collector);
|
---|
| 238 | seq.AddSubOperator(qualityLogger);
|
---|
| 239 | seq.AddSubOperator(validationQualityLogger);
|
---|
| 240 |
|
---|
| 241 | loggingOperator.OperatorGraph.AddOperator(seq);
|
---|
| 242 | loggingOperator.OperatorGraph.InitialOperator = seq;
|
---|
| 243 | return loggingOperator;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[2385] | 246 | protected override IOperator CreateTerminationCondition() {
|
---|
| 247 | CombinedOperator terminationCritertion = new CombinedOperator();
|
---|
| 248 | terminationCritertion.Name = "TerminationCondition";
|
---|
| 249 | GreaterThanComparator bestSolutionAge = new GreaterThanComparator();
|
---|
| 250 | bestSolutionAge.GetVariableInfo("LeftSide").ActualName = "BestValidationSolutionAge";
|
---|
| 251 | bestSolutionAge.GetVariableInfo("RightSide").ActualName = "MaxBestValidationSolutionAge";
|
---|
| 252 | bestSolutionAge.GetVariableInfo("Result").ActualName = "TerminationCriterion";
|
---|
| 253 |
|
---|
| 254 | IOperator combinedTerminationCriterion = AlgorithmBase.CombineTerminationCriterions(base.CreateTerminationCondition(), bestSolutionAge);
|
---|
[2977] | 255 |
|
---|
[2385] | 256 | terminationCritertion.OperatorGraph.AddOperator(combinedTerminationCriterion);
|
---|
| 257 | terminationCritertion.OperatorGraph.InitialOperator = combinedTerminationCriterion;
|
---|
| 258 | return terminationCritertion;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 |
|
---|
[2340] | 262 | protected override IOperator CreatePostProcessingOperator() {
|
---|
[2356] | 263 | CombinedOperator op = new CombinedOperator();
|
---|
| 264 | op.Name = "ModelAnalyser";
|
---|
| 265 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 266 | seq.AddSubOperator(DefaultStructureIdentificationOperators.CreatePreparationForPostProcessingOperator());
|
---|
| 267 |
|
---|
| 268 | UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
|
---|
| 269 | SequentialProcessor solutionProc = new SequentialProcessor();
|
---|
| 270 | solutionProc.AddSubOperator(CreateModelAnalyzerOperator());
|
---|
| 271 |
|
---|
| 272 | subScopesProc.AddSubOperator(solutionProc);
|
---|
| 273 | seq.AddSubOperator(subScopesProc);
|
---|
| 274 |
|
---|
| 275 | op.OperatorGraph.AddOperator(seq);
|
---|
| 276 | op.OperatorGraph.InitialOperator = seq;
|
---|
| 277 | return op;
|
---|
[2340] | 278 | }
|
---|
| 279 |
|
---|
[2356] | 280 | protected virtual IOperator CreateModelAnalyzerOperator() {
|
---|
| 281 | return DefaultRegressionOperators.CreatePostProcessingOperator();
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[2340] | 284 | protected CombinedOperator GetProblemInjector() {
|
---|
| 285 | return (CombinedOperator)GetInitializationOperator().SubOperators[0];
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[2344] | 288 | protected virtual IAnalyzerModel CreateGPModel() {
|
---|
[2340] | 289 | IScope bestModelScope = Engine.GlobalScope.SubScopes[0];
|
---|
[2356] | 290 | IAnalyzerModel model = new AnalyzerModel();
|
---|
| 291 | DefaultStructureIdentificationOperators.PopulateAnalyzerModel(bestModelScope, model);
|
---|
| 292 | DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
|
---|
[2344] | 293 | return model;
|
---|
[2340] | 294 | }
|
---|
[2335] | 295 | }
|
---|
| 296 | }
|
---|