Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1418: Reintegrated branch into trunk.

File size: 8.7 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;
[5809]35using HeuristicLab.PluginInfrastructure;
[3877]36
37namespace HeuristicLab.Algorithms.DataAnalysis {
38  /// <summary>
39  /// Linear regression data analysis algorithm.
40  /// </summary>
[5809]41  [NonDiscoverableType]
[3877]42  [Item("Linear Regression", "Linear regression data analysis algorithm.")]
43  [StorableClass]
[4437]44  public sealed class LinearRegression : EngineAlgorithm, IStorableContent {
[3877]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
[4437]50    public string Filename { get; set; }
[3877]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) { }
[4722]102    [StorableHook(HookType.AfterDeserialization)]
103    private void AfterDeserialization() {
104      Initialize();
105    }
[3877]106
[4722]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    }
[3877]115    public override IDeepCloneable Clone(Cloner cloner) {
[4722]116      return new LinearRegression(this, cloner);
[3877]117    }
118
119    public override void Prepare() {
120      if (Problem != null) base.Prepare();
121    }
122
[3886]123    protected override void Problem_Reset(object sender, EventArgs e) {
[3892]124      UpdateAlgorithmParameterValues();
[3886]125      base.Problem_Reset(sender, e);
126    }
127
[3877]128    #region Events
129    protected override void OnProblemChanged() {
130      solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
131      evaluator.RegressionProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
[3892]132      analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
133      UpdateAlgorithmParameterValues();
[3886]134      Problem.Reset += new EventHandler(Problem_Reset);
[3877]135      base.OnProblemChanged();
136    }
137
[3892]138
[3877]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;
[3886]166        Problem.Reset += new EventHandler(Problem_Reset);
[3877]167      }
168    }
[3892]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    }
[3877]184    #endregion
185  }
186}
Note: See TracBrowser for help on using the repository browser.