1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis.Evaluators;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis.Regression.LinearRegression;
|
---|
32 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
|
---|
33 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers;
|
---|
34 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
37 | /// <summary>
|
---|
38 | /// Linear regression data analysis algorithm.
|
---|
39 | /// </summary>
|
---|
40 | [Item("Linear Regression", "Linear regression data analysis algorithm.")]
|
---|
41 | [StorableClass]
|
---|
42 | public sealed class LinearRegression : EngineAlgorithm, IStorableContent {
|
---|
43 | private const string TrainingSamplesStartParameterName = "Training start";
|
---|
44 | private const string TrainingSamplesEndParameterName = "Training end";
|
---|
45 | private const string LinearRegressionModelParameterName = "LinearRegressionModel";
|
---|
46 | private const string ModelInterpreterParameterName = "Model interpreter";
|
---|
47 |
|
---|
48 | public string Filename { get; set; }
|
---|
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 | [StorableHook(HookType.AfterDeserialization)]
|
---|
101 | private void AfterDeserialization() {
|
---|
102 | Initialize();
|
---|
103 | }
|
---|
104 |
|
---|
105 | private LinearRegression(LinearRegression original, Cloner cloner)
|
---|
106 | : base(original, cloner) {
|
---|
107 | solutionCreator = cloner.Clone(original.solutionCreator);
|
---|
108 | evaluator = cloner.Clone(original.evaluator);
|
---|
109 | mseEvaluator = cloner.Clone(original.mseEvaluator);
|
---|
110 | analyzer = cloner.Clone(original.analyzer);
|
---|
111 | Initialize();
|
---|
112 | }
|
---|
113 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
114 | return new LinearRegression(this, cloner);
|
---|
115 | }
|
---|
116 |
|
---|
117 | public override void Prepare() {
|
---|
118 | if (Problem != null) base.Prepare();
|
---|
119 | }
|
---|
120 |
|
---|
121 | protected override void Problem_Reset(object sender, EventArgs e) {
|
---|
122 | UpdateAlgorithmParameterValues();
|
---|
123 | base.Problem_Reset(sender, e);
|
---|
124 | }
|
---|
125 |
|
---|
126 | #region Events
|
---|
127 | protected override void OnProblemChanged() {
|
---|
128 | solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
129 | evaluator.RegressionProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
130 | analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
131 | UpdateAlgorithmParameterValues();
|
---|
132 | Problem.Reset += new EventHandler(Problem_Reset);
|
---|
133 | base.OnProblemChanged();
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | #endregion
|
---|
138 |
|
---|
139 | #region Helpers
|
---|
140 | private void Initialize() {
|
---|
141 | solutionCreator.SamplesStartParameter.ActualName = TrainingSamplesStartParameter.Name;
|
---|
142 | solutionCreator.SamplesEndParameter.ActualName = TrainingSamplesEndParameter.Name;
|
---|
143 | solutionCreator.SymbolicExpressionTreeParameter.ActualName = LinearRegressionModelParameterName;
|
---|
144 |
|
---|
145 | evaluator.SymbolicExpressionTreeParameter.ActualName = solutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
146 | evaluator.SymbolicExpressionTreeInterpreterParameter.ActualName = ModelInterpreterParameter.Name;
|
---|
147 | evaluator.ValuesParameter.ActualName = "Training values";
|
---|
148 | evaluator.SamplesStartParameter.ActualName = TrainingSamplesStartParameterName;
|
---|
149 | evaluator.SamplesEndParameter.ActualName = TrainingSamplesEndParameterName;
|
---|
150 |
|
---|
151 | mseEvaluator.ValuesParameter.ActualName = "Training values";
|
---|
152 | mseEvaluator.MeanSquaredErrorParameter.ActualName = "Training MSE";
|
---|
153 |
|
---|
154 | analyzer.SymbolicExpressionTreeParameter.ActualName = solutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
155 | analyzer.SymbolicExpressionTreeParameter.Depth = 0;
|
---|
156 | analyzer.QualityParameter.ActualName = mseEvaluator.MeanSquaredErrorParameter.ActualName;
|
---|
157 | analyzer.QualityParameter.Depth = 0;
|
---|
158 | analyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = ModelInterpreterParameter.Name;
|
---|
159 |
|
---|
160 | if (Problem != null) {
|
---|
161 | solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
162 | evaluator.RegressionProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
163 | analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
164 | Problem.Reset += new EventHandler(Problem_Reset);
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | private void UpdateAlgorithmParameterValues() {
|
---|
169 | TrainingSamplesStartParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesStart;
|
---|
170 | TrainingSamplesEndParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesEnd;
|
---|
171 | //var targetValues =
|
---|
172 | // Problem.DataAnalysisProblemData.Dataset.GetVariableValues(Problem.DataAnalysisProblemData.TargetVariable.Value,
|
---|
173 | // TrainingSamplesStartParameter.Value.Value, TrainingSamplesEndParameter.Value.Value);
|
---|
174 | //double range = targetValues.Max() - targetValues.Min();
|
---|
175 | //double lowerEstimationLimit = targetValues.Average() - 10.0 * range;
|
---|
176 | //double upperEstimationLimit = targetValues.Average() + 10.0 * range;
|
---|
177 | //evaluator.LowerEstimationLimitParameter.Value = new DoubleValue(lowerEstimationLimit);
|
---|
178 | //evaluator.UpperEstimationLimitParameter.Value = new DoubleValue(upperEstimationLimit);
|
---|
179 | //analyzer.LowerEstimationLimitParameter.Value = new DoubleValue(lowerEstimationLimit);
|
---|
180 | //analyzer.UpperEstimationLimitParameter.Value = new DoubleValue(upperEstimationLimit);
|
---|
181 | }
|
---|
182 | #endregion
|
---|
183 | }
|
---|
184 | }
|
---|