Index: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs
===================================================================
--- branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs	(revision 16253)
+++ branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs	(revision 16268)
@@ -69,9 +69,9 @@
       get { return (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
     }
-    public IValueParameter<ReadOnlyCheckedItemCollection<StringValue>> TargetVariablesParameter {
-      get { return (IValueParameter<ReadOnlyCheckedItemCollection<StringValue>>)Parameters[TargetVariablesParameterName]; }
-    }
-    public IValueParameter<ReadOnlyCheckedItemCollection<StringValue>> FunctionSetParameter {
-      get { return (IValueParameter<ReadOnlyCheckedItemCollection<StringValue>>)Parameters[FunctionSetParameterName]; }
+    public IValueParameter<ReadOnlyCheckedItemList<StringValue>> TargetVariablesParameter {
+      get { return (IValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters[TargetVariablesParameterName]; }
+    }
+    public IValueParameter<ReadOnlyCheckedItemList<StringValue>> FunctionSetParameter {
+      get { return (IValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters[FunctionSetParameterName]; }
     }
     public IFixedValueParameter<IntValue> MaximumLengthParameter {
@@ -105,9 +105,9 @@
     IDataAnalysisProblemData IDataAnalysisProblem.ProblemData { get { return ProblemData; } }
 
-    public ReadOnlyCheckedItemCollection<StringValue> TargetVariables {
+    public ReadOnlyCheckedItemList<StringValue> TargetVariables {
       get { return TargetVariablesParameter.Value; }
     }
 
-    public ReadOnlyCheckedItemCollection<StringValue> FunctionSet {
+    public ReadOnlyCheckedItemList<StringValue> FunctionSet {
       get { return FunctionSetParameter.Value; }
     }
@@ -171,9 +171,9 @@
     public Problem()
       : base() {
-      var targetVariables = new CheckedItemCollection<StringValue>().AsReadOnly(); // HACK: it would be better to provide a new class derived from IDataAnalysisProblem
+      var targetVariables = new CheckedItemList<StringValue>().AsReadOnly(); // HACK: it would be better to provide a new class derived from IDataAnalysisProblem
       var functions = CreateFunctionSet();
       Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, "The data captured from the dynamical system. Use CSV import functionality to import data.", new RegressionProblemData()));
-      Parameters.Add(new ValueParameter<ReadOnlyCheckedItemCollection<StringValue>>(TargetVariablesParameterName, "Target variables (overrides setting in ProblemData)", targetVariables));
-      Parameters.Add(new ValueParameter<ReadOnlyCheckedItemCollection<StringValue>>(FunctionSetParameterName, "The list of allowed functions", functions));
+      Parameters.Add(new ValueParameter<ReadOnlyCheckedItemList<StringValue>>(TargetVariablesParameterName, "Target variables (overrides setting in ProblemData)", targetVariables));
+      Parameters.Add(new ValueParameter<ReadOnlyCheckedItemList<StringValue>>(FunctionSetParameterName, "The list of allowed functions", functions));
       Parameters.Add(new FixedValueParameter<IntValue>(MaximumLengthParameterName, "The maximally allowed length of each expression. Set to a small value (5 - 25). Default = 10", new IntValue(10)));
       Parameters.Add(new FixedValueParameter<IntValue>(MaximumParameterOptimizationIterationsParameterName, "The maximum number of iterations for optimization of parameters (using L-BFGS). More iterations makes the algorithm slower, fewer iterations might prevent convergence in the optimization scheme. Default = 100", new IntValue(100)));
@@ -235,5 +235,5 @@
       var rows = episodes.SelectMany(e => Enumerable.Range(e.Start, e.End - e.Start)).ToArray();
       var problemData = ProblemData;
-      var targetVars = TargetVariables.CheckedItems.Select(i => i.Value).ToArray();
+      var targetVars = TargetVariables.CheckedItems.OrderBy(i => i.Index).Select(i => i.Value.Value).ToArray();
       var latentVariables = Enumerable.Range(1, NumberOfLatentVariables).Select(i => "λ" + i).ToArray(); // TODO: must coincide with the variables which are actually defined in the grammar and also for which we actually have trees
       var targetValues = new double[rows.Length, targetVars.Length];
@@ -258,5 +258,6 @@
         }
       }
-      var theta = paramNodes.Select(_ => random.NextDouble() * 2.0 - 1.0).ToArray(); // init params randomly from Unif(-1,1)
+      // init params randomly from Unif(-1e-5, 1e-5)
+      var theta = paramNodes.Select(_ => random.NextDouble() * 2.0e-5 - 1.0e-5).ToArray();
 
       optTheta = new double[0];
@@ -266,4 +267,5 @@
         alglib.minlbfgscreate(Math.Min(theta.Length, 5), theta, out state);
         alglib.minlbfgssetcond(state, 0.0, 0.0, 0.0, MaximumParameterOptimizationIterations);
+        alglib.minlbfgssetgradientcheck(state, 1e-6);
         alglib.minlbfgsoptimize(state, EvaluateObjectiveAndGradient, null,
           new object[] { trees, targetVars, problemData, targetValues, episodes.ToArray(), NumericIntegrationSteps, latentVariables, OdeSolver }); //TODO: create a type
@@ -386,5 +388,5 @@
 
       var problemData = ProblemData;
-      var targetVars = TargetVariables.CheckedItems.Select(i => i.Value).ToArray();
+      var targetVars = TargetVariables.CheckedItems.OrderBy(i => i.Index).Select(i => i.Value.Value).ToArray();
       var latentVariables = Enumerable.Range(1, NumberOfLatentVariables).Select(i => "λ" + i).ToArray(); // TODO: must coincide with the variables which are actually defined in the grammar and also for which we actually have trees
 
@@ -741,4 +743,5 @@
                       var varName = n.Symbol.Name;
                       var varIdx = Array.IndexOf(calculatedVariables, varName); // TODO: perf!
+                      if (varIdx < 0) throw new InvalidProgramException();
                       var y_i = CVODES.NV_Get_Ith_S(y, (long)varIdx);
                       nodeValues.Add(n, Tuple.Create(y_i, Vector.Zero)); // no gradient needed
@@ -782,4 +785,6 @@
               var varName = n.Symbol.Name;
               var varIdx = Array.IndexOf(calculatedVariables, varName); // TODO: perf!
+              if (varIdx < 0) throw new InvalidProgramException();
+
               var y_i = CVODES.NV_Get_Ith_S(y, (long)varIdx);
               var gArr = new double[CVODES.NV_LENGTH_S(y)]; // backing array
@@ -829,4 +834,6 @@
                 var varName = n.Symbol.Name;
                 var varIdx = Array.IndexOf(calculatedVariables, varName); // TODO: perf!
+                if (varIdx < 0) throw new InvalidProgramException();
+
                 var y_i = CVODES.NV_Get_Ith_S(y, (long)varIdx);
                 var gArr = new double[d]; // backing array
@@ -1053,5 +1060,5 @@
     }
 
-    private void CheckedFunctionsChanged(object sender, CollectionItemsChangedEventArgs<StringValue> e) {
+    private void CheckedFunctionsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
       UpdateGrammarAndEncoding();
     }
@@ -1061,5 +1068,5 @@
     }
 
-    private void CheckedTargetVariablesChanged(object sender, CollectionItemsChangedEventArgs<StringValue> e) {
+    private void CheckedTargetVariablesChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
       UpdateGrammarAndEncoding();
     }
@@ -1090,6 +1097,6 @@
     }
 
-    private ReadOnlyCheckedItemCollection<StringValue> CreateFunctionSet() {
-      var l = new CheckedItemCollection<StringValue>();
+    private ReadOnlyCheckedItemList<StringValue> CreateFunctionSet() {
+      var l = new CheckedItemList<StringValue>();
       l.Add(new StringValue("+").AsReadOnly());
       l.Add(new StringValue("*").AsReadOnly());
@@ -1113,7 +1120,10 @@
 
     private void UpdateTargetVariables() {
-      var currentlySelectedVariables = TargetVariables.CheckedItems.Select(i => i.Value).ToArray();
-
-      var newVariablesList = new CheckedItemCollection<StringValue>(ProblemData.Dataset.VariableNames.Select(str => new StringValue(str).AsReadOnly()).ToArray()).AsReadOnly();
+      var currentlySelectedVariables = TargetVariables.CheckedItems
+        .OrderBy(i => i.Index)
+        .Select(i => i.Value.Value)
+        .ToArray();
+
+      var newVariablesList = new CheckedItemList<StringValue>(ProblemData.Dataset.VariableNames.Select(str => new StringValue(str).AsReadOnly()).ToArray()).AsReadOnly();
       var matchingItems = newVariablesList.Where(item => currentlySelectedVariables.Contains(item.Value)).ToArray();
       foreach (var matchingItem in matchingItems) {
@@ -1138,5 +1148,5 @@
       // whenever ProblemData is changed we create a new grammar with the necessary symbols
       var g = new SimpleSymbolicExpressionGrammar();
-      g.AddSymbols(FunctionSet.CheckedItems.Select(i => i.Value).ToArray(), 2, 2);
+      g.AddSymbols(FunctionSet.CheckedItems.OrderBy(i => i.Index).Select(i => i.Value.Value).ToArray(), 2, 2);
 
       // TODO
@@ -1147,5 +1157,5 @@
       //}, 1, 1);
 
-      foreach (var variableName in ProblemData.AllowedInputVariables.Union(TargetVariables.CheckedItems.Select(i => i.Value)))
+      foreach (var variableName in ProblemData.AllowedInputVariables.Union(TargetVariables.CheckedItems.Select(i => i.Value.Value)))
         g.AddTerminalSymbol(variableName);
 
