Changeset 15273 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Generators/CodeGenerator/CodeGeneratorUtils.cs
- Timestamp:
- 07/19/17 12:55:58 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Generators/CodeGenerator/CodeGeneratorUtils.cs
r15189 r15273 7 7 8 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; 9 12 10 13 using Stack; 11 14 12 15 internal static class CodeGeneratorUtils { 13 internal static Expression CreateExpressionOrErc( 16 private static readonly Expression Noop = ExpressionTable.GetStatelessExpression<ExecNoopExpression>(); 17 18 internal static Expression MapToExpression( 14 19 IRandom random, 15 IReadOnlyList<string> enabledExpressions,16 20 IReadOnlyErcOptions ercOptions, 21 IReadOnlyExpressionsConfiguration config, 17 22 IDictionary<string, Expression> customExpressions = null) { 18 23 var customCount = customExpressions == null ? 0 : customExpressions.Count; 19 var index = random.Next(enabledExpressions.Count + customCount); 24 var enabledExpressionCount = config.EnabledExpressions.Count; 25 var index = random.Next(enabledExpressionCount + customCount); 20 26 21 return index >= enabledExpressions.Count22 ? customExpressions.Values.ElementAt(index - enabledExpression s.Count)23 : CreateExpressionOrErc(index, random, enabledExpressions, ercOptions);27 return index >= config.EnabledExpressions.Count 28 ? customExpressions.Values.ElementAt(index - enabledExpressionCount) 29 : MapToExpression(index, random, ercOptions, config); 24 30 } 25 31 26 internal static Expression CreateExpressionOrErc(32 internal static Expression MapToExpression( 27 33 int index, 28 34 IRandom random, 29 IReadOnlyList<string> enabledExpressions, 30 IReadOnlyErcOptions ercOptions) { 31 var name = enabledExpressions[index]; 35 IReadOnlyErcOptions ercOptions, 36 IReadOnlyExpressionsConfiguration config) { 32 37 33 return CreateExpressionOrErc(name, random, ercOptions); 38 switch (index) { 39 case PushSolutionEncoding.Noop: 40 return Noop; 41 42 case PushSolutionEncoding.Erc: 43 var stackType = config.ExpressionsPerStackCount.RandomWeightedOrDefault(random, pair => pair.Value).Key; 44 45 if (stackType == default(StackTypes)) 46 break; 47 48 return CreateRandomErcExpression(stackType, random, ercOptions); 49 50 case PushSolutionEncoding.In: 51 if (config.InExpressionCount == 0) break; 52 53 var nr = random.Next(0, config.InExpressionCount) + 1; 54 return ExpressionTable.InExpressionTable[nr]; 55 } 56 57 var name = config.EnabledExpressions[index]; 58 return ExpressionTable.GetExpression(name); 34 59 } 35 60 36 private static Expression CreateExpressionOrErc(37 string expressionName,38 IRandom random,39 IReadOnlyErcOptions ercOptions) {40 //var x = random.NextDouble();41 //Expression expression = null;61 //private static Expression CreateExpressionOrErc( 62 // string expressionName, 63 // IRandom random, 64 // IReadOnlyErcOptions ercOptions) { 65 // var x = random.NextDouble(); 66 // Expression expression = null; 42 67 43 //if (x < ercOptions.ErcProbability) {44 //var expressionType = ExpressionTable.NameToTypeTable[expressionName];45 //expression = CreateRandomErcExpression(46 //ExpressionTable.TypeToAttributeTable[expressionType].StackType,47 //random,48 //ercOptions);49 //}68 // if (x < ercOptions.ErcProbability) { 69 // var expressionType = ExpressionTable.NameToTypeTable[expressionName]; 70 // expression = CreateRandomErcExpression( 71 // ExpressionTable.TypeToAttributeTable[expressionType].StackType, 72 // random, 73 // ercOptions); 74 // } 50 75 51 //if (expression == null || expression is ExecNoopExpression) {52 //expression = ExpressionTable.GetExpression(expressionName);53 //}76 // if (expression == null || expression is ExecNoopExpression) { 77 // expression = ExpressionTable.GetExpression(expressionName); 78 // } 54 79 55 //return expression; 80 // return expression; 81 //} 56 82 57 var expression = ExpressionTable.GetExpression(expressionName);58 59 return expression;60 }61 62 private static readonly Expression Noop = ExpressionTable.GetStatelessExpression<ExecNoopExpression>();63 83 /// <summary> 64 84 /// Create a ErcExpression whereby the type of the expression conforms to the passed stack type.
Note: See TracChangeset
for help on using the changeset viewer.