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, 13 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
Line 
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;
31using System.Linq;
32using HeuristicLab.DataAnalysis;
33using HeuristicLab.Operators.Programmable;
34using HeuristicLab.GP.Algorithms;
35
36namespace HeuristicLab.GP.StructureIdentification {
37  public class StandardGPRegression : HeuristicLab.GP.Algorithms.StandardGP, IStochasticAlgorithm {
38
39    public override string Name { get { return "StandardGP - StructureIdentification"; } }
40
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; }
44    }
45
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 {
52      get {
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();
57      }
58    }
59
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
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
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
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
122    public StandardGPRegression()
123      : base() {
124      PunishmentFactor = 10.0;
125      MaxBestValidationSolutionAge = 30;
126    }
127
128    protected override IOperator CreateFunctionLibraryInjector() {
129      return DefaultStructureIdentificationOperators.CreateFunctionLibraryInjector();
130    }
131
132    protected override IOperator CreateProblemInjector() {
133      return DefaultRegressionOperators.CreateProblemInjector();
134    }
135
136    protected override IOperator CreateInitialPopulationEvaluator() {
137      return DefaultStructureIdentificationOperators.CreateInitialPopulationEvaluator();
138    }
139
140    protected override IOperator CreateEvaluationOperator() {
141      return DefaultStructureIdentificationOperators.CreateEvaluator();
142    }
143
144
145    protected override IOperator CreateGenerationStepHook() {
146      IOperator op = DefaultStructureIdentificationOperators.CreateGenerationStepHook();
147      op.AddSubOperator(CreateBestSolutionProcessor());
148      return op;
149    }
150
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
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
170      seq.AddSubOperator(evaluatedSolutionsStorer);
171      seq.AddSubOperator(resetBestSolutionAge);
172
173      op.OperatorGraph.AddOperator(seq);
174      op.OperatorGraph.InitialOperator = seq;
175      return op;
176    }
177
178
179    protected override VariableInjector CreateGlobalInjector() {
180      VariableInjector injector = base.CreateGlobalInjector();
181      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
182      injector.AddVariable(new HeuristicLab.Core.Variable("BestValidationSolutionAge", new IntData()));
183      injector.AddVariable(new HeuristicLab.Core.Variable("MaxBestValidationSolutionAge", new IntData()));
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
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
230    protected override IOperator CreatePostProcessingOperator() {
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;
246    }
247
248    protected virtual IOperator CreateModelAnalyzerOperator() {
249      return DefaultRegressionOperators.CreatePostProcessingOperator();
250    }
251
252    protected CombinedOperator GetProblemInjector() {
253      return (CombinedOperator)GetInitializationOperator().SubOperators[0];
254    }
255
256    protected virtual IAnalyzerModel CreateGPModel() {
257      IScope bestModelScope = Engine.GlobalScope.SubScopes[0];
258      IAnalyzerModel model = new AnalyzerModel();
259      DefaultStructureIdentificationOperators.PopulateAnalyzerModel(bestModelScope, model);
260      DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
261      return model;
262    }
263  }
264}
Note: See TracBrowser for help on using the repository browser.