Changeset 14875 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Generators/CodeGenerator/CodeGeneratorUtils.cs
- Timestamp:
- 04/18/17 01:15:25 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Generators/CodeGenerator/CodeGeneratorUtils.cs
r14834 r14875 3 3 4 4 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator { 5 5 6 using HeuristicLab.Core; 6 using HeuristicLab.Problems.ProgramSynthesis. Push.Erc.Interfaces;7 using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Interfaces; 7 8 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 8 9 using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; 9 10 10 11 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 12 internal static Expression CreateExpressionOrErc( 24 13 IRandom random, … … 26 15 IReadOnlyErcOptions ercOptions, 27 16 IDictionary<string, Expression> customExpressions = null) { 28 var expression = CreateExpression(random, enabledExpressions, customExpressions); 29 return CreateExpressionOrErc(expression, random, ercOptions); 17 var customCount = customExpressions == null ? 0 : customExpressions.Count; 18 var index = random.Next(enabledExpressions.Count + customCount); 19 20 return index >= enabledExpressions.Count 21 ? customExpressions.ElementAt(index - (enabledExpressions.Count - 1)).Value 22 : CreateExpressionOrErc(index, random, enabledExpressions, ercOptions); 30 23 } 31 24 … … 36 29 IReadOnlyErcOptions ercOptions) { 37 30 var name = enabledExpressions[index]; 38 var expression = ExpressionTable.GetExpression(name);39 31 40 return CreateExpressionOrErc( expression, random, ercOptions);32 return CreateExpressionOrErc(name, random, ercOptions); 41 33 } 42 34 43 35 private static Expression CreateExpressionOrErc( 44 Expression expression,36 string expressionName, 45 37 IRandom random, 46 38 IReadOnlyErcOptions ercOptions) { 47 39 var x = random.NextDouble(); 40 Expression expression = null; 48 41 49 42 if (x < ercOptions.ErcProbability) { 50 var tmp = CreateRandomErcExpression( 51 ExpressionTable.TypeToAttributeTable[expression.GetType()].StackType, 43 var expressionType = ExpressionTable.NameToTypeTable[expressionName]; 44 expression = CreateRandomErcExpression( 45 ExpressionTable.TypeToAttributeTable[expressionType].StackType, 52 46 random, 53 47 ercOptions); 48 } 54 49 55 if (!(tmp is ExecNoopExpression)) { 56 expression = tmp; 57 } 50 if (expression == null || expression is ExecNoopExpression) { 51 expression = ExpressionTable.GetExpression(expressionName); 58 52 } 59 53
Note: See TracChangeset
for help on using the changeset viewer.