Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/StandardGPRegression.cs @ 2395

Last change on this file since 2395 was 2395, checked in by gkronber, 15 years ago

Increased the limit for termination of GP engines when overfitting is likely. #762 (ValidationQuality improvement progress as additional stopping criterion for default StructId Engines)

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