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

Last change on this file since 14834 was 14834, checked in by pkimmesw, 3 years ago

#2665 LexicaseSelector, Performance improvements, UI Fixes, Debugger only shows used stacks, fixed Debugger stepping, Added vector expressions, ERCOptions,

File size: 4.8 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3
4namespace HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator {
5  using HeuristicLab.Core;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Erc.Interfaces;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
8  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
9
10  internal static class CodeGeneratorUtils {
11    internal static Expression CreateExpression(IRandom random, IReadOnlyList<string> enabledExpressions, IDictionary<string, Expression> customExpressions = null) {
12      var customCount = customExpressions == null ? 0 : customExpressions.Count;
13      var index = random.Next(0, enabledExpressions.Count + customCount);
14
15      if ((index >= 0) && (index < enabledExpressions.Count)) {
16        var name = enabledExpressions[index];
17        return ExpressionTable.GetExpression(name);
18      }
19
20      return customExpressions.ElementAt(index - (enabledExpressions.Count - 1)).Value;
21    }
22
23    internal static Expression CreateExpressionOrErc(
24      IRandom random,
25      IReadOnlyList<string> enabledExpressions,
26      IReadOnlyErcOptions ercOptions,
27      IDictionary<string, Expression> customExpressions = null) {
28      var expression = CreateExpression(random, enabledExpressions, customExpressions);
29      return CreateExpressionOrErc(expression, random, ercOptions);
30    }
31
32    internal static Expression CreateExpressionOrErc(
33      int index,
34      IRandom random,
35      IReadOnlyList<string> enabledExpressions,
36      IReadOnlyErcOptions ercOptions) {
37      var name = enabledExpressions[index];
38      var expression = ExpressionTable.GetExpression(name);
39
40      return CreateExpressionOrErc(expression, random, ercOptions);
41    }
42
43    private static Expression CreateExpressionOrErc(
44      Expression expression,
45      IRandom random,
46      IReadOnlyErcOptions ercOptions) {
47      var x = random.NextDouble();
48
49      if (x < ercOptions.ErcProbability) {
50        var tmp = CreateRandomErcExpression(
51            ExpressionTable.TypeToAttributeTable[expression.GetType()].StackType,
52            random,
53            ercOptions);
54
55        if (!(tmp is ExecNoopExpression)) {
56          expression = tmp;
57        }
58      }
59
60      return expression;
61    }
62
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          if (ercOptions.IntegerErcOptions == null || !ercOptions.IntegerErcOptions.IsEnabled)
74            return ExpressionTable.GetStatelessExpression<ExecNoopExpression>();
75
76          var intValue = ErcUtils.GetInteger(ercOptions.IntegerErcOptions, random);
77
78          return new IntegerPushExpression(intValue);
79
80        case StackTypes.Float:
81          if (ercOptions.FloatErcOptions == null || !ercOptions.FloatErcOptions.IsEnabled)
82            return ExpressionTable.GetStatelessExpression<ExecNoopExpression>();
83
84          var doubleValue = ErcUtils.GetDouble(ercOptions.FloatErcOptions, random);
85
86          return new FloatPushExpression(doubleValue);
87
88        case StackTypes.Boolean:
89          return ercOptions.BooleanErcOptions == null || !ercOptions.BooleanErcOptions.IsEnabled
90            ? ExpressionTable.GetStatelessExpression<ExecNoopExpression>()
91            : new BooleanPushExpression(ErcUtils.GetBoolean(ercOptions.BooleanErcOptions, random));
92
93        case StackTypes.Name:
94          return ercOptions.NameErcOptions == null || !ercOptions.NameErcOptions.IsEnabled
95            ? ExpressionTable.GetStatelessExpression<ExecNoopExpression>()
96            : new NamePushExpression(ErcUtils.GetName(ercOptions.NameErcOptions, random));
97
98        case StackTypes.String:
99          return ercOptions.StringErcOptions == null || !ercOptions.StringErcOptions.IsEnabled
100            ? ExpressionTable.GetStatelessExpression<ExecNoopExpression>()
101            : new StringPushExpression(ErcUtils.GetString(ercOptions.StringErcOptions, random));
102
103        case StackTypes.Char:
104          return ercOptions.CharErcOptions == null || !ercOptions.CharErcOptions.IsEnabled
105            ? ExpressionTable.GetStatelessExpression<ExecNoopExpression>()
106            : new CharPushExpression(ErcUtils.GetChar(ercOptions.CharErcOptions, random));
107
108        default:
109          return ExpressionTable.GetStatelessExpression<ExecNoopExpression>();
110      }
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.