Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/StandardGP.cs @ 2356

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

Moved common (not algorithm specific) model analyzer code (same operator tree in all regression/classification/time-series-prognosis engines) to HL.Modeling. #625, #705, #739.

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