Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SimpleSymbolicRegressionEvaluator.cs @ 3755

Last change on this file since 3755 was 3652, checked in by gkronber, 15 years ago

Added analyzer to calculate and track min, avg, and max R² on the training set for symbolic regression problems. #999 (Refactor algorithm analysis and tracing)

File size: 7.7 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 System.Drawing;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
34using HeuristicLab.Problems.DataAnalysis;
35using HeuristicLab.Operators;
36using HeuristicLab.Problems.DataAnalysis.Symbolic;
37
38namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
39  [Item("SimpleSymbolicRegressionEvaluator", "Evaluates a symbolic regression solution and outputs a matrix of target and estimated values.")]
40  [StorableClass]
41  public class SimpleSymbolicRegressionEvaluator : SingleSuccessorOperator {
42    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
43    private const string FunctionTreeParameterName = "FunctionTree";
44    private const string RegressionProblemDataParameterName = "RegressionProblemData";
45    private const string SamplesStartParameterName = "SamplesStart";
46    private const string SamplesEndParameterName = "SamplesEnd";
47    private const string ValuesParameterName = "Values";
48    private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
49    private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
50
51    #region ISymbolicRegressionEvaluator Members
52    public ILookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
53      get { return (ILookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
54    }
55
56    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
57      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[FunctionTreeParameterName]; }
58    }
59
60    public ILookupParameter<DataAnalysisProblemData> RegressionProblemDataParameter {
61      get { return (ILookupParameter<DataAnalysisProblemData>)Parameters[RegressionProblemDataParameterName]; }
62    }
63
64    public IValueLookupParameter<IntValue> SamplesStartParameter {
65      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; }
66    }
67
68    public IValueLookupParameter<IntValue> SamplesEndParameter {
69      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; }
70    }
71    public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter {
72      get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
73    }
74    public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter {
75      get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
76    }
77
78    public ILookupParameter<DoubleMatrix> ValuesParameter {
79      get { return (ILookupParameter<DoubleMatrix>)Parameters[ValuesParameterName]; }
80    }
81
82    #endregion
83    #region properties
84    public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
85      get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; }
86    }
87    public SymbolicExpressionTree SymbolicExpressionTree {
88      get { return SymbolicExpressionTreeParameter.ActualValue; }
89    }
90    public DataAnalysisProblemData RegressionProblemData {
91      get { return RegressionProblemDataParameter.ActualValue; }
92    }
93    public IntValue SamplesStart {
94      get { return SamplesStartParameter.ActualValue; }
95    }
96    public IntValue SamplesEnd {
97      get { return SamplesEndParameter.ActualValue; }
98    }
99    public DoubleValue UpperEstimationLimit {
100      get { return UpperEstimationLimitParameter.ActualValue; }
101    }
102    public DoubleValue LowerEstimationLimit {
103      get { return LowerEstimationLimitParameter.ActualValue; }
104    }
105
106    #endregion
107
108    public SimpleSymbolicRegressionEvaluator()
109      : base() {
110      Parameters.Add(new LookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic expression tree."));
111      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(FunctionTreeParameterName, "The symbolic regression solution encoded as a symbolic expression tree."));
112      Parameters.Add(new LookupParameter<DataAnalysisProblemData>(RegressionProblemDataParameterName, "The problem data on which the symbolic regression solution should be evaluated."));
113      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The start index of the dataset partition on which the symbolic regression solution should be evaluated."));
114      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The end index of the dataset partition on which the symbolic regression solution should be evaluated."));
115      Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit that should be used as cut off value for the output values of symbolic expression trees."));
116      Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit that should be used as cut off value for the output values of symbolic expression trees."));
117      Parameters.Add(new LookupParameter<DoubleMatrix>(ValuesParameterName, "The matrix of target and estimated values as generated by the symbolic regression solution."));
118    }
119
120    public override IOperation Apply() {
121      Dataset dataset = RegressionProblemData.Dataset;
122      string targetVariable = RegressionProblemData.TargetVariable.Value;
123      ISymbolicExpressionTreeInterpreter interpreter = SymbolicExpressionTreeInterpreter;
124      SymbolicExpressionTree tree = SymbolicExpressionTree;
125      int start = SamplesStart.Value;
126      int end = SamplesEnd.Value;
127      double lowerEstimationLimit = LowerEstimationLimit.Value;
128      double upperEstimationLimit = UpperEstimationLimit.Value;
129      int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
130      var estimatedValues = from x in interpreter.GetSymbolicExpressionTreeValues(tree, dataset, Enumerable.Range(start, end - start))
131                            let boundedX = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, x))
132                            select double.IsNaN(boundedX) ? upperEstimationLimit : boundedX;
133      var originalValues = from row in Enumerable.Range(start, end - start) select dataset[row, targetVariableIndex];
134      // NB: indexes must match SimpleEvaluator.ORIGINAL_INDEX and SimpleEvaluator.ESTIMATED_INDEX
135      ValuesParameter.ActualValue = new DoubleMatrix(MatrixExtensions<double>.Create(originalValues.ToArray(), estimatedValues.ToArray()));
136      return base.Apply();
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.