Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.GP.StructureIdentification/3.3/StandardGPRegression.cs @ 2594

Last change on this file since 2594 was 2440, checked in by gkronber, 15 years ago

Fixed #784 (ProblemInjector should be changed to read variable names instead of indexes for input and target variables)

File size: 11.9 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 { 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
60    public IEnumerable<string> AllowedVariables {
61      get {
62        ItemList<StringData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<StringData>>("AllowedFeatures", null, false);
63        return allowedVariables.Select(x => x.Data);
64      }
65      set {
66        ItemList<StringData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<StringData>>("AllowedFeatures", null, false);
67        foreach (string x in value) allowedVariables.Add(new StringData(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      injector.AddVariable(new HeuristicLab.Core.Variable("MaxNumberOfTrainingSamples", new IntData(4000)));
185      return injector;
186    }
187
188    protected override IOperator CreateLoggingOperator() {
189      CombinedOperator loggingOperator = new CombinedOperator();
190      loggingOperator.Name = "Logging";
191      SequentialProcessor seq = new SequentialProcessor();
192
193      DataCollector collector = new DataCollector();
194      ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
195      names.Add(new StringData("BestQuality"));
196      names.Add(new StringData("AverageQuality"));
197      names.Add(new StringData("WorstQuality"));
198      names.Add(new StringData("BestValidationQuality"));
199      names.Add(new StringData("AverageValidationQuality"));
200      names.Add(new StringData("WorstValidationQuality"));
201      names.Add(new StringData("EvaluatedSolutions"));
202      QualityLogger qualityLogger = new QualityLogger();
203      QualityLogger validationQualityLogger = new QualityLogger();
204      validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
205      validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
206      seq.AddSubOperator(collector);
207      seq.AddSubOperator(qualityLogger);
208      seq.AddSubOperator(validationQualityLogger);
209
210      loggingOperator.OperatorGraph.AddOperator(seq);
211      loggingOperator.OperatorGraph.InitialOperator = seq;
212      return loggingOperator;
213    }
214
215    protected override IOperator CreateTerminationCondition() {
216      CombinedOperator terminationCritertion = new CombinedOperator();
217      terminationCritertion.Name = "TerminationCondition";
218      GreaterThanComparator bestSolutionAge = new GreaterThanComparator();
219      bestSolutionAge.GetVariableInfo("LeftSide").ActualName = "BestValidationSolutionAge";
220      bestSolutionAge.GetVariableInfo("RightSide").ActualName = "MaxBestValidationSolutionAge";
221      bestSolutionAge.GetVariableInfo("Result").ActualName = "TerminationCriterion";
222
223      IOperator combinedTerminationCriterion = AlgorithmBase.CombineTerminationCriterions(base.CreateTerminationCondition(), bestSolutionAge);
224     
225      terminationCritertion.OperatorGraph.AddOperator(combinedTerminationCriterion);
226      terminationCritertion.OperatorGraph.InitialOperator = combinedTerminationCriterion;
227      return terminationCritertion;
228    }
229
230
231    protected override IOperator CreatePostProcessingOperator() {
232      CombinedOperator op = new CombinedOperator();
233      op.Name = "ModelAnalyser";
234      SequentialProcessor seq = new SequentialProcessor();
235      seq.AddSubOperator(DefaultStructureIdentificationOperators.CreatePreparationForPostProcessingOperator());
236
237      UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
238      SequentialProcessor solutionProc = new SequentialProcessor();
239      solutionProc.AddSubOperator(CreateModelAnalyzerOperator());
240
241      subScopesProc.AddSubOperator(solutionProc);
242      seq.AddSubOperator(subScopesProc);
243
244      op.OperatorGraph.AddOperator(seq);
245      op.OperatorGraph.InitialOperator = seq;
246      return op;
247    }
248
249    protected virtual IOperator CreateModelAnalyzerOperator() {
250      return DefaultRegressionOperators.CreatePostProcessingOperator();
251    }
252
253    protected CombinedOperator GetProblemInjector() {
254      return (CombinedOperator)GetInitializationOperator().SubOperators[0];
255    }
256
257    protected virtual IAnalyzerModel CreateGPModel() {
258      IScope bestModelScope = Engine.GlobalScope.SubScopes[0];
259      IAnalyzerModel model = new AnalyzerModel();
260      DefaultStructureIdentificationOperators.PopulateAnalyzerModel(bestModelScope, model);
261      DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
262      return model;
263    }
264  }
265}
Note: See TracBrowser for help on using the repository browser.