Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2419 was 2419, checked in by mkommend, 15 years ago

moved actual training samples from DefaulRegressionOperator to specific algorithms (ticket #755)

File size: 11.9 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
[2340]41    public virtual int TargetVariable {
42      get { return ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data; }
43      set { ProblemInjector.GetVariableValue<IntData>("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
[2375]60    public IEnumerable<int> AllowedVariables {
61      get {
62        ItemList<IntData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<IntData>>("AllowedFeatures", null, false);
63        return allowedVariables.Select(x => x.Data);
64      }
65      set {
66        ItemList<IntData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<IntData>>("AllowedFeatures", null, false);
67        foreach (int x in value) allowedVariables.Add(new IntData(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
[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
[2361]122    public StandardGPRegression()
[2335]123      : base() {
124      PunishmentFactor = 10.0;
[2395]125      MaxBestValidationSolutionAge = 30;
[2335]126    }
127
128    protected override IOperator CreateFunctionLibraryInjector() {
[2356]129      return DefaultStructureIdentificationOperators.CreateFunctionLibraryInjector();
[2335]130    }
131
132    protected override IOperator CreateProblemInjector() {
[2356]133      return DefaultRegressionOperators.CreateProblemInjector();
[2335]134    }
135
136    protected override IOperator CreateInitialPopulationEvaluator() {
[2356]137      return DefaultStructureIdentificationOperators.CreateInitialPopulationEvaluator();
[2335]138    }
139
140    protected override IOperator CreateEvaluationOperator() {
[2356]141      return DefaultStructureIdentificationOperators.CreateEvaluator();
[2335]142    }
143
144
145    protected override IOperator CreateGenerationStepHook() {
[2363]146      IOperator op = DefaultStructureIdentificationOperators.CreateGenerationStepHook();
147      op.AddSubOperator(CreateBestSolutionProcessor());
148      return op;
[2335]149    }
150
[2363]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
[2385]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
[2363]170      seq.AddSubOperator(evaluatedSolutionsStorer);
[2385]171      seq.AddSubOperator(resetBestSolutionAge);
[2363]172
173      op.OperatorGraph.AddOperator(seq);
174      op.OperatorGraph.InitialOperator = seq;
175      return op;
176    }
177
178
[2335]179    protected override VariableInjector CreateGlobalInjector() {
180      VariableInjector injector = base.CreateGlobalInjector();
181      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
[2385]182      injector.AddVariable(new HeuristicLab.Core.Variable("BestValidationSolutionAge", new IntData()));
183      injector.AddVariable(new HeuristicLab.Core.Variable("MaxBestValidationSolutionAge", new IntData()));
[2419]184      injector.AddVariable(new HeuristicLab.Core.Variable("MaxNumberOfTrainingSamples", new IntData(4000)));
[2335]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
[2385]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
[2340]231    protected override IOperator CreatePostProcessingOperator() {
[2356]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;
[2340]247    }
248
[2356]249    protected virtual IOperator CreateModelAnalyzerOperator() {
250      return DefaultRegressionOperators.CreatePostProcessingOperator();
251    }
252
[2340]253    protected CombinedOperator GetProblemInjector() {
254      return (CombinedOperator)GetInitializationOperator().SubOperators[0];
255    }
256
[2344]257    protected virtual IAnalyzerModel CreateGPModel() {
[2340]258      IScope bestModelScope = Engine.GlobalScope.SubScopes[0];
[2356]259      IAnalyzerModel model = new AnalyzerModel();
260      DefaultStructureIdentificationOperators.PopulateAnalyzerModel(bestModelScope, model);
261      DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
[2344]262      return model;
[2340]263    }
[2335]264  }
265}
Note: See TracBrowser for help on using the repository browser.