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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 | using HeuristicLab.Random;
|
---|
34 | using HeuristicLab.Analysis;
|
---|
35 | using HeuristicLab.Problems.DataAnalysis;
|
---|
36 | using HeuristicLab.Problems.DataAnalysis.Regression.LinearRegression;
|
---|
37 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
|
---|
38 | using HeuristicLab.Problems.DataAnalysis.Evaluators;
|
---|
39 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers;
|
---|
40 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
41 |
|
---|
42 | namespace 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 | 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 | [StorableHook(HookType.AfterDeserialization)]
|
---|
141 | private void Initialize() {
|
---|
142 | solutionCreator.SamplesStartParameter.ActualName = TrainingSamplesStartParameter.Name;
|
---|
143 | solutionCreator.SamplesEndParameter.ActualName = TrainingSamplesEndParameter.Name;
|
---|
144 | solutionCreator.SymbolicExpressionTreeParameter.ActualName = LinearRegressionModelParameterName;
|
---|
145 |
|
---|
146 | evaluator.SymbolicExpressionTreeParameter.ActualName = solutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
147 | evaluator.SymbolicExpressionTreeInterpreterParameter.ActualName = ModelInterpreterParameter.Name;
|
---|
148 | evaluator.ValuesParameter.ActualName = "Training values";
|
---|
149 | evaluator.SamplesStartParameter.ActualName = TrainingSamplesStartParameterName;
|
---|
150 | evaluator.SamplesEndParameter.ActualName = TrainingSamplesEndParameterName;
|
---|
151 |
|
---|
152 | mseEvaluator.ValuesParameter.ActualName = "Training values";
|
---|
153 | mseEvaluator.MeanSquaredErrorParameter.ActualName = "Training MSE";
|
---|
154 |
|
---|
155 | analyzer.SymbolicExpressionTreeParameter.ActualName = solutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
156 | analyzer.SymbolicExpressionTreeParameter.Depth = 0;
|
---|
157 | analyzer.QualityParameter.ActualName = mseEvaluator.MeanSquaredErrorParameter.ActualName;
|
---|
158 | analyzer.QualityParameter.Depth = 0;
|
---|
159 | analyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = ModelInterpreterParameter.Name;
|
---|
160 |
|
---|
161 | if (Problem != null) {
|
---|
162 | solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
163 | evaluator.RegressionProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
164 | analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
|
---|
165 | Problem.Reset += new EventHandler(Problem_Reset);
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | private void UpdateAlgorithmParameterValues() {
|
---|
170 | TrainingSamplesStartParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesStart;
|
---|
171 | TrainingSamplesEndParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesEnd;
|
---|
172 | //var targetValues =
|
---|
173 | // Problem.DataAnalysisProblemData.Dataset.GetVariableValues(Problem.DataAnalysisProblemData.TargetVariable.Value,
|
---|
174 | // TrainingSamplesStartParameter.Value.Value, TrainingSamplesEndParameter.Value.Value);
|
---|
175 | //double range = targetValues.Max() - targetValues.Min();
|
---|
176 | //double lowerEstimationLimit = targetValues.Average() - 10.0 * range;
|
---|
177 | //double upperEstimationLimit = targetValues.Average() + 10.0 * range;
|
---|
178 | //evaluator.LowerEstimationLimitParameter.Value = new DoubleValue(lowerEstimationLimit);
|
---|
179 | //evaluator.UpperEstimationLimitParameter.Value = new DoubleValue(upperEstimationLimit);
|
---|
180 | //analyzer.LowerEstimationLimitParameter.Value = new DoubleValue(lowerEstimationLimit);
|
---|
181 | //analyzer.UpperEstimationLimitParameter.Value = new DoubleValue(upperEstimationLimit);
|
---|
182 | }
|
---|
183 | #endregion
|
---|
184 | }
|
---|
185 | }
|
---|