Changeset 14730


Ignore:
Timestamp:
03/08/17 10:23:51 (5 months ago)
Author:
mkommend
Message:

#2665: Removed memory pressure from expressions.

Location:
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions
Files:
2 edited

Legend:

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

    r14727 r14730  
    1717    public readonly bool IsEmpty;
    1818
    19     private readonly Lazy<int> depth;
    20     private readonly Lazy<int[]> treeIndex;
    21     private readonly Lazy<int> hashCode;
    22     private readonly Lazy<string> stringRepresentation;
     19
     20    private string stringRepresentation;
     21    private string StringRepresentation {
     22      get {
     23        if (string.IsNullOrEmpty(stringRepresentation))
     24          stringRepresentation = BuildString();
     25        return stringRepresentation;
     26      }
     27    }
     28
     29    private int depth = -1;
     30    private int Depth {
     31      get {
     32        if (depth == -1) depth = CalcDepth();
     33        return depth;
     34      }
     35    }
     36
     37    private int[] treeIndex = null;
     38    public int[] TreeIndex {
     39      get {
     40        if (treeIndex == null) treeIndex = BuildTreeIndex();
     41        return treeIndex;
     42      }
     43    }
     44
     45    private int hashCode;
     46    private int HashCode {
     47      get {
     48        if (hashCode == default(int)) hashCode = HashExpressions();
     49        return hashCode;
     50      }
     51    }
    2352
    2453    public PushProgram(Expression[] expressions) {
    2554      this.Expressions = expressions;
    2655      this.IsEmpty = expressions.Length == 0;
    27 
    28       this.depth = new Lazy<int>(this.CalcDepth, true);
    29       this.treeIndex = new Lazy<int[]>(this.BuildTreeIndex, true);
    30       this.hashCode = new Lazy<int>(this.HashExpressions, true);
    31       this.stringRepresentation = new Lazy<string>(this.BuildString, true);
    3256    }
    3357
    34     public int Depth { get { return depth.Value; } }
    35     public int[] TreeIndex { get { return treeIndex.Value; } }
    3658
    3759    public override int GetHashCode() {
    38       return this.hashCode.Value;
     60      return HashCode;
    3961    }
    4062
    4163    public override string ToString() {
    42       return this.stringRepresentation.Value;
     64      return this.StringRepresentation;
    4365    }
    4466
     
    4769    /// </summary>
    4870    /// <returns></returns>
    49     public int TotalCount
    50     {
    51       get
    52       {
     71    public int TotalCount {
     72      get {
    5373        return this.IsEmpty
    5474            ? 1
    55             // + 1 because "this" is also counted
    56             : this.treeIndex.Value[0] + 1;
     75          // + 1 because "this" is also counted
     76            : TreeIndex[0] + 1;
    5777      }
    5878    }
     
    111131
    112132    private int[] BuildTreeIndex() {
    113       var TreeIndex = new int[this.Expressions.Count];
     133      var local_treeIndex = new int[this.Expressions.Count];
    114134
    115135      var next = 1;
     
    118138        var subExpression = this.Expressions[i];
    119139
    120         TreeIndex[i] = next;
     140        local_treeIndex[i] = next;
    121141
    122142        if (subExpression.CanExpand) {
     
    127147      }
    128148
    129       return TreeIndex;
     149      return local_treeIndex;
    130150    }
    131151
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/StatefullExpression.cs

    r14727 r14730  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
    2   using System;
    32
    43  public abstract class StatefullExpression<T> : Expression {
    54
    65    public readonly T State;
    7     private readonly Lazy<int> hashCode;
     6
     7    private int hashCode;
     8    private int HashCode {
     9      get {
     10        if (hashCode == default(int)) hashCode = CalcHashCode();
     11        return hashCode;
     12      }
     13    }
    814
    915    protected StatefullExpression(T state) {
    1016      this.State = state;
    11       this.hashCode = new Lazy<int>(this.CalcHashCode, true);
    1217    }
    1318
     
    3035
    3136    public override int GetHashCode() {
    32       return this.hashCode.Value;
     37      return HashCode;
    3338    }
    3439  }
Note: See TracChangeset for help on using the changeset viewer.