Index: branches/GP.Symbols (TimeLag, Diff, Integral)/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SymbolicRegressionConditionalPearsonsRSquaredEvaluator.cs
===================================================================
--- branches/GP.Symbols (TimeLag, Diff, Integral)/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SymbolicRegressionConditionalPearsonsRSquaredEvaluator.cs	(revision 5074)
+++ branches/GP.Symbols (TimeLag, Diff, Integral)/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SymbolicRegressionConditionalPearsonsRSquaredEvaluator.cs	(revision 5074)
@@ -0,0 +1,109 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using HeuristicLab.Common;
+using HeuristicLab.Core;
+using HeuristicLab.Data;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+using HeuristicLab.Parameters;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+using HeuristicLab.Problems.DataAnalysis.Evaluators;
+using HeuristicLab.Problems.DataAnalysis.Symbolic;
+using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
+
+namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Evaluators {
+  [Item("SymbolicRegressionConditionalPearsonsRSquaredEvaluator", "Evaluates a symbolic regression solution.")]
+  [StorableClass]
+  public class SymbolicRegressionConditionalPearsonsRSquaredEvaluator : SingleObjectiveSymbolicRegressionEvaluator {
+    private const string ConditionVariableParameterName = "ConditionVariable";
+
+    public IValueParameter<StringValue> ConditionVariableParameter {
+      get { return (IValueParameter<StringValue>)Parameters[ConditionVariableParameterName]; }
+    }
+    public StringValue ConditionVariable {
+      get { return ConditionVariableParameter.Value; }
+    }
+
+    public SymbolicRegressionConditionalPearsonsRSquaredEvaluator()
+      : base() {
+      Parameters.Add(new ValueLookupParameter<StringValue>(ConditionVariableParameterName, "The variable name that states which samples should be skipped for evaluation."));
+    }
+    [StorableConstructor]
+    protected SymbolicRegressionConditionalPearsonsRSquaredEvaluator(bool deserializing) : base(deserializing) { }
+    protected SymbolicRegressionConditionalPearsonsRSquaredEvaluator(SymbolicRegressionConditionalPearsonsRSquaredEvaluator original, Cloner cloner)
+      : base(original, cloner) {
+    }
+    public override IDeepCloneable Clone(Cloner cloner) {
+      return new SymbolicRegressionConditionalPearsonsRSquaredEvaluator(this, cloner);
+    }
+
+    public override double Evaluate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows) {
+      double mse = Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, ConditionVariable.Value);
+      return mse;
+    }
+
+    public static double Calculate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows, string conditionVariable) {
+      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows);
+      IEnumerable<double> originalValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
+      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
+      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
+      IEnumerator<int> rowsEnumerator = rows.GetEnumerator();
+      OnlinePearsonsRSquaredEvaluator r2Evaluator = new OnlinePearsonsRSquaredEvaluator();
+
+      int minLag = 0;
+      var laggedTreeNodes = solution.IterateNodesPrefix().OfType<LaggedVariableTreeNode>();
+      if (laggedTreeNodes.Any())
+        minLag = laggedTreeNodes.Min(laggedTreeNode => laggedTreeNode.Symbol.MinLag);
+
+      while (originalEnumerator.MoveNext() && estimatedEnumerator.MoveNext() && rowsEnumerator.MoveNext()) {
+        double estimated = estimatedEnumerator.Current;
+        double original = originalEnumerator.Current;
+        int row = rowsEnumerator.Current;
+
+        bool evaluate = true;
+        for (int i = minLag; i <= 0 && evaluate; i++) {
+          evaluate = evaluate && dataset[conditionVariable, row - i].IsAlmost(0.0);
+        }
+
+        if (evaluate)
+          if (dataset[conditionVariable, row].IsAlmost(0.0) && dataset[conditionVariable, row - 1].IsAlmost(0.0)) {
+            if (double.IsNaN(estimated))
+              estimated = upperEstimationLimit;
+            else
+              estimated = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, estimated));
+            r2Evaluator.Add(original, estimated);
+          }
+      }
+
+      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext() || rowsEnumerator.MoveNext()) {
+        throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
+      } else {
+        return r2Evaluator.RSquared;
+      }
+    }
+
+
+  }
+}
