Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.3/LinearRegression.cs @ 5492

Last change on this file since 5492 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 8.6 KB
RevLine 
[3877]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3877]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;
[4068]30using HeuristicLab.Problems.DataAnalysis.Evaluators;
[3877]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]
[4437]43  public sealed class LinearRegression : EngineAlgorithm, IStorableContent {
[3877]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
[4437]49    public string Filename { get; set; }
[3877]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) { }
[4722]101    [StorableHook(HookType.AfterDeserialization)]
102    private void AfterDeserialization() {
103      Initialize();
104    }
[3877]105
[4722]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    }
[3877]114    public override IDeepCloneable Clone(Cloner cloner) {
[4722]115      return new LinearRegression(this, cloner);
[3877]116    }
117
118    public override void Prepare() {
119      if (Problem != null) base.Prepare();
120    }
121
[3886]122    protected override void Problem_Reset(object sender, EventArgs e) {
[3892]123      UpdateAlgorithmParameterValues();
[3886]124      base.Problem_Reset(sender, e);
125    }
126
[3877]127    #region Events
128    protected override void OnProblemChanged() {
129      solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
130      evaluator.RegressionProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
[3892]131      analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
132      UpdateAlgorithmParameterValues();
[3886]133      Problem.Reset += new EventHandler(Problem_Reset);
[3877]134      base.OnProblemChanged();
135    }
136
[3892]137
[3877]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;
[3886]165        Problem.Reset += new EventHandler(Problem_Reset);
[3877]166      }
167    }
[3892]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    }
[3877]183    #endregion
184  }
185}
Note: See TracBrowser for help on using the repository browser.