Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.StructureIdentification/3.3/StandardGPRegression.cs @ 5955

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

Moved linear scaling functionality out of tree evaluator into a separate operator. #823 (Implement tree evaluator with linear scaling to improve convergence in symbolic regression.)

File size: 13.4 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 string TargetVariable {
42      get { return ProblemInjector.GetVariableValue<StringData>("TargetVariable", null, false).Data; }
43      set {
44        ProblemInjector.GetVariableValue<StringData>("TargetVariable", null, false).Data = value;
45      }
46    }
47
48    public virtual Dataset Dataset {
49      get { return ProblemInjector.GetVariableValue<Dataset>("Dataset", null, false); }
50      set {
51        ProblemInjector.GetVariable("Dataset").Value = value;
52      }
53    }
54
55    public virtual IAnalyzerModel Model {
56      get {
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();
61      }
62    }
63
64    public IEnumerable<string> AllowedVariables {
65      get {
66        ItemList<StringData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<StringData>>("AllowedFeatures", null, false);
67        return allowedVariables.Select(x => x.Data);
68      }
69      set {
70        ItemList<StringData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<StringData>>("AllowedFeatures", null, false);
71        foreach (string x in value) allowedVariables.Add(new StringData(x));
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
105    public virtual double PunishmentFactor {
106      get { return GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data; }
107      set {
108        GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data = value;
109      }
110    }
111
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
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
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
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
150    public StandardGPRegression()
151      : base() {
152      PunishmentFactor = 10.0;
153      MaxBestValidationSolutionAge = 30;
154    }
155
156    protected override IOperator CreateFunctionLibraryInjector() {
157      return DefaultStructureIdentificationOperators.CreateFunctionLibraryInjector();
158    }
159
160    protected override IOperator CreateProblemInjector() {
161      return DefaultRegressionOperators.CreateProblemInjector();
162    }
163
164    protected override IOperator CreateInitialPopulationEvaluator() {
165      return DefaultStructureIdentificationOperators.CreateInitialPopulationEvaluator();
166    }
167
168    protected override IOperator CreateEvaluationOperator() {
169      return DefaultStructureIdentificationOperators.CreateEvaluator();
170    }
171
172
173    protected override IOperator CreateGenerationStepHook() {
174      IOperator op = DefaultStructureIdentificationOperators.CreateGenerationStepHook();
175      op.AddSubOperator(CreateBestSolutionProcessor());
176      return op;
177    }
178
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
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
198      seq.AddSubOperator(evaluatedSolutionsStorer);
199      seq.AddSubOperator(resetBestSolutionAge);
200
201      op.OperatorGraph.AddOperator(seq);
202      op.OperatorGraph.InitialOperator = seq;
203      return op;
204    }
205
206
207    protected override VariableInjector CreateGlobalInjector() {
208      VariableInjector injector = base.CreateGlobalInjector();
209      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
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)));
213      injector.AddVariable(new HeuristicLab.Core.Variable("BestValidationSolutionAge", new IntData()));
214      injector.AddVariable(new HeuristicLab.Core.Variable("MaxBestValidationSolutionAge", new IntData()));
215      injector.AddVariable(new HeuristicLab.Core.Variable("MaxNumberOfTrainingSamples", new IntData(4000)));
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
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);
255
256      terminationCritertion.OperatorGraph.AddOperator(combinedTerminationCriterion);
257      terminationCritertion.OperatorGraph.InitialOperator = combinedTerminationCriterion;
258      return terminationCritertion;
259    }
260
261
262    protected override IOperator CreatePostProcessingOperator() {
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;
278    }
279
280    protected virtual IOperator CreateModelAnalyzerOperator() {
281      return DefaultRegressionOperators.CreatePostProcessingOperator();
282    }
283
284    protected CombinedOperator GetProblemInjector() {
285      return (CombinedOperator)GetInitializationOperator().SubOperators[0];
286    }
287
288    protected virtual IAnalyzerModel CreateGPModel() {
289      IScope bestModelScope = Engine.GlobalScope.SubScopes[0];
290      IAnalyzerModel model = new AnalyzerModel();
291      DefaultStructureIdentificationOperators.PopulateAnalyzerModel(bestModelScope, model);
292      DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
293      return model;
294    }
295  }
296}
Note: See TracBrowser for help on using the repository browser.