Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4402 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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