Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.StructureIdentification/3.3/OffspringSelectionGPRegression.cs @ 8104

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