Index: /branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
===================================================================
--- /branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs	(revision 14719)
+++ /branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs	(revision 14720)
@@ -25,4 +25,5 @@
 using System.Linq;
 using System.Text;
+using System.Text.RegularExpressions;
 using HeuristicLab.Common;
 using HeuristicLab.Core;
@@ -56,51 +57,89 @@
 
     private string VariableName2Identifier(string name) {
-      return "_" + string.Join("_", Encoding.UTF8.GetBytes(name));
+      // tries to convert a variable name to a valid C# identifier.
+      // the following code would work for all possible variable names
+      // return "_" + string.Join("_", Encoding.UTF8.GetBytes(name));
+
+      /*
+       * identifier-start-character:
+       *    letter-character
+       *    _ (the underscore character U+005F)
+       *  identifier-part-characters:
+       *    identifier-part-character
+       *    identifier-part-characters   identifier-part-character
+       *  identifier-part-character:
+       *    letter-character
+       *    decimal-digit-character
+       *    connecting-character
+       *    combining-character
+       *    formatting-character
+       *  letter-character:
+       *    A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl 
+       *    A unicode-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or Nl
+       *  combining-character:
+       *    A Unicode character of classes Mn or Mc 
+       *    A unicode-escape-sequence representing a character of classes Mn or Mc
+       *  decimal-digit-character:
+       *    A Unicode character of the class Nd 
+       *    A unicode-escape-sequence representing a character of the class Nd
+       *  connecting-character:
+       *    A Unicode character of the class Pc 
+       *    A unicode-escape-sequence representing a character of the class Pc
+       *  formatting-character:
+       *    A Unicode character of the class Cf 
+       *    A unicode-escape-sequence representing a character of the class Cf
+       */
+
+      var invalidIdentifierStarts = new Regex(@"[^_\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}]");
+      var invalidIdentifierParts = new Regex(@"[^\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Cf}]");
+      return "@" +
+        (invalidIdentifierStarts.IsMatch(name.Substring(0, 1)) ? "_" : "") + // prepend '_' if necessary
+        invalidIdentifierParts.Replace(name, "_");
     }
 
     private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
       // TODO: adapt to interpreter semantics. The HL interpreter also allows Boolean operations on reals
-      if (node.Subtrees.Any()) {
-        if (node.Symbol is Addition) {
+      if(node.Subtrees.Any()) {
+        if(node.Symbol is Addition) {
           FormatOperator(node, "+", strBuilder);
-        } else if (node.Symbol is And) {
+        } else if(node.Symbol is And) {
           FormatOperator(node, "&&", strBuilder);
-        } else if (node.Symbol is Average) {
+        } else if(node.Symbol is Average) {
           FormatFunction(node, "Average", strBuilder);
-        } else if (node.Symbol is Cosine) {
+        } else if(node.Symbol is Cosine) {
           FormatFunction(node, "Math.Cos", strBuilder);
-        } else if (node.Symbol is Division) {
+        } else if(node.Symbol is Division) {
           FormatDivision(node, strBuilder);
-        } else if (node.Symbol is Exponential) {
+        } else if(node.Symbol is Exponential) {
           FormatFunction(node, "Math.Exp", strBuilder);
-        } else if (node.Symbol is GreaterThan) {
+        } else if(node.Symbol is GreaterThan) {
           FormatOperator(node, ">", strBuilder);
-        } else if (node.Symbol is IfThenElse) {
+        } else if(node.Symbol is IfThenElse) {
           FormatFunction(node, "EvaluateIf", strBuilder);
-        } else if (node.Symbol is LessThan) {
+        } else if(node.Symbol is LessThan) {
           FormatOperator(node, "<", strBuilder);
-        } else if (node.Symbol is Logarithm) {
+        } else if(node.Symbol is Logarithm) {
           FormatFunction(node, "Math.Log", strBuilder);
-        } else if (node.Symbol is Multiplication) {
+        } else if(node.Symbol is Multiplication) {
           FormatOperator(node, "*", strBuilder);
-        } else if (node.Symbol is Not) {
+        } else if(node.Symbol is Not) {
           FormatOperator(node, "!", strBuilder);
-        } else if (node.Symbol is Or) {
+        } else if(node.Symbol is Or) {
           FormatOperator(node, "||", strBuilder);
-        } else if (node.Symbol is Xor) {
+        } else if(node.Symbol is Xor) {
           FormatOperator(node, "^", strBuilder);
-        } else if (node.Symbol is Sine) {
+        } else if(node.Symbol is Sine) {
           FormatFunction(node, "Math.Sin", strBuilder);
-        } else if (node.Symbol is Subtraction) {
+        } else if(node.Symbol is Subtraction) {
           FormatSubtraction(node, strBuilder);
-        } else if (node.Symbol is Tangent) {
+        } else if(node.Symbol is Tangent) {
           FormatFunction(node, "Math.Tan", strBuilder);
-        } else if (node.Symbol is Square) {
+        } else if(node.Symbol is Square) {
           FormatSquare(node, strBuilder);
-        } else if (node.Symbol is SquareRoot) {
+        } else if(node.Symbol is SquareRoot) {
           FormatFunction(node, "Math.Sqrt", strBuilder);
-        } else if (node.Symbol is Power) {
+        } else if(node.Symbol is Power) {
           FormatFunction(node, "Math.Pow", strBuilder);
-        } else if (node.Symbol is Root) {
+        } else if(node.Symbol is Root) {
           FormatRoot(node, strBuilder);
         } else {
@@ -108,14 +147,14 @@
         }
       } else {
-        if (node is VariableTreeNode) {
+        if(node is VariableTreeNode) {
           var varNode = node as VariableTreeNode;
           strBuilder.AppendFormat("{0} * {1}", VariableName2Identifier(varNode.VariableName), varNode.Weight.ToString("g17", CultureInfo.InvariantCulture));
-        } else if (node is ConstantTreeNode) {
+        } else if(node is ConstantTreeNode) {
           var constNode = node as ConstantTreeNode;
           strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture));
-        } else if (node.Symbol is FactorVariable) {
+        } else if(node.Symbol is FactorVariable) {
           var factorNode = node as FactorVariableTreeNode;
           FormatFactor(factorNode, strBuilder);
-        } else if (node.Symbol is BinaryFactorVariable) {
+        } else if(node.Symbol is BinaryFactorVariable) {
           var binFactorNode = node as BinaryFactorVariableTreeNode;
           FormatBinaryFactor(binFactorNode, strBuilder);
@@ -150,5 +189,5 @@
 
     private void FormatDivision(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
-      if (node.SubtreeCount == 1) {
+      if(node.SubtreeCount == 1) {
         strBuilder.Append("1.0 / ");
         FormatRecursively(node.GetSubtree(0), strBuilder);
@@ -156,6 +195,6 @@
         FormatRecursively(node.GetSubtree(0), strBuilder);
         strBuilder.Append("/ (");
-        for (int i = 1; i < node.SubtreeCount; i++) {
-          if (i > 1) strBuilder.Append(" * ");
+        for(int i = 1; i < node.SubtreeCount; i++) {
+          if(i > 1) strBuilder.Append(" * ");
           FormatRecursively(node.GetSubtree(i), strBuilder);
         }
@@ -165,5 +204,5 @@
 
     private void FormatSubtraction(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
-      if (node.SubtreeCount == 1) {
+      if(node.SubtreeCount == 1) {
         strBuilder.Append("-");
         FormatRecursively(node.GetSubtree(0), strBuilder);
@@ -176,7 +215,7 @@
     private void FormatOperator(ISymbolicExpressionTreeNode node, string symbol, StringBuilder strBuilder) {
       strBuilder.Append("(");
-      foreach (var child in node.Subtrees) {
+      foreach(var child in node.Subtrees) {
         FormatRecursively(child, strBuilder);
-        if (child != node.Subtrees.Last())
+        if(child != node.Subtrees.Last())
           strBuilder.Append(" " + symbol + " ");
       }
@@ -186,7 +225,7 @@
     private void FormatFunction(ISymbolicExpressionTreeNode node, string function, StringBuilder strBuilder) {
       strBuilder.Append(function + "(");
-      foreach (var child in node.Subtrees) {
+      foreach(var child in node.Subtrees) {
         FormatRecursively(child, strBuilder);
-        if (child != node.Subtrees.Last())
+        if(child != node.Subtrees.Last())
           strBuilder.Append(", ");
       }
@@ -207,10 +246,10 @@
       // here we don't have access to problemData to determine the type for each variable (double/string) therefore we must distinguish based on the symbol type
       HashSet<string> doubleVarNames = new HashSet<string>();
-      foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is VariableTreeNode || x is VariableConditionTreeNode)) {
+      foreach(var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is VariableTreeNode || x is VariableConditionTreeNode)) {
         doubleVarNames.Add(((IVariableTreeNode)node).VariableName);
       }
 
       HashSet<string> stringVarNames = new HashSet<string>();
-      foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is BinaryFactorVariableTreeNode || x is FactorVariableTreeNode)) {
+      foreach(var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is BinaryFactorVariableTreeNode || x is FactorVariableTreeNode)) {
         stringVarNames.Add(((IVariableTreeNode)node).VariableName);
       }
@@ -219,5 +258,5 @@
       strBuilder.Append(string.Join(", ", orderedNames));
 
-      if (stringVarNames.Any() && doubleVarNames.Any())
+      if(stringVarNames.Any() && doubleVarNames.Any())
         strBuilder.AppendLine(",");
       orderedNames = doubleVarNames.OrderBy(n => n, new NaturalStringComparer()).Select(n => "double " + VariableName2Identifier(n) + " /* " + n + " */");
