Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2577 was 2566, checked in by gkronber, 15 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: 12.4 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
[2440]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; }
[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
[2440]60    public IEnumerable<string> AllowedVariables {
[2375]61      get {
[2440]62        ItemList<StringData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<StringData>>("AllowedFeatures", null, false);
[2375]63        return allowedVariables.Select(x => x.Data);
64      }
65      set {
[2440]66        ItemList<StringData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<StringData>>("AllowedFeatures", null, false);
67        foreach (string x in value) allowedVariables.Add(new StringData(x));
[2375]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
[2566]122    public override IOperator FunctionLibraryInjector {
123      get {
124        CombinedOperator funLibInjector = (CombinedOperator)GetInitializationOperator().SubOperators[1];
125        return funLibInjector.OperatorGraph.InitialOperator.SubOperators[0];
126      }
127      set {
128        CombinedOperator funLibInjector = (CombinedOperator)GetInitializationOperator().SubOperators[1];
129        IOperator seq = funLibInjector.OperatorGraph.InitialOperator;
130        seq.RemoveSubOperator(0);
131        seq.AddSubOperator(value, 0);
132      }
133    }
134
[2361]135    public StandardGPRegression()
[2335]136      : base() {
137      PunishmentFactor = 10.0;
[2395]138      MaxBestValidationSolutionAge = 30;
[2335]139    }
140
141    protected override IOperator CreateFunctionLibraryInjector() {
[2356]142      return DefaultStructureIdentificationOperators.CreateFunctionLibraryInjector();
[2335]143    }
144
145    protected override IOperator CreateProblemInjector() {
[2356]146      return DefaultRegressionOperators.CreateProblemInjector();
[2335]147    }
148
149    protected override IOperator CreateInitialPopulationEvaluator() {
[2356]150      return DefaultStructureIdentificationOperators.CreateInitialPopulationEvaluator();
[2335]151    }
152
153    protected override IOperator CreateEvaluationOperator() {
[2356]154      return DefaultStructureIdentificationOperators.CreateEvaluator();
[2335]155    }
156
157
158    protected override IOperator CreateGenerationStepHook() {
[2363]159      IOperator op = DefaultStructureIdentificationOperators.CreateGenerationStepHook();
160      op.AddSubOperator(CreateBestSolutionProcessor());
161      return op;
[2335]162    }
163
[2363]164    private IOperator CreateBestSolutionProcessor() {
165      CombinedOperator op = new CombinedOperator();
166      op.Name = "BestSolutionProcessor";
167      SequentialProcessor seq = new SequentialProcessor();
168
169      ProgrammableOperator evaluatedSolutionsStorer = new ProgrammableOperator();
170      evaluatedSolutionsStorer.RemoveVariableInfo("Result");
171      evaluatedSolutionsStorer.AddVariableInfo(new VariableInfo("Input", "Value to copy", typeof(IntData), VariableKind.In));
172      evaluatedSolutionsStorer.AddVariableInfo(new VariableInfo("Output", "Value to write", typeof(IntData), VariableKind.New));
173      evaluatedSolutionsStorer.GetVariableInfo("Input").ActualName = "EvaluatedSolutions";
174      evaluatedSolutionsStorer.GetVariableInfo("Output").ActualName = "EvaluatedSolutions";
175      evaluatedSolutionsStorer.Code = "Output.Data = Input.Data;";
176
[2385]177      ProgrammableOperator resetBestSolutionAge = new ProgrammableOperator();
178      resetBestSolutionAge.RemoveVariable("Result");
179      resetBestSolutionAge.AddVariableInfo(
180        new VariableInfo("BestValidationSolutionAge", "Age of best validation solution", typeof(IntData), VariableKind.In | VariableKind.Out));
181      resetBestSolutionAge.Code = "BestValidationSolutionAge.Data = 0;";
182
[2363]183      seq.AddSubOperator(evaluatedSolutionsStorer);
[2385]184      seq.AddSubOperator(resetBestSolutionAge);
[2363]185
186      op.OperatorGraph.AddOperator(seq);
187      op.OperatorGraph.InitialOperator = seq;
188      return op;
189    }
190
191
[2335]192    protected override VariableInjector CreateGlobalInjector() {
193      VariableInjector injector = base.CreateGlobalInjector();
194      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
[2385]195      injector.AddVariable(new HeuristicLab.Core.Variable("BestValidationSolutionAge", new IntData()));
196      injector.AddVariable(new HeuristicLab.Core.Variable("MaxBestValidationSolutionAge", new IntData()));
[2419]197      injector.AddVariable(new HeuristicLab.Core.Variable("MaxNumberOfTrainingSamples", new IntData(4000)));
[2335]198      return injector;
199    }
200
201    protected override IOperator CreateLoggingOperator() {
202      CombinedOperator loggingOperator = new CombinedOperator();
203      loggingOperator.Name = "Logging";
204      SequentialProcessor seq = new SequentialProcessor();
205
206      DataCollector collector = new DataCollector();
207      ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
208      names.Add(new StringData("BestQuality"));
209      names.Add(new StringData("AverageQuality"));
210      names.Add(new StringData("WorstQuality"));
211      names.Add(new StringData("BestValidationQuality"));
212      names.Add(new StringData("AverageValidationQuality"));
213      names.Add(new StringData("WorstValidationQuality"));
214      names.Add(new StringData("EvaluatedSolutions"));
215      QualityLogger qualityLogger = new QualityLogger();
216      QualityLogger validationQualityLogger = new QualityLogger();
217      validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
218      validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
219      seq.AddSubOperator(collector);
220      seq.AddSubOperator(qualityLogger);
221      seq.AddSubOperator(validationQualityLogger);
222
223      loggingOperator.OperatorGraph.AddOperator(seq);
224      loggingOperator.OperatorGraph.InitialOperator = seq;
225      return loggingOperator;
226    }
227
[2385]228    protected override IOperator CreateTerminationCondition() {
229      CombinedOperator terminationCritertion = new CombinedOperator();
230      terminationCritertion.Name = "TerminationCondition";
231      GreaterThanComparator bestSolutionAge = new GreaterThanComparator();
232      bestSolutionAge.GetVariableInfo("LeftSide").ActualName = "BestValidationSolutionAge";
233      bestSolutionAge.GetVariableInfo("RightSide").ActualName = "MaxBestValidationSolutionAge";
234      bestSolutionAge.GetVariableInfo("Result").ActualName = "TerminationCriterion";
235
236      IOperator combinedTerminationCriterion = AlgorithmBase.CombineTerminationCriterions(base.CreateTerminationCondition(), bestSolutionAge);
237     
238      terminationCritertion.OperatorGraph.AddOperator(combinedTerminationCriterion);
239      terminationCritertion.OperatorGraph.InitialOperator = combinedTerminationCriterion;
240      return terminationCritertion;
241    }
242
243
[2340]244    protected override IOperator CreatePostProcessingOperator() {
[2356]245      CombinedOperator op = new CombinedOperator();
246      op.Name = "ModelAnalyser";
247      SequentialProcessor seq = new SequentialProcessor();
248      seq.AddSubOperator(DefaultStructureIdentificationOperators.CreatePreparationForPostProcessingOperator());
249
250      UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
251      SequentialProcessor solutionProc = new SequentialProcessor();
252      solutionProc.AddSubOperator(CreateModelAnalyzerOperator());
253
254      subScopesProc.AddSubOperator(solutionProc);
255      seq.AddSubOperator(subScopesProc);
256
257      op.OperatorGraph.AddOperator(seq);
258      op.OperatorGraph.InitialOperator = seq;
259      return op;
[2340]260    }
261
[2356]262    protected virtual IOperator CreateModelAnalyzerOperator() {
263      return DefaultRegressionOperators.CreatePostProcessingOperator();
264    }
265
[2340]266    protected CombinedOperator GetProblemInjector() {
267      return (CombinedOperator)GetInitializationOperator().SubOperators[0];
268    }
269
[2344]270    protected virtual IAnalyzerModel CreateGPModel() {
[2340]271      IScope bestModelScope = Engine.GlobalScope.SubScopes[0];
[2356]272      IAnalyzerModel model = new AnalyzerModel();
273      DefaultStructureIdentificationOperators.PopulateAnalyzerModel(bestModelScope, model);
274      DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
[2344]275      return model;
[2340]276    }
[2335]277  }
278}
Note: See TracBrowser for help on using the repository browser.