Changeset 15017 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/IndividualMapper.cs
- Timestamp:
- 06/01/17 09:28:34 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/IndividualMapper.cs
r14897 r15017 1 1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem { 2 using System.Collections.Generic; 2 3 3 4 using HeuristicLab.Core; … … 7 8 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool; 8 9 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 10 using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions; 9 11 using HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator; 10 12 using HeuristicLab.Random; … … 17 19 public static PushProgram ToPushProgram(this Individual individual, IReadOnlyPushConfiguration config) { 18 20 return individual.IntegerVector().ToPushProgram(config); 21 } 22 23 public static PushProgram ToPushProgram(this Individual individual, IReadOnlyPushConfiguration config, IRandom random) { 24 return individual.IntegerVector().ToPushProgram(config, random); 19 25 } 20 26 … … 35 41 } 36 42 37 p rivatestatic int GetSeed(this IntegerVector vector) {38 var seed = 0;43 public static int GetSeed(this IntegerVector vector) { 44 var seed = 17; 39 45 for (var i = 0; i < vector.Length; i++) 40 seed += (i + 1) *vector[i];46 seed = seed * 23 + vector[i]; 41 47 return seed; 42 48 } 43 49 44 private static PushProgram ToPushProgram(this IntegerVector vector, IReadOnlyPushConfiguration config, IRandom random) { 45 var enabledExpressions = config.EnabledExpressions; 46 var ercOptions = config.ErcOptions; 47 //var programLength = random.Next(config.MinPointsInProgram, config.MaxPointsInProgram + 1); 48 var expressions = new Expression[vector.Length]; 50 public static PushProgram ToPushProgram(this IntegerVector vector, IReadOnlyPushConfiguration config, IRandom random) { 51 var currentIndex = 0; 52 var close = 0; 49 53 50 for (var i = 0; i < vector.Length; i++) 51 expressions[i] = CodeGeneratorUtils.CreateExpressionOrErc(vector[i], random, enabledExpressions, ercOptions); 54 return FromPlush(vector, ref currentIndex, ref close, 0, config, random); 55 } 56 57 private static PushProgram FromPlush(IntegerVector vector, ref int currentIndex, ref int close, int depth, IReadOnlyPushConfiguration config, IRandom random) { 58 if (currentIndex >= vector.Length) 59 return PushProgram.Empty; 60 61 var expressions = new List<Expression>(); 62 63 for (; currentIndex < vector.Length; currentIndex++) { 64 var expression = CodeGeneratorUtils.CreateExpressionOrErc( 65 vector[currentIndex] % config.EnabledExpressions.Count, 66 random, 67 config.EnabledExpressions, 68 config.ErcOptions); 69 70 var expressionType = expression.GetType(); 71 close += random.NextBiased(0, config.MaxParenthesesClose, config.ParenthesesCloseBiasLevel); 72 73 74 // check if expression requires additional blocks 75 if (ExpressionTable.TypeToAttributeTable.ContainsKey(expressionType)) { 76 var attr = ExpressionTable.TypeToAttributeTable[expressionType]; 77 78 for (var blockIdx = 0u; blockIdx < attr.ExecIn && currentIndex < vector.Length; blockIdx++) { 79 if (close != 0) { 80 close--; 81 expressions.Add(PushProgram.Empty); 82 } else { 83 currentIndex++; 84 var subProgram = FromPlush(vector, ref currentIndex, ref close, depth + 1, config, random); 85 var subExpression = subProgram.Count == 1 ? subProgram.Expressions[0] : subProgram; 86 expressions.Add(subExpression); 87 } 88 } 89 } 90 91 expressions.Add(expression); 92 93 if (close > 0 && depth > 0) { 94 close--; 95 break; 96 } 97 98 if (depth == 0) { 99 close = 0; 100 } 101 } 52 102 53 103 return new PushProgram(expressions);
Note: See TracChangeset
for help on using the changeset viewer.