source: branches/GP.Symbols (TimeLag, Diff, Integral)/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SymbolicRegressionConditionalPearsonsRSquaredEvaluator.cs @ 5074

Last change on this file since 5074 was 5074, checked in by mkommend, 10 years ago

Corrected clonining of conditional evaluator (ticket #1256).

File size: 5.3 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
22
23using System;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis.Evaluators;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
35
36namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Evaluators {
37  [Item("SymbolicRegressionConditionalPearsonsRSquaredEvaluator", "Evaluates a symbolic regression solution.")]
38  [StorableClass]
39  public class SymbolicRegressionConditionalPearsonsRSquaredEvaluator : SingleObjectiveSymbolicRegressionEvaluator {
40    private const string ConditionVariableParameterName = "ConditionVariable";
41
42    public IValueParameter<StringValue> ConditionVariableParameter {
43      get { return (IValueParameter<StringValue>)Parameters[ConditionVariableParameterName]; }
44    }
45    public StringValue ConditionVariable {
46      get { return ConditionVariableParameter.Value; }
47    }
48
49    public SymbolicRegressionConditionalPearsonsRSquaredEvaluator()
50      : base() {
51      Parameters.Add(new ValueLookupParameter<StringValue>(ConditionVariableParameterName, "The variable name that states which samples should be skipped for evaluation."));
52    }
53    [StorableConstructor]
54    protected SymbolicRegressionConditionalPearsonsRSquaredEvaluator(bool deserializing) : base(deserializing) { }
55    protected SymbolicRegressionConditionalPearsonsRSquaredEvaluator(SymbolicRegressionConditionalPearsonsRSquaredEvaluator original, Cloner cloner)
56      : base(original, cloner) {
57    }
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new SymbolicRegressionConditionalPearsonsRSquaredEvaluator(this, cloner);
60    }
61
62    public override double Evaluate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows) {
63      double mse = Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, ConditionVariable.Value);
64      return mse;
65    }
66
67    public static double Calculate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows, string conditionVariable) {
68      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows);
69      IEnumerable<double> originalValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
70      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
71      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
72      IEnumerator<int> rowsEnumerator = rows.GetEnumerator();
73      OnlinePearsonsRSquaredEvaluator r2Evaluator = new OnlinePearsonsRSquaredEvaluator();
74
75      int minLag = 0;
76      var laggedTreeNodes = solution.IterateNodesPrefix().OfType<LaggedVariableTreeNode>();
77      if (laggedTreeNodes.Any())
78        minLag = laggedTreeNodes.Min(laggedTreeNode => laggedTreeNode.Symbol.MinLag);
79
80      while (originalEnumerator.MoveNext() && estimatedEnumerator.MoveNext() && rowsEnumerator.MoveNext()) {
81        double estimated = estimatedEnumerator.Current;
82        double original = originalEnumerator.Current;
83        int row = rowsEnumerator.Current;
84
85        bool evaluate = true;
86        for (int i = minLag; i <= 0 && evaluate; i++) {
87          evaluate = evaluate && dataset[conditionVariable, row - i].IsAlmost(0.0);
88        }
89
90        if (evaluate)
91          if (dataset[conditionVariable, row].IsAlmost(0.0) && dataset[conditionVariable, row - 1].IsAlmost(0.0)) {
92            if (double.IsNaN(estimated))
93              estimated = upperEstimationLimit;
94            else
95              estimated = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, estimated));
96            r2Evaluator.Add(original, estimated);
97          }
98      }
99
100      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext() || rowsEnumerator.MoveNext()) {
101        throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
102      } else {
103        return r2Evaluator.RSquared;
104      }
105    }
106
107
108  }
109}
Note: See TracBrowser for help on using the repository browser.