Index: /branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/ConstrainedConstantOptimizationEvaluator.cs
===================================================================
--- /branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/ConstrainedConstantOptimizationEvaluator.cs	(revision 17135)
+++ /branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/ConstrainedConstantOptimizationEvaluator.cs	(revision 17136)
@@ -153,6 +153,5 @@
 
         if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) {
-          var evaluationRows = GenerateRowsToEvaluate();
-          quality = SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, double.MinValue, double.MaxValue, ProblemDataParameter.ActualValue, evaluationRows, applyLinearScaling: false);
+          throw new NotSupportedException();
         }
 
@@ -165,6 +164,5 @@
 
       } else {
-        var evaluationRows = GenerateRowsToEvaluate();
-        quality = SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, double.MinValue, double.MaxValue, ProblemDataParameter.ActualValue, evaluationRows, applyLinearScaling: false);
+        throw new NotSupportedException();
       }
       QualityParameter.ActualValue = new DoubleValue(quality);
@@ -292,4 +290,5 @@
       // buffers for calculate_jacobian
       var target = problemData.TargetVariableTrainingValues.ToArray();
+      var targetVariance = target.VariancePop();
       var fi_eval = new double[target.Length];
       var jac_eval = new double[target.Length, thetaValues.Count];
@@ -337,5 +336,4 @@
         try {
           alglib.minnscreate(thetaValues.Count, thetaValues.ToArray(), out state);
-          // alglib.minnssetalgoslp(state);        // SLP is more robust but slower
           alglib.minnssetbc(state, thetaValues.Select(_ => -10000.0).ToArray(), thetaValues.Select(_ => +10000.0).ToArray());
           alglib.minnssetcond(state, 1E-7, maxIterations);
@@ -353,5 +351,5 @@
           counter.GradientEvaluations += rep.nfev;
 
-          if (rep.terminationtype != -8) {
+          if (rep.terminationtype > 0) {
             // update parameters in tree
             var pIdx = 0;
@@ -363,15 +361,12 @@
               }
             }
-
             // note: we keep the optimized constants even when the tree is worse.
           }
-
+          if (Math.Abs(rep.nlcerr) > 0.01) return targetVariance; // constraints are violated
         } catch (ArithmeticException) {
-          // eval MSE of original tree
-          return SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling: false);
-
+          return targetVariance;
         } catch (alglib.alglibexception) {
           // eval MSE of original tree
-          return SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling: false);
+          return targetVariance;
         }
       } else if (solver.Contains("minnlc")) {
@@ -413,12 +408,10 @@
             // note: we keep the optimized constants even when the tree is worse.
           }
+          if (Math.Abs(rep.nlcerr) > 0.01) return targetVariance; // constraints are violated
 
         } catch (ArithmeticException) {
-          // eval MSE of original tree
-          return SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling: false);
-
+          return targetVariance;
         } catch (alglib.alglibexception) {
-          // eval MSE of original tree
-          return SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling: false);
+          return targetVariance;
         }
       } else {
@@ -428,5 +421,6 @@
 
       // evaluate tree with updated constants
-      return SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling: false);
+      var residualVariance = SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling: false);
+      return Math.Min(residualVariance, targetVariance);
     }
 
