Changeset 14392 for branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/CodeGenerator.cs
- Timestamp:
- 11/15/16 21:10:17 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/CodeGenerator.cs
r14328 r14392 1 using System.Collections.Generic; 1 using System; 2 using System.Collections.Generic; 2 3 using System.Linq; 3 4 using HeuristicLab.Algorithms.PushGP.Expressions; 5 using HeuristicLab.Algorithms.PushGP.Interpreter; 6 using HeuristicLab.Core; 4 7 using HeuristicLab.Random; 5 8 6 9 namespace HeuristicLab.Algorithms.PushGP.Generators 7 10 { 8 public static class CodeGenerator 11 /// <summary> 12 /// The following is the standard Push random code generation algorithm, which is used for the CODE.RAND instruction. 13 /// It may also be useful for the initialization of programs in evolutionary computation systems, and it is used for this purpose in PushGP. 14 /// It produces a uniform distribution of sizes and what seems to be a reasonable distribution of shapes, in a reasonable amount of time. 15 /// </summary> 16 /// <see cref="http://faculty.hampshire.edu/lspector/push3-description.html#RandomCode"/> 17 public class CodeGenerator 9 18 { 10 private static FastRandom random = new FastRandom(); 11 public static IEnumerable<Expression> RandomCode(int maxPoints) 19 private static IRandom random = new FastRandom(); 20 private readonly IInterpreter interpreter; 21 22 public CodeGenerator(IInterpreter interpreter) 12 23 { 13 var actualPoints = random.Next(1, maxPoints); 14 15 return RandomCodeWithSize(actualPoints).ToArray(); 24 this.interpreter = interpreter; 16 25 } 17 26 18 private static IEnumerable<Expression> RandomCodeWithSize(int points) 27 /// <summary> 28 /// Generates a random program with a given max size 29 /// </summary> 30 /// <param name="maxPoints"></param> 31 /// <returns>Returns an ExpandExpression if maxPoints smaller or equal to Configuration.MaxProgramPoints and Noop otherwise</returns> 32 public Expression RandomProgram(int maxPoints) 33 { 34 return ExecExpandExpression.TryCreate(interpreter.CodeGenerator.RandomCode(maxPoints).ToArray(), interpreter.Configuration); 35 } 36 37 public IEnumerable<Expression> RandomCode(int maxPoints) 38 { 39 var actualPoints = random.Next(1, Math.Max(1, maxPoints)); 40 41 return RandomCodeWithSize(actualPoints); 42 } 43 44 private IEnumerable<Expression> RandomCodeWithSize(int points) 19 45 { 20 46 if (points == 1) 21 47 { 22 // TODO: If this is an "ephemeral random constant"???? then return a randomly - chosen value of the appropriate type; 23 var opCode = (OpCode)random.Next(OpCodeExtensions.Min, OpCodeExtensions.Max); 24 25 return new[] { ExpressionFactory.Create(opCode) }; 48 var index = random.Next(0, ExpressionTable.Count + this.interpreter.CustomExpressions.Count - 2); 49 return new[] { this.CreateExpression(index) }; 26 50 } 27 51 else 28 52 { 29 53 var sizesThisLevel = Decompose(points - 1, points - 1); 30 31 54 return sizesThisLevel.SelectMany(size => RandomCodeWithSize(size)).Shuffle(random); 32 55 } 33 56 } 34 57 35 private staticIEnumerable<int> Decompose(int number, int maxParts)58 private IEnumerable<int> Decompose(int number, int maxParts) 36 59 { 37 60 if (number == 1 || maxParts == 1) … … 42 65 { 43 66 var thisPart = random.Next(1, number - 1); 44 45 67 return new[] { thisPart }.Concat(Decompose(number - thisPart, maxParts - 1)); 46 68 } 47 69 } 70 71 private Expression CreateExpression(int index) 72 { 73 return (index >= 0 && index < ExpressionTable.Count) 74 ? ExpressionTable.GetExpression(index) 75 : new NameDefineXExecExpression(this.interpreter.CustomExpressions.ElementAt(index - (ExpressionTable.Count - 1)).Key); 76 } 48 77 } 49 78 }
Note: See TracChangeset
for help on using the changeset viewer.