Free cookie consent management tool by TermsFeed Policy Generator

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

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

Sorted usings and removed unused usings in entire solution (#1094)

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