Changeset 15017 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PushProgram.cs
- Timestamp:
- 06/01/17 09:28:34 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PushProgram.cs
r14952 r15017 5 5 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { 6 6 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; 11 10 using Interpreter; 12 13 public interface IMutablePushProgram { 14 void SetAtIndex(int index, Expression expression); 15 } 11 using Persistence.Default.CompositeSerializers.Storable; 16 12 17 13 [Serializable] 18 14 [StorableClass] 19 public sealed class PushProgram : Expression, IPooledObject , IMutablePushProgram{15 public sealed class PushProgram : Expression, IPooledObject { 20 16 #if DEBUG 21 private const int S tringRepresentationCountBeforeAggregate = 50;17 private const int STRING_REPRESENTATION_COUNT_BEFORE_AGGREGATE = 10; 22 18 #else 23 private const int S tringRepresentationCountBeforeAggregate = 10;19 private const int STRING_REPRESENTATION_COUNT_BEFORE_AGGREGATE = 1; 24 20 #endif 25 21 … … 27 23 public static readonly PushProgram Empty = new PushProgram(); 28 24 29 private const string Prefix = "(";30 private const string P refixReduced = "[";31 private const string P ostfix = ")";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 = "|"; 33 29 private const string Delimiter = " "; 34 30 … … 112 108 } 113 109 110 [NonSerialized] 114 111 private string stringRepresentation; 115 112 public override string StringRepresentation … … 117 114 get 118 115 { 119 if (stringRepresentation == null) stringRepresentation = BuildString();120 return stringRepresentation;121 122 } 123 124 116 return stringRepresentation ?? (stringRepresentation = BuildStringRepresentation()); 117 } 118 } 119 120 121 [NonSerialized] 125 122 private int? depth; 126 123 public int Depth … … 133 130 } 134 131 132 [NonSerialized] 135 133 private int[] treeIndex; 136 134 private int[] TreeIndex … … 138 136 get 139 137 { 140 if (treeIndex == null) treeIndex = BuildTreeIndex();141 return treeIndex;142 143 } 144 138 return treeIndex ?? (treeIndex = BuildTreeIndex()); 139 } 140 } 141 142 [NonSerialized] 145 143 private int? hashCode; 146 144 public override int GetHashCode() { 147 if (hashCode == null) hashCode = HashExpressions();145 if (hashCode == null) hashCode = expressions.HashCode(); 148 146 return hashCode.Value; 149 147 } 150 148 151 void IMutablePushProgram.SetAtIndex(int index, Expression expression) {152 153 }154 155 149 public override bool Equals(object obj) { 150 if (obj == null) 151 return false; 152 156 153 if (ReferenceEquals(this, obj)) 157 154 return true; … … 183 180 return StringRepresentation; 184 181 } 182 183 [NonSerialized] 184 private int? totalCount; 185 185 186 186 /// <summary> … … 192 192 get 193 193 { 194 return IsEmpty 194 if (totalCount == null) { 195 totalCount = IsEmpty 195 196 ? 1 196 197 // + 1 because "this" is also counted 197 198 : 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; 198 256 } 199 257 } … … 247 305 248 306 private int CalcDepth() { 249 var maxDepth = 1;307 var maxDepth = 0; 250 308 for (var i = 0; i < Count; i++) { 251 309 var expression = expressions[i]; 252 310 if (!expression.IsProgram) continue; 311 253 312 var expandExpression = (PushProgram)expression; 254 255 313 if (expandExpression.Depth > maxDepth) 256 314 maxDepth = expandExpression.Depth; … … 260 318 } 261 319 262 private string BuildString () {320 private string BuildStringRepresentation() { 263 321 // prevent too big string 264 return TotalCount > StringRepresentationCountBeforeAggregate322 return Count > STRING_REPRESENTATION_COUNT_BEFORE_AGGREGATE 265 323 ? PrefixReduced + Count + PostfixReduced 266 : Prefix + string.Join(Delimiter, expressions ) + Postfix;324 : Prefix + string.Join(Delimiter, expressions.Reverse()) + Postfix; 267 325 } 268 326 269 327 private int[] BuildTreeIndex() { 270 var local_treeIndex = new int[Count]; 271 328 var localTreeIndex = new int[Count]; 272 329 var next = 1; 273 330 … … 275 332 var subExpression = expressions[i]; 276 333 277 local _treeIndex[i] = next;334 localTreeIndex[i] = next; 278 335 279 336 if (subExpression.IsProgram) { … … 284 341 } 285 342 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; 297 344 } 298 345
Note: See TracChangeset
for help on using the changeset viewer.