Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.LinearRegression/3.2/LinearRegression.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: 8.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 System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using System.Xml;
28using System.Diagnostics;
29using HeuristicLab.DataAnalysis;
30using HeuristicLab.Data;
31using HeuristicLab.Operators;
32using HeuristicLab.GP.StructureIdentification;
33using HeuristicLab.Modeling;
34using HeuristicLab.GP;
35using HeuristicLab.Random;
36using HeuristicLab.GP.Interfaces;
37
38namespace HeuristicLab.LinearRegression {
39  public class LinearRegression : ItemBase, IEditable, IAlgorithm {
40
41    public virtual string Name { get { return "LinearRegression"; } }
42    public virtual string Description { get { return "TODO"; } }
43
44    private SequentialEngine.SequentialEngine engine;
45    public virtual IEngine Engine {
46      get { return engine; }
47    }
48
49    public virtual Dataset Dataset {
50      get { return ProblemInjector.GetVariableValue<Dataset>("Dataset", null, false); }
51      set { ProblemInjector.GetVariable("Dataset").Value = value; }
52    }
53
54    public virtual int TargetVariable {
55      get { return ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data; }
56      set { ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data = value; }
57    }
58
59    public virtual IOperator ProblemInjector {
60      get {
61        IOperator main = GetMainOperator();
62        CombinedOperator probInjector = (CombinedOperator)main.SubOperators[2];
63        return probInjector.OperatorGraph.InitialOperator.SubOperators[0];
64      }
65      set {
66        IOperator main = GetMainOperator();
67        CombinedOperator probInjector = (CombinedOperator)main.SubOperators[2];
68        probInjector.OperatorGraph.InitialOperator.RemoveSubOperator(0);
69        probInjector.OperatorGraph.InitialOperator.AddSubOperator(value, 0);
70      }
71    }
72
73    public virtual IAnalyzerModel Model {
74      get {
75        if (!engine.Terminated) throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
76        IScope bestModelScope = engine.GlobalScope;
77        return CreateLRModel(bestModelScope);
78      }
79    }
80
81    public LinearRegression() {
82      engine = new SequentialEngine.SequentialEngine();
83      CombinedOperator algo = CreateAlgorithm();
84      engine.OperatorGraph.AddOperator(algo);
85      engine.OperatorGraph.InitialOperator = algo;
86    }
87
88    protected virtual CombinedOperator CreateAlgorithm() {
89      CombinedOperator algo = new CombinedOperator();
90      SequentialProcessor seq = new SequentialProcessor();
91      algo.Name = Name;
92      seq.Name = Name;
93
94      IOperator globalInjector = CreateGlobalInjector();
95
96      HL3TreeEvaluatorInjector treeEvaluatorInjector = new HL3TreeEvaluatorInjector();
97
98
99      LinearRegressionOperator lrOperator = new LinearRegressionOperator();
100      lrOperator.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
101      lrOperator.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
102
103      seq.AddSubOperator(globalInjector);
104      seq.AddSubOperator(new RandomInjector());
105      seq.AddSubOperator(CreateProblemInjector());
106      seq.AddSubOperator(treeEvaluatorInjector);
107      seq.AddSubOperator(lrOperator);
108      seq.AddSubOperator(CreatePostProcessingOperator());
109
110      algo.OperatorGraph.InitialOperator = seq;
111      algo.OperatorGraph.AddOperator(seq);
112
113      return algo;
114    }
115
116    protected virtual IOperator CreateProblemInjector() {
117      return DefaultRegressionOperators.CreateProblemInjector();
118    }
119
120    protected virtual VariableInjector CreateGlobalInjector() {
121      VariableInjector injector = new VariableInjector();
122      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData(1000)));
123      injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0)));
124
125      return injector;
126    }
127
128    protected virtual IOperator CreatePostProcessingOperator() {
129      CombinedOperator op = new CombinedOperator();
130      op.Name = "Model Analyzer";
131
132      SequentialProcessor seq = new SequentialProcessor();
133      HL3TreeEvaluatorInjector evaluatorInjector = new HL3TreeEvaluatorInjector();
134      evaluatorInjector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData(1000.0)));
135      evaluatorInjector.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
136
137      #region simple evaluators
138      SimpleEvaluator trainingEvaluator = new SimpleEvaluator();
139      trainingEvaluator.Name = "TrainingEvaluator";
140      trainingEvaluator.GetVariableInfo("FunctionTree").ActualName = "LinearRegressionModel";
141      trainingEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
142      trainingEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
143      trainingEvaluator.GetVariableInfo("Values").ActualName = "TrainingValues";
144      trainingEvaluator.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
145      SimpleEvaluator validationEvaluator = new SimpleEvaluator();
146      validationEvaluator.Name = "ValidationEvaluator";
147      validationEvaluator.GetVariableInfo("FunctionTree").ActualName = "LinearRegressionModel";
148      validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
149      validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
150      validationEvaluator.GetVariableInfo("Values").ActualName = "ValidationValues";
151      validationEvaluator.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
152      SimpleEvaluator testEvaluator = new SimpleEvaluator();
153      testEvaluator.Name = "TestEvaluator";
154      testEvaluator.GetVariableInfo("FunctionTree").ActualName = "LinearRegressionModel";
155      testEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart";
156      testEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
157      testEvaluator.GetVariableInfo("Values").ActualName = "TestValues";
158      testEvaluator.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
159      seq.AddSubOperator(evaluatorInjector);
160      seq.AddSubOperator(trainingEvaluator);
161      seq.AddSubOperator(validationEvaluator);
162      seq.AddSubOperator(testEvaluator);
163      #endregion
164
165      #region variable impacts
166      // calculate and set variable impacts
167      VariableNamesExtractor namesExtractor = new VariableNamesExtractor();
168      namesExtractor.GetVariableInfo("VariableNames").ActualName = "InputVariableNames";
169      namesExtractor.GetVariableInfo("FunctionTree").ActualName = "LinearRegressionModel";
170
171      PredictorBuilder predictorBuilder = new PredictorBuilder();
172      predictorBuilder.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
173      predictorBuilder.GetVariableInfo("FunctionTree").ActualName = "LinearRegressionModel";
174
175      seq.AddSubOperator(namesExtractor);
176      seq.AddSubOperator(predictorBuilder);
177      #endregion
178
179      seq.AddSubOperator(CreateModelAnalyzerOperator());
180
181      op.OperatorGraph.AddOperator(seq);
182      op.OperatorGraph.InitialOperator = seq;
183      return op;
184    }
185
186    protected virtual IOperator CreateModelAnalyzerOperator() {
187      return DefaultRegressionOperators.CreatePostProcessingOperator();
188    }
189
190    protected virtual IAnalyzerModel CreateLRModel(IScope bestModelScope) {
191      var model = new AnalyzerModel();
192      DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
193      return model;
194    }
195
196    protected virtual IOperator GetMainOperator() {
197      CombinedOperator lr = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
198      return lr.OperatorGraph.InitialOperator;
199    }
200
201    protected virtual IOperator GetVariableInjector() {
202      return GetMainOperator().SubOperators[0];
203    }
204
205    public override IView CreateView() {
206      return engine.CreateView();
207    }
208
209    #region IEditable Members
210
211    public virtual IEditor CreateEditor() {
212      return engine.CreateEditor();
213    }
214
215    #endregion
216  }
217}
Note: See TracBrowser for help on using the repository browser.