Changeset 14515


Ignore:
Timestamp:
12/21/16 13:37:55 (3 years ago)
Author:
bburlacu
Message:

#2704: Fix infix formatting bug, refactored generation of polynomial to respect user choices (with or without exp/log).

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

Legend:

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

    r14505 r14515  
    121121          // the Label should be known to the infix parser
    122122          if (Label == "+" || Label == "-" || Label == "*" || Label == "/") {
     123            if (Label == "/")
     124              sb.Append("1 / ");
    123125            if (args.Count == 1) {
    124126              sb.Append(string.Format("{0}", args[0].PrintInfix()));
  • branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/ExpressionGenerator.cs

    r14510 r14515  
    3333
    3434    public static Expression Polynomial(IRandom uniformRandom, Expression[] variables, bool useLog, bool useExp) {
     35      const int count = 10; // how many instances of each template should be produce?
     36      int i = 1;
     37      // the main template
     38      var template = new RandomArityTemplate(Sum, new UniformDistributedRandom(uniformRandom, 2, count)) { Label = "+" };
     39      template.AddArguments(variables);
     40
    3541      var sumTemplate = new RandomArityTemplate(Sum, new UniformDistributedRandom(uniformRandom, 1, variables.Length)) { Label = "+" };
    3642      sumTemplate.AddArguments(variables);
     
    3945      productTemplate.AddArguments(variables);
    4046
    41       const int count = 10; // how many instances of each template should be produce?
    42       int i = 1;
    43 
    44       var logTemplate = new FixedArityTemplate(Log, 1) { Label = "log" };
    45       logTemplate.AddArguments(variables);
    46       logTemplate.AddArguments(Enumerable.Range(1, count).Select(x => sumTemplate.Instantiate(string.Format("sum{0}", i++), uniformRandom)));
    47 
    48       var expTemplate = new FixedArityTemplate(Exp, 1) { Label = "exp" };
    49       expTemplate.AddArguments(variables);
    50       expTemplate.AddArguments(Enumerable.Range(1, count).Select(x => productTemplate.Instantiate(string.Format("prod{0}", i++), uniformRandom)));
    51 
    5247      var inverseTemplate = new FixedArityTemplate(Division, 1) { Label = "/" };
    5348      inverseTemplate.AddArguments(variables);
    5449      inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => sumTemplate.Instantiate(string.Format("sum{0}", i++), uniformRandom)));
    5550      inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => productTemplate.Instantiate(string.Format("prod{0}", i++), uniformRandom)));
    56       inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => logTemplate.Instantiate(string.Format("log{0}", i++), uniformRandom)));
    57       inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => expTemplate.Instantiate(string.Format("exp{0}", i++), uniformRandom)));
    5851
    59       var template = new RandomArityTemplate(Sum, new UniformDistributedRandom(uniformRandom, 2, count)) { Label = "+" };
    60       template.AddArguments(variables);
    61       template.AddArguments(Enumerable.Range(1, count).Select(x => logTemplate.Instantiate(string.Format("log{0}", i++), uniformRandom)));
    62       template.AddArguments(Enumerable.Range(1, count).Select(x => expTemplate.Instantiate(string.Format("exp{0}", i++), uniformRandom)));
     52      if (useLog) {
     53        var logTemplate = new FixedArityTemplate(Log, 1) { Label = "log" };
     54        logTemplate.AddArguments(variables);
     55        logTemplate.AddArguments(Enumerable.Range(1, count).Select(x => sumTemplate.Instantiate(string.Format("sum{0}", i++), uniformRandom)));
     56        template.AddArguments(Enumerable.Range(1, count).Select(x => logTemplate.Instantiate(string.Format("log{0}", i++), uniformRandom)));
     57        inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => logTemplate.Instantiate(string.Format("log{0}", i++), uniformRandom)));
     58      }
     59
     60      if (useExp) {
     61        var expTemplate = new FixedArityTemplate(Exp, 1) { Label = "exp" };
     62        expTemplate.AddArguments(variables);
     63        expTemplate.AddArguments(Enumerable.Range(1, count).Select(x => productTemplate.Instantiate(string.Format("prod{0}", i++), uniformRandom)));
     64        template.AddArguments(Enumerable.Range(1, count).Select(x => expTemplate.Instantiate(string.Format("exp{0}", i++), uniformRandom)));
     65        inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => expTemplate.Instantiate(string.Format("exp{0}", i++), uniformRandom)));
     66      }
     67
    6368      template.AddArguments(Enumerable.Range(1, count).Select(x => inverseTemplate.Instantiate(string.Format("inv{0}", i++), uniformRandom)));
    64 
    6569      return template.Instantiate("polynomial", uniformRandom);
    6670    }
Note: See TracChangeset for help on using the changeset viewer.