Index: branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/ConstrainedNLS.cs
===================================================================
--- branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/ConstrainedNLS.cs	(revision 17197)
+++ branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/ConstrainedNLS.cs	(revision 17197)
@@ -0,0 +1,152 @@
+﻿using System;
+using HeuristicLab.Optimization;
+using HEAL.Attic;
+using HeuristicLab.Common;
+using System.Threading;
+using HeuristicLab.Core;
+using HeuristicLab.Data;
+using HeuristicLab.Parameters;
+using System.Linq;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+using HeuristicLab.Analysis;
+
+namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Extensions {
+  [StorableType("676B237C-DD9C-4F24-B64F-D44B0FA1F6A6")]
+  [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 120)]
+  [Item(Name = "ConstrainedNLS", Description = "Non-linear Regression with non-linear constraints")]
+  public class ConstrainedNLS : BasicAlgorithm {
+    public static readonly string IterationsParameterName = "Iterations";
+    public static readonly string SolverParameterName = "Solver";
+    public static readonly string ModelStructureParameterName = "Model structure";
+
+
+    public IFixedValueParameter<IntValue> IterationsParameter {
+      get { return (IFixedValueParameter<IntValue>)Parameters[IterationsParameterName]; }
+    }
+    public IFixedValueParameter<StringValue> ModelStructureParameter {
+      get { return (IFixedValueParameter<StringValue>)Parameters[ModelStructureParameterName]; }
+    }
+    public IConstrainedValueParameter<StringValue> SolverParameter { get { return (IConstrainedValueParameter<StringValue>)Parameters[SolverParameterName]; } }
+
+    public IFixedValueParameter<DoubleValue> FuncToleranceRelParameter {
+      get { return (IFixedValueParameter<DoubleValue>)Parameters["FuncToleranceRel"]; }
+    }
+    public IFixedValueParameter<DoubleValue> FuncToleranceAbsParameter {
+      get { return (IFixedValueParameter<DoubleValue>)Parameters["FuncToleranceAbs"]; }
+    }
+    public IFixedValueParameter<DoubleValue> MaxTimeParameter {
+      get { return (IFixedValueParameter<DoubleValue>)Parameters["MaxTime"]; }
+    }
+    public int Iterations { get { return IterationsParameter.Value.Value; } set { IterationsParameter.Value.Value = value; } }
+
+    public StringValue Solver {
+      get { return SolverParameter.Value; }
+      set { throw new NotImplementedException(); }
+    }
+
+    public string ModelStructure {
+      get { return ModelStructureParameter.Value.Value; }
+      set { ModelStructureParameter.Value.Value = value; }
+    }
+
+    public double FuncToleranceRel { get { return FuncToleranceRelParameter.Value.Value; } set { FuncToleranceRelParameter.Value.Value = value; } }
+    public double FuncToleranceAbs { get { return FuncToleranceAbsParameter.Value.Value; } set { FuncToleranceAbsParameter.Value.Value = value; } }
+    public double MaxTime { get { return MaxTimeParameter.Value.Value; } set { MaxTimeParameter.Value.Value = value; } }
+
+    public ConstrainedNLS() : base() {
+      Problem = new RegressionProblem();
+
+      Parameters.Add(new FixedValueParameter<StringValue>(ModelStructureParameterName, "The function for which the parameters must be fit (only numeric constants are tuned).", new StringValue("1.0 * x*x + 0.0")));
+      Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "Determines how many iterations should be calculated while optimizing the constant of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue(10)));
+      var validSolvers = new ItemSet<StringValue>(new[] { "MMA", "COBYLA", "CCSAQ", "ISRES" }.Select(s => new StringValue(s).AsReadOnly()));
+      Parameters.Add(new ConstrainedValueParameter<StringValue>(SolverParameterName, "The solver algorithm", validSolvers, validSolvers.First()));
+      Parameters.Add(new FixedValueParameter<DoubleValue>("FuncToleranceRel", new DoubleValue(0)));
+      Parameters.Add(new FixedValueParameter<DoubleValue>("FuncToleranceAbs", new DoubleValue(0)));
+      Parameters.Add(new FixedValueParameter<DoubleValue>("MaxTime", new DoubleValue(10)));
+    }
+
+    public ConstrainedNLS(ConstrainedNLS original, Cloner cloner) : base(original, cloner) {
+    }
+
+    [StorableConstructor]
+    protected ConstrainedNLS(StorableConstructorFlag _) : base(_) {
+    }
+
+    public override bool SupportsPause => false;
+
+    public override IDeepCloneable Clone(Cloner cloner) {
+      return new ConstrainedNLS(this, cloner);
+    }
+
+    protected override void Run(CancellationToken cancellationToken) {
+      var parser = new InfixExpressionParser();
+      var tree = parser.Parse(ModelStructure);
+      var problem = (IRegressionProblem)Problem;
+
+
+      #region prepare results
+      var functionEvaluations = new IntValue(0);
+      Results.AddOrUpdateResult("Evaluations", functionEvaluations);
+      var bestError = new DoubleValue(double.MaxValue);
+      Results.AddOrUpdateResult("Best error", bestError);
+      Results.AddOrUpdateResult("Tree", tree);
+      var qualitiesTable = new DataTable("Qualities");
+      var curQualityRow = new DataRow("Current Quality");
+      var bestQualityRow = new DataRow("Best Quality");
+      qualitiesTable.Rows.Add(bestQualityRow);
+      qualitiesTable.Rows.Add(curQualityRow);
+      Results.AddOrUpdateResult("Qualities", qualitiesTable);
+
+      var curConstraintValue = new DoubleValue(0);
+      Results.AddOrUpdateResult("Current Constraint Value", curConstraintValue);
+      var curConstraintIdx = new IntValue(0);
+      Results.AddOrUpdateResult("Current Constraint Index", curConstraintIdx);
+
+      var curConstraintRow = new DataRow("Constraint Value");
+      var constraintsTable = new DataTable("Constraints");
+
+      constraintsTable.Rows.Add(curConstraintRow);
+      Results.AddOrUpdateResult("Constraints", constraintsTable);
+
+      #endregion
+
+      var state = new ConstrainedNLSInternal(Solver.Value, tree, Iterations, problem.ProblemData, FuncToleranceRel, FuncToleranceAbs, MaxTime);
+
+      // we use a listener model here to get state from the solver      
+
+      state.FunctionEvaluated += State_FunctionEvaluated;
+      state.ConstraintEvaluated += State_ConstraintEvaluated;
+
+      state.Optimize();
+      bestError.Value = state.BestError;
+      curQualityRow.Values.Add(state.CurError);
+      bestQualityRow.Values.Add(bestError.Value);
+
+
+      Results.AddOrUpdateResult("Best solution", CreateSolution(state.BestTree, problem.ProblemData));
+
+
+      // local function
+      void State_FunctionEvaluated() {
+        if (cancellationToken.IsCancellationRequested) state.RequestStop();
+        functionEvaluations.Value++;
+        bestError.Value = state.BestError;
+        curQualityRow.Values.Add(state.CurError);
+        bestQualityRow.Values.Add(bestError.Value);
+      }
+
+      // local function
+      void State_ConstraintEvaluated(int constraintIdx, double value) {
+        curConstraintIdx.Value = constraintIdx;
+        curConstraintValue.Value = value;
+        curConstraintRow.Values.Add(value);
+      }
+    }
+
+    private static ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree tree, IRegressionProblemData problemData) {
+      var model = new SymbolicRegressionModel(problemData.TargetVariable, tree, new SymbolicDataAnalysisExpressionTreeLinearInterpreter());
+      // model.Scale(problemData);
+      return model.CreateRegressionSolution((IRegressionProblemData)problemData.Clone());
+    }
+  }
+}
Index: branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/ConstrainedNLSInternal.cs
===================================================================
--- branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/ConstrainedNLSInternal.cs	(revision 17197)
+++ branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/ConstrainedNLSInternal.cs	(revision 17197)
@@ -0,0 +1,446 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using HeuristicLab.Common;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+
+namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Extensions {
+  internal class ConstrainedNLSInternal {
+    private readonly int maxIterations;
+    public int MaxIterations => maxIterations;
+
+    private readonly string solver;
+    public string Solver => solver;
+
+    private readonly ISymbolicExpressionTree expr;
+    public ISymbolicExpressionTree Expr => expr;
+
+    private readonly IRegressionProblemData problemData;
+
+    public IRegressionProblemData ProblemData => problemData;
+
+
+    public event Action FunctionEvaluated;
+    public event Action<int, double> ConstraintEvaluated;
+
+    private double bestError = double.MaxValue;
+    public double BestError => bestError;
+
+    private double curError = double.MaxValue;
+    public double CurError => curError;
+
+    private double[] bestSolution;
+    public double[] BestSolution => bestSolution;
+
+    private ISymbolicExpressionTree bestTree;
+    public ISymbolicExpressionTree BestTree => bestTree;
+
+    // begin internal state
+    private IntPtr nlopt;
+    private SymbolicDataAnalysisExpressionTreeLinearInterpreter interpreter;
+    private readonly NLOpt.nlopt_func calculateObjectiveDelegate; // must hold the delegate to prevent GC
+    private readonly IntPtr[] constraintDataPtr; // must hold the objects to prevent GC
+    private readonly NLOpt.nlopt_func[] calculateConstraintDelegates; // must hold the delegates to prevent GC
+    private readonly List<double> thetaValues;
+    private readonly IDictionary<string, Interval> dataIntervals;
+    private readonly int[] trainingRows;
+    private readonly double[] target;
+    private readonly ISymbolicExpressionTree preparedTree;
+    private readonly ISymbolicExpressionTreeNode[] preparedTreeParameterNodes;
+    private readonly List<ConstantTreeNode>[] allThetaNodes;
+    private readonly double[] fi_eval;
+    private readonly double[,] jac_eval;
+    private readonly ISymbolicExpressionTree scaledTree;
+
+    // end internal state
+
+
+    // for data exchange to/from optimizer in native code
+    [StructLayout(LayoutKind.Sequential)]
+    private struct ConstraintData {
+      public int Idx;
+      public ISymbolicExpressionTree Tree;
+      public ISymbolicExpressionTreeNode[] ParameterNodes;
+    }
+
+    internal ConstrainedNLSInternal(string solver, ISymbolicExpressionTree expr, int maxIterations, IRegressionProblemData problemData, double ftol_rel = 0, double ftol_abs = 0, double maxTime = 0) {
+      this.solver = solver;
+      this.expr = expr;
+      this.maxIterations = maxIterations;
+      this.problemData = problemData;
+      this.interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
+
+
+      var intervalConstraints = problemData.IntervalConstraints;
+      dataIntervals = problemData.VariableRanges.GetIntervals();
+      trainingRows = problemData.TrainingIndices.ToArray();
+      // buffers 
+      target = problemData.TargetVariableTrainingValues.ToArray();
+      var targetStDev = target.StandardDeviationPop();
+      var targetVariance = targetStDev * targetStDev;
+      var targetMean = target.Average();
+      var pred = interpreter.GetSymbolicExpressionTreeValues(expr, problemData.Dataset, trainingRows).ToArray();
+
+      if (pred.Any(pi => double.IsInfinity(pi) || double.IsNaN(pi))) throw new ArgumentException("The expression produces NaN or infinite values.");
+
+      #region linear scaling
+      var predStDev = pred.StandardDeviationPop();
+      if (predStDev == 0) throw new ArgumentException("The expression is constant.");
+      var predMean = pred.Average();
+
+      var scalingFactor = targetStDev / predStDev;
+      var offset = targetMean - predMean * scalingFactor;
+
+      scaledTree = CopyAndScaleTree(expr, scalingFactor, offset);
+      #endregion
+
+      // convert constants to variables named theta...
+      var treeForDerivation = ReplaceConstWithVar(scaledTree, out List<string> thetaNames, out thetaValues); // copies the tree
+
+      // create trees for relevant derivatives
+      Dictionary<string, ISymbolicExpressionTree> derivatives = new Dictionary<string, ISymbolicExpressionTree>();
+      allThetaNodes = thetaNames.Select(_ => new List<ConstantTreeNode>()).ToArray();
+      var constraintTrees = new List<ISymbolicExpressionTree>();
+      foreach (var constraint in intervalConstraints.Constraints) {
+        if (constraint.IsDerivation) {
+          if (!problemData.AllowedInputVariables.Contains(constraint.Variable))
+            throw new ArgumentException($"Invalid constraint: the variable {constraint.Variable} does not exist in the dataset.");
+          var df = DerivativeCalculator.Derive(treeForDerivation, constraint.Variable);
+
+          // NLOpt requires constraint expressions of the form c(x) <= 0
+          // -> we make two expressions, one for the lower bound and one for the upper bound
+
+          if (constraint.Interval.UpperBound < double.PositiveInfinity) {
+            var df_smaller_upper = Subtract((ISymbolicExpressionTree)df.Clone(), CreateConstant(constraint.Interval.UpperBound));
+            // convert variables named theta back to constants
+            var df_prepared = ReplaceVarWithConst(df_smaller_upper, thetaNames, thetaValues, allThetaNodes);
+            constraintTrees.Add(df_prepared);
+          }
+          if (constraint.Interval.LowerBound > double.NegativeInfinity) {
+            var df_larger_lower = Subtract(CreateConstant(constraint.Interval.LowerBound), (ISymbolicExpressionTree)df.Clone());
+            // convert variables named theta back to constants
+            var df_prepared = ReplaceVarWithConst(df_larger_lower, thetaNames, thetaValues, allThetaNodes);
+            constraintTrees.Add(df_prepared);
+          }
+        } else {
+          if (constraint.Interval.UpperBound < double.PositiveInfinity) {
+            var f_smaller_upper = Subtract((ISymbolicExpressionTree)treeForDerivation.Clone(), CreateConstant(constraint.Interval.UpperBound));
+            // convert variables named theta back to constants
+            var df_prepared = ReplaceVarWithConst(f_smaller_upper, thetaNames, thetaValues, allThetaNodes);
+            constraintTrees.Add(df_prepared);
+          }
+          if (constraint.Interval.LowerBound > double.NegativeInfinity) {
+            var f_larger_lower = Subtract(CreateConstant(constraint.Interval.LowerBound), (ISymbolicExpressionTree)treeForDerivation.Clone());
+            // convert variables named theta back to constants
+            var df_prepared = ReplaceVarWithConst(f_larger_lower, thetaNames, thetaValues, allThetaNodes);
+            constraintTrees.Add(df_prepared);
+          }
+        }
+      }
+
+      preparedTree = ReplaceVarWithConst(treeForDerivation, thetaNames, thetaValues, allThetaNodes);
+      preparedTreeParameterNodes = GetParameterNodes(preparedTree, allThetaNodes);
+
+      var dim = thetaValues.Count;
+      fi_eval = new double[target.Length]; // init buffer;
+      jac_eval = new double[target.Length, dim]; // init buffer
+
+
+      var minVal = Math.Min(-1000.0, thetaValues.Min());
+      var maxVal = Math.Max(1000.0, thetaValues.Max());
+      var lb = Enumerable.Repeat(minVal, thetaValues.Count).ToArray();
+      var up = Enumerable.Repeat(maxVal, thetaValues.Count).ToArray();
+      nlopt = NLOpt.nlopt_create(GetSolver(solver), (uint)dim);
+
+      NLOpt.nlopt_set_lower_bounds(nlopt, lb);
+      NLOpt.nlopt_set_upper_bounds(nlopt, up);
+      calculateObjectiveDelegate = new NLOpt.nlopt_func(CalculateObjective); // keep a reference to the delegate (see below)
+      NLOpt.nlopt_set_min_objective(nlopt, calculateObjectiveDelegate, IntPtr.Zero);
+
+
+      constraintDataPtr = new IntPtr[constraintTrees.Count];
+      calculateConstraintDelegates = new NLOpt.nlopt_func[constraintTrees.Count]; // make sure we keep a reference to the delegates (otherwise GC will free delegate objects see https://stackoverflow.com/questions/7302045/callback-delegates-being-collected#7302258)
+      for (int i = 0; i < constraintTrees.Count; i++) {
+        var constraintData = new ConstraintData() { Idx = i, Tree = constraintTrees[i], ParameterNodes = GetParameterNodes(constraintTrees[i], allThetaNodes) };
+        constraintDataPtr[i] = Marshal.AllocHGlobal(Marshal.SizeOf<ConstraintData>());
+        Marshal.StructureToPtr(constraintData, constraintDataPtr[i], fDeleteOld: false);
+        calculateConstraintDelegates[i] = new NLOpt.nlopt_func(CalculateConstraint);
+        NLOpt.nlopt_add_inequality_constraint(nlopt, calculateConstraintDelegates[i], constraintDataPtr[i], 1e-8);
+      }
+
+      NLOpt.nlopt_set_ftol_rel(nlopt, ftol_rel);
+      NLOpt.nlopt_set_ftol_abs(nlopt, ftol_abs);
+      NLOpt.nlopt_set_maxtime(nlopt, maxTime);
+      NLOpt.nlopt_set_maxeval(nlopt, maxIterations);
+    }
+
+    ~ConstrainedNLSInternal() {
+      if (nlopt != IntPtr.Zero)
+        NLOpt.nlopt_destroy(nlopt);
+      if (constraintDataPtr != null) {
+        for (int i = 0; i < constraintDataPtr.Length; i++)
+          if (constraintDataPtr[i] != IntPtr.Zero)
+            Marshal.FreeHGlobal(constraintDataPtr[i]);
+      }
+    }
+
+
+    internal void Optimize() {
+      var x = thetaValues.ToArray();  /* initial guess */
+      double minf = double.MaxValue; /* minimum objective value upon return */
+      var res = NLOpt.nlopt_optimize(nlopt, x, ref minf);
+      bestSolution = x;
+      bestError = minf;
+      
+      if (res < 0) {
+        throw new InvalidOperationException($"NLOpt failed {res} {NLOpt.nlopt_get_errmsg(nlopt)}");
+      } else {
+        // update parameters in tree
+        var pIdx = 0;
+        // here we lose the two last parameters (for linear scaling)
+        foreach (var node in scaledTree.IterateNodesPostfix()) {
+          if (node is ConstantTreeNode constTreeNode) {
+            constTreeNode.Value = x[pIdx++];
+          } else if (node is VariableTreeNode varTreeNode) {
+            varTreeNode.Weight = x[pIdx++];
+          }
+        }
+        if (pIdx != x.Length) throw new InvalidProgramException();
+      }
+      bestTree = scaledTree;
+    }
+
+    double CalculateObjective(uint dim, double[] curX, double[] grad, IntPtr data) {
+      UpdateThetaValues(curX);
+      var sse = 0.0;
+
+      if (grad != null) {
+        var autoDiffEval = new VectorAutoDiffEvaluator();
+        autoDiffEval.Evaluate(preparedTree, problemData.Dataset, trainingRows,
+          preparedTreeParameterNodes, fi_eval, jac_eval);
+
+        // calc sum of squared errors and gradient
+        for (int j = 0; j < grad.Length; j++) grad[j] = 0;
+        for (int i = 0; i < target.Length; i++) {
+          var r = target[i] - fi_eval[i];
+          sse += r * r;
+          for (int j = 0; j < grad.Length; j++) {
+            grad[j] -= 2 * r * jac_eval[i, j];
+          }
+        }
+        // average
+        for (int j = 0; j < grad.Length; j++) { grad[j] /= target.Length; }
+      } else {
+        var eval = new VectorEvaluator();
+        var prediction = eval.Evaluate(preparedTree, problemData.Dataset, trainingRows);
+
+        // calc sum of squared errors and gradient
+        sse = 0.0;
+        for (int i = 0; i < target.Length; i++) {
+          var r = target[i] - prediction[i];
+          sse += r * r;
+        }
+      }
+
+      UpdateBestSolution(sse / target.Length, curX);
+      RaiseFunctionEvaluated();
+
+      if (double.IsNaN(sse)) return double.MaxValue;
+      return sse / target.Length;
+    }
+
+    private void UpdateBestSolution(double curF, double[] curX) {
+      if (double.IsNaN(curF) || double.IsInfinity(curF)) return;
+      else if (curF < bestError) {
+        bestError = curF;
+        bestSolution = (double[])curX.Clone();
+      }
+    }
+
+    private void UpdateConstraintViolations(int constraintIdx, double value) {
+      if (double.IsNaN(value) || double.IsInfinity(value)) return;
+      RaiseConstraintEvaluated(constraintIdx, value);
+      // else if (curF < bestError) {
+      //   bestError = curF;
+      //   bestSolution = (double[])curX.Clone();
+      // }
+    }
+
+    double CalculateConstraint(uint dim, double[] curX, double[] grad, IntPtr data) {
+      UpdateThetaValues(curX);
+      var intervalEvaluator = new IntervalEvaluator();
+      var constraintData = Marshal.PtrToStructure<ConstraintData>(data);
+
+      if (grad != null) for (int j = 0; j < grad.Length; j++) grad[j] = 0; // clear grad
+
+      var interval = intervalEvaluator.Evaluate(constraintData.Tree, dataIntervals, constraintData.ParameterNodes,
+        out double[] lowerGradient, out double[] upperGradient);
+
+      // we transformed this to a constraint c(x) <= 0, so only the upper bound is relevant for us
+      if (grad != null) for (int j = 0; j < grad.Length; j++) { grad[j] = upperGradient[j]; }
+      UpdateConstraintViolations(constraintData.Idx, interval.UpperBound);
+      if (double.IsNaN(interval.UpperBound)) return double.MaxValue;
+      else return interval.UpperBound;
+    }
+
+
+    void UpdateThetaValues(double[] theta) {
+      for (int i = 0; i < theta.Length; ++i) {
+        foreach (var constNode in allThetaNodes[i]) constNode.Value = theta[i];
+      }
+    }
+
+    internal void RequestStop() {
+      NLOpt.nlopt_set_force_stop(nlopt, 1); // hopefully NLOpt is thread safe  , val must be <> 0 otherwise no effect
+    }
+
+    private void RaiseFunctionEvaluated() {
+      FunctionEvaluated?.Invoke();
+    }
+
+    private void RaiseConstraintEvaluated(int idx, double value) {
+      ConstraintEvaluated?.Invoke(idx, value);
+    }
+
+
+    #region helper
+
+    private static ISymbolicExpressionTree CopyAndScaleTree(ISymbolicExpressionTree tree, double scalingFactor, double offset) {
+      var m = (ISymbolicExpressionTree)tree.Clone();
+
+      var add = MakeNode<Addition>(MakeNode<Multiplication>(m.Root.GetSubtree(0).GetSubtree(0), CreateConstant(scalingFactor)), CreateConstant(offset));
+      m.Root.GetSubtree(0).RemoveSubtree(0);
+      m.Root.GetSubtree(0).AddSubtree(add);
+      return m;
+    }
+
+    private static void UpdateConstants(ISymbolicExpressionTreeNode[] nodes, double[] constants) {
+      if (nodes.Length != constants.Length) throw new InvalidOperationException();
+      for (int i = 0; i < nodes.Length; i++) {
+        if (nodes[i] is VariableTreeNode varNode) varNode.Weight = constants[i];
+        else if (nodes[i] is ConstantTreeNode constNode) constNode.Value = constants[i];
+      }
+    }
+
+    private NLOpt.nlopt_algorithm GetSolver(string solver) {
+      if (solver.Contains("MMA")) return NLOpt.nlopt_algorithm.NLOPT_LD_MMA;
+      if (solver.Contains("COBYLA")) return NLOpt.nlopt_algorithm.NLOPT_LN_COBYLA;
+      if (solver.Contains("CCSAQ")) return NLOpt.nlopt_algorithm.NLOPT_LD_CCSAQ;
+      if (solver.Contains("ISRES")) return NLOpt.nlopt_algorithm.NLOPT_GN_ISRES;
+      throw new ArgumentException($"Unknown solver {solver}");
+    }
+
+    private static ISymbolicExpressionTreeNode[] GetParameterNodes(ISymbolicExpressionTree tree, List<ConstantTreeNode>[] allNodes) {
+      // TODO better solution necessary
+      var treeConstNodes = tree.IterateNodesPostfix().OfType<ConstantTreeNode>().ToArray();
+      var paramNodes = new ISymbolicExpressionTreeNode[allNodes.Length];
+      for (int i = 0; i < paramNodes.Length; i++) {
+        paramNodes[i] = allNodes[i].SingleOrDefault(n => treeConstNodes.Contains(n));
+      }
+      return paramNodes;
+    }
+
+    private static ISymbolicExpressionTree ReplaceVarWithConst(ISymbolicExpressionTree tree, List<string> thetaNames, List<double> thetaValues, List<ConstantTreeNode>[] thetaNodes) {
+      var copy = (ISymbolicExpressionTree)tree.Clone();
+      var nodes = copy.IterateNodesPostfix().ToList();
+      for (int i = 0; i < nodes.Count; i++) {
+        var n = nodes[i] as VariableTreeNode;
+        if (n != null) {
+          var thetaIdx = thetaNames.IndexOf(n.VariableName);
+          if (thetaIdx >= 0) {
+            var parent = n.Parent;
+            if (thetaNodes[thetaIdx].Any()) {
+              // HACK: REUSE CONSTANT TREE NODE IN SEVERAL TREES
+              // we use this trick to allow autodiff over thetas when thetas occurr multiple times in the tree (e.g. in derived trees)
+              var constNode = thetaNodes[thetaIdx].First();
+              var childIdx = parent.IndexOfSubtree(n);
+              parent.RemoveSubtree(childIdx);
+              parent.InsertSubtree(childIdx, constNode);
+            } else {
+              var constNode = (ConstantTreeNode)CreateConstant(thetaValues[thetaIdx]);
+              var childIdx = parent.IndexOfSubtree(n);
+              parent.RemoveSubtree(childIdx);
+              parent.InsertSubtree(childIdx, constNode);
+              thetaNodes[thetaIdx].Add(constNode);
+            }
+          }
+        }
+      }
+      return copy;
+    }
+
+    private static ISymbolicExpressionTree ReplaceConstWithVar(ISymbolicExpressionTree tree, out List<string> thetaNames, out List<double> thetaValues) {
+      thetaNames = new List<string>();
+      thetaValues = new List<double>();
+      var copy = (ISymbolicExpressionTree)tree.Clone();
+      var nodes = copy.IterateNodesPostfix().ToList();
+
+      int n = 1;
+      for (int i = 0; i < nodes.Count; ++i) {
+        var node = nodes[i];
+        if (node is ConstantTreeNode constantTreeNode) {
+          var thetaVar = (VariableTreeNode)new Problems.DataAnalysis.Symbolic.Variable().CreateTreeNode();
+          thetaVar.Weight = 1;
+          thetaVar.VariableName = $"θ{n++}";
+
+          thetaNames.Add(thetaVar.VariableName);
+          thetaValues.Add(constantTreeNode.Value);
+
+          var parent = constantTreeNode.Parent;
+          if (parent != null) {
+            var index = constantTreeNode.Parent.IndexOfSubtree(constantTreeNode);
+            parent.RemoveSubtree(index);
+            parent.InsertSubtree(index, thetaVar);
+          }
+        }
+        if (node is VariableTreeNode varTreeNode) {
+          var thetaVar = (VariableTreeNode)new Problems.DataAnalysis.Symbolic.Variable().CreateTreeNode();
+          thetaVar.Weight = 1;
+          thetaVar.VariableName = $"θ{n++}";
+
+          thetaNames.Add(thetaVar.VariableName);
+          thetaValues.Add(varTreeNode.Weight);
+
+          var parent = varTreeNode.Parent;
+          if (parent != null) {
+            var index = varTreeNode.Parent.IndexOfSubtree(varTreeNode);
+            parent.RemoveSubtree(index);
+            var prodNode = MakeNode<Multiplication>();
+            varTreeNode.Weight = 1.0;
+            prodNode.AddSubtree(varTreeNode);
+            prodNode.AddSubtree(thetaVar);
+            parent.InsertSubtree(index, prodNode);
+          }
+        }
+      }
+      return copy;
+    }
+
+    private static ISymbolicExpressionTreeNode CreateConstant(double value) {
+      var constantNode = (ConstantTreeNode)new Constant().CreateTreeNode();
+      constantNode.Value = value;
+      return constantNode;
+    }
+
+    private static ISymbolicExpressionTree Subtract(ISymbolicExpressionTree t, ISymbolicExpressionTreeNode b) {
+      var sub = MakeNode<Subtraction>(t.Root.GetSubtree(0).GetSubtree(0), b);
+      t.Root.GetSubtree(0).RemoveSubtree(0);
+      t.Root.GetSubtree(0).InsertSubtree(0, sub);
+      return t;
+    }
+    private static ISymbolicExpressionTree Subtract(ISymbolicExpressionTreeNode b, ISymbolicExpressionTree t) {
+      var sub = MakeNode<Subtraction>(b, t.Root.GetSubtree(0).GetSubtree(0));
+      t.Root.GetSubtree(0).RemoveSubtree(0);
+      t.Root.GetSubtree(0).InsertSubtree(0, sub);
+      return t;
+    }
+
+    private static ISymbolicExpressionTreeNode MakeNode<T>(params ISymbolicExpressionTreeNode[] fs) where T : ISymbol, new() {
+      var node = new T().CreateTreeNode();
+      foreach (var f in fs) node.AddSubtree(f);
+      return node;
+    }
+    #endregion
+  }
+}
Index: branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Extensions.csproj
===================================================================
--- branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Extensions.csproj	(revision 17196)
+++ branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Extensions.csproj	(revision 17197)
@@ -68,4 +68,6 @@
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="ConstrainedNLS.cs" />
+    <Compile Include="ConstrainedNLSInternal.cs" />
     <Compile Include="NLOpt.cs" />
     <Compile Include="NLOptEvaluator.cs" />
@@ -81,4 +83,8 @@
   </ItemGroup>
   <ItemGroup>
+    <ProjectReference Include="..\HeuristicLab.Analysis\3.3\HeuristicLab.Analysis-3.3.csproj">
+      <Project>{887425B4-4348-49ED-A457-B7D2C26DDBF9}</Project>
+      <Name>HeuristicLab.Analysis-3.3</Name>
+    </ProjectReference>
     <ProjectReference Include="..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj">
       <Project>{958b43bc-cc5c-4fa2-8628-2b3b01d890b6}</Project>
@@ -126,4 +132,8 @@
       <Private>False</Private>
     </ProjectReference>
+    <ProjectReference Include="..\HeuristicLab.Problems.Instances\3.3\HeuristicLab.Problems.Instances-3.3.csproj">
+      <Project>{3540E29E-4793-49E7-8EE2-FEA7F61C3994}</Project>
+      <Name>HeuristicLab.Problems.Instances-3.3</Name>
+    </ProjectReference>
   </ItemGroup>
   <ItemGroup>
Index: branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/NLOpt.cs
===================================================================
--- branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/NLOpt.cs	(revision 17196)
+++ branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/NLOpt.cs	(revision 17197)
@@ -169,8 +169,8 @@
 
     [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
-    public static extern nlopt_result nlopt_set_precond_min_objective(IntPtr nlopt_opt, nlopt_func f, nlopt_precond pre, IntPtr f_data);
-
-    [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
-    public static extern nlopt_result nlopt_set_precond_max_objective(IntPtr nlopt_opt, nlopt_func f, nlopt_precond pre, IntPtr f_data);
+    public static extern nlopt_result nlopt_set_precond_min_objective(IntPtr nlopt_opt, nlopt_func f, nlopt_precond pre, IntPtr f_data);  // only used by CCSAQ optimizer in NLOpt version 2.6.1
+
+    [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
+    public static extern nlopt_result nlopt_set_precond_max_objective(IntPtr nlopt_opt, nlopt_func f, nlopt_precond pre, IntPtr f_data);  // only used by CCSAQ optimizer in NLOpt version 2.6.1
 
     [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
