Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Generators/CodeGenerator/CodeGeneratorUtils.cs @ 16958

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

#2665 Fixed analyzer, fixed Plush encoding + operators, adpated print evaluation according to McPhee

File size: 7.5 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  using HeuristicLab.Problems.ProgramSynthesis.Base.Extensions;
10  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
11  using HeuristicLab.Problems.ProgramSynthesis.Push.SolutionCreator;
12
13  using Stack;
14
15  internal static class CodeGeneratorUtils {
16    private static readonly Expression Noop = ExpressionTable.GetStatelessExpression<ExecNoopExpression>();
17
18    internal static Expression MapToExpression(
19      IRandom random,
20      IReadOnlyErcOptions ercOptions,
21      IReadOnlyExpressionsConfiguration config,
22      IDictionary<string, Expression> customExpressions = null) {
23      var customCount = customExpressions == null ? 0 : customExpressions.Count;
24      var enabledExpressionCount = config.EnabledExpressions.Count;
25      var index = random.Next(enabledExpressionCount + customCount);
26
27      return index >= config.EnabledExpressions.Count
28        ? customExpressions.Values.ElementAt(index - enabledExpressionCount)
29        : MapToExpression(index, random, ercOptions, config);
30    }
31
32    internal static Expression MapToExpression(
33      int index,
34      IReadOnlyExpressionsConfiguration config) {
35
36      switch (index) {
37        case PushSolutionEncoding.Noop:
38          return Noop;
39      }
40
41      var name = config.EnabledExpressions[index];
42      return ExpressionTable.GetExpression(name);
43    }
44
45    internal static Expression MapToExpression(
46      int index,
47      IRandom random,
48      IReadOnlyErcOptions ercOptions,
49      IReadOnlyExpressionsConfiguration config) {
50
51      switch (index) {
52        case PushSolutionEncoding.Noop:
53          return Noop;
54
55        case PushSolutionEncoding.Erc:
56          var stackType = config.ExpressionsPerStackCount.RandomWeightedOrDefault(random, pair => pair.Value).Key;
57
58          if (stackType == default(StackTypes))
59            break;
60
61          return CreateRandomErcExpression(stackType, random, ercOptions);
62
63        case PushSolutionEncoding.In:
64          if (config.InExpressionCount == 0) break;
65
66          var nr = random.Next(0, config.InExpressionCount) + 1;
67          return ExpressionTable.InExpressionTable[nr];
68      }
69
70      var name = config.EnabledExpressions[index];
71      return ExpressionTable.GetExpression(name);
72    }
73
74    public static Expression GetRandomExpression(IRandom random, IReadOnlyErcOptions ercOptions, IReadOnlyExpressionsConfiguration instructions, double inInstructionProbability) {
75      var x = random.NextDouble();
76      var inInstructionRelativeProbability = ercOptions.ErcProbability + inInstructionProbability;
77
78      Expression expression;
79
80      if (ercOptions.ErcProbability > 0 && x <= ercOptions.ErcProbability) {
81        var stackType = instructions.ExpressionsPerStackCount.RandomWeightedOrDefault(random, pair => pair.Value).Key;
82
83        expression = stackType == default(StackTypes)
84          ? GetRandomExpression(random, instructions)
85          : CreateRandomErcExpression(stackType, random, ercOptions);
86      } else if (
87        inInstructionProbability > 0 &&
88        x <= inInstructionRelativeProbability &&
89        instructions.InExpressionCount > 0) {
90
91        var index = random.Next(0, instructions.InExpressionCount);
92        expression = ExpressionTable.InExpressionTable[index];
93      } else {
94        expression = GetRandomExpression(random, instructions);
95      }
96
97      return expression;
98    }
99
100    private static Expression GetRandomExpression(
101      IRandom random,
102      IReadOnlyExpressionsConfiguration instructionsConfig) {
103      var index = random.Next(0, instructionsConfig.EnabledExpressions.Count);
104      var expressionName = instructionsConfig.EnabledExpressions[index];
105      var expression = ExpressionTable.GetExpression(expressionName);
106
107      return expression;
108    }
109
110    //private static Expression CreateExpressionOrErc(
111    //  string expressionName,
112    //  IRandom random,
113    //  IReadOnlyErcOptions ercOptions) {
114    //  var x = random.NextDouble();
115    //  Expression expression = null;
116
117    //  if (x < ercOptions.ErcProbability) {
118    //    var expressionType = ExpressionTable.NameToTypeTable[expressionName];
119    //    expression = CreateRandomErcExpression(
120    //        ExpressionTable.TypeToAttributeTable[expressionType].StackType,
121    //        random,
122    //        ercOptions);
123    //  }
124
125    //  if (expression == null || expression is ExecNoopExpression) {
126    //    expression = ExpressionTable.GetExpression(expressionName);
127    //  }
128
129    //  return expression;
130    //}
131
132    /// <summary>
133    /// Create a ErcExpression whereby the type of the expression conforms to the passed stack type.
134    /// </summary>
135    /// <param name="type"></param>
136    /// <param name="random"></param>
137    /// <param name="pushConfiguration"></param>
138    /// <returns>ErcExpression or Noop if the required ErcOptionConstants is not configured or enabled in the passed config.</returns>
139    internal static Expression CreateRandomErcExpression(StackTypes type, IRandom random, IReadOnlyErcOptions ercOptions) {
140      switch (type) {
141        case StackTypes.Integer:
142          return ercOptions.IntegerErcOptions == null || !ercOptions.IntegerErcOptions.IsEnabled
143             ? Noop
144             : new IntegerPushExpression(ercOptions.IntegerErcOptions.GetErcValue(random));
145
146        case StackTypes.Float:
147          return ercOptions.FloatErcOptions == null || !ercOptions.FloatErcOptions.IsEnabled
148            ? Noop
149            : new FloatPushExpression(ercOptions.FloatErcOptions.GetErcValue(random));
150
151        case StackTypes.Boolean:
152          return ercOptions.BooleanErcOptions == null || !ercOptions.BooleanErcOptions.IsEnabled
153            ? Noop
154            : new BooleanPushExpression(ercOptions.BooleanErcOptions.GetErcValue(random));
155
156        case StackTypes.Name:
157          return ercOptions.NameErcOptions == null || !ercOptions.NameErcOptions.IsEnabled
158            ? Noop
159            : new NamePushExpression(ercOptions.NameErcOptions.GetErcValue(random));
160
161        case StackTypes.String:
162          return ercOptions.StringErcOptions == null || !ercOptions.StringErcOptions.IsEnabled
163            ? Noop
164            : new StringPushExpression(ercOptions.StringErcOptions.GetErcValue(random));
165
166        case StackTypes.Char:
167          return ercOptions.CharErcOptions == null || !ercOptions.CharErcOptions.IsEnabled
168            ? Noop
169            : new CharPushExpression(ercOptions.CharErcOptions.GetErcValue(random));
170
171        case StackTypes.IntegerVector:
172          return ercOptions.IntegerVectorErcOptions == null || !ercOptions.IntegerVectorErcOptions.IsEnabled
173            ? Noop
174            : new IntegerVectorPushExpression(ercOptions.IntegerVectorErcOptions.GetErcValue(random).Select(i => (long)i).ToList());
175
176        case StackTypes.FloatVector:
177          return ercOptions.FloatVectorErcOptions == null || !ercOptions.FloatVectorErcOptions.IsEnabled
178            ? Noop
179            : new FloatVectorPushExpression(ercOptions.FloatVectorErcOptions.GetErcValue(random));
180
181        case StackTypes.StringVector:
182          return ercOptions.StringVectorErcOptions == null || !ercOptions.StringVectorErcOptions.IsEnabled
183            ? Noop
184            : new StringVectorPushExpression(ercOptions.StringVectorErcOptions.GetErcValue(random));
185
186        default:
187          return Noop;
188      }
189    }
190  }
191}
Note: See TracBrowser for help on using the repository browser.