Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/11/17 12:42:31 (7 years ago)
Author:
pkimmesw
Message:

#2665 Merged ExecExpandExpression and PushProgram due to performance reasons, Tested managed object pooling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExecExpressions.cs

    r14744 r14745  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
    2   using System;
    3   using System.Collections.Generic;
    4   using System.Linq;
    52
    63  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
     
    85
    96  using Interpreter;
    10 
    11   public class ExecExpandExpression : StatefulExpression<PushProgram> {
    12     public static readonly ExecExpandExpression Empty = new ExecExpandExpression(PushProgram.Empty);
    13 
    14     public ExecExpandExpression(PushProgram program)
    15       : base(program) {
    16     }
    17 
    18     public ExecExpandExpression(params Expression[] state) : this(new PushProgram(state)) {
    19     }
    20 
    21     public ExecExpandExpression(IEnumerable<Expression> state) : this(state.ToArray()) {
    22       // TODO: lazy evaluation of ToArray()
    23     }
    24 
    25     public override string StringRepresentation { get { return this.State.ToString(); } }
    26 
    27     public ExecExpandExpression Copy() {
    28       // as the empty list is a static stateless expression, no copy required
    29       return ReferenceEquals(this, Empty) || this.State.IsEmpty
    30         ? Empty
    31         : new ExecExpandExpression(this.State.Copy());
    32     }
    33 
    34     public override bool Eval(IPushInterpreter interpreter) {
    35       interpreter.ExecStack.Push(this.State.Expressions);
    36       return true;
    37     }
    38 
    39     /// <summary>
    40     ///     Returns the element with the given index whereby the tree is indexed in depth first manner
    41     /// </summary>
    42     /// <param name="index">The index of the required element</param>
    43     /// <returns>The required element</returns>
    44     public Expression GetFromTree(int index) {
    45       return GetFromTree(index, (root, parent, child, childIndex, parentIndex) => child);
    46     }
    47 
    48     /// <returns>The required element</returns>
    49     public T GetFromTree<T>(int index,
    50         Func<ExecExpandExpression, ExecExpandExpression, Expression, int, int, T> resolver) {
    51       return GetFromTree(index, 0, null, resolver);
    52     }
    53 
    54     private T GetFromTree<T>(int index, int parentIndex, ExecExpandExpression parent,
    55         Func<ExecExpandExpression, ExecExpandExpression, Expression, int, int, T> resolver) {
    56       if (index == 0) return resolver(parent, parent, this, 0, parentIndex);
    57 
    58       var min = State.Expressions.Count - 1;
    59       var max = 0;
    60       var mid = (min + max) / 2;
    61 
    62       while ((max <= min) && (this.State.TreeIndex[mid] != index)) {
    63         if (this.State.TreeIndex[mid] > index) max = mid + 1;
    64         else min = mid - 1;
    65 
    66         mid = (min + max) / 2;
    67       }
    68 
    69       return this.State.TreeIndex[mid] == index
    70           ? resolver(parent, this, State.Expressions[mid], mid, parentIndex)
    71           : (State.Expressions[mid + 1] as ExecExpandExpression).GetFromTree(index - this.State.TreeIndex[mid + 1], mid + 1,
    72               this, resolver);
    73     }
    74   }
    757
    768  /// <summary>
     
    11042      var execYExpression = ExpressionTable.GetStatelessExpression<ExecYExpression>();
    11143
    112       var result = new ExecExpandExpression(top, execYExpression);
     44      var result = new PushProgram(top, execYExpression);
    11345
    11446      interpreter.ExecStack.SetTop(result);
     
    14981      var c = interpreter.ExecStack.Top;
    15082
    151       var newTop = new ExecExpandExpression(c, b);
     83      var newTop = new PushProgram(c, b);
    15284
    15385      interpreter.ExecStack.SetTop(newTop);
Note: See TracChangeset for help on using the changeset viewer.