Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/11/17 20:07:13 (8 years ago)
Author:
pkimmesw
Message:

#2665 PooledPushProgram reduces memory usage and increases performance

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Simplifier/RandomSimplifier.cs

    r14745 r14746  
    1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Simplifier {
    2   using System;
    3   using HeuristicLab.Core;
    4   using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
     1//namespace HeuristicLab.Problems.ProgramSynthesis.Push.Simplifier {
     2//  using System;
     3//  using HeuristicLab.Core;
     4//  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
    55
    6   public class RandomSimplifier : ISimplifier {
    7     public int Trys { get; set; }
     6//  public class RandomSimplifier : ISimplifier {
     7//    public int Trys { get; set; }
    88
    9     public PushProgram Simplify(PushProgram program, IRandom random, Predicate<PushProgram> isBetter) {
     9//    public PushProgram Simplify(PushProgram program, IRandom random, Predicate<PushProgram> isBetter) {
    1010
    11       if (program.TotalCount == 1) {
    12         return isBetter(PushProgram.Empty) ? PushProgram.Empty : program;
    13       }
     11//      if (program.TotalCount == 1) {
     12//        return isBetter(PushProgram.Empty) ? PushProgram.Empty : program;
     13//      }
    1414
    15       var copy = program.Copy();
    16       var maxTries = Math.Min(Trys, program.TotalCount - 2);
    17       var successfulRemoves = 0;
     15//      var copy = program.Copy();
     16//      var maxTries = Math.Min(Trys, program.TotalCount - 2);
     17//      var successfulRemoves = 0;
    1818
    19       for (var i = 0; i < maxTries; i++) {
    20         var rndIndex = random.Next(1, program.TotalCount - 1 - successfulRemoves);
    21         var node = copy.GetFromTree(
    22           rndIndex,
    23           (super, parent, child, childIndex, parentIndex) => new {
    24             Super = super,
    25             Parent = parent,
    26             ChildIndex = childIndex,
    27             ParentIndex = parentIndex
    28           });
     19//      for (var i = 0; i < maxTries; i++) {
     20//        var rndIndex = random.Next(1, program.TotalCount - 1 - successfulRemoves);
     21//        var node = copy.GetFromTree(
     22//          rndIndex,
     23//          (super, parent, child, childIndex, parentIndex) => new {
     24//            Super = super,
     25//            Parent = parent,
     26//            ChildIndex = childIndex,
     27//            ParentIndex = parentIndex
     28//          });
    2929
    30         var oldParentExpressions = node.Parent.State;
    31         var newParentExpressions = RemoveAt(oldParentExpressions, node.ChildIndex);
    32         var newParent = new PushProgram(newParentExpressions);
     30//        var oldParentExpressions = node.Parent.State;
     31//        var newParentExpressions = RemoveAt(oldParentExpressions, node.ChildIndex);
     32//        var newParent = new PushProgram(newParentExpressions);
    3333
    34         var superExpressions = node.Super == null ? copy.State : node.Super.State;
    35         superExpressions[node.ParentIndex] = newParent;
     34//        var superExpressions = node.Super == null ? copy.State : node.Super.State;
     35//        superExpressions[node.ParentIndex] = newParent;
    3636
    37         if (isBetter(copy)) {
    38           successfulRemoves++;
    39         } else {
    40           superExpressions[node.ParentIndex] = node.Parent;
    41         }
    42       }
     37//        if (isBetter(copy)) {
     38//          successfulRemoves++;
     39//        } else {
     40//          superExpressions[node.ParentIndex] = node.Parent;
     41//        }
     42//      }
    4343
    44       return copy;
    45     }
     44//      return copy;
     45//    }
    4646
    47     private static T[] RemoveAt<T>(T[] source, int index) {
    48       var dest = new T[source.Length - 1];
    49       if (index > 0)
    50         Array.Copy(source, 0, dest, 0, index);
     47//    private static T[] RemoveAt<T>(T[] source, int index) {
     48//      var dest = new T[source.Length - 1];
     49//      if (index > 0)
     50//        Array.Copy(source, 0, dest, 0, index);
    5151
    52       if (index < source.Length - 1)
    53         Array.Copy(source, index + 1, dest, index, source.Length - index - 1);
     52//      if (index < source.Length - 1)
     53//        Array.Copy(source, index + 1, dest, index, source.Length - index - 1);
    5454
    55       return dest;
    56     }
    57   }
    58 }
     55//      return dest;
     56//    }
     57//  }
     58//}
Note: See TracChangeset for help on using the changeset viewer.