Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Algorithms.DataAnalysis/3.3/LinearRegression.cs @ 8975

Last change on this file since 8975 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

File size: 8.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis;
30using HeuristicLab.Problems.DataAnalysis.Evaluators;
31using HeuristicLab.Problems.DataAnalysis.Regression.LinearRegression;
32using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
33using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers;
34using HeuristicLab.Problems.DataAnalysis.Symbolic;
35using HeuristicLab.PluginInfrastructure;
36
37namespace HeuristicLab.Algorithms.DataAnalysis {
38  /// <summary>
39  /// Linear regression data analysis algorithm.
40  /// </summary>
41  [NonDiscoverableType]
42  [Item("Linear Regression", "Linear regression data analysis algorithm.")]
43  [StorableClass]
44  public sealed class LinearRegression : EngineAlgorithm, IStorableContent {
45    private const string TrainingSamplesStartParameterName = "Training start";
46    private const string TrainingSamplesEndParameterName = "Training end";
47    private const string LinearRegressionModelParameterName = "LinearRegressionModel";
48    private const string ModelInterpreterParameterName = "Model interpreter";
49
50    public string Filename { get; set; }
51
52    #region Problem Properties
53    public override Type ProblemType {
54      get { return typeof(DataAnalysisProblem); }
55    }
56    public new DataAnalysisProblem Problem {
57      get { return (DataAnalysisProblem)base.Problem; }
58      set { base.Problem = value; }
59    }
60    #endregion
61
62    #region parameter properties
63    public IValueParameter<IntValue> TrainingSamplesStartParameter {
64      get { return (IValueParameter<IntValue>)Parameters[TrainingSamplesStartParameterName]; }
65    }
66    public IValueParameter<IntValue> TrainingSamplesEndParameter {
67      get { return (IValueParameter<IntValue>)Parameters[TrainingSamplesEndParameterName]; }
68    }
69    public IValueParameter<ISymbolicExpressionTreeInterpreter> ModelInterpreterParameter {
70      get { return (IValueParameter<ISymbolicExpressionTreeInterpreter>)Parameters[ModelInterpreterParameterName]; }
71    }
72    #endregion
73
74    [Storable]
75    private LinearRegressionSolutionCreator solutionCreator;
76    [Storable]
77    private SimpleSymbolicRegressionEvaluator evaluator;
78    [Storable]
79    private SimpleMSEEvaluator mseEvaluator;
80    [Storable]
81    private BestSymbolicRegressionSolutionAnalyzer analyzer;
82    public LinearRegression()
83      : base() {
84      Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesStartParameterName, "The first index of the data set partition to use for training."));
85      Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesEndParameterName, "The last index of the data set partition to use for training."));
86      Parameters.Add(new ValueParameter<ISymbolicExpressionTreeInterpreter>(ModelInterpreterParameterName, "The interpreter to use for evaluation of the model.", new SimpleArithmeticExpressionInterpreter()));
87
88      solutionCreator = new LinearRegressionSolutionCreator();
89      evaluator = new SimpleSymbolicRegressionEvaluator();
90      mseEvaluator = new SimpleMSEEvaluator();
91      analyzer = new BestSymbolicRegressionSolutionAnalyzer();
92
93      OperatorGraph.InitialOperator = solutionCreator;
94      solutionCreator.Successor = evaluator;
95      evaluator.Successor = mseEvaluator;
96      mseEvaluator.Successor = analyzer;
97
98      Initialize();
99    }
100    [StorableConstructor]
101    private LinearRegression(bool deserializing) : base(deserializing) { }
102    [StorableHook(HookType.AfterDeserialization)]
103    private void AfterDeserialization() {
104      Initialize();
105    }
106
107    private LinearRegression(LinearRegression original, Cloner cloner)
108      : base(original, cloner) {
109      solutionCreator = cloner.Clone(original.solutionCreator);
110      evaluator = cloner.Clone(original.evaluator);
111      mseEvaluator = cloner.Clone(original.mseEvaluator);
112      analyzer = cloner.Clone(original.analyzer);
113      Initialize();
114    }
115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new LinearRegression(this, cloner);
117    }
118
119    public override void Prepare() {
120      if (Problem != null) base.Prepare();
121    }
122
123    protected override void Problem_Reset(object sender, EventArgs e) {
124      UpdateAlgorithmParameterValues();
125      base.Problem_Reset(sender, e);
126    }
127
128    #region Events
129    protected override void OnProblemChanged() {
130      solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
131      evaluator.RegressionProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
132      analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
133      UpdateAlgorithmParameterValues();
134      Problem.Reset += new EventHandler(Problem_Reset);
135      base.OnProblemChanged();
136    }
137
138
139    #endregion
140
141    #region Helpers
142    private void Initialize() {
143      solutionCreator.SamplesStartParameter.ActualName = TrainingSamplesStartParameter.Name;
144      solutionCreator.SamplesEndParameter.ActualName = TrainingSamplesEndParameter.Name;
145      solutionCreator.SymbolicExpressionTreeParameter.ActualName = LinearRegressionModelParameterName;
146
147      evaluator.SymbolicExpressionTreeParameter.ActualName = solutionCreator.SymbolicExpressionTreeParameter.ActualName;
148      evaluator.SymbolicExpressionTreeInterpreterParameter.ActualName = ModelInterpreterParameter.Name;
149      evaluator.ValuesParameter.ActualName = "Training values";
150      evaluator.SamplesStartParameter.ActualName = TrainingSamplesStartParameterName;
151      evaluator.SamplesEndParameter.ActualName = TrainingSamplesEndParameterName;
152
153      mseEvaluator.ValuesParameter.ActualName = "Training values";
154      mseEvaluator.MeanSquaredErrorParameter.ActualName = "Training MSE";
155
156      analyzer.SymbolicExpressionTreeParameter.ActualName = solutionCreator.SymbolicExpressionTreeParameter.ActualName;
157      analyzer.SymbolicExpressionTreeParameter.Depth = 0;
158      analyzer.QualityParameter.ActualName = mseEvaluator.MeanSquaredErrorParameter.ActualName;
159      analyzer.QualityParameter.Depth = 0;
160      analyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = ModelInterpreterParameter.Name;
161
162      if (Problem != null) {
163        solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
164        evaluator.RegressionProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
165        analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
166        Problem.Reset += new EventHandler(Problem_Reset);
167      }
168    }
169
170    private void UpdateAlgorithmParameterValues() {
171      TrainingSamplesStartParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesStart;
172      TrainingSamplesEndParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesEnd;
173      //var targetValues =
174      //  Problem.DataAnalysisProblemData.Dataset.GetVariableValues(Problem.DataAnalysisProblemData.TargetVariable.Value,
175      //  TrainingSamplesStartParameter.Value.Value, TrainingSamplesEndParameter.Value.Value);
176      //double range = targetValues.Max() - targetValues.Min();
177      //double lowerEstimationLimit = targetValues.Average() - 10.0 * range;
178      //double upperEstimationLimit = targetValues.Average() + 10.0 * range;
179      //evaluator.LowerEstimationLimitParameter.Value = new DoubleValue(lowerEstimationLimit);
180      //evaluator.UpperEstimationLimitParameter.Value = new DoubleValue(upperEstimationLimit);
181      //analyzer.LowerEstimationLimitParameter.Value = new DoubleValue(lowerEstimationLimit);
182      //analyzer.UpperEstimationLimitParameter.Value = new DoubleValue(upperEstimationLimit);
183    }
184    #endregion
185  }
186}
Note: See TracBrowser for help on using the repository browser.