Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5437 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

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