Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed problems with classification algorithms. #746 (CEDMA server is not compatible with new data-modeling algorithms)

File size: 7.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 HeuristicLab.DataAnalysis;
32using HeuristicLab.Operators.Programmable;
33
34namespace HeuristicLab.GP.StructureIdentification {
35  public class StandardGPRegression : HeuristicLab.GP.Algorithms.StandardGP, IStochasticAlgorithm {
36
37    public override string Name { get { return "StandardGP - StructureIdentification"; } }
38
39    public virtual int TargetVariable {
40      get { return ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data; }
41      set { ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data = value; }
42    }
43
44    public virtual Dataset Dataset {
45      get { return ProblemInjector.GetVariableValue<Dataset>("Dataset", null, false); }
46      set { ProblemInjector.GetVariable("Dataset").Value = value; }
47    }
48
49    public virtual IAnalyzerModel Model {
50      get {
51        if (!Engine.Terminated)
52          throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
53        else
54          return CreateGPModel();
55      }
56    }
57
58    public virtual double PunishmentFactor {
59      get { return GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data; }
60      set { GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data = value; }
61    }
62
63    public override IOperator ProblemInjector {
64      get { return GetProblemInjector().OperatorGraph.InitialOperator.SubOperators[0]; }
65      set {
66        value.Name = "ProblemInjector";
67        CombinedOperator problemInjector = GetProblemInjector();
68        problemInjector.OperatorGraph.RemoveOperator(ProblemInjector.Guid);
69        problemInjector.OperatorGraph.AddOperator(value);
70        problemInjector.OperatorGraph.InitialOperator.AddSubOperator(value, 0);
71      }
72    }
73
74    public StandardGPRegression()
75      : base() {
76
77      PunishmentFactor = 10.0;
78    }
79
80    protected override IOperator CreateFunctionLibraryInjector() {
81      return DefaultStructureIdentificationOperators.CreateFunctionLibraryInjector();
82    }
83
84    protected override IOperator CreateProblemInjector() {
85      return DefaultRegressionOperators.CreateProblemInjector();
86    }
87
88    protected override IOperator CreateInitialPopulationEvaluator() {
89      return DefaultStructureIdentificationOperators.CreateInitialPopulationEvaluator();
90    }
91
92    protected override IOperator CreateEvaluationOperator() {
93      return DefaultStructureIdentificationOperators.CreateEvaluator();
94    }
95
96
97    protected override IOperator CreateGenerationStepHook() {
98      IOperator op = DefaultStructureIdentificationOperators.CreateGenerationStepHook();
99      op.AddSubOperator(CreateBestSolutionProcessor());
100      return op;
101    }
102
103    private IOperator CreateBestSolutionProcessor() {
104      CombinedOperator op = new CombinedOperator();
105      op.Name = "BestSolutionProcessor";
106      SequentialProcessor seq = new SequentialProcessor();
107
108      ProgrammableOperator evaluatedSolutionsStorer = new ProgrammableOperator();
109      evaluatedSolutionsStorer.RemoveVariableInfo("Result");
110      evaluatedSolutionsStorer.AddVariableInfo(new VariableInfo("Input", "Value to copy", typeof(IntData), VariableKind.In));
111      evaluatedSolutionsStorer.AddVariableInfo(new VariableInfo("Output", "Value to write", typeof(IntData), VariableKind.New));
112      evaluatedSolutionsStorer.GetVariableInfo("Input").ActualName = "EvaluatedSolutions";
113      evaluatedSolutionsStorer.GetVariableInfo("Output").ActualName = "EvaluatedSolutions";
114      evaluatedSolutionsStorer.Code = "Output.Data = Input.Data;";
115
116      seq.AddSubOperator(evaluatedSolutionsStorer);
117
118      op.OperatorGraph.AddOperator(seq);
119      op.OperatorGraph.InitialOperator = seq;
120      return op;
121    }
122
123
124    protected override VariableInjector CreateGlobalInjector() {
125      VariableInjector injector = base.CreateGlobalInjector();
126      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
127      return injector;
128    }
129
130    protected override IOperator CreateLoggingOperator() {
131      CombinedOperator loggingOperator = new CombinedOperator();
132      loggingOperator.Name = "Logging";
133      SequentialProcessor seq = new SequentialProcessor();
134
135      DataCollector collector = new DataCollector();
136      ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
137      names.Add(new StringData("BestQuality"));
138      names.Add(new StringData("AverageQuality"));
139      names.Add(new StringData("WorstQuality"));
140      names.Add(new StringData("BestValidationQuality"));
141      names.Add(new StringData("AverageValidationQuality"));
142      names.Add(new StringData("WorstValidationQuality"));
143      names.Add(new StringData("EvaluatedSolutions"));
144      QualityLogger qualityLogger = new QualityLogger();
145      QualityLogger validationQualityLogger = new QualityLogger();
146      validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
147      validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
148      seq.AddSubOperator(collector);
149      seq.AddSubOperator(qualityLogger);
150      seq.AddSubOperator(validationQualityLogger);
151
152      loggingOperator.OperatorGraph.AddOperator(seq);
153      loggingOperator.OperatorGraph.InitialOperator = seq;
154      return loggingOperator;
155    }
156
157    protected override IOperator CreatePostProcessingOperator() {
158      CombinedOperator op = new CombinedOperator();
159      op.Name = "ModelAnalyser";
160      SequentialProcessor seq = new SequentialProcessor();
161      seq.AddSubOperator(DefaultStructureIdentificationOperators.CreatePreparationForPostProcessingOperator());
162
163      UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
164      SequentialProcessor solutionProc = new SequentialProcessor();
165      solutionProc.AddSubOperator(CreateModelAnalyzerOperator());
166
167      subScopesProc.AddSubOperator(solutionProc);
168      seq.AddSubOperator(subScopesProc);
169
170      op.OperatorGraph.AddOperator(seq);
171      op.OperatorGraph.InitialOperator = seq;
172      return op;
173    }
174
175    protected virtual IOperator CreateModelAnalyzerOperator() {
176      return DefaultRegressionOperators.CreatePostProcessingOperator();
177    }
178
179    protected CombinedOperator GetProblemInjector() {
180      return (CombinedOperator)GetInitializationOperator().SubOperators[0];
181    }
182
183    protected virtual IAnalyzerModel CreateGPModel() {
184      IScope bestModelScope = Engine.GlobalScope.SubScopes[0];
185      IAnalyzerModel model = new AnalyzerModel();
186      DefaultStructureIdentificationOperators.PopulateAnalyzerModel(bestModelScope, model);
187      DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
188      return model;
189    }
190  }
191}
Note: See TracBrowser for help on using the repository browser.