Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Algorithms.DataAnalysis/3.3/LinearRegression.cs @ 7461

Last change on this file since 7461 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

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