Index: /branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/Expression.cs
===================================================================
--- /branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/Expression.cs	(revision 14504)
+++ /branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/Expression.cs	(revision 14505)
@@ -29,162 +29,162 @@
 
 namespace HeuristicLab.ExpressionGenerator {
-    public class Expression : IExpression {
-        // unique name for each expression 
-        public string Name { get; set; }
-        // optional label (non-unique) - useful for string (infix) representations
-        public string Label { get; set; }
+  public class Expression : IExpression {
+    // unique name for each expression 
+    public string Name { get; set; }
+    // optional label (non-unique) - useful for string (infix) representations
+    public string Label { get; set; }
 
-        public ExpressionType Type { get; private set; }
+    public ExpressionType Type { get; private set; }
 
-        private readonly List<Expression> arguments;
-        public IEnumerable<Expression> Arguments {
-            get { return arguments ?? Enumerable.Empty<Expression>(); }
+    private readonly List<Expression> arguments;
+    public IEnumerable<Expression> Arguments {
+      get { return arguments ?? Enumerable.Empty<Expression>(); }
+    }
+
+    public IRandom Distribution { get; private set; }
+
+    public Func<IEnumerable<double>, double> Transform { get; private set; }
+
+    public double Value { get; private set; }
+
+    public Expression(string name, double value) {
+      Type = ExpressionType.Constant;
+      Value = value;
+      Name = name;
+    }
+
+    public Expression(string name, IRandom distribution) {
+      Type = ExpressionType.RandomVariable;
+      Distribution = distribution;
+      Name = name;
+    }
+
+    public Expression(string name, Func<IEnumerable<double>, double> transform, IEnumerable<Expression> arguments) {
+      Type = ExpressionType.Function;
+      Transform = transform;
+      this.arguments = this.arguments ?? new List<Expression>();
+      this.arguments.AddRange(arguments);
+      Name = name;
+    }
+
+    public static Expression Constant(string name, double value) {
+      return new Expression(name, value);
+    }
+
+    public static Expression RandomVariable(string name, IRandom distribution) {
+      return new Expression(name, distribution);
+    }
+
+    public static Expression Function(string name, Func<IEnumerable<double>, double> transform, IEnumerable<Expression> arguments) {
+      return new Expression(name, transform, arguments);
+    }
+
+    public override string ToString() {
+      var sb = new StringBuilder();
+
+      switch (Type) {
+        case ExpressionType.Constant:
+          sb.Append(Value.ToString("0.000", CultureInfo.CurrentCulture));
+          break;
+        case ExpressionType.RandomVariable:
+          sb.Append(string.Format("{0} ~ {1}", Name, GetStringDescription(Distribution)));
+          break;
+        case ExpressionType.Function:
+          sb.Append(string.Format("{0} = f(", Name));
+          if (Arguments.Any()) {
+            for (int i = 0; i < arguments.Count - 1; ++i)
+              sb.Append(string.Format("{0}, ", arguments[i].Name));
+            sb.Append(string.Format("{0})", arguments.Last().Name));
+          }
+          break;
+      }
+
+      return sb.ToString();
+    }
+
+    public string PrintInfix() {
+      var sb = new StringBuilder();
+      switch (Type) {
+        case ExpressionType.Constant:
+          sb.Append(Value < 0
+              ? string.Format("(- {0})", Math.Abs(Value).ToString("0.000", CultureInfo.CurrentCulture))
+              : Value.ToString("0.000", CultureInfo.CurrentCulture));
+          break;
+        case ExpressionType.RandomVariable:
+          sb.Append(Name);
+          break;
+        case ExpressionType.Function:
+          if (!Arguments.Any())
+            break;
+          var args = Arguments.ToList();
+          sb.Append("(");
+          // the Label should be known to the infix parser
+          if (Label == "+" || Label == "-" || Label == "*" || Label == "/") {
+            if (args.Count == 1) {
+              sb.Append(string.Format("{0}", args[0].PrintInfix()));
+            } else {
+              sb.Append("(");
+              var last = args.Last();
+              for (int i = 0; i < args.Count - 1; ++i) {
+                var arg = args[i];
+                sb.Append(string.Format("{0} {1} ", arg.PrintInfix(), Label));
+              }
+              sb.Append(string.Format("{0})", last.PrintInfix()));
+            }
+          } else {
+            sb.Append(string.Format("{0}(", Label));
+            var last = args.Last();
+            for (int i = 0; i < args.Count - 1; ++i) {
+              var arg = args[i];
+              sb.Append(string.Format("{0}, ", arg.PrintInfix(), Label));
+            }
+            sb.Append(string.Format("{0})", last.PrintInfix()));
+          }
+          sb.Append(")");
+          break;
+      }
+      return sb.ToString();
+    }
+
+    public string PrintDot() {
+      var stack = new Stack<Expression>();
+      stack.Push(this);
+
+      var sb = new StringBuilder();
+      sb.AppendLine("digraph g {");
+
+      while (stack.Count > 0) {
+        var top = stack.Pop();
+
+        if (!top.Arguments.Any())
+          continue;
+
+        foreach (var arg in top.Arguments) {
+          var from = top.Arguments.Any() ? top.Name : top.ToString();
+          var to = arg.Arguments.Any() ? arg.Name : arg.ToString();
+          sb.AppendLine(string.Format("\"{0}\" -> \"{1}\";", from, to));
+          stack.Push(arg);
         }
+      }
 
-        public IRandom Distribution { get; private set; }
+      sb.AppendLine("}");
+      return sb.ToString();
+    }
 
-        public Func<IEnumerable<double>, double> Transform { get; private set; }
+    private static string GetStringDescription(IRandom random) {
+      var normal = random as NormalDistributedRandom;
+      if (normal != null)
+        return string.Format("N({0}, {1})", normal.Mu, normal.Sigma);
 
-        public double Value { get; private set; }
+      var uniform = random as UniformDistributedRandom;
+      if (uniform != null)
+        return string.Format("U({0}, {1})", uniform.Min, uniform.Max);
 
-        public Expression(string name, double value) {
-            Type = ExpressionType.Constant;
-            Value = value;
-            Name = name;
-        }
+      var gamma = random as GammaDistributedRandom;
+      if (gamma != null)
+        return string.Format("G({0}, {1})", gamma.Shape, gamma.Rate);
 
-        public Expression(string name, IRandom distribution) {
-            Type = ExpressionType.RandomVariable;
-            Distribution = distribution;
-            Name = name;
-        }
-
-        public Expression(string name, Func<IEnumerable<double>, double> transform, IEnumerable<Expression> arguments) {
-            Type = ExpressionType.Function;
-            Transform = transform;
-            this.arguments = this.arguments ?? new List<Expression>();
-            this.arguments.AddRange(arguments);
-            Name = name;
-        }
-
-        public static Expression Constant(string name, double value) {
-            return new Expression(name, value);
-        }
-
-        public static Expression RandomVariable(string name, IRandom distribution) {
-            return new Expression(name, distribution);
-        }
-
-        public static Expression Function(string name, Func<IEnumerable<double>, double> transform, IEnumerable<Expression> arguments) {
-            return new Expression(name, transform, arguments);
-        }
-
-        public override string ToString() {
-            var sb = new StringBuilder();
-
-            switch (Type) {
-                case ExpressionType.Constant:
-                    sb.Append(Value.ToString("0.000", CultureInfo.CurrentCulture));
-                    break;
-                case ExpressionType.RandomVariable:
-                    sb.Append(string.Format("{0} ~ {1}", Name, GetStringDescription(Distribution)));
-                    break;
-                case ExpressionType.Function:
-                    sb.Append(string.Format("{0} = f(", Name));
-                    if (Arguments.Any()) {
-                        for (int i = 0; i < arguments.Count - 1; ++i)
-                            sb.Append(string.Format("{0}, ", arguments[i].Name));
-                        sb.Append(string.Format("{0})", arguments.Last().Name));
-                    }
-                    break;
-            }
-
-            return sb.ToString();
-        }
-
-        public string PrintInfix() {
-            var sb = new StringBuilder();
-            switch (Type) {
-                case ExpressionType.Constant:
-                    sb.Append(Value < 0
-                        ? string.Format("(- {0})", Math.Abs(Value).ToString("0.000", CultureInfo.CurrentCulture))
-                        : Value.ToString("0.000", CultureInfo.CurrentCulture));
-                    break;
-                case ExpressionType.RandomVariable:
-                    sb.Append(Name);
-                    break;
-                case ExpressionType.Function:
-                    if (!Arguments.Any())
-                        break;
-                    var args = Arguments.ToList();
-                    sb.Append("(");
-                    // the Label should be known to the infix parser
-                    if (Label == "+" || Label == "-" || Label == "*" || Label == "/") {
-                        if (args.Count == 1) {
-                            sb.Append(string.Format("{0}", args[0].PrintInfix()));
-                        } else {
-                            sb.Append("(");
-                            var last = args.Last();
-                            for (int i = 0; i < args.Count - 1; ++i) {
-                                var arg = args[i];
-                                sb.Append(string.Format("{0} {1}", arg.PrintInfix(), Label));
-                            }
-                            sb.Append(string.Format(" {0})", last.PrintInfix()));
-                        }
-                    } else {
-                        sb.Append(string.Format("{0}(", Label));
-                        var last = args.Last();
-                        for (int i = 0; i < args.Count - 1; ++i) {
-                            var arg = args[i];
-                            sb.Append(string.Format("{0}, ", arg.PrintInfix(), Label));
-                        }
-                        sb.Append(string.Format("{0})", last.PrintInfix()));
-                    }
-                    sb.Append(")");
-                    break;
-            }
-            return sb.ToString();
-        }
-
-        public string PrintDot() {
-            var stack = new Stack<Expression>();
-            stack.Push(this);
-
-            var sb = new StringBuilder();
-            sb.AppendLine("digraph g {");
-
-            while (stack.Count > 0) {
-                var top = stack.Pop();
-
-                if (!top.Arguments.Any())
-                    continue;
-
-                foreach (var arg in top.Arguments) {
-                    var from = top.Arguments.Any() ? top.Name : top.ToString();
-                    var to = arg.Arguments.Any() ? arg.Name : arg.ToString();
-                    sb.AppendLine(string.Format("\"{0}\" -> \"{1}\";", from, to));
-                    stack.Push(arg);
-                }
-            }
-
-            sb.AppendLine("}");
-            return sb.ToString();
-        }
-
-        private static string GetStringDescription(IRandom random) {
-            var normal = random as NormalDistributedRandom;
-            if (normal != null)
-                return string.Format("N({0}, {1})", normal.Mu, normal.Sigma);
-
-            var uniform = random as UniformDistributedRandom;
-            if (uniform != null)
-                return string.Format("U({0}, {1})", uniform.Min, uniform.Max);
-
-            var gamma = random as GammaDistributedRandom;
-            if (gamma != null)
-                return string.Format("G({0}, {1})", gamma.Shape, gamma.Rate);
-
-            return random.ToString();
-        }
+      return random.ToString();
     }
+  }
 }
Index: /branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/ExpressionGenerator.cs
===================================================================
--- /branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/ExpressionGenerator.cs	(revision 14505)
+++ /branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/ExpressionGenerator.cs	(revision 14505)
@@ -0,0 +1,108 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using HeuristicLab.Core;
+using HeuristicLab.Random;
+
+namespace HeuristicLab.ExpressionGenerator {
+  public static class ExpressionGenerator {
+    public static IEnumerable<Expression> GenerateRandomDistributedVariables(int numberOfVariables, string variablePrefix, IRandom randomDistribution) {
+      return Enumerable.Range(1, numberOfVariables).Select(x => Expression.RandomVariable(string.Format("{0}{1}", variablePrefix, x), randomDistribution));
+    }
+
+    public static Expression Polynomial(IRandom uniformRandom, Expression[] variables, bool useLog, bool useExp) {
+      var sumTemplate = new RandomArityTemplate(Sum, new UniformDistributedRandom(uniformRandom, 1, variables.Length)) { Label = "+" };
+      sumTemplate.AddArguments(variables);
+
+      var productTemplate = new RandomArityTemplate(Product, new UniformDistributedRandom(uniformRandom, 1, variables.Length)) { Label = "*" };
+      productTemplate.AddArguments(variables);
+
+
+      //      var count = (int)Enumerable.Range(1, variables.Length).Sum(x => EnumerableExtensions.BinomialCoefficient(x, variables.Length));
+      var count = 10;
+      int i = 1;
+
+      var logTemplate = new FixedArityTemplate(Log, 1) { Label = "log" };
+      logTemplate.AddArguments(variables);
+      logTemplate.AddArguments(Enumerable.Range(1, count).Select(x => sumTemplate.Instantiate(string.Format("sum{0}", i++), uniformRandom, true)));
+
+      var expTemplate = new FixedArityTemplate(Exp, 1) { Label = "exp" };
+      expTemplate.AddArguments(variables);
+      expTemplate.AddArguments(Enumerable.Range(1, count).Select(x => productTemplate.Instantiate(string.Format("prod{0}", i++), uniformRandom, true)));
+
+      var inverseTemplate = new FixedArityTemplate(Division, 1);
+      inverseTemplate.AddArguments(variables);
+      inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => sumTemplate.Instantiate(string.Format("sum{0}", i++), uniformRandom, true)));
+      inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => productTemplate.Instantiate(string.Format("prod{0}", i++), uniformRandom, true)));
+      inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => logTemplate.Instantiate(string.Format("log{0}", i++), uniformRandom, true)));
+      inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => expTemplate.Instantiate(string.Format("exp{0}", i++), uniformRandom, true)));
+
+
+      var template = new RandomArityTemplate(Sum, new UniformDistributedRandom(uniformRandom, 2, count)) { Label = "+" };
+      template.AddArguments(variables);
+      template.AddArguments(Enumerable.Range(1, count).Select(x => logTemplate.Instantiate(string.Format("log{0}", i++), uniformRandom)));
+      template.AddArguments(Enumerable.Range(1, count).Select(x => expTemplate.Instantiate(string.Format("exp{0}", i++), uniformRandom)));
+      template.AddArguments(Enumerable.Range(1, count).Select(x => inverseTemplate.Instantiate(string.Format("inv{0}", i++), uniformRandom)));
+
+      return template.Instantiate("polynomial", uniformRandom);
+    }
+
+    #region static functions
+    private static double Sum(IEnumerable<double> args) {
+      return args.Sum();
+    }
+
+    private static double Product(IEnumerable<double> args) {
+      return args.Aggregate((x, y) => x * y);
+    }
+
+    private static double Division(IEnumerable<double> args) {
+      if (args.Count() == 1)
+        return 1 / args.First();
+
+      return args.Aggregate((x, y) => x / y);
+    }
+
+    private static double Subtraction(IEnumerable<double> args) {
+      if (args.Count() == 1)
+        return -args.First();
+
+      return args.Aggregate((x, y) => x - y);
+    }
+
+    private static double Exp(IEnumerable<double> args) {
+      return Math.Exp(args.Single());
+    }
+
+    private static double Log(IEnumerable<double> args) {
+      return Math.Log(args.Single());
+    }
+
+    private static double Square(IEnumerable<double> args) {
+      var v = args.Single();
+      return v * v;
+    }
+    #endregion
+  }
+}
Index: /branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/HeuristicLab.ExpressionGenerator.csproj
===================================================================
--- /branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/HeuristicLab.ExpressionGenerator.csproj	(revision 14504)
+++ /branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/HeuristicLab.ExpressionGenerator.csproj	(revision 14505)
@@ -74,4 +74,5 @@
     <Compile Include="Expression.cs" />
     <Compile Include="ExpressionEvaluator.cs" />
+    <Compile Include="ExpressionGenerator.cs" />
     <Compile Include="ExpressionTemplate.cs" />
     <Compile Include="Interfaces\IExpression.cs" />
