Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3877 was 3877, checked in by gkronber, 14 years ago

Added linear regression and support vector machine algorithms for data analysis. #1012, #1009

File size: 7.9 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 System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Random;
34using HeuristicLab.Analysis;
35using HeuristicLab.Problems.DataAnalysis;
36using HeuristicLab.Problems.DataAnalysis.Regression.LinearRegression;
37using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
38using HeuristicLab.Problems.DataAnalysis.Evaluators;
39using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers;
40using HeuristicLab.Problems.DataAnalysis.Symbolic;
41
42namespace HeuristicLab.Algorithms.DataAnalysis {
43  /// <summary>
44  /// Linear regression data analysis algorithm.
45  /// </summary>
46  [Item("Linear Regression", "Linear regression data analysis algorithm.")]
47  [Creatable("Data Analysis")]
48  [StorableClass]
49  public sealed class LinearRegression : EngineAlgorithm {
50    private const string TrainingSamplesStartParameterName = "Training start";
51    private const string TrainingSamplesEndParameterName = "Training end";
52    private const string LinearRegressionModelParameterName = "LinearRegressionModel";
53    private const string ModelInterpreterParameterName = "Model interpreter";
54
55
56    #region Problem Properties
57    public override Type ProblemType {
58      get { return typeof(DataAnalysisProblem); }
59    }
60    public new DataAnalysisProblem Problem {
61      get { return (DataAnalysisProblem)base.Problem; }
62      set { base.Problem = value; }
63    }
64    #endregion
65
66    #region parameter properties
67    public IValueParameter<IntValue> TrainingSamplesStartParameter {
68      get { return (IValueParameter<IntValue>)Parameters[TrainingSamplesStartParameterName]; }
69    }
70    public IValueParameter<IntValue> TrainingSamplesEndParameter {
71      get { return (IValueParameter<IntValue>)Parameters[TrainingSamplesEndParameterName]; }
72    }
73    public IValueParameter<ISymbolicExpressionTreeInterpreter> ModelInterpreterParameter {
74      get { return (IValueParameter<ISymbolicExpressionTreeInterpreter>)Parameters[ModelInterpreterParameterName]; }
75    }
76    #endregion
77
78    [Storable]
79    private LinearRegressionSolutionCreator solutionCreator;
80    [Storable]
81    private SimpleSymbolicRegressionEvaluator evaluator;
82    [Storable]
83    private SimpleMSEEvaluator mseEvaluator;
84    [Storable]
85    private BestSymbolicRegressionSolutionAnalyzer analyzer;
86    public LinearRegression()
87      : base() {
88      Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesStartParameterName, "The first index of the data set partition to use for training."));
89      Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesEndParameterName, "The last index of the data set partition to use for training."));
90      Parameters.Add(new ValueParameter<ISymbolicExpressionTreeInterpreter>(ModelInterpreterParameterName, "The interpreter to use for evaluation of the model.", new SimpleArithmeticExpressionInterpreter()));
91
92      solutionCreator = new LinearRegressionSolutionCreator();
93      evaluator = new SimpleSymbolicRegressionEvaluator();
94      mseEvaluator = new SimpleMSEEvaluator();
95      analyzer = new BestSymbolicRegressionSolutionAnalyzer();
96
97      OperatorGraph.InitialOperator = solutionCreator;
98      solutionCreator.Successor = evaluator;
99      evaluator.Successor = mseEvaluator;
100      mseEvaluator.Successor = analyzer;
101
102      Initialize();
103    }
104    [StorableConstructor]
105    private LinearRegression(bool deserializing) : base(deserializing) { }
106
107    public override IDeepCloneable Clone(Cloner cloner) {
108      LinearRegression clone = (LinearRegression)base.Clone(cloner);
109      clone.solutionCreator = (LinearRegressionSolutionCreator)cloner.Clone(solutionCreator);
110      clone.evaluator = (SimpleSymbolicRegressionEvaluator)cloner.Clone(evaluator);
111      clone.mseEvaluator = (SimpleMSEEvaluator)cloner.Clone(mseEvaluator);
112      clone.analyzer = (BestSymbolicRegressionSolutionAnalyzer)cloner.Clone(analyzer);
113      clone.Initialize();
114      return clone;
115    }
116
117    public override void Prepare() {
118      if (Problem != null) base.Prepare();
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      TrainingSamplesStartParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesStart;
127      TrainingSamplesEndParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesEnd;
128      base.OnProblemChanged();
129    }
130
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.LowerEstimationLimitParameter.Value = new DoubleValue(double.NegativeInfinity);
141      evaluator.UpperEstimationLimitParameter.Value = new DoubleValue(double.PositiveInfinity);
142      evaluator.SymbolicExpressionTreeParameter.ActualName = solutionCreator.SymbolicExpressionTreeParameter.ActualName;
143      evaluator.SymbolicExpressionTreeInterpreterParameter.ActualName = ModelInterpreterParameter.Name;
144      evaluator.ValuesParameter.ActualName = "Training values";
145      evaluator.SamplesStartParameter.ActualName = TrainingSamplesStartParameterName;
146      evaluator.SamplesEndParameter.ActualName = TrainingSamplesEndParameterName;
147
148      mseEvaluator.ValuesParameter.ActualName = "Training values";
149      mseEvaluator.MeanSquaredErrorParameter.ActualName = "Training MSE";
150
151      analyzer.SymbolicExpressionTreeParameter.ActualName = solutionCreator.SymbolicExpressionTreeParameter.ActualName;
152      analyzer.SymbolicExpressionTreeParameter.Depth = 0;
153      analyzer.QualityParameter.ActualName = mseEvaluator.MeanSquaredErrorParameter.ActualName;
154      analyzer.QualityParameter.Depth = 0;
155      analyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = ModelInterpreterParameter.Name;
156      analyzer.LowerEstimationLimitParameter.Value = new DoubleValue(double.NegativeInfinity);
157      analyzer.UpperEstimationLimitParameter.Value = new DoubleValue(double.PositiveInfinity);
158
159      if (Problem != null) {
160        solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
161        evaluator.RegressionProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
162        analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
163      }
164    }
165    #endregion
166  }
167}
Note: See TracBrowser for help on using the repository browser.