Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14410 for branches


Ignore:
Timestamp:
11/24/16 16:38:20 (8 years ago)
Author:
bburlacu
Message:

#2704: Added the possibility to sample possible arguments without repetition when instantiating expression templates.

Location:
branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/Expression.cs

    r14409 r14410  
    8989        case ExpressionType.Function:
    9090          sb.Append(string.Format("{0} (", Label));
    91           if (arguments != null) {
     91          if (Arguments.Any()) {
    9292            for (int i = 0; i < arguments.Count - 1; ++i)
    9393              sb.Append(string.Format("{0}, ", arguments[i]));
     
    9797      }
    9898
     99      return sb.ToString();
     100    }
     101
     102    public string PrintDot() {
     103      var stack = new Stack<Expression>();
     104      stack.Push(this);
     105
     106      var sb = new StringBuilder();
     107      sb.AppendLine("digraph g {");
     108
     109      while (stack.Count > 0) {
     110        var top = stack.Pop();
     111
     112        if (!top.Arguments.Any())
     113          continue;
     114
     115        foreach (var arg in top.Arguments) {
     116          var from = top.Arguments.Any() ? top.Label : top.ToString();
     117          var to = arg.Arguments.Any() ? arg.Label : arg.ToString();
     118          sb.AppendLine(string.Format("\"{0}\" -> \"{1}\";", from, to));
     119          stack.Push(arg);
     120        }
     121      }
     122
     123      sb.AppendLine("}");
    99124      return sb.ToString();
    100125    }
  • branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/ExpressionTemplate.cs

    r14409 r14410  
    3232    protected readonly string label;
    3333
    34     public abstract Expression Instantiate(IRandom random);
     34    public abstract Expression Instantiate(IRandom random, bool sampleWithRepetition = false);
    3535
    3636    protected ExpressionTemplate(string label, Func<IEnumerable<double>, double> transform) {
     
    6262    }
    6363
    64     public override Expression Instantiate(IRandom random) {
     64    public override Expression Instantiate(IRandom random, bool sampleWithRepetition = false) {
    6565      var arity = (int)Math.Round(arityDistribution.NextDouble());
    66       var args = arguments.SampleProportional(random, arity, arguments.Select(x => x.Item2)).Select(x => x.Item1);
     66      var weights = arguments.Select(x => x.Item2);
     67      var args = sampleWithRepetition
     68        ? arguments.SampleProportional(random, arity, weights).Select(x => x.Item1)
     69        : arguments.SampleProportionalWithoutRepetition(random, arity, weights).Select(x => x.Item1);
    6770      return Expression.Function(label, transform, args);
    6871    }
     
    7578    }
    7679
    77     public override Expression Instantiate(IRandom random) {
     80    public override Expression Instantiate(IRandom random, bool sampleWithRepetition = false) {
    7881      var args = arguments.SampleProportional(random, arity, arguments.Select(x => x.Item2)).Select(x => x.Item1);
    7982      return Expression.Function(label, transform, args);
  • branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/Interfaces/IExpression.cs

    r14409 r14410  
    2929  public interface IExpression {
    3030    ExpressionType Type { get; }
     31    Func<IEnumerable<double>, double> Transform { get; }
    3132    IEnumerable<Expression> Arguments { get; }
     33    IRandom Distribution { get; }
    3234    double Value { get; }
    33     IRandom Distribution { get; }
    34     Func<IEnumerable<double>, double> Transform { get; }
    3535  }
    3636}
Note: See TracChangeset for help on using the changeset viewer.