Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/OffspringSelectionGPRegression.cs @ 2900

Last change on this file since 2900 was 2566, checked in by gkronber, 14 years ago

Added new predefined function libraries for symbolic regression algorithms. Changed CEDMA dispatcher to choose a function library randomly. #813 (GP structure-identification algorithms that use only a simple function library)

File size: 13.1 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      injector.AddVariable(new HeuristicLab.Core.Variable("BestValidationSolutionAge", new IntData()));
201      injector.AddVariable(new HeuristicLab.Core.Variable("MaxBestValidationSolutionAge", new IntData()));
202      injector.AddVariable(new HeuristicLab.Core.Variable("MaxNumberOfTrainingSamples", new IntData(4000)));
203      return injector;
204    }
205
206
207    protected override IOperator CreateLoggingOperator() {
208      CombinedOperator loggingOperator = new CombinedOperator();
209      loggingOperator.Name = "Logging";
210      SequentialProcessor seq = new SequentialProcessor();
211
212      DataCollector collector = new DataCollector();
213      ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
214      names.Add(new StringData("BestQuality"));
215      names.Add(new StringData("AverageQuality"));
216      names.Add(new StringData("WorstQuality"));
217      names.Add(new StringData("BestValidationQuality"));
218      names.Add(new StringData("AverageValidationQuality"));
219      names.Add(new StringData("WorstValidationQuality"));
220      names.Add(new StringData("EvaluatedSolutions"));
221      names.Add(new StringData("SelectionPressure"));
222      QualityLogger qualityLogger = new QualityLogger();
223      QualityLogger validationQualityLogger = new QualityLogger();
224      validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
225      validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
226      seq.AddSubOperator(collector);
227      seq.AddSubOperator(qualityLogger);
228      seq.AddSubOperator(validationQualityLogger);
229
230      loggingOperator.OperatorGraph.AddOperator(seq);
231      loggingOperator.OperatorGraph.InitialOperator = seq;
232      return loggingOperator;
233    }
234
235    protected override IOperator CreateTerminationCondition() {
236      CombinedOperator terminationCritertion = new CombinedOperator();
237      terminationCritertion.Name = "TerminationCondition";
238      GreaterThanComparator bestSolutionAge = new GreaterThanComparator();
239      bestSolutionAge.GetVariableInfo("LeftSide").ActualName = "BestValidationSolutionAge";
240      bestSolutionAge.GetVariableInfo("RightSide").ActualName = "MaxBestValidationSolutionAge";
241      bestSolutionAge.GetVariableInfo("Result").ActualName = "TerminationCriterion";
242
243      IOperator combinedTerminationCriterion = AlgorithmBase.CombineTerminationCriterions(base.CreateTerminationCondition(), bestSolutionAge);
244
245      terminationCritertion.OperatorGraph.AddOperator(combinedTerminationCriterion);
246      terminationCritertion.OperatorGraph.InitialOperator = combinedTerminationCriterion;
247      return terminationCritertion;
248    }
249
250    protected override IOperator CreatePostProcessingOperator() {
251      CombinedOperator op = new CombinedOperator();
252      op.Name = "ModelAnalyser";
253      SequentialProcessor seq = new SequentialProcessor();
254      seq.AddSubOperator(DefaultStructureIdentificationOperators.CreatePreparationForPostProcessingOperator());
255
256      UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
257      SequentialProcessor solutionProc = new SequentialProcessor();
258      solutionProc.AddSubOperator(CreateModelAnalyzerOperator());
259
260      subScopesProc.AddSubOperator(solutionProc);
261      seq.AddSubOperator(subScopesProc);
262
263      op.OperatorGraph.AddOperator(seq);
264      op.OperatorGraph.InitialOperator = seq;
265      return op;
266    }
267
268    protected virtual IOperator CreateModelAnalyzerOperator() {
269      return DefaultRegressionOperators.CreatePostProcessingOperator();
270    }
271
272    protected CombinedOperator GetProblemInjector() {
273      return (CombinedOperator)GetInitializationOperator().SubOperators[0];
274    }
275
276    protected virtual IAnalyzerModel CreateGPModel() {
277      IScope bestModelScope = Engine.GlobalScope.SubScopes[0];
278      var model = new AnalyzerModel();
279
280      model.SetMetaData("SelectionPressure", bestModelScope.GetVariableValue<DoubleData>("SelectionPressure", false).Data);
281      DefaultStructureIdentificationOperators.PopulateAnalyzerModel(bestModelScope, model);
282      DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
283
284      return model;
285    }
286  }
287}
Note: See TracBrowser for help on using the repository browser.