Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/01/17 09:28:34 (7 years ago)
Author:
pkimmesw
Message:

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/IndividualMapper.cs

    r14897 r15017  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem {
     2  using System.Collections.Generic;
    23
    34  using HeuristicLab.Core;
     
    78  using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
    89  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
     10  using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
    911  using HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator;
    1012  using HeuristicLab.Random;
     
    1719    public static PushProgram ToPushProgram(this Individual individual, IReadOnlyPushConfiguration config) {
    1820      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);
    1925    }
    2026
     
    3541    }
    3642
    37     private static int GetSeed(this IntegerVector vector) {
    38       var seed = 0;
     43    public static int GetSeed(this IntegerVector vector) {
     44      var seed = 17;
    3945      for (var i = 0; i < vector.Length; i++)
    40         seed += (i + 1) * vector[i];
     46        seed = seed * 23 + vector[i];
    4147      return seed;
    4248    }
    4349
    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;
    4953
    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      }
    52102
    53103      return new PushProgram(expressions);
Note: See TracChangeset for help on using the changeset viewer.