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/Expressions/PushProgram.cs

    r14952 r15017  
    55namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
    66  using System.Diagnostics;
    7 
    8   using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    9   using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
    10 
     7  using Constants;
     8  using Data.Pool;
     9  using Extensions;
    1110  using Interpreter;
    12 
    13   public interface IMutablePushProgram {
    14     void SetAtIndex(int index, Expression expression);
    15   }
     11  using Persistence.Default.CompositeSerializers.Storable;
    1612
    1713  [Serializable]
    1814  [StorableClass]
    19   public sealed class PushProgram : Expression, IPooledObject, IMutablePushProgram {
     15  public sealed class PushProgram : Expression, IPooledObject {
    2016#if DEBUG
    21     private const int StringRepresentationCountBeforeAggregate = 50;
     17    private const int STRING_REPRESENTATION_COUNT_BEFORE_AGGREGATE = 10;
    2218#else
    23     private const int StringRepresentationCountBeforeAggregate = 10;
     19    private const int STRING_REPRESENTATION_COUNT_BEFORE_AGGREGATE = 1;
    2420#endif
    2521
     
    2723    public static readonly PushProgram Empty = new PushProgram();
    2824
    29     private const string Prefix = "(";
    30     private const string PrefixReduced = "[";
    31     private const string Postfix = ")";
    32     private const string PostfixReduced = "]";
     25    private const string Prefix = PushEnvironment.ProgramStartSymbolStr + " ";
     26    private const string Postfix = " " + PushEnvironment.ProgramEndSymbolStr;
     27    private const string PrefixReduced = "|";
     28    private const string PostfixReduced = "|";
    3329    private const string Delimiter = " ";
    3430
     
    112108    }
    113109
     110    [NonSerialized]
    114111    private string stringRepresentation;
    115112    public override string StringRepresentation
     
    117114      get
    118115      {
    119         if (stringRepresentation == null) stringRepresentation = BuildString();
    120         return stringRepresentation;
    121       }
    122     }
    123 
    124 
     116        return stringRepresentation ?? (stringRepresentation = BuildStringRepresentation());
     117      }
     118    }
     119
     120
     121    [NonSerialized]
    125122    private int? depth;
    126123    public int Depth
     
    133130    }
    134131
     132    [NonSerialized]
    135133    private int[] treeIndex;
    136134    private int[] TreeIndex
     
    138136      get
    139137      {
    140         if (treeIndex == null) treeIndex = BuildTreeIndex();
    141         return treeIndex;
    142       }
    143     }
    144 
     138        return treeIndex ?? (treeIndex = BuildTreeIndex());
     139      }
     140    }
     141
     142    [NonSerialized]
    145143    private int? hashCode;
    146144    public override int GetHashCode() {
    147       if (hashCode == null) hashCode = HashExpressions();
     145      if (hashCode == null) hashCode = expressions.HashCode();
    148146      return hashCode.Value;
    149147    }
    150148
    151     void IMutablePushProgram.SetAtIndex(int index, Expression expression) {
    152 
    153     }
    154 
    155149    public override bool Equals(object obj) {
     150      if (obj == null)
     151        return false;
     152
    156153      if (ReferenceEquals(this, obj))
    157154        return true;
     
    183180      return StringRepresentation;
    184181    }
     182
     183    [NonSerialized]
     184    private int? totalCount;
    185185
    186186    /// <summary>
     
    192192      get
    193193      {
    194         return IsEmpty
     194        if (totalCount == null) {
     195          totalCount = IsEmpty
    195196            ? 1
    196197            // + 1 because "this" is also counted
    197198            : TreeIndex[0] + 1;
     199        }
     200
     201        return totalCount.Value;
     202      }
     203    }
     204
     205    [NonSerialized]
     206    private int? totalExpressionCount;
     207    /// <summary>
     208    /// Returns the amount of none program expressions
     209    /// </summary>
     210    /// <returns></returns>
     211    public int TotalExpressionCount
     212    {
     213      get
     214      {
     215        if (totalExpressionCount == null) {
     216          totalExpressionCount = 0;
     217
     218          for (var i = 0; i < Count; i++) {
     219            var expression = expressions[i];
     220
     221            totalExpressionCount += expression.IsProgram
     222              ? ((PushProgram)expression).TotalExpressionCount
     223              : 1;
     224          }
     225        }
     226
     227        return totalExpressionCount.Value;
     228      }
     229    }
     230
     231    [NonSerialized]
     232    private int? branches;
     233    /// <summary>
     234    /// Returns the amount of branches in the whole tree. Even an empty program represents at least one branch
     235    /// </summary>
     236    /// <returns></returns>
     237    public int Branches
     238    {
     239      get
     240      {
     241        if (branches == null) {
     242          branches = 1;
     243
     244          for (var i = 0; i < Count; i++) {
     245            var expression = expressions[i];
     246
     247            if (!expression.IsProgram)
     248              continue;
     249
     250            var program = (PushProgram)expression;
     251            branches += program.Branches;
     252          }
     253        }
     254
     255        return branches.Value;
    198256      }
    199257    }
     
    247305
    248306    private int CalcDepth() {
    249       var maxDepth = 1;
     307      var maxDepth = 0;
    250308      for (var i = 0; i < Count; i++) {
    251309        var expression = expressions[i];
    252310        if (!expression.IsProgram) continue;
     311
    253312        var expandExpression = (PushProgram)expression;
    254 
    255313        if (expandExpression.Depth > maxDepth)
    256314          maxDepth = expandExpression.Depth;
     
    260318    }
    261319
    262     private string BuildString() {
     320    private string BuildStringRepresentation() {
    263321      // prevent too big string
    264       return TotalCount > StringRepresentationCountBeforeAggregate
     322      return Count > STRING_REPRESENTATION_COUNT_BEFORE_AGGREGATE
    265323        ? PrefixReduced + Count + PostfixReduced
    266         : Prefix + string.Join(Delimiter, expressions) + Postfix;
     324        : Prefix + string.Join(Delimiter, expressions.Reverse()) + Postfix;
    267325    }
    268326
    269327    private int[] BuildTreeIndex() {
    270       var local_treeIndex = new int[Count];
    271 
     328      var localTreeIndex = new int[Count];
    272329      var next = 1;
    273330
     
    275332        var subExpression = expressions[i];
    276333
    277         local_treeIndex[i] = next;
     334        localTreeIndex[i] = next;
    278335
    279336        if (subExpression.IsProgram) {
     
    284341      }
    285342
    286       return local_treeIndex;
    287     }
    288 
    289     private int HashExpressions() {
    290       var hash = 19 * 31 + typeof(PushProgram).GetHashCode();
    291 
    292       for (var i = 0; i < Count; i++) {
    293         hash = hash * 31 + expressions[i].GetHashCode();
    294       }
    295 
    296       return hash;
     343      return localTreeIndex;
    297344    }
    298345
Note: See TracChangeset for help on using the changeset viewer.