Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented !IStorableContent separately for each algorithm (#1193)

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
102    public override IDeepCloneable Clone(Cloner cloner) {
103      LinearRegression clone = (LinearRegression)base.Clone(cloner);
104      clone.solutionCreator = (LinearRegressionSolutionCreator)cloner.Clone(solutionCreator);
105      clone.evaluator = (SimpleSymbolicRegressionEvaluator)cloner.Clone(evaluator);
106      clone.mseEvaluator = (SimpleMSEEvaluator)cloner.Clone(mseEvaluator);
107      clone.analyzer = (BestSymbolicRegressionSolutionAnalyzer)cloner.Clone(analyzer);
108      clone.Initialize();
109      return clone;
110    }
111
112    public override void Prepare() {
113      if (Problem != null) base.Prepare();
114    }
115
116    protected override void Problem_Reset(object sender, EventArgs e) {
117      UpdateAlgorithmParameterValues();
118      base.Problem_Reset(sender, e);
119    }
120
121    #region Events
122    protected override void OnProblemChanged() {
123      solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
124      evaluator.RegressionProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
125      analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
126      UpdateAlgorithmParameterValues();
127      Problem.Reset += new EventHandler(Problem_Reset);
128      base.OnProblemChanged();
129    }
130
131
132    #endregion
133
134    #region Helpers
135    [StorableHook(HookType.AfterDeserialization)]
136    private void Initialize() {
137      solutionCreator.SamplesStartParameter.ActualName = TrainingSamplesStartParameter.Name;
138      solutionCreator.SamplesEndParameter.ActualName = TrainingSamplesEndParameter.Name;
139      solutionCreator.SymbolicExpressionTreeParameter.ActualName = LinearRegressionModelParameterName;
140
141      evaluator.SymbolicExpressionTreeParameter.ActualName = solutionCreator.SymbolicExpressionTreeParameter.ActualName;
142      evaluator.SymbolicExpressionTreeInterpreterParameter.ActualName = ModelInterpreterParameter.Name;
143      evaluator.ValuesParameter.ActualName = "Training values";
144      evaluator.SamplesStartParameter.ActualName = TrainingSamplesStartParameterName;
145      evaluator.SamplesEndParameter.ActualName = TrainingSamplesEndParameterName;
146
147      mseEvaluator.ValuesParameter.ActualName = "Training values";
148      mseEvaluator.MeanSquaredErrorParameter.ActualName = "Training MSE";
149
150      analyzer.SymbolicExpressionTreeParameter.ActualName = solutionCreator.SymbolicExpressionTreeParameter.ActualName;
151      analyzer.SymbolicExpressionTreeParameter.Depth = 0;
152      analyzer.QualityParameter.ActualName = mseEvaluator.MeanSquaredErrorParameter.ActualName;
153      analyzer.QualityParameter.Depth = 0;
154      analyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = ModelInterpreterParameter.Name;
155
156      if (Problem != null) {
157        solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
158        evaluator.RegressionProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
159        analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
160        Problem.Reset += new EventHandler(Problem_Reset);
161      }
162    }
163
164    private void UpdateAlgorithmParameterValues() {
165      TrainingSamplesStartParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesStart;
166      TrainingSamplesEndParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesEnd;
167      //var targetValues =
168      //  Problem.DataAnalysisProblemData.Dataset.GetVariableValues(Problem.DataAnalysisProblemData.TargetVariable.Value,
169      //  TrainingSamplesStartParameter.Value.Value, TrainingSamplesEndParameter.Value.Value);
170      //double range = targetValues.Max() - targetValues.Min();
171      //double lowerEstimationLimit = targetValues.Average() - 10.0 * range;
172      //double upperEstimationLimit = targetValues.Average() + 10.0 * range;
173      //evaluator.LowerEstimationLimitParameter.Value = new DoubleValue(lowerEstimationLimit);
174      //evaluator.UpperEstimationLimitParameter.Value = new DoubleValue(upperEstimationLimit);
175      //analyzer.LowerEstimationLimitParameter.Value = new DoubleValue(lowerEstimationLimit);
176      //analyzer.UpperEstimationLimitParameter.Value = new DoubleValue(upperEstimationLimit);
177    }
178    #endregion
179  }
180}
Note: See TracBrowser for help on using the repository browser.