Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14411 for branches


Ignore:
Timestamp:
11/24/16 16:58:48 (8 years ago)
Author:
bburlacu
Message:

#2704: Slightly improve usage of ExpressionTemplate class.

File:
1 edited

Legend:

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

    r14410 r14411  
    2828namespace HeuristicLab.ExpressionGenerator {
    2929  public abstract class ExpressionTemplate {
    30     protected List<Tuple<Expression, double>> arguments;
    31     protected readonly Func<IEnumerable<double>, double> transform;
    32     protected readonly string label;
     30    private List<Tuple<Expression, double>> arguments;
     31    private readonly Func<IEnumerable<double>, double> transform;
     32    private readonly string label;
     33
     34    protected Expression Instantiate(IRandom random, int n, bool sampleWithRepetition = false) {
     35      var weights = arguments.Select(x => x.Item2);
     36      var args = sampleWithRepetition
     37       ? arguments.SampleProportional(random, n, weights).Select(x => x.Item1)
     38       : arguments.SampleProportionalWithoutRepetition(random, n, weights).Select(x => x.Item1);
     39      return Expression.Function(label, transform, args);
     40    }
    3341
    3442    public abstract Expression Instantiate(IRandom random, bool sampleWithRepetition = false);
     
    6472    public override Expression Instantiate(IRandom random, bool sampleWithRepetition = false) {
    6573      var arity = (int)Math.Round(arityDistribution.NextDouble());
    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);
    70       return Expression.Function(label, transform, args);
     74      return Instantiate(random, arity, sampleWithRepetition);
    7175    }
    7276  }
     
    7983
    8084    public override Expression Instantiate(IRandom random, bool sampleWithRepetition = false) {
    81       var args = arguments.SampleProportional(random, arity, arguments.Select(x => x.Item2)).Select(x => x.Item1);
    82       return Expression.Function(label, transform, args);
     85      return Instantiate(random, arity, sampleWithRepetition);
    8386    }
    8487  }
Note: See TracChangeset for help on using the changeset viewer.