Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Generators/CodeGenerator/CodeGeneratorUtils.cs @ 15189

Last change on this file since 15189 was 15189, checked in by pkimmesw, 7 years ago

#2665 Fixed small issues, testet benchmark suite, added INX Expressions

File size: 4.9 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3
4namespace HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator {
5  using Core;
6  using Expressions;
7
8  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc;
9
10  using Stack;
11
12  internal static class CodeGeneratorUtils {
13    internal static Expression CreateExpressionOrErc(
14      IRandom random,
15      IReadOnlyList<string> enabledExpressions,
16      IReadOnlyErcOptions ercOptions,
17      IDictionary<string, Expression> customExpressions = null) {
18      var customCount = customExpressions == null ? 0 : customExpressions.Count;
19      var index = random.Next(enabledExpressions.Count + customCount);
20
21      return index >= enabledExpressions.Count
22        ? customExpressions.Values.ElementAt(index - enabledExpressions.Count)
23        : CreateExpressionOrErc(index, random, enabledExpressions, ercOptions);
24    }
25
26    internal static Expression CreateExpressionOrErc(
27      int index,
28      IRandom random,
29      IReadOnlyList<string> enabledExpressions,
30      IReadOnlyErcOptions ercOptions) {
31      var name = enabledExpressions[index];
32
33      return CreateExpressionOrErc(name, random, ercOptions);
34    }
35
36    private static Expression CreateExpressionOrErc(
37      string expressionName,
38      IRandom random,
39      IReadOnlyErcOptions ercOptions) {
40      //var x = random.NextDouble();
41      //Expression expression = null;
42
43      //if (x < ercOptions.ErcProbability) {
44      //  var expressionType = ExpressionTable.NameToTypeTable[expressionName];
45      //  expression = CreateRandomErcExpression(
46      //      ExpressionTable.TypeToAttributeTable[expressionType].StackType,
47      //      random,
48      //      ercOptions);
49      //}
50
51      //if (expression == null || expression is ExecNoopExpression) {
52      //  expression = ExpressionTable.GetExpression(expressionName);
53      //}
54
55      //return expression;
56
57      var expression = ExpressionTable.GetExpression(expressionName);
58
59      return expression;
60    }
61
62    private static readonly Expression Noop = ExpressionTable.GetStatelessExpression<ExecNoopExpression>();
63    /// <summary>
64    /// Create a ErcExpression whereby the type of the expression conforms to the passed stack type.
65    /// </summary>
66    /// <param name="type"></param>
67    /// <param name="random"></param>
68    /// <param name="pushConfiguration"></param>
69    /// <returns>ErcExpression or Noop if the required ErcOptionConstants is not configured or enabled in the passed config.</returns>
70    internal static Expression CreateRandomErcExpression(StackTypes type, IRandom random, IReadOnlyErcOptions ercOptions) {
71      switch (type) {
72        case StackTypes.Integer:
73          return ercOptions.IntegerErcOptions == null || !ercOptions.IntegerErcOptions.IsEnabled
74             ? Noop
75             : new IntegerPushExpression(ercOptions.IntegerErcOptions.GetErcValue(random));
76
77        case StackTypes.Float:
78          return ercOptions.FloatErcOptions == null || !ercOptions.FloatErcOptions.IsEnabled
79            ? Noop
80            : new FloatPushExpression(ercOptions.FloatErcOptions.GetErcValue(random));
81
82        case StackTypes.Boolean:
83          return ercOptions.BooleanErcOptions == null || !ercOptions.BooleanErcOptions.IsEnabled
84            ? Noop
85            : new BooleanPushExpression(ercOptions.BooleanErcOptions.GetErcValue(random));
86
87        case StackTypes.Name:
88          return ercOptions.NameErcOptions == null || !ercOptions.NameErcOptions.IsEnabled
89            ? Noop
90            : new NamePushExpression(ercOptions.NameErcOptions.GetErcValue(random));
91
92        case StackTypes.String:
93          return ercOptions.StringErcOptions == null || !ercOptions.StringErcOptions.IsEnabled
94            ? Noop
95            : new StringPushExpression(ercOptions.StringErcOptions.GetErcValue(random));
96
97        case StackTypes.Char:
98          return ercOptions.CharErcOptions == null || !ercOptions.CharErcOptions.IsEnabled
99            ? Noop
100            : new CharPushExpression(ercOptions.CharErcOptions.GetErcValue(random));
101
102        case StackTypes.IntegerVector:
103          return ercOptions.IntegerVectorErcOptions == null || !ercOptions.IntegerVectorErcOptions.IsEnabled
104            ? Noop
105            : new IntegerVectorPushExpression(ercOptions.IntegerVectorErcOptions.GetErcValue(random).Select(i => (long)i).ToList());
106
107        case StackTypes.FloatVector:
108          return ercOptions.FloatVectorErcOptions == null || !ercOptions.FloatVectorErcOptions.IsEnabled
109            ? Noop
110            : new FloatVectorPushExpression(ercOptions.FloatVectorErcOptions.GetErcValue(random));
111
112        case StackTypes.StringVector:
113          return ercOptions.StringVectorErcOptions == null || !ercOptions.StringVectorErcOptions.IsEnabled
114            ? Noop
115            : new StringVectorPushExpression(ercOptions.StringVectorErcOptions.GetErcValue(random));
116
117        default:
118          return Noop;
119      }
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.