Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/15/16 21:10:17 (8 years ago)
Author:
pkimmesw
Message:

#2665 Full Push 3.0 instruction set and tests; Added first benchmark test (count odds) for random walk tests;

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;
     1using System;
     2using System.Collections.Generic;
    23using System.Linq;
    34using HeuristicLab.Algorithms.PushGP.Expressions;
     5using HeuristicLab.Algorithms.PushGP.Interpreter;
     6using HeuristicLab.Core;
    47using HeuristicLab.Random;
    58
    69namespace HeuristicLab.Algorithms.PushGP.Generators
    710{
    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
    918    {
    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)
    1223        {
    13             var actualPoints = random.Next(1, maxPoints);
    14 
    15             return RandomCodeWithSize(actualPoints).ToArray();
     24            this.interpreter = interpreter;
    1625        }
    1726
    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)
    1945        {
    2046            if (points == 1)
    2147            {
    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) };
    2650            }
    2751            else
    2852            {
    2953                var sizesThisLevel = Decompose(points - 1, points - 1);
    30 
    3154                return sizesThisLevel.SelectMany(size => RandomCodeWithSize(size)).Shuffle(random);
    3255            }
    3356        }
    3457
    35         private static IEnumerable<int> Decompose(int number, int maxParts)
     58        private IEnumerable<int> Decompose(int number, int maxParts)
    3659        {
    3760            if (number == 1 || maxParts == 1)
     
    4265            {
    4366                var thisPart = random.Next(1, number - 1);
    44 
    4567                return new[] { thisPart }.Concat(Decompose(number - thisPart, maxParts - 1));
    4668            }
    4769        }
     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        }
    4877    }
    4978}
Note: See TracChangeset for help on using the changeset viewer.