Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/20/16 22:57:11 (8 years ago)
Author:
pkimmesw
Message:

#2665 Added Problem.ProgramSynthesis Project, Fixed Expression Issues, Fixed Code Generation

Location:
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/BooleanGenerator.cs

    r14392 r14513  
    1 using HeuristicLab.Core;
    2 using HeuristicLab.Random;
     1namespace HeuristicLab.Algorithms.PushGP.Generators {
     2  using System.Threading;
    33
    4 namespace HeuristicLab.Algorithms.PushGP.Generators
    5 {
    6     public static class BooleanGenerator
    7     {
    8         private static IRandom rand = new FastRandom();
     4  using HeuristicLab.Algorithms.PushGP.Data.Random;
     5  using HeuristicLab.Core;
    96
    10         public static bool RandomBoolean()
    11         {
    12             return rand.NextDouble() > 0.5 ? true : false;
    13         }
     7  public static class BooleanGenerator {
     8    private static readonly ThreadLocal<IRandom> rand = RandomFactory.GetRandom();
     9
     10    public static bool RandomBoolean() {
     11      return rand.Value.NextDouble() > 0.5;
    1412    }
     13  }
    1514}
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/CodeGenerator.cs

    r14398 r14513  
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using HeuristicLab.Algorithms.PushGP.Expressions;
    5 using HeuristicLab.Algorithms.PushGP.Interpreter;
    6 using HeuristicLab.Core;
    7 using HeuristicLab.Random;
     1namespace HeuristicLab.Algorithms.PushGP.Generators {
     2  using System;
     3  using System.Collections.Generic;
     4  using System.Linq;
     5  using System.Threading;
    86
    9 namespace HeuristicLab.Algorithms.PushGP.Generators
    10 {
     7  using HeuristicLab.Algorithms.PushGP.Data.Random;
     8  using HeuristicLab.Algorithms.PushGP.Expressions;
     9  using HeuristicLab.Core;
     10  using HeuristicLab.Random;
     11
     12  /// <summary>
     13  ///     The following is the standard Push random code generation algorithm, which is used for the CODE.RAND instruction.
     14  ///     It may also be useful for the initialization of programs in evolutionary computation systems, and it is used for
     15  ///     this purpose in PushGP.
     16  ///     It produces a uniform distribution of sizes and what seems to be a reasonable distribution of shapes, in a
     17  ///     reasonable amount of time.
     18  /// </summary>
     19  /// <see cref="http://faculty.hampshire.edu/lspector/push3-description.html#RandomCode" />
     20  public static class CodeGenerator {
     21    private static readonly ThreadLocal<IRandom> random = RandomFactory.GetRandom();
     22
    1123    /// <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.
     24    ///     Generates a random program with a given max size
    1525    /// </summary>
    16     /// <see cref="http://faculty.hampshire.edu/lspector/push3-description.html#RandomCode"/>
    17     public class CodeGenerator
    18     {
    19         private static IRandom random = new FastRandom();
    20         private readonly IInterpreter interpreter;
     26    /// <param name="maxPoints"></param>
     27    /// <returns>Returns an ExpandExpression if maxPoints smaller or equal to Configuration.MaxProgramPoints and Noop otherwise</returns>
     28    public static ExecExpandExpression RandomProgram(int maxPoints, IDictionary<string, Expression> customExpressions = null) {
     29      var code = RandomCode(maxPoints, customExpressions).ToArray();
    2130
    22         public CodeGenerator(IInterpreter interpreter)
    23         {
    24             this.interpreter = interpreter;
    25         }
     31      return new ExecExpandExpression(code);
     32    }
    2633
    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         }
     34    public static IEnumerable<Expression> RandomCode(int maxPoints, IDictionary<string, Expression> customExpressions = null) {
     35      var actualPoints = random.Value.Next(1, Math.Max(1, maxPoints));
    3636
    37         public IEnumerable<Expression> RandomCode(int maxPoints)
    38         {
    39             var actualPoints = random.Next(1, Math.Max(1, maxPoints));
     37      return RandomCodeWithSize(actualPoints, customExpressions);
     38    }
    4039
    41             return RandomCodeWithSize(actualPoints);
    42         }
     40    private static IEnumerable<Expression> RandomCodeWithSize(
     41      int points,
     42      IDictionary<string, Expression> customExpressions = null) {
     43      if (points == 1) {
     44        var customCount = customExpressions == null ? 0 : customExpressions.Count - 1;
     45        var index = random.Value.Next(0, ExpressionTable.Count + customCount - 1);
     46        return new[] { CreateExpression(index, customExpressions) };
     47      }
    4348
    44         private IEnumerable<Expression> RandomCodeWithSize(int points)
    45         {
    46             if (points == 1)
    47             {
    48                 var index = random.Next(0, ExpressionTable.Count + interpreter.CustomExpressions.Count - 2);
    49                 return new[] { CreateExpression(index) };
    50             }
    51             else
    52             {
    53                 var sizesThisLevel = Decompose(points - 1, points - 1);
    54                 return sizesThisLevel.SelectMany(size => RandomCodeWithSize(size)).Shuffle(random);
    55             }
    56         }
     49      var sizesThisLevel = Decompose(points - 1, points - 1);
     50      return sizesThisLevel.SelectMany(size => RandomCodeWithSize(size, customExpressions)).Shuffle(random.Value);
     51    }
    5752
    58         private IEnumerable<int> Decompose(int number, int maxParts)
    59         {
    60             if (number == 1 || maxParts == 1)
    61             {
    62                 return new[] { number };
    63             }
    64             else
    65             {
    66                 var thisPart = random.Next(1, number - 1);
    67                 return new[] { thisPart }.Concat(Decompose(number - thisPart, maxParts - 1));
    68             }
    69         }
     53    private static IEnumerable<int> Decompose(int number, int maxParts) {
     54      if ((number == 1) || (maxParts == 1)) return new[] { number };
     55      var thisPart = random.Value.Next(1, number - 1);
     56      return new[] { thisPart }.Concat(Decompose(number - thisPart, maxParts - 1));
     57    }
    7058
    71         private Expression CreateExpression(int index)
    72         {
    73             return (index >= 0 && index < ExpressionTable.Count)
    74                 ? ExpressionTable.GetExpression(index)
    75                 : new NameDefineXExecExpression(interpreter.CustomExpressions.ElementAt(index - (ExpressionTable.Count - 1)).Key);
    76         }
     59    private static Expression CreateExpression(int index, IDictionary<string, Expression> customExpressions = null) {
     60      if ((index >= 0) && (index < ExpressionTable.Count)) return ExpressionTable.GetExpression(index);
     61
     62      if (customExpressions == null) throw new IndexOutOfRangeException();
     63      return new NameDefineXExecExpression(customExpressions.ElementAt(index - (ExpressionTable.Count - 1)).Key);
    7764    }
     65  }
    7866}
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/FloatGenerator.cs

    r14392 r14513  
    1 using HeuristicLab.Core;
    2 using HeuristicLab.Random;
     1namespace HeuristicLab.Algorithms.PushGP.Generators {
     2  using System.Threading;
    33
    4 namespace HeuristicLab.Algorithms.PushGP.Generators
    5 {
    6     public class FloatGenerator
    7     {
    8         private static IRandom rand = new FastRandom();
     4  using HeuristicLab.Algorithms.PushGP.Data.Random;
     5  using HeuristicLab.Core;
    96
    10         public static double RandomFloat(double min = double.MinValue, double max = double.MaxValue)
    11         {
    12             return rand.NextDouble() * (max - min) + min;
    13         }
     7  public class FloatGenerator {
     8    private static readonly ThreadLocal<IRandom> rand = RandomFactory.GetRandom();
     9
     10    public static double RandomFloat(double min = double.MinValue, double max = double.MaxValue) {
     11      return rand.Value.NextDouble() * (max - min) + min;
    1412    }
     13  }
    1514}
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/IntegerGenerator.cs

    r14392 r14513  
    1 using HeuristicLab.Core;
    2 using HeuristicLab.Random;
     1namespace HeuristicLab.Algorithms.PushGP.Generators {
     2  using System.Threading;
    33
    4 namespace HeuristicLab.Algorithms.PushGP.Generators
    5 {
    6     public static class IntegerGenerator
    7     {
    8         private static IRandom rand = new FastRandom();
     4  using HeuristicLab.Algorithms.PushGP.Data.Random;
     5  using HeuristicLab.Core;
    96
    10         public static long RandomInteger(int min = int.MinValue, int max = int.MaxValue)
    11         {
    12             return rand.Next(min, max);
    13         }
     7  public static class IntegerGenerator {
     8    private static readonly ThreadLocal<IRandom> rand = RandomFactory.GetRandom();
     9
     10    public static long RandomInteger(int min = int.MinValue, int max = int.MaxValue) {
     11      return rand.Value.Next(min, max);
    1412    }
     13  }
    1514}
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/NameGenerator.cs

    r14392 r14513  
    1 using System;
    2 using HeuristicLab.Core;
    3 using HeuristicLab.Random;
     1namespace HeuristicLab.Algorithms.PushGP.Generators {
     2  using System;
     3  using System.Threading;
    44
    5 namespace HeuristicLab.Algorithms.PushGP.Generators
    6 {
    7     public class NameGenerator
    8     {
    9         public const int DefaultRandomNameLength = 10;
    10         private static IRandom rand = new FastRandom();
    11         private static char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
     5  using HeuristicLab.Algorithms.PushGP.Data.Random;
     6  using HeuristicLab.Core;
    127
    13         private int index = 0;
     8  public class NameGenerator {
     9    public const int DefaultRandomNameLength = 10;
    1410
    15         public string GetNextString()
    16         {
    17             var length = index < chars.Length ? 1 : (int)(Math.Log(index, chars.Length)) + 1;
    18             var result = new char[length];
    19             var tmp = index;
    20             index++;
     11    private static readonly ThreadLocal<IRandom> rand = RandomFactory.GetRandom();
    2112
    22             for (var i = length; i > 0; i--)
    23             {
    24                 var a = (int)Math.Pow(chars.Length, i - 1);
    25                 var b = tmp / a;
     13    private static readonly char[] chars =
     14      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
    2615
    27                 result[length - i] = chars[b];
     16    private int index;
    2817
    29                 tmp -= a * b;
    30             }
     18    public string GetNextString() {
     19      var length = this.index < chars.Length ? 1 : (int)Math.Log(this.index, chars.Length) + 1;
     20      var result = new char[length];
     21      var tmp = this.index;
     22      this.index++;
    3123
    32             return new string(result);
    33         }
     24      for (var i = length; i > 0; i--) {
     25        var a = (int)Math.Pow(chars.Length, i - 1);
     26        var b = tmp / a;
    3427
    35         public static string RandomName(int length = DefaultRandomNameLength)
    36         {
    37             var name = new char[length];
     28        result[length - i] = chars[b];
    3829
    39             for (var i = 0; i < length; i++)
    40             {
    41                 name[i] = chars[rand.Next(chars.Length)];
    42             }
     30        tmp -= a * b;
     31      }
    4332
    44             return new string(name);
    45         }
     33      return new string(result);
    4634    }
     35
     36    public static string RandomName(int length = DefaultRandomNameLength) {
     37      var name = new char[length];
     38
     39      for (var i = 0; i < length; i++) name[i] = chars[rand.Value.Next(chars.Length)];
     40
     41      return new string(name);
     42    }
     43  }
    4744}
Note: See TracChangeset for help on using the changeset viewer.