Index: anches/HeuristicLab.Problems.GaussianProcessTuning/HeuristicLab.Problems.GaussianProcessTuning/Interpreter MATLAB.cs
===================================================================
--- /branches/HeuristicLab.Problems.GaussianProcessTuning/HeuristicLab.Problems.GaussianProcessTuning/Interpreter MATLAB.cs	(revision 10757)
+++ 	(revision )
@@ -1,458 +1,0 @@
-﻿using System;
-using System.Linq;
-using System.Reflection;
-using System.Text;
-using HeuristicLab.Common;
-using HeuristicLab.Core;
-using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
-using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
-using HeuristicLab.Problems.DataAnalysis;
-using MLApp;
-using System.Collections.Generic;
-
-namespace HeuristicLab.Problems.GaussianProcessTuning {
-  [StorableClass]
-  [Item("Interpreter", "An interpreter for Gaussian process configurations represented as trees.")]
-  public class Interpreter : Item {
-    [ThreadStatic]
-    private MLApp.MLApp ml;
-
-    private MLApp.MLApp MLApp {
-      get {
-        if (ml == null) {
-          ml = new MLApp.MLApp();
-        }
-        return ml;
-      }
-    }
-
-    [StorableConstructor]
-    protected Interpreter(bool deserializing) : base(deserializing) { }
-    protected Interpreter(Interpreter original, Cloner cloner)
-      : base(original, cloner) {
-    }
-    public Interpreter()
-      : base() { }
-    public override IDeepCloneable Clone(Cloner cloner) {
-      return new Interpreter(this, cloner);
-    }
-
-    public double EvaluateGaussianProcessConfiguration(ISymbolicExpressionTree tree, IRegressionProblemData problemData) {
-      string meanExpression, meanHyperParameter;
-      string covExpression, covHyperParameter;
-      string likFunction, likHyperParameter;
-      GetMeanFunction(tree, problemData.AllowedInputVariables.Count(), out meanExpression, out meanHyperParameter);
-      GetCovFunction(tree, problemData.AllowedInputVariables.Count(), out covExpression, out covHyperParameter);
-      GetLikelihoodFunction(tree, out likFunction, out likHyperParameter);
-
-      double[,] y = new double[problemData.TrainingIndizes.Count(), 1];
-      double[,] yImg = new double[problemData.TrainingIndizes.Count(), 1];
-
-      int r, c;
-      r = 0;
-      foreach (var e in problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndizes)) {
-        y[r++, 0] = e;
-      }
-      double[,] x = new double[y.Length, problemData.AllowedInputVariables.Count()];
-      double[,] xImg = new double[y.Length, problemData.AllowedInputVariables.Count()];
-      c = 0;
-      foreach (var allowedInput in problemData.AllowedInputVariables) {
-        r = 0;
-        foreach (var e in problemData.Dataset.GetDoubleValues(allowedInput, problemData.TrainingIndizes)) {
-          x[r++, c] = e;
-        }
-        c++;
-      }
-
-      object oldX = null;
-      try { oldX = MLApp.GetVariable("x", "base"); }
-      catch {
-      }
-      if (oldX == null || oldX is Missing || ((double[,])oldX).Length != x.Length) {
-        MLApp.PutFullMatrix("y", "base", y, yImg);
-        MLApp.PutFullMatrix("x", "base", x, xImg);
-      }
-      ExecuteMatlab("hyp0 = " + GetHyperParameterString(meanHyperParameter, covHyperParameter, likHyperParameter) + ";");
-      ExecuteMatlab("infFun =  " + GetInferenceMethodString(tree) + ";");
-      ExecuteMatlab("meanExpr = " + meanExpression + ";");
-      ExecuteMatlab("covExpr = " + covExpression + ";");
-      ExecuteMatlab("likExp = " + likFunction + ";");
-      try
-      {
-        ExecuteMatlab("hyp = minimize(hyp0,'gp', -50, infFun, meanExpr, covExpr, likExp, x, y);");
-        ExecuteMatlab("[nlZ, dnlZ] = gp(hyp, infFun, meanExpr, covExpr, likExp, x, y);");
-        ExecuteMatlab("if isnan(nlZ) nlz = " + double.MaxValue + "; end;");
-        var d = MLApp.GetVariable("nlZ", "base");
-        if (d is Missing) return double.PositiveInfinity;
-        else return (double)d;
-      }
-      catch {
-        return double.PositiveInfinity;
-      }
-    }
-
-    public void EvaluateGaussianProcessConfiguration(ISymbolicExpressionTree tree, IRegressionProblemData trainingData, Dataset testData, IEnumerable<int> testRows,
-      out double[] means, out double[] variances) {
-      string meanExpression, meanHyperParameter;
-      string covExpression, covHyperParameter;
-      string likFunction, likHyperParameter;
-      GetMeanFunction(tree, trainingData.AllowedInputVariables.Count(), out meanExpression, out meanHyperParameter);
-      GetCovFunction(tree, trainingData.AllowedInputVariables.Count(), out covExpression, out covHyperParameter);
-      GetLikelihoodFunction(tree, out likFunction, out likHyperParameter);
-
-      double[,] y = new double[trainingData.TrainingIndizes.Count(), 1];
-      double[,] yImg = new double[trainingData.TrainingIndizes.Count(), 1];
-
-      int r, c;
-      r = 0;
-      foreach (var e in trainingData.Dataset.GetDoubleValues(trainingData.TargetVariable, trainingData.TrainingIndizes)) {
-        y[r++, 0] = e;
-      }
-      double[,] x = new double[y.Length, trainingData.AllowedInputVariables.Count()];
-      double[,] xImg = new double[y.Length, trainingData.AllowedInputVariables.Count()];
-      c = 0;
-      foreach (var allowedInput in trainingData.AllowedInputVariables) {
-        r = 0;
-        foreach (var e in trainingData.Dataset.GetDoubleValues(allowedInput, trainingData.TrainingIndizes)) {
-          x[r++, c] = e;
-        }
-        c++;
-      }
-
-      double[,] xTest = new double[testRows.Count(), trainingData.AllowedInputVariables.Count()];
-      double[,] xTestImg = new double[testRows.Count(), trainingData.AllowedInputVariables.Count()];
-      c = 0;
-      foreach (var allowedInput in trainingData.AllowedInputVariables) {
-        r = 0;
-        foreach (var e in testData.GetDoubleValues(allowedInput, testRows)) {
-          xTest[r++, c] = e;
-        }
-        c++;
-      }
-
-      object oldX = null;
-      try {
-        oldX = MLApp.GetVariable("x", "base");
-      }
-      catch {
-      }
-      if (oldX == null || oldX is Missing || ((double[,])oldX).Length != x.Length) {
-        MLApp.PutFullMatrix("y", "base", y, yImg);
-        MLApp.PutFullMatrix("x", "base", x, xImg);
-      }
-      MLApp.PutFullMatrix("xTest", "base", xTest, xTestImg);
-      ExecuteMatlab("hyp0 = " + GetHyperParameterString(meanHyperParameter, covHyperParameter, likHyperParameter) + ";");
-      ExecuteMatlab("infFun =  " + GetInferenceMethodString(tree) + ";");
-      ExecuteMatlab("meanExpr = " + meanExpression + ";");
-      ExecuteMatlab("covExpr = " + covExpression + ";");
-      ExecuteMatlab("likExp = " + likFunction + ";");
-      ExecuteMatlab(
-        "try " +
-        "  hyp = minimize(hyp0,'gp', -50, infFun, meanExpr, covExpr, likExp, x, y);" +
-        "  [ymu, ys2, fmu, fs2] = gp(hyp, infFun, meanExpr, covExpr, likExp, x, y, xTest); " +
-        "catch " +
-        "  ymu = zeros(size(xTest, 1), 1); " +
-        "  ys2 = ones(size(xTest, 1), 1); " +
-        "end");
-      var meansMat = (double[,])MLApp.GetVariable("ymu", "base");
-      var variancesMat = (double[,])MLApp.GetVariable("ys2", "base");
-      means = new double[meansMat.GetLength(0)];
-      for (int i = 0; i < means.Length; i++)
-        means[i] = meansMat[i, 0];
-      variances = new double[variancesMat.GetLength(0)];
-      for (int i = 0; i < variances.Length; i++)
-        variances[i] = variancesMat[i, 0];
-    }
-
-    private string GetInferenceMethodString(ISymbolicExpressionTree tree) {
-      return "'infEP'";
-    }
-
-    private void GetMeanFunction(ISymbolicExpressionTree tree, int dimension, out string expression, out string hyperParameter) {
-      var expressionBuilder = new StringBuilder();
-      var hyperParameterBuilder = new StringBuilder();
-      // var mask = CalculateMask(tree.Root.GetSubtree(0).GetSubtree(0).GetSubtree(0)) ?? 
-      var mask = new bool[dimension];
-
-      hyperParameterBuilder.Append("[");
-
-      GetMeanFunction(tree.Root.GetSubtree(0).GetSubtree(0).GetSubtree(0), expressionBuilder, hyperParameterBuilder, mask);
-
-      hyperParameterBuilder.Append("]");
-
-      expression = expressionBuilder.ToString();
-      hyperParameter = hyperParameterBuilder.ToString();
-    }
-
-    private void GetCovFunction(ISymbolicExpressionTree tree, int dimension, out string expression, out string hyperParameter) {
-      var expressionBuilder = new StringBuilder();
-      var hyperParameterBuilder = new StringBuilder();
-      //expressionBuilder.Append("{");
-      hyperParameterBuilder.Append("[");
-
-      var mask = new bool[dimension];
-      GetCovFunction(tree.Root.GetSubtree(0).GetSubtree(0).GetSubtree(1), expressionBuilder, hyperParameterBuilder, mask);
-
-      hyperParameterBuilder.Append("]");
-      //expressionBuilder.Append("}");
-
-      expression = expressionBuilder.ToString();
-      hyperParameter = hyperParameterBuilder.ToString();
-    }
-
-
-    private void GetLikelihoodFunction(ISymbolicExpressionTree tree, out string expression, out string hyperParameter) {
-      var expressionBuilder = new StringBuilder();
-      var hyperParameterBuilder = new StringBuilder();
-      hyperParameterBuilder.Append("[");
-      GetLikelihoodFunction(tree.Root.GetSubtree(0).GetSubtree(0).GetSubtree(2), expressionBuilder,
-                                   hyperParameterBuilder);
-      hyperParameterBuilder.Append("]");
-      expression = expressionBuilder.ToString();
-      hyperParameter = hyperParameterBuilder.ToString();
-    }
-
-    private void GetLikelihoodFunction(ISymbolicExpressionTreeNode node, StringBuilder expressionBuilder, StringBuilder hyperParameterBuilder) {
-      if (node.Symbol is LikGauss) {
-        var likNode = node as LikGaussTreeNode;
-        expressionBuilder.Append("'likGauss'");
-        hyperParameterBuilder.Append(likNode.Sigma);
-      } else {
-        throw new ArgumentException("unknown likelihood function " + node.Symbol);
-      }
-    }
-
-    private string GetHyperParameterString(string meanHyp, string covHyp, string likHyp) {
-      return "struct('mean', " + meanHyp +
-             ", 'cov', " + covHyp +
-             ", 'lik', " + likHyp +
-             ")";
-      ;
-    }
-
-
-    private void GetCovFunction(ISymbolicExpressionTreeNode node, StringBuilder expressionStringBuilder, StringBuilder hyperParameterStringBuilder, bool[] mask) {
-      if (node.Symbol is CovConst) {
-        var constNode = node as CovConstTreeNode;
-        expressionStringBuilder.Append("{'covConst'}");
-        hyperParameterStringBuilder.Append(constNode.Sigma).Append("; ");
-      } else if (node.Symbol is CovLin) {
-        expressionStringBuilder.Append("{'covLIN'}");
-      } else if (node.Symbol is CovLinArd) {
-        var covNode = node as CovLinArdTreeNode;
-        expressionStringBuilder.Append("{'covLINard'}");
-        hyperParameterStringBuilder.Append(ToVectorString(covNode.Lambda, mask));
-      } else if (node.Symbol is CovSeArd) {
-        var covNode = node as CovSeArdTreeNode;
-        expressionStringBuilder.Append("{'covSEard'}");
-        hyperParameterStringBuilder.Append("[").Append(ToVectorString(covNode.Lambda, mask));
-        hyperParameterStringBuilder.AppendFormat("; {0} ]", covNode.Sigma);
-      } else if (node.Symbol is CovSeIso) {
-        var covNode = node as CovSeIsoTreeNode;
-        expressionStringBuilder.Append("{'covSEiso'}");
-        hyperParameterStringBuilder.AppendFormat("[{0}", covNode.L);
-        hyperParameterStringBuilder.AppendFormat("; {0}]", covNode.Sigma);
-      } else if (node.Symbol is CovSum) {
-        expressionStringBuilder.Append("{'covSum', {");
-        hyperParameterStringBuilder.Append("[");
-        GetCovFunction(node.GetSubtree(0), expressionStringBuilder, hyperParameterStringBuilder, mask);
-        foreach (var subTree in node.Subtrees.Skip(1)) {
-          expressionStringBuilder.Append(", ");
-          hyperParameterStringBuilder.Append("; ");
-          GetCovFunction(subTree, expressionStringBuilder, hyperParameterStringBuilder, mask);
-        }
-        hyperParameterStringBuilder.Append("]");
-        expressionStringBuilder.Append("}}");
-      } else if (node.Symbol is CovProd) {
-        expressionStringBuilder.Append("{'covProd', {");
-        hyperParameterStringBuilder.Append("[");
-        GetCovFunction(node.GetSubtree(0), expressionStringBuilder, hyperParameterStringBuilder, mask);
-        foreach (var subTree in node.Subtrees.Skip(1)) {
-          expressionStringBuilder.Append(", ");
-          hyperParameterStringBuilder.Append("; ");
-          GetCovFunction(subTree, expressionStringBuilder, hyperParameterStringBuilder, mask);
-        }
-        hyperParameterStringBuilder.Append("]");
-        expressionStringBuilder.Append("}}");
-      } else if (node.Symbol is CovScale) {
-        var covNode = node as CovScaleTreeNode;
-        expressionStringBuilder.Append("{'covScale', ");
-        hyperParameterStringBuilder.AppendFormat("[{0}; ", covNode.Alpha);
-        GetCovFunction(node.GetSubtree(0), expressionStringBuilder, hyperParameterStringBuilder, mask);
-        hyperParameterStringBuilder.Append("]");
-        expressionStringBuilder.Append("}");
-      } else if (node.Symbol is CovMask) {
-        var covNode = (CovMaskTreeNode)node;
-        // when nothing is masked then we can just return the child
-        if (!covNode.Mask.Any(t => t == false)) {
-          GetCovFunction(node.GetSubtree(0), expressionStringBuilder, hyperParameterStringBuilder, mask);
-        } else {
-          expressionStringBuilder.Append("{'covMask', {");
-          hyperParameterStringBuilder.Append("[");
-          expressionStringBuilder.Append(ToVectorString(covNode.Mask)).Append(", ");
-          int startIndex = expressionStringBuilder.Length;
-          GetCovFunction(node.GetSubtree(0), expressionStringBuilder, hyperParameterStringBuilder, covNode.Mask);
-          expressionStringBuilder.Remove(startIndex, 1);
-          expressionStringBuilder.Remove(expressionStringBuilder.Length - 1, 1);
-          hyperParameterStringBuilder.Append("]");
-          expressionStringBuilder.Append("}}");
-        }
-      } else {
-        throw new ArgumentException("unknown symbol " + node.Symbol);
-      }
-    }
-
-
-    private void GetMeanFunction(ISymbolicExpressionTreeNode node, StringBuilder expressionStringBuilder, StringBuilder hyperParameterStringBuilder, bool[] mask) {
-      if (node.Symbol is MeanConst) {
-        var constNode = node as MeanConstTreeNode;
-        expressionStringBuilder.Append("{'meanConst'}");
-        hyperParameterStringBuilder.Append(constNode.Value).Append("; ");
-      } else if (node.Symbol is MeanLinear) {
-        var meanLinNode = node as MeanLinearTreeNode;
-        expressionStringBuilder.Append("{'meanLinear'}");
-        hyperParameterStringBuilder.Append(ToVectorString(meanLinNode.Alpha, mask));
-      } else if (node.Symbol is MeanMask) {
-        var meanMaskNode = (MeanMaskTreeNode)node;
-        // when nothing is masked then we can just return the child
-        if (!meanMaskNode.Mask.Any(t => t == false)) {
-          GetMeanFunction(node.GetSubtree(0), expressionStringBuilder, hyperParameterStringBuilder, mask);
-        } else {
-          expressionStringBuilder.Append("{'meanMask', {");
-          hyperParameterStringBuilder.Append("[");
-          expressionStringBuilder.Append(ToVectorString(meanMaskNode.Mask)).Append(", ");
-          int startIndex = expressionStringBuilder.Length;
-          GetMeanFunction(node.GetSubtree(0), expressionStringBuilder, hyperParameterStringBuilder, meanMaskNode.Mask);
-          expressionStringBuilder.Remove(startIndex, 1);
-          expressionStringBuilder.Remove(expressionStringBuilder.Length - 1, 1);
-          hyperParameterStringBuilder.Append("]");
-          expressionStringBuilder.Append("}}");
-        }
-      } else if (node.Symbol is MeanOne) {
-        expressionStringBuilder.Append("{'meanOne'}");
-      } else if (node.Symbol is MeanPow) {
-        var meanPowSymbol = (MeanPow)node.Symbol;
-        expressionStringBuilder.Append("{'meanPow', {");
-        hyperParameterStringBuilder.Append("[");
-        expressionStringBuilder.Append(meanPowSymbol.Exponent).Append(", ");
-        GetMeanFunction(node.GetSubtree(0), expressionStringBuilder, hyperParameterStringBuilder, mask);
-        hyperParameterStringBuilder.Append("]");
-        expressionStringBuilder.Append("}}");
-      } else if (node.Symbol is MeanProd) {
-        expressionStringBuilder.Append("{'meanProd', {");
-        hyperParameterStringBuilder.Append("[");
-        GetMeanFunction(node.GetSubtree(0), expressionStringBuilder, hyperParameterStringBuilder, mask);
-        foreach (var subTree in node.Subtrees.Skip(1)) {
-          expressionStringBuilder.Append(", ");
-          hyperParameterStringBuilder.Append("; ");
-          GetMeanFunction(subTree, expressionStringBuilder, hyperParameterStringBuilder, mask);
-        }
-        hyperParameterStringBuilder.Append("]");
-        expressionStringBuilder.Append("}}");
-      } else if (node.Symbol is MeanScale) {
-        var meanScaleNode = node as MeanScaleTreeNode;
-        expressionStringBuilder.Append("{'meanScale', ");
-        hyperParameterStringBuilder.AppendFormat("[{0}; ", meanScaleNode.Alpha);
-        GetMeanFunction(node.GetSubtree(0), expressionStringBuilder, hyperParameterStringBuilder, mask);
-        hyperParameterStringBuilder.Append("]");
-        expressionStringBuilder.Append("}");
-      } else if (node.Symbol is MeanSum) {
-        expressionStringBuilder.Append("{'meanSum', {");
-        hyperParameterStringBuilder.Append("[");
-        GetMeanFunction(node.GetSubtree(0), expressionStringBuilder, hyperParameterStringBuilder, mask);
-        foreach (var subTree in node.Subtrees.Skip(1)) {
-          expressionStringBuilder.Append(", ");
-          hyperParameterStringBuilder.Append("; ");
-          GetMeanFunction(subTree, expressionStringBuilder, hyperParameterStringBuilder, mask);
-        }
-        hyperParameterStringBuilder.Append("]");
-        expressionStringBuilder.Append("}}");
-      } else if (node.Symbol is MeanZero) {
-        expressionStringBuilder.Append("{'meanZero'}");
-      } else {
-        throw new ArgumentException("Unknown mean function", "node");
-      }
-    }
-
-    //private bool[] CalculateMask(ISymbolicExpressionTreeNode node) {
-    //  var maskNode = node as MeanMaskTreeNode;
-    //  if (maskNode != null) {
-    //    bool[] newMask = CombineMasksProd(maskNode.Mask, CalculateMask(node.GetSubtree(0)));
-    //    return newMask;
-    //  } else if (node.Symbol is MeanProd) {
-    //    bool[] newMask = CalculateMask(node.GetSubtree(0));
-    //    foreach (var subTree in node.Subtrees.Skip(1)) {
-    //      newMask = CombineMasksProd(newMask, CalculateMask(subTree));
-    //    }
-    //    return newMask;
-    //  } else if (node.Symbol is MeanSum) {
-    //    bool[] newMask = CalculateMask(node.GetSubtree(0));
-    //    foreach (var subTree in node.Subtrees.Skip(1)) {
-    //      newMask = CombineMasksSum(newMask, CalculateMask(subTree));
-    //    }
-    //    return newMask;
-    //  } else if (node.SubtreeCount == 1) {
-    //    return CalculateMask(node.GetSubtree(0));
-    //  } else if (node is SymbolicExpressionTreeTerminalNode) {
-    //    return null;
-    //  } else {
-    //    throw new NotImplementedException();
-    //  }
-    //}
-
-    //private bool[] CombineMasksProd(bool[] m, bool[] n) {
-    //  if (m == null) return n;
-    //  if (n == null) return m;
-    //  if (m.Length != n.Length) throw new ArgumentException();
-    //  bool[] res = new bool[m.Length];
-    //  for (int i = 0; i < res.Length; i++)
-    //    res[i] = m[i] | n[i];
-    //  return res;
-    //}
-
-
-    //private bool[] CombineMasksSum(bool[] m, bool[] n) {
-    //  if (m == null) return n;
-    //  if (n == null) return m;
-    //  if (m.Length != n.Length) throw new ArgumentException();
-    //  bool[] res = new bool[m.Length];
-    //  for (int i = 0; i < res.Length; i++)
-    //    res[i] = m[i] & n[i];
-    //  return res;
-    //}
-
-    private string ToVectorString(bool[] b) {
-      var strBuilder = new StringBuilder();
-      strBuilder.Append("[");
-      if (b.Length == 1) // workaround for bug in GPML
-      {
-        if (!b[0]) strBuilder.Append("1");
-      } else {
-        for (int i = 0; i < b.Length; i++) {
-          if (i > 0) strBuilder.Append(", ");
-          strBuilder.Append(b[i] ? "0" : "1");
-        }
-      }
-      strBuilder.Append("]");
-      return strBuilder.ToString();
-    }
-    private string ToVectorString(double[] xs, bool[] mask) {
-      if (xs.Length != mask.Length) throw new ArgumentException();
-      var strBuilder = new StringBuilder();
-      strBuilder.Append("[");
-      for (int i = 0; i < xs.Length; i++)
-        if (!mask[i]) {
-          if (i > 0) strBuilder.Append("; ");
-          strBuilder.Append(xs[i]);
-        }
-      strBuilder.Append("]");
-      return strBuilder.ToString();
-    }
-
-
-    private void ExecuteMatlab(string command) {
-      var result = MLApp.Execute(command);
-      if (result.Contains("???")) throw new ArgumentException(command + " " + result, "command");
-    }
-  }
-}
