- Timestamp:
- 11/16/16 23:14:01 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP
- Files:
-
- 5 added
- 2 deleted
- 33 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/ExpressionTable.cs
r14392 r14398 8 8 public static class ExpressionTable 9 9 { 10 private static Dictionary<string, Expression> expressionTable;10 private static Dictionary<string, Expression> statelessExpressionTable; 11 11 private static Dictionary<string, Func<Expression>> statefullExpressionFactory; 12 12 … … 15 15 static ExpressionTable() 16 16 { 17 expressionTable = GetExperssionTable(e => e.ToString());18 statefullExpressionFactory = GetStatefullExpressionFactory(e => e. ToString());17 statelessExpressionTable = GetStatelessExperssionTable(e => e.StringRepresentation); 18 statefullExpressionFactory = GetStatefullExpressionFactory(e => e.StringRepresentation); 19 19 20 Count = expressionTable.Count + statefullExpressionFactory.Count;20 Count = statelessExpressionTable.Count + statefullExpressionFactory.Count; 21 21 } 22 22 23 private static Dictionary<string, Expression> GetExperssionTable(Func<Expression, string> keyFunc) 24 { 25 return GetExperssionTable(e => new KeyValuePair<string, Expression>(keyFunc(e), e)); 26 } 27 28 private static Dictionary<string, Expression> GetExperssionTable(Func<Expression, KeyValuePair<string, Expression>> pairFunc) 23 private static Dictionary<string, Expression> GetStatelessExperssionTable(Func<Expression, string> keyFunc) 29 24 { 30 25 var dictionary = new Dictionary<string, Expression>(); 31 var expressionTypes = GetExpressionTypes( filterStatefull: false);26 var expressionTypes = GetExpressionTypes(typeof(StatelessExpression)); 32 27 33 28 foreach (var type in expressionTypes) 34 29 { 35 var expression = (Expression)Activator.CreateInstance(type);36 var pair = pairFunc(expression);30 var expression = Activator.CreateInstance(type) as Expression; 31 var key = keyFunc(expression); 37 32 38 dictionary.Add(pair.Key, pair.Value); 33 try 34 { 35 dictionary.Add(key, expression); 36 } catch(Exception e) 37 { 38 Console.WriteLine(e); 39 } 39 40 } 40 41 … … 45 46 { 46 47 var dictionary = new Dictionary<string, Func<Expression>>(); 47 var expressionTypes = GetExpressionTypes( filterStatefull: true);48 var expressionTypes = GetExpressionTypes(typeof(StatefullExpression)); 48 49 49 50 foreach (var type in expressionTypes) … … 58 59 var key = keyFunc(expression); 59 60 60 dictionary.Add(key, creator); 61 try 62 { 63 dictionary.Add(key, creator); 64 } 65 catch (Exception e) 66 { 67 Console.WriteLine(e); 68 } 61 69 } 62 70 … … 64 72 } 65 73 66 private static IEnumerable<Type> GetExpressionTypes( bool filterStatefull)74 private static IEnumerable<Type> GetExpressionTypes(Type baseType) 67 75 { 68 76 return from domainAssembly in AppDomain.CurrentDomain.GetAssemblies() 69 77 from assemblyType in domainAssembly.GetTypes() 70 let type = typeof(Expression)71 78 where !assemblyType.IsAbstract && 72 assemblyType.IsSubclassOf(type) && 73 Attribute.IsDefined(assemblyType, typeof(StatefullExpressionAttribute)) == filterStatefull && 79 assemblyType.IsSubclassOf(baseType) && 74 80 assemblyType.GetConstructor(Type.EmptyTypes) != null 75 81 select assemblyType; 76 82 } 77 83 78 public static int StatelessCount { get { return expressionTable.Count; } }84 public static int StatelessCount { get { return statelessExpressionTable.Count; } } 79 85 80 86 public static int StatefullCount { get { return statefullExpressionFactory.Count; } } … … 83 89 { 84 90 return index < StatelessCount 85 ? expressionTable.ElementAt(index).Value91 ? statelessExpressionTable.ElementAt(index).Value 86 92 : statefullExpressionFactory.ElementAt(index - StatelessCount).Value(); 87 93 } … … 102 108 { 103 109 Expression expression; 104 if ( expressionTable.TryGetValue(symbol, out expression)) return expression;110 if (statelessExpressionTable.TryGetValue(symbol, out expression)) return expression; 105 111 else throw new NotSupportedException("Expression not supported: " + symbol); 106 112 } … … 108 114 public static bool TryGetStatelessExpression(string name, out Expression expression) 109 115 { 110 return expressionTable.TryGetValue(name, out expression);116 return statelessExpressionTable.TryGetValue(name, out expression); 111 117 } 112 118 -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/BooleanExpressions.cs
r14392 r14398 8 8 public class BooleanAndExpression : PushResultExpression<bool> 9 9 { 10 public BooleanAndExpression() : base("BOOLEAN.AND") { } 10 protected override string InitStringRepresentation() 11 { 12 return "BOOLEAN.AND"; 13 } 11 14 12 15 public override void Eval(IInterpreter interpreter) 13 16 { 14 this.Eval(interpreter.BooleanStack, 2, values => values[0] && values[1]);17 Eval(interpreter.BooleanStack, 2, values => values[0] && values[1]); 15 18 } 16 19 } … … 21 24 public class BooleanOrExpression : PushResultExpression<bool> 22 25 { 23 public BooleanOrExpression() : base("BOOLEAN.OR") { } 26 protected override string InitStringRepresentation() 27 { 28 return "BOOLEAN.OR"; 29 } 24 30 25 31 public override void Eval(IInterpreter interpreter) 26 32 { 27 this.Eval(interpreter.BooleanStack, 2, values => values[0] || values[1]);33 Eval(interpreter.BooleanStack, 2, values => values[0] || values[1]); 28 34 } 29 35 } … … 34 40 public class BooleanNotExpression : PushResultExpression<bool> 35 41 { 36 p ublic BooleanNotExpression() : base("BOOLEAN.NOT") {}42 protected override string InitStringRepresentation() { return "BOOLEAN.NOT"; } 37 43 38 44 public override void Eval(IInterpreter interpreter) 39 45 { 40 this.Eval(interpreter.BooleanStack, 1, values => !values[0]);46 Eval(interpreter.BooleanStack, 1, values => !values[0]); 41 47 } 42 48 } … … 45 51 /// Pushes FALSE if the top FLOAT is 0.0, or TRUE otherwise. 46 52 /// </summary> 47 public class BooleanFromFloatExpression : Expression53 public class BooleanFromFloatExpression : StatelessExpression 48 54 { 49 p ublic BooleanFromFloatExpression() : base("BOOLEAN.FROMFLOAT") {}55 protected override string InitStringRepresentation() { return "BOOLEAN.FROMFLOAT"; } 50 56 51 57 public override void Eval(IInterpreter interpreter) … … 63 69 /// Pushes FALSE if the top INTEGER is 0, or TRUE otherwise. 64 70 /// </summary> 65 public class BooleanFromIntegerExpression : Expression71 public class BooleanFromIntegerExpression : StatelessExpression 66 72 { 67 p ublic BooleanFromIntegerExpression() : base("BOOLEAN.FROMINTEGER") {}73 protected override string InitStringRepresentation() { return "BOOLEAN.FROMINTEGER"; } 68 74 69 75 public override void Eval(IInterpreter interpreter) -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/CodeExpressions.cs
r14392 r14398 17 17 /// manipulates the stack then this final pop may end up popping something else. 18 18 /// </summary> 19 public class CodeDoExpression : Expression 20 { 21 public CodeDoExpression() : base("CODE.DO") { } 19 public class CodeDoExpression : StatelessExpression 20 { 21 protected override string InitStringRepresentation() 22 { 23 return "CODE.DO"; 24 } 22 25 23 26 public override void Eval(IInterpreter interpreter) … … 35 38 /// Like CODE.DO but pops the stack before, rather than after, the recursive execution 36 39 /// </summary> 37 public class CodeDoXExpression : Expression 38 { 39 public CodeDoXExpression() : base("CODE.DO*") { } 40 public class CodeDoXExpression : StatelessExpression 41 { 42 protected override string InitStringRepresentation() 43 { 44 return "CODE.DO*"; 45 } 40 46 41 47 public override void Eval(IInterpreter interpreter) … … 53 59 /// Does nothing. 54 60 /// </summary> 55 public class CodeNoopExpression : Expression 56 { 57 public CodeNoopExpression() : base("CODE.NOOP") { } 61 public class CodeNoopExpression : StatelessExpression 62 { 63 protected override string InitStringRepresentation() 64 { 65 return "CODE.NOOP"; 66 } 58 67 59 68 public override void Eval(IInterpreter interpreter) … … 67 76 /// This can be implemented by moving the top item on the EXEC stack onto the CODE stack. 68 77 /// </summary> 69 public class CodeQuoteExpression : Expression 70 { 71 public CodeQuoteExpression() : base("CODE.QUOTE") { } 78 public class CodeQuoteExpression : StatelessExpression 79 { 80 protected override string InitStringRepresentation() 81 { 82 return "CODE.QUOTE"; 83 } 72 84 73 85 public override void Eval(IInterpreter interpreter) … … 87 99 /// (and the BOOLEAN value upon which the decision was made) are popped. 88 100 /// </summary> 89 public class CodeIfExpression : Expression 90 { 91 public CodeIfExpression() : base("CODE.IF") { } 101 public class CodeIfExpression : StatelessExpression 102 { 103 protected override string InitStringRepresentation() 104 { 105 return "CODE.IF"; 106 } 92 107 93 108 public override void Eval(IInterpreter interpreter) … … 111 126 /// literal (that is, something not surrounded by parentheses) then it is surrounded by parentheses first. 112 127 /// </summary> 113 public class CodeAppendExpression : Expression 114 { 115 public CodeAppendExpression() : base("CODE.APPEND") { } 128 public class CodeAppendExpression : StatelessExpression 129 { 130 protected override string InitStringRepresentation() 131 { 132 return "CODE.APPEND"; 133 } 116 134 117 135 public override void Eval(IInterpreter interpreter) … … 130 148 if (isFirstList) 131 149 { 132 var list1 = first as ExecExpandExpression;150 var expand1 = first as ExecExpandExpression; 133 151 134 152 if (isSecondList) 135 153 { 136 var list2 = second as ExecExpandExpression;137 138 result = new List<Expression>( list2.Expressions.Length + list1.Expressions.Length);139 result.AddRange( list2.Expressions);140 result.AddRange( list1.Expressions);154 var expand2 = second as ExecExpandExpression; 155 156 result = new List<Expression>(expand2.Expressions.Length + expand1.Expressions.Length); 157 result.AddRange(expand2.Expressions); 158 result.AddRange(expand1.Expressions); 141 159 } 142 160 else 143 161 { 144 result = new List<Expression>( list1.Expressions.Length + 1);162 result = new List<Expression>(expand1.Expressions.Length + 1); 145 163 result.Add(second); 146 result.AddRange( list1.Expressions);164 result.AddRange(expand1.Expressions); 147 165 } 148 166 } 149 167 else if (isSecondList) 150 168 { 151 var list2 = second as ExecExpandExpression;152 153 result = new List<Expression>( list2.Expressions.Length + 1);169 var expand2 = second as ExecExpandExpression; 170 171 result = new List<Expression>(expand2.Expressions.Length + 1); 154 172 result.Add(first); 155 result.AddRange( list2.Expressions);173 result.AddRange(expand2.Expressions); 156 174 } 157 175 else … … 168 186 /// and FALSE otherwise (that is, if it is something surrounded by parentheses). 169 187 /// </summary> 170 public class CodeAtomExpression : Expression 171 { 172 public CodeAtomExpression() : base("CODE.ATOM") { } 188 public class CodeAtomExpression : StatelessExpression 189 { 190 protected override string InitStringRepresentation() 191 { 192 return "CODE.ATOM"; 193 } 173 194 174 195 public override void Eval(IInterpreter interpreter) … … 189 210 /// The name derives from the similar Lisp function; a more generic name would be "FIRST". 190 211 /// </summary> 191 public class CodeCarExpression : Expression 192 { 193 public CodeCarExpression() : base("CODE.CAR") { } 212 public class CodeCarExpression : StatelessExpression 213 { 214 protected override string InitStringRepresentation() 215 { 216 return "CODE.CAR"; 217 } 194 218 195 219 public override void Eval(IInterpreter interpreter) … … 216 240 /// the similar Lisp function; a more generic name would be "REST". 217 241 /// </summary> 218 public class CodeCdrExpression : Expression 219 { 220 public CodeCdrExpression() : base("CODE.CDR") { } 242 public class CodeCdrExpression : StatelessExpression 243 { 244 protected override string InitStringRepresentation() 245 { 246 return "CODE.CDR"; 247 } 221 248 222 249 public override void Eval(IInterpreter interpreter) … … 230 257 if (top.GetType() == typeof(ExecExpandExpression)) 231 258 { 232 var exp ression= top as ExecExpandExpression;233 234 if (exp ression.Expressions.Length == 0)259 var expand = top as ExecExpandExpression; 260 261 if (expand.IsEmpty) 235 262 { 236 263 return; … … 238 265 else 239 266 { 240 var length = exp ression.Expressions.Length - 1;267 var length = expand.Expressions.Length - 1; 241 268 var newExpressions = new Expression[length]; 242 269 243 Array.Copy(exp ression.Expressions, 0, newExpressions, 0, length);270 Array.Copy(expand.Expressions, 0, newExpressions, 0, length); 244 271 245 272 expandExpression = new ExecExpandExpression(newExpressions); … … 260 287 /// second piece of code is "X" then this pushes "( X A B )" (after popping the argument). 261 288 /// </summary> 262 public class CodeConsExpression : Expression 263 { 264 public CodeConsExpression() : base("CODE.CONS") { } 289 public class CodeConsExpression : StatelessExpression 290 { 291 protected override string InitStringRepresentation() 292 { 293 return "CODE.CONS"; 294 } 265 295 266 296 public override void Eval(IInterpreter interpreter) … … 286 316 /// and the second piece of code is "( A )" then this pushes ( C ( A ) ). Pushes an empty list if there is no such container. 287 317 /// </summary> 288 public class CodeContainerExpression : Expression 289 { 290 public CodeContainerExpression() : base("CODE.CONTAINER") { } 318 public class CodeContainerExpression : StatelessExpression 319 { 320 protected override string InitStringRepresentation() 321 { 322 return "CODE.CONTAINER"; 323 } 291 324 292 325 public override void Eval(IInterpreter interpreter) … … 301 334 var source = interpreter.CodeStack.Top; 302 335 303 var container = this.GetContainer(null, source as ExecExpandExpression, target);336 var container = GetContainer(null, source as ExecExpandExpression, target); 304 337 var result = container == null 305 338 ? ExecExpandExpression.Empty … … 330 363 /// item anywhere (e.g. in a sub-list). 331 364 /// </summary> 332 public class CodeContainsExpression : Expression 333 { 334 public CodeContainsExpression() : base("CODE.CONTAINS") { } 365 public class CodeContainsExpression : StatelessExpression 366 { 367 protected override string InitStringRepresentation() 368 { 369 return "CODE.CONTAINS"; 370 } 335 371 336 372 public override void Eval(IInterpreter interpreter) … … 352 388 /// may then be executed with a call to CODE.DO or a similar instruction). 353 389 /// </summary> 354 public class CodeDefinitionExpression : Expression 355 { 356 public CodeDefinitionExpression() : base("CODE.DEFINITION") { } 390 public class CodeDefinitionExpression : StatelessExpression 391 { 392 protected override string InitStringRepresentation() 393 { 394 return "CODE.DEFINITION"; 395 } 357 396 358 397 public override void Eval(IInterpreter interpreter) … … 385 424 /// </list> 386 425 /// </summary> 387 public class CodeDiscrepancyExpression : Expression 388 { 389 public CodeDiscrepancyExpression() : base("CODE.DISCREPANCY") { } 426 public class CodeDiscrepancyExpression : StatelessExpression 427 { 428 protected override string InitStringRepresentation() 429 { 430 return "CODE.DISCREPANCY"; 431 } 390 432 391 433 public override void Eval(IInterpreter interpreter) … … 401 443 DetermineUniqueItems(expressions[1], firstItems); 402 444 403 var discrepancy = this.GetDiscrepancy(firstItems, secondItems);445 var discrepancy = GetDiscrepancy(firstItems, secondItems); 404 446 405 447 interpreter.IntegerStack.Push(discrepancy); … … 434 476 if (source.GetType() == typeof(ExecExpandExpression)) 435 477 { 436 var list= source as ExecExpandExpression;437 438 foreach (var e in list.Expressions)478 var expand = source as ExecExpandExpression; 479 480 foreach (var e in expand.Expressions) 439 481 { 440 482 var id = e.GetHashCode(); … … 464 506 /// absolute value is taken in case it is negative) to ensure that it is within the meaningful range. 465 507 /// </summary> 466 public class CodeExtractExpression : Expression 467 { 468 public CodeExtractExpression() : base("CODE.EXTRACT") { } 508 public class CodeExtractExpression : StatelessExpression 509 { 510 protected override string InitStringRepresentation() 511 { 512 return "CODE.EXTRACT"; 513 } 469 514 470 515 public override void Eval(IInterpreter interpreter) … … 488 533 /// Pops the BOOLEAN stack and pushes the popped item (TRUE or FALSE) onto the CODE stack. 489 534 /// </summary> 490 public class CodeFromBooleanExpression : Expression 491 { 492 public CodeFromBooleanExpression() : base("CODE.FROMBOOLEAN") { } 535 public class CodeFromBooleanExpression : StatelessExpression 536 { 537 protected override string InitStringRepresentation() 538 { 539 return "CODE.FROMBOOLEAN"; 540 } 493 541 494 542 public override void Eval(IInterpreter interpreter) … … 507 555 /// Pops the FLOAT stack and pushes the popped item onto the CODE stack. 508 556 /// </summary> 509 public class CodeFromFloatExpression : Expression 510 { 511 public CodeFromFloatExpression() : base("CODE.FROMFLOAT") { } 557 public class CodeFromFloatExpression : StatelessExpression 558 { 559 protected override string InitStringRepresentation() 560 { 561 return "CODE.FROMFLOAT"; 562 } 512 563 513 564 public override void Eval(IInterpreter interpreter) … … 526 577 /// Pops the INTEGER stack and pushes the popped integer onto the CODE stack. 527 578 /// </summary> 528 public class CodeFromIntegerExpression : Expression 529 { 530 public CodeFromIntegerExpression() : base("CODE.FROMINTEGER") { } 579 public class CodeFromIntegerExpression : StatelessExpression 580 { 581 protected override string InitStringRepresentation() 582 { 583 return "CODE.FROMINTEGER"; 584 } 531 585 532 586 public override void Eval(IInterpreter interpreter) … … 545 599 /// Pops the NAME stack and pushes the popped item onto the CODE stack. 546 600 /// </summary> 547 public class CodeFromNameExpression : Expression 548 { 549 public CodeFromNameExpression() : base("CODE.FROMNAME") { } 601 public class CodeFromNameExpression : StatelessExpression 602 { 603 protected override string InitStringRepresentation() 604 { 605 return "CODE.FROMNAME"; 606 } 550 607 551 608 public override void Eval(IInterpreter interpreter) … … 565 622 /// by the top item of the INTEGER stack (and replacing whatever was there formerly). 566 623 /// </summary> 567 public class CodeInsertExpression : Expression 568 { 569 public CodeInsertExpression() : base("CODE.INSERT") { } 624 public class CodeInsertExpression : StatelessExpression 625 { 626 protected override string InitStringRepresentation() 627 { 628 return "CODE.INSERT"; 629 } 570 630 571 631 public override void Eval(IInterpreter interpreter) … … 602 662 /// matter what they contain. 603 663 /// </summary> 604 public class CodeLengthExpression : Expression 605 { 606 public CodeLengthExpression() : base("CODE.LENGTH") { } 664 public class CodeLengthExpression : StatelessExpression 665 { 666 protected override string InitStringRepresentation() 667 { 668 return "CODE.LENGTH"; 669 } 607 670 608 671 public override void Eval(IInterpreter interpreter) … … 626 689 /// Pushes a list of the top two items of the CODE stack onto the CODE stack. 627 690 /// </summary> 628 public class CodeListExpression : Expression 629 { 630 public CodeListExpression() : base("CODE.LIST") { } 691 public class CodeListExpression : StatelessExpression 692 { 693 protected override string InitStringRepresentation() 694 { 695 return "CODE.LIST"; 696 } 631 697 632 698 public override void Eval(IInterpreter interpreter) … … 652 718 /// (which is coerced to a list if necessary). Pushes FALSE onto the BOOLEAN stack otherwise. 653 719 /// </summary> 654 public class CodeMemberExpression : Expression 655 { 656 public CodeMemberExpression() : base("CODE.MEMBER") { } 720 public class CodeMemberExpression : StatelessExpression 721 { 722 protected override string InitStringRepresentation() 723 { 724 return "CODE.MEMBER"; 725 } 657 726 658 727 public override void Eval(IInterpreter interpreter) … … 676 745 /// modulo the length of the expression into which it is indexing. 677 746 /// </summary> 678 public class CodeNthExpression : Expression 679 { 680 public CodeNthExpression() : base("CODE.NTH") { } 747 public class CodeNthExpression : StatelessExpression 748 { 749 protected override string InitStringRepresentation() 750 { 751 return "CODE.NTH"; 752 } 681 753 682 754 public override void Eval(IInterpreter interpreter) … … 693 765 if (expression.GetType() == typeof(ExecExpandExpression)) 694 766 { 695 var subExpression s = (expression as ExecExpandExpression).Expressions;696 697 nthExpression = subExpression s.Length == 0767 var subExpression = expression as ExecExpandExpression; 768 769 nthExpression = subExpression.IsEmpty 698 770 ? ExecExpandExpression.Empty 699 : subExpression s[subExpressions.Length - 1 - Math.Abs(n % subExpressions.Length)];771 : subExpression.Expressions[subExpression.Expressions.Length - 1 - Math.Abs(n % subExpression.Expressions.Length)]; 700 772 } 701 773 else … … 714 786 /// its first element. 715 787 /// </summary> 716 public class CodeNthCdrExpression : Expression 717 { 718 public CodeNthCdrExpression() : base("CODE.NTHCDR") { } 788 public class CodeNthCdrExpression : StatelessExpression 789 { 790 protected override string InitStringRepresentation() 791 { 792 return "CODE.NTHCDR"; 793 } 719 794 720 795 public override void Eval(IInterpreter interpreter) … … 749 824 /// Pushes TRUE onto the BOOLEAN stack if the top item of the CODE stack is an empty list, or FALSE otherwise. 750 825 /// </summary> 751 public class CodeNullExpression : Expression 752 { 753 public CodeNullExpression() : base("CODE.NULL") { } 826 public class CodeNullExpression : StatelessExpression 827 { 828 protected override string InitStringRepresentation() 829 { 830 return "CODE.NULL"; 831 } 754 832 755 833 public override void Eval(IInterpreter interpreter) … … 768 846 /// (which is coerced to a list if necessary). Pushes -1 if no match is found. 769 847 /// </summary> 770 public class CodePositionExpression : Expression 771 { 772 public CodePositionExpression() : base("CODE.POSITION") { } 848 public class CodePositionExpression : StatelessExpression 849 { 850 protected override string InitStringRepresentation() 851 { 852 return "CODE.POSITION"; 853 } 773 854 774 855 public override void Eval(IInterpreter interpreter) … … 784 865 if (first.GetType() == typeof(ExecExpandExpression)) 785 866 { 786 var list= first as ExecExpandExpression;787 position = list.Expressions.Length - 1 - Array.FindIndex(list.Expressions, e => e.Equals(second));867 var expand = first as ExecExpandExpression; 868 position = expand.Expressions.Length - 1 - Array.FindIndex(expand.Expressions, e => e.Equals(second)); 788 869 } 789 870 else if (first.Equals(second)) … … 800 881 /// and pair of parentheses counts as a point. 801 882 /// </summary> 802 public class CodeSizeExpression : Expression 803 { 804 public CodeSizeExpression() : base("CODE.SIZE") { } 883 public class CodeSizeExpression : StatelessExpression 884 { 885 protected override string InitStringRepresentation() 886 { 887 return "CODE.SIZE"; 888 } 805 889 806 890 public override void Eval(IInterpreter interpreter) … … 824 908 /// cases with empty-list arguments. If any of these problematic possibilities occurs the stack is left unchanged. 825 909 /// </summary> 826 public class CodeSubstitutionExpression : Expression 827 { 828 public CodeSubstitutionExpression() : base("CODE.SUBST") { } 910 public class CodeSubstitutionExpression : StatelessExpression 911 { 912 protected override string InitStringRepresentation() 913 { 914 return "CODE.SUBST"; 915 } 829 916 830 917 public override void Eval(IInterpreter interpreter) -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/DefineExpressions.cs
r14392 r14398 10 10 /// </summary> 11 11 /// <typeparam name="T">Stacktype</typeparam> 12 public abstract class DefineExpression<T> : Expression12 public abstract class DefineExpression<T> : StatelessExpression 13 13 { 14 public DefineExpression(string stringRepresentation) : base(stringRepresentation)15 { }16 17 14 protected void Eval(IStack<T> stack, IStack<string> nameStack, IDictionary<string, Expression> customExpressions, Func<T, Expression> creator) 18 15 { … … 38 35 public class CodeDefineExpression : DefineExpression<Expression> 39 36 { 40 p ublic CodeDefineExpression() : base("CODE.DEFINE")37 protected override string InitStringRepresentation() 41 38 { 39 return "CODE.DEFINE"; 42 40 } 43 41 … … 48 46 return; 49 47 50 this.Eval(48 Eval( 51 49 interpreter.CodeStack, 52 50 interpreter.NameStack, … … 60 58 public class ExecDefineExpression : DefineExpression<Expression> 61 59 { 62 p ublic ExecDefineExpression() : base("EXEC.DEFINE")60 protected override string InitStringRepresentation() 63 61 { 62 return "EXEC.DEFINE"; 64 63 } 65 64 … … 69 68 return; 70 69 71 this.Eval(70 Eval( 72 71 interpreter.ExecStack, 73 72 interpreter.NameStack, … … 81 80 public class FloatDefineExpression : DefineExpression<double> 82 81 { 83 p ublic FloatDefineExpression() : base("FLOAT.DEFINE")82 protected override string InitStringRepresentation() 84 83 { 84 return "FLOAT.DEFINE"; 85 85 } 86 86 87 87 public override void Eval(IInterpreter interpreter) 88 88 { 89 this.Eval(89 Eval( 90 90 interpreter.FloatStack, 91 91 interpreter.NameStack, … … 97 97 public class IntegerDefineExpression : DefineExpression<long> 98 98 { 99 p ublic IntegerDefineExpression() : base("INTEGER.DEFINE")99 protected override string InitStringRepresentation() 100 100 { 101 return "INTEGER.DEFINE"; 101 102 } 102 103 103 104 public override void Eval(IInterpreter interpreter) 104 105 { 105 this.Eval(106 Eval( 106 107 interpreter.IntegerStack, 107 108 interpreter.NameStack, … … 113 114 public class BooleanDefineExpression : DefineExpression<bool> 114 115 { 115 p ublic BooleanDefineExpression() : base("BOOLENA.DEFINE")116 protected override string InitStringRepresentation() 116 117 { 118 return "BOOLEAN.DEFINE"; 117 119 } 118 120 119 121 public override void Eval(IInterpreter interpreter) 120 122 { 121 this.Eval(123 Eval( 122 124 interpreter.BooleanStack, 123 125 interpreter.NameStack, -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/DoCountExpressions.cs
r14392 r14398 1 1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 using HeuristicLab.Algorithms.PushGP.Stack; 3 using HeuristicLab.Common; 3 4 4 5 namespace HeuristicLab.Algorithms.PushGP.Expressions … … 6 7 public abstract class DoCountExpression : LoopExpression 7 8 { 8 public DoCountExpression(string stringRepresentation) : base(stringRepresentation) { } 9 public DoCountExpression() 10 { } 11 12 public DoCountExpression(LoopExpression origin, Cloner cloner) : base(origin, cloner) 13 { } 9 14 10 15 protected override bool HasInsufficientArguments(IInterpreter interpreter, IStack<Expression> sourceStack) … … 17 22 protected override void InitState(IInterpreter interpreter, IStack<Expression> sourceStack) 18 23 { 19 this.body = sourceStack.Pop();20 this.destinationIndex = interpreter.IntegerStack.Top;21 this.currentIndex = 1;22 this.incrementor = 1;24 body = sourceStack.Pop(); 25 destinationIndex = interpreter.IntegerStack.Top; 26 currentIndex = 1; 27 incrementor = 1; 23 28 24 29 interpreter.IntegerStack.SetTop(0); … … 34 39 /// ( 0 '1 - IntegerArg>' CODE.QUOTE 'CodeArg' CODE.DO*RANGE ) 35 40 /// </summary> 36 [StatefullExpression]37 41 public class CodeDoCountExpression : DoCountExpression 38 42 { 39 public CodeDoCountExpression() : base("CODE.DO*COUNT") { } 43 public CodeDoCountExpression() 44 { } 45 46 protected CodeDoCountExpression(CodeDoCountExpression origin, Cloner cloner) : base(origin, cloner) 47 { } 48 49 public override IDeepCloneable Clone(Cloner cloner) 50 { 51 return new CodeDoCountExpression(this, cloner); 52 } 53 54 protected override string InitStringRepresentation() 55 { 56 return "CODE.DO*COUNT"; 57 } 40 58 41 59 public override void Eval(IInterpreter interpreter) 42 60 { 43 this.Eval(interpreter, interpreter.CodeStack);61 Eval(interpreter, interpreter.CodeStack); 44 62 } 45 63 } … … 54 72 /// ( 0 '1 - IntegerArg' EXEC.DO* RANGE'ExecArg' ) 55 73 /// </summary> 56 [StatefullExpression]57 74 public class ExecDoCountExpression : DoCountExpression 58 75 { 59 public ExecDoCountExpression() : base("EXEC.DO*COUNT") { } 76 public ExecDoCountExpression() 77 { } 78 79 protected ExecDoCountExpression(ExecDoCountExpression origin, Cloner cloner) : base(origin, cloner) 80 { } 81 82 public override IDeepCloneable Clone(Cloner cloner) 83 { 84 return new ExecDoCountExpression(this, cloner); 85 } 86 87 protected override string InitStringRepresentation() 88 { 89 return "EXEC.DO*COUNT"; 90 } 60 91 61 92 public override void Eval(IInterpreter interpreter) 62 93 { 63 this.Eval(interpreter, interpreter.ExecStack);94 Eval(interpreter, interpreter.ExecStack); 64 95 } 65 96 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/DoRangeExpressions.cs
r14392 r14398 1 1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 using HeuristicLab.Algorithms.PushGP.Stack; 3 using HeuristicLab.Common; 3 4 4 5 namespace HeuristicLab.Algorithms.PushGP.Expressions … … 6 7 public abstract class DoRangeExpression : LoopExpression 7 8 { 8 public DoRangeExpression(string stringRepresentation) : base(stringRepresentation) { } 9 public DoRangeExpression() 10 { } 11 12 public DoRangeExpression(LoopExpression origin, Cloner cloner) : base(origin, cloner) 13 { } 9 14 10 15 protected override bool HasInsufficientArguments(IInterpreter interpreter, IStack<Expression> sourceStack) … … 17 22 protected override void InitState(IInterpreter interpreter, IStack<Expression> sourceStack) 18 23 { 19 this.body = sourceStack.Pop();20 this.destinationIndex = interpreter.IntegerStack.Pop();21 this.currentIndex = interpreter.IntegerStack.Top;22 this.incrementor = this.destinationIndex < this.currentIndex ? -1 : 1;23 this.currentIndex += this.incrementor;24 body = sourceStack.Pop(); 25 destinationIndex = interpreter.IntegerStack.Pop(); 26 currentIndex = interpreter.IntegerStack.Top; 27 incrementor = destinationIndex < currentIndex ? -1 : 1; 28 currentIndex += incrementor; 24 29 } 25 30 } … … 38 43 /// that is less than the specified current index. 39 44 /// </summary> 40 [StatefullExpression]41 45 public class CodeDoRangeExpression : DoRangeExpression 42 46 { 43 public CodeDoRangeExpression() : base("CODE.DO*RANGE") { } 47 public CodeDoRangeExpression() 48 { } 49 50 protected CodeDoRangeExpression(CodeDoRangeExpression origin, Cloner cloner) : base(origin, cloner) 51 { } 52 53 public override IDeepCloneable Clone(Cloner cloner) 54 { 55 return new CodeDoRangeExpression(this, cloner); 56 } 57 58 protected override string InitStringRepresentation() 59 { 60 return "CODE.DO*RANGE"; 61 } 44 62 45 63 public override void Eval(IInterpreter interpreter) 46 64 { 47 this.Eval(interpreter, interpreter.CodeStack);65 Eval(interpreter, interpreter.CodeStack); 48 66 } 49 67 } … … 63 81 /// index that is less than the specified current index. 64 82 /// </summary> 65 [StatefullExpression]66 83 public class ExecDoRangeExpression : DoRangeExpression 67 84 { 68 public ExecDoRangeExpression() : base("EXEC.DO*RANGE") { } 85 public ExecDoRangeExpression() 86 { } 87 88 protected ExecDoRangeExpression(ExecDoRangeExpression origin, Cloner cloner) : base(origin, cloner) 89 { } 90 91 public override IDeepCloneable Clone(Cloner cloner) 92 { 93 return new ExecDoRangeExpression(this, cloner); 94 } 95 96 protected override string InitStringRepresentation() 97 { 98 return "EXEC.DO*RANGE"; 99 } 69 100 70 101 public override void Eval(IInterpreter interpreter) 71 102 { 72 this.Eval(interpreter, interpreter.ExecStack);103 Eval(interpreter, interpreter.ExecStack); 73 104 } 74 105 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/DoTimesExpressions.cs
r14392 r14398 1 1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 using HeuristicLab.Algorithms.PushGP.Stack; 3 using HeuristicLab.Common; 3 4 4 5 namespace HeuristicLab.Algorithms.PushGP.Expressions … … 6 7 public abstract class DoTimesExpression : LoopExpression 7 8 { 8 public DoTimesExpression(string stringRepresentation) : base(stringRepresentation) { } 9 public DoTimesExpression() 10 { } 11 12 public DoTimesExpression(LoopExpression origin, Cloner cloner) : base(origin, cloner) 13 { } 9 14 10 15 protected override bool HasInsufficientArguments(IInterpreter interpreter, IStack<Expression> sourceStack) … … 17 22 protected override void InitState(IInterpreter interpreter, IStack<Expression> sourceStack) 18 23 { 19 this.body = sourceStack.Pop();20 this.destinationIndex = interpreter.IntegerStack.Pop();21 this.currentIndex = 1;22 this.incrementor = 1;24 body = sourceStack.Pop(); 25 destinationIndex = interpreter.IntegerStack.Pop(); 26 currentIndex = 1; 27 incrementor = 1; 23 28 } 24 29 25 30 protected override void PushIteration(IInterpreter interpreter) 26 31 { 27 interpreter.ExecStack.Push(this, this.body);32 interpreter.ExecStack.Push(this, body); 28 33 } 29 34 30 35 protected override void PushLastIteration(IInterpreter interpreter) 31 36 { 32 interpreter.ExecStack.Push( this.body);37 interpreter.ExecStack.Push(body); 33 38 } 34 39 } … … 40 45 /// in the call to CODE.DO*RANGE. 41 46 /// </summary> 42 [StatefullExpression] 47 43 48 public class CodeDoTimesExpression : DoTimesExpression 44 49 { 45 public CodeDoTimesExpression() : base("CODE.DO*TIMES") { } 50 public CodeDoTimesExpression() 51 { } 52 53 protected CodeDoTimesExpression(CodeDoTimesExpression origin, Cloner cloner) : base(origin, cloner) 54 { } 55 56 public override IDeepCloneable Clone(Cloner cloner) 57 { 58 return new CodeDoTimesExpression(this, cloner); 59 } 60 61 protected override string InitStringRepresentation() 62 { 63 return "CODE.DO*TIMES"; 64 } 46 65 47 66 public override void Eval(IInterpreter interpreter) 48 67 { 49 this.Eval(interpreter, interpreter.CodeStack);68 Eval(interpreter, interpreter.CodeStack); 50 69 } 51 70 } … … 57 76 /// pushed by EXEC.DO*RANGE, prior to the execution of the loop body. 58 77 /// </summary> 59 [StatefullExpression] 78 60 79 public class ExecDoTimesExpression : DoTimesExpression 61 80 { 62 public ExecDoTimesExpression() : base("EXEC.DO*TIMES") { } 81 public ExecDoTimesExpression() 82 { } 83 84 protected ExecDoTimesExpression(ExecDoTimesExpression origin, Cloner cloner) : base(origin, cloner) 85 { } 86 87 public override IDeepCloneable Clone(Cloner cloner) 88 { 89 return new ExecDoTimesExpression(this, cloner); 90 } 91 92 protected override string InitStringRepresentation() 93 { 94 return "EXEC.DO*TIMES"; 95 } 63 96 64 97 public override void Eval(IInterpreter interpreter) 65 98 { 66 this.Eval(interpreter, interpreter.ExecStack);99 Eval(interpreter, interpreter.ExecStack); 67 100 } 68 101 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/DuplicateExpressions.cs
r14397 r14398 9 9 /// </summary> 10 10 /// <typeparam name="T">Stacktype</typeparam> 11 public abstract class DuplicateExpression<T> : Expression11 public abstract class DuplicateExpression<T> : StatelessExpression 12 12 { 13 public DuplicateExpression(string stringRepresentation) : base(stringRepresentation)14 { }15 16 13 public void Eval(IStack<T> stack) 17 14 { … … 25 22 public class IntegerDuplicateExpression : DuplicateExpression<long> 26 23 { 27 public IntegerDuplicateExpression() : base("INTEGER.DUP") 28 { } 24 protected override string InitStringRepresentation() { return "INTEGER.DUP"; } 29 25 30 26 public override void Eval(IInterpreter interpreter) 31 27 { 32 this.Eval(interpreter.IntegerStack);28 Eval(interpreter.IntegerStack); 33 29 } 34 30 } … … 36 32 public class FloatDuplicateExpression : DuplicateExpression<double> 37 33 { 38 public FloatDuplicateExpression() : base("FLOAT.DUP") 39 { } 34 protected override string InitStringRepresentation() { return "FLOAT.DUP"; } 40 35 41 36 public override void Eval(IInterpreter interpreter) 42 37 { 43 this.Eval(interpreter.FloatStack);38 Eval(interpreter.FloatStack); 44 39 } 45 40 } … … 47 42 public class BooleanDuplicateExpression : DuplicateExpression<bool> 48 43 { 49 public BooleanDuplicateExpression() : base("BOOLEAN.DUP") 50 { } 44 protected override string InitStringRepresentation() { return "BOOLEAN.DUP"; } 51 45 52 46 public override void Eval(IInterpreter interpreter) 53 47 { 54 this.Eval(interpreter.BooleanStack);48 Eval(interpreter.BooleanStack); 55 49 } 56 50 } … … 58 52 public class NameDuplicateExpression : DuplicateExpression<string> 59 53 { 60 public NameDuplicateExpression() : base("NAME.DUP") 61 { } 54 protected override string InitStringRepresentation() { return "NAME.DUP"; } 62 55 63 56 public override void Eval(IInterpreter interpreter) 64 57 { 65 this.Eval(interpreter.NameStack);58 Eval(interpreter.NameStack); 66 59 } 67 60 } … … 69 62 public class ExecDuplicateExpression : DuplicateExpression<Expression> 70 63 { 71 public ExecDuplicateExpression() : base("EXEC.DUP") 72 { } 64 protected override string InitStringRepresentation() { return "EXEC.DUP"; } 73 65 74 66 public override void Eval(IInterpreter interpreter) 75 67 { 76 this.Eval(interpreter.ExecStack);68 Eval(interpreter.ExecStack); 77 69 } 78 70 } … … 80 72 public class CodeDuplicateExpression : DuplicateExpression<Expression> 81 73 { 82 public CodeDuplicateExpression() : base("CODE.DUP") 83 { } 74 protected override string InitStringRepresentation() { return "CODE.DUP"; } 84 75 85 76 public override void Eval(IInterpreter interpreter) 86 77 { 87 this.Eval(interpreter.CodeStack);78 Eval(interpreter.CodeStack); 88 79 } 89 80 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/ERCExpressions.cs
r14392 r14398 1 using HeuristicLab.Algorithms.PushGP.Generators; 1 using System; 2 using HeuristicLab.Algorithms.PushGP.Generators; 2 3 using HeuristicLab.Algorithms.PushGP.Interpreter; 3 4 using HeuristicLab.Algorithms.PushGP.Stack; 5 using HeuristicLab.Common; 4 6 using HeuristicLab.Core; 5 7 using HeuristicLab.Random; … … 10 12 { 11 13 private readonly T value; 12 public ERCExpression(string stringRepresentation, T value) : base(stringRepresentation) 14 15 public ERCExpression(T value) 13 16 { 14 17 this.value = value; … … 17 20 protected void Eval(IStack<T> stack) 18 21 { 19 stack.Push(this.value); 22 stack.Push(value); 23 } 24 25 /// <summary> 26 /// Does not make sense 27 /// </summary> 28 /// <returns></returns> 29 protected override string InitStringRepresentation() 30 { 31 throw new NotImplementedException(); 32 } 33 34 /// <summary> 35 /// Does not make sense 36 /// </summary> 37 /// <returns></returns> 38 protected override int InitId() 39 { 40 throw new NotImplementedException(); 41 } 42 43 /// <summary> 44 /// Does not make sense 45 /// </summary> 46 /// <returns></returns> 47 public override IDeepCloneable Clone(Cloner cloner) 48 { 49 throw new NotImplementedException(); 20 50 } 21 51 } … … 23 53 public class IntegerERCExpression : ERCExpression<long> 24 54 { 25 public IntegerERCExpression() : base( "INTEGER.ERC",IntegerGenerator.RandomInteger())55 public IntegerERCExpression() : base(IntegerGenerator.RandomInteger()) 26 56 { 27 57 } … … 29 59 public override void Eval(IInterpreter interpreter) 30 60 { 31 this.Eval(interpreter.IntegerStack);61 Eval(interpreter.IntegerStack); 32 62 } 33 63 } … … 37 67 protected bool isInitialized = false; 38 68 39 public FloatERCExpression() : base( "FLOAT.ERC",FloatGenerator.RandomFloat())69 public FloatERCExpression() : base(FloatGenerator.RandomFloat()) 40 70 { 41 71 } … … 43 73 public override void Eval(IInterpreter interpreter) 44 74 { 45 this.Eval(interpreter.FloatStack);75 Eval(interpreter.FloatStack); 46 76 } 47 77 } … … 49 79 public class BooleanERCExpression : ERCExpression<bool> 50 80 { 51 public BooleanERCExpression() : base( "BOOLEAN.ERC",BooleanGenerator.RandomBoolean())81 public BooleanERCExpression() : base(BooleanGenerator.RandomBoolean()) 52 82 { 53 83 } … … 55 85 public override void Eval(IInterpreter interpreter) 56 86 { 57 this.Eval(interpreter.BooleanStack);87 Eval(interpreter.BooleanStack); 58 88 } 59 89 } … … 63 93 private static IRandom random = new FastRandom(); 64 94 65 public NameERCExpression() : base( "NAME.ERC",NameGenerator.RandomName())95 public NameERCExpression() : base(NameGenerator.RandomName()) 66 96 { 67 97 } … … 69 99 public override void Eval(IInterpreter interpreter) 70 100 { 71 this.Eval(interpreter.NameStack);101 Eval(interpreter.NameStack); 72 102 } 73 103 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/EqualsExpressions.cs
r14392 r14398 8 8 /// </summary> 9 9 /// <typeparam name="T">Stacktype</typeparam> 10 public abstract class EqualsExpression<T> : Expression10 public abstract class EqualsExpression<T> : StatelessExpression 11 11 { 12 public EqualsExpression(string stringRepresentation) : base(stringRepresentation)13 { }14 15 12 public void Eval(IStack<T> stack, IStack<bool> booleanStack) 16 13 { … … 26 23 public class IntegerEqualsExpression : EqualsExpression<long> 27 24 { 28 public IntegerEqualsExpression() : base("INTEGER.=") 29 { } 25 protected override string InitStringRepresentation() { return "INTEGER.="; } 30 26 31 27 public override void Eval(IInterpreter interpreter) 32 28 { 33 this.Eval(interpreter.IntegerStack, interpreter.BooleanStack);29 Eval(interpreter.IntegerStack, interpreter.BooleanStack); 34 30 } 35 31 } … … 37 33 public class FloatEqualsExpression : EqualsExpression<double> 38 34 { 39 public FloatEqualsExpression() : base("FLOAT.=") 40 { } 35 protected override string InitStringRepresentation() { return "FLOAT.="; } 41 36 42 37 public override void Eval(IInterpreter interpreter) 43 38 { 44 this.Eval(interpreter.FloatStack, interpreter.BooleanStack);39 Eval(interpreter.FloatStack, interpreter.BooleanStack); 45 40 } 46 41 } … … 48 43 public class BooleanEqualsExpression : EqualsExpression<bool> 49 44 { 50 public BooleanEqualsExpression() : base("BOOLEAN.=") 51 { } 45 protected override string InitStringRepresentation() { return "BOOLEAN.="; } 52 46 53 47 public override void Eval(IInterpreter interpreter) 54 48 { 55 this.Eval(interpreter.BooleanStack, interpreter.BooleanStack);49 Eval(interpreter.BooleanStack, interpreter.BooleanStack); 56 50 } 57 51 } … … 59 53 public class NameEqualsExpression : EqualsExpression<string> 60 54 { 61 public NameEqualsExpression() : base("NAME.=") 62 { } 55 protected override string InitStringRepresentation() { return "NAME.="; } 63 56 64 57 public override void Eval(IInterpreter interpreter) 65 58 { 66 this.Eval(interpreter.NameStack, interpreter.BooleanStack);59 Eval(interpreter.NameStack, interpreter.BooleanStack); 67 60 } 68 61 } … … 70 63 public class ExecEqualsExpression : EqualsExpression<Expression> 71 64 { 72 public ExecEqualsExpression() : base("EXEC.=") 73 { } 65 protected override string InitStringRepresentation() { return "EXEC.="; } 74 66 75 67 public override void Eval(IInterpreter interpreter) 76 68 { 77 this.Eval(interpreter.ExecStack, interpreter.BooleanStack);69 Eval(interpreter.ExecStack, interpreter.BooleanStack); 78 70 } 79 71 } … … 81 73 public class CodeEqualsExpression : EqualsExpression<Expression> 82 74 { 83 public CodeEqualsExpression() : base("CODE.=") 84 { } 75 protected override string InitStringRepresentation() { return "CODE.="; } 85 76 86 77 public override void Eval(IInterpreter interpreter) 87 78 { 88 this.Eval(interpreter.CodeStack, interpreter.BooleanStack);79 Eval(interpreter.CodeStack, interpreter.BooleanStack); 89 80 } 90 81 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/ExecExpressions.cs
r14397 r14398 8 8 namespace HeuristicLab.Algorithms.PushGP.Expressions 9 9 { 10 public class ExecExpandExpression : Expression10 public class ExecExpandExpression : StatefullExpression 11 11 { 12 12 private const string prefix = "( "; … … 15 15 16 16 public readonly Expression[] Expressions; 17 public readonly bool IsEmpty; 18 17 19 private List<Expression> treeIndex; 18 20 19 21 public static readonly Expression Empty = new ExecExpandExpression(new Expression[0]); 20 22 21 public ExecExpandExpression(Expression[] expressions) : base() 22 { 23 this.Expressions = expressions; 23 public ExecExpandExpression(Expression[] expressions) 24 { 25 Expressions = expressions; 26 IsEmpty = expressions.Length == 0; 24 27 } 25 28 … … 27 30 public ExecExpandExpression(ExecExpandExpression original, Cloner cloner) : base(original, cloner) 28 31 { 29 this.Expressions = new Expression[original.Expressions.Length];30 Array.Copy(original.Expressions, this.Expressions, original.Expressions.Length);32 Expressions = new Expression[original.Expressions.Length]; 33 Array.Copy(original.Expressions, Expressions, original.Expressions.Length); 31 34 32 35 if (original.treeIndex != null) 33 36 { 34 this.treeIndex = original.treeIndex.Select(e => cloner.Clone(e)).ToList(); 35 } 37 treeIndex = original.treeIndex.Select(e => cloner.Clone(e)).ToList(); 38 } 39 40 IsEmpty = original.IsEmpty; 36 41 } 37 42 … … 54 59 } 55 60 56 private string GetStringRepresentation() 57 { 58 if (this.StringRepresentation == null) 59 { 60 var sb = new StringBuilder(); 61 62 sb.Append(prefix); 63 64 for (var i = this.Expressions.Length - 1; i >= 0; i--) 61 protected override string InitStringRepresentation() 62 { 63 var sb = new StringBuilder(); 64 65 sb.Append(prefix); 66 67 for (var i = Expressions.Length - 1; i >= 0; i--) 68 { 69 sb.Append(Expressions[i].ToString() + delimiter); 70 } 71 72 sb.Append(postfix); 73 74 return sb.ToString(); 75 } 76 77 protected override int InitId() 78 { 79 unchecked 80 { 81 var hash = 19; 82 foreach (var e in Expressions) 65 83 { 66 sb.Append(this.Expressions[i].ToString() + delimiter);84 hash *= 31 + e.GetHashCode(); 67 85 } 68 86 69 sb.Append(postfix); 70 71 this.StringRepresentation = sb.ToString(); 72 } 73 74 return this.StringRepresentation; 75 } 76 77 private int GetId() 78 { 79 if (this.Id == default(int)) 80 { 81 unchecked 82 { 83 this.Id = 19; 84 foreach (var e in Expressions) 85 { 86 this.Id *= 31 + e.GetHashCode(); 87 } 88 } 89 } 90 91 return this.Id; 87 return hash; 88 } 92 89 } 93 90 … … 97 94 } 98 95 96 public override int GetHashCode() 97 { 98 return base.GetHashCode(); 99 } 100 99 101 public override bool Equals(object obj) 100 102 { … … 111 113 var other = obj as ExecExpandExpression; 112 114 113 return this.Expressions.SequenceEqual(other.Expressions); 114 } 115 116 public override int GetHashCode() 117 { 118 return this.GetId(); 119 } 120 121 public override string ToString() 122 { 123 return this.GetStringRepresentation(); 124 } 125 126 public override void Eval(IInterpreter interpreter) 127 { 128 interpreter.ExecStack.Push(this.Expressions); 115 return Expressions.SequenceEqual(other.Expressions); 116 } 117 118 public override void Eval(IInterpreter interpreter) 119 { 120 interpreter.ExecStack.Push(Expressions); 129 121 } 130 122 … … 137 129 get 138 130 { 139 this.BuildIndexIfNecessary();140 141 return t his.treeIndex.Count;131 BuildIndexIfNecessary(); 132 133 return treeIndex.Count; 142 134 } 143 135 } … … 150 142 public Expression GetFromTree(int index) 151 143 { 152 this.BuildIndexIfNecessary();153 154 return t his.treeIndex[index];144 BuildIndexIfNecessary(); 145 146 return treeIndex[index]; 155 147 } 156 148 157 149 private void BuildIndexIfNecessary() 158 150 { 159 if (t his.treeIndex != null)160 return; 161 162 t his.treeIndex = new List<Expression>(this.Expressions.Length) { this };163 164 for (var i = this.Expressions.Length - 1; i >= 0; i--)165 { 166 var subExpression = this.Expressions[i];151 if (treeIndex != null) 152 return; 153 154 treeIndex = new List<Expression>(Expressions.Length) { this }; 155 156 for (var i = Expressions.Length - 1; i >= 0; i--) 157 { 158 var subExpression = Expressions[i]; 167 159 168 160 if (subExpression.GetType() == typeof(ExecExpandExpression)) 169 161 { 170 var list= subExpression as ExecExpandExpression;171 172 list.BuildIndexIfNecessary();173 t his.treeIndex.AddRange(list.treeIndex);162 var expand = subExpression as ExecExpandExpression; 163 164 expand.BuildIndexIfNecessary(); 165 treeIndex.AddRange(expand.treeIndex); 174 166 } 175 167 else 176 168 { 177 t his.treeIndex.Add(subExpression);169 treeIndex.Add(subExpression); 178 170 } 179 171 … … 188 180 /// the BOOLEAN stack. 189 181 /// </summary> 190 public class ExecIfExpression : Expression191 { 192 p ublic ExecIfExpression() : base("EXEC.IF") {}182 public class ExecIfExpression : StatelessExpression 183 { 184 protected override string InitStringRepresentation() { return "EXEC.IF"; } 193 185 194 186 public override void Eval(IInterpreter interpreter) … … 212 204 /// "( EXEC.Y <TopItem> )". 213 205 /// </summary> 214 public class ExecYExpression : Expression 215 { 216 public ExecYExpression() : base("EXEC.Y") 217 { 218 } 206 public class ExecYExpression : StatelessExpression 207 { 208 protected override string InitStringRepresentation() { return "EXEC.Y"; } 219 209 220 210 public override void Eval(IInterpreter interpreter) … … 237 227 /// The Push implementation of the "K combinator". Removes the second item on the EXEC stack. 238 228 /// </summary> 239 public class ExecKExpression : Expression240 { 241 p ublic ExecKExpression() : base("EXEC.K") {}229 public class ExecKExpression : StatelessExpression 230 { 231 protected override string InitStringRepresentation() { return "EXEC.K"; } 242 232 243 233 public override void Eval(IInterpreter interpreter) … … 256 246 /// another instance of C, followed by another instance of A. 257 247 /// </summary> 258 public class ExecSExpression : Expression259 { 260 p ublic ExecSExpression() : base("EXEC.S") {}248 public class ExecSExpression : StatelessExpression 249 { 250 protected override string InitStringRepresentation() { return "EXEC.S"; } 261 251 262 252 public override void Eval(IInterpreter interpreter) -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Expression.cs
r14392 r14398 6 6 public abstract class Expression : DeepCloneable 7 7 { 8 public Expression() { } 8 private string stringRepresentation; 9 private int id; 9 10 10 public Expression( string stringRepresenation) : this(stringRepresenation, stringRepresenation.GetHashCode())11 public Expression() 11 12 { } 12 13 13 public Expression(string stringRepresenation, int id) 14 /// <summary> 15 /// Clone Constructor 16 /// </summary> 17 protected Expression(Expression origin, Cloner cloner) 14 18 { 15 this.StringRepresentation = stringRepresenation;16 this.Id =id;19 stringRepresentation = origin.stringRepresentation; 20 id = origin.id; 17 21 } 18 19 // copy constructor20 public Expression(Expression original, Cloner cloner) : base(original, cloner)21 {22 this.StringRepresentation = original.StringRepresentation;23 this.Id = original.Id;24 }25 26 protected int Id { get; set; }27 protected string StringRepresentation { get; set; }28 22 29 23 public abstract void Eval(IInterpreter interpreter); 30 24 31 public override string ToString() 25 protected abstract int InitId(); 26 27 protected abstract string InitStringRepresentation(); 28 29 public string StringRepresentation 32 30 { 33 return this.StringRepresentation; 31 get 32 { 33 if (stringRepresentation == null) 34 { 35 stringRepresentation = InitStringRepresentation(); 36 } 37 38 return stringRepresentation; 39 } 40 } 41 42 public int Id 43 { 44 get 45 { 46 if (id == default(int)) 47 { 48 id = InitId(); 49 } 50 51 return id; 52 } 34 53 } 35 54 36 55 public override bool Equals(object obj) 37 56 { 38 return obj != null && this.GetType() == obj.GetType(); 57 return obj != null && (GetHashCode() == obj.GetHashCode() || GetType() == obj.GetType()); 58 } 59 60 public override string ToString() 61 { 62 return StringRepresentation; 39 63 } 40 64 41 65 public override int GetHashCode() 42 66 { 43 return this.Id; 44 } 45 46 public override IDeepCloneable Clone(Cloner cloner) 47 { 48 return this; 67 return Id; 49 68 } 50 69 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/FloatExpressions.cs
r14392 r14398 9 9 public class FloatAddExpression : PushResultExpression<double> 10 10 { 11 public FloatAddExpression() : base("FLOAT.+") 12 { } 13 14 public override void Eval(IInterpreter interpreter) 15 { 16 this.Eval(interpreter.FloatStack, 2, values => values[0] + values[1]); 11 protected override string InitStringRepresentation() { return "FLOAT.+"; } 12 13 public override void Eval(IInterpreter interpreter) 14 { 15 Eval(interpreter.FloatStack, 2, values => values[0] + values[1]); 17 16 } 18 17 } … … 23 22 public class FloatSubtractExpression : PushResultExpression<double> 24 23 { 25 public FloatSubtractExpression() : base("FLOAT.-") 26 { } 27 28 public override void Eval(IInterpreter interpreter) 29 { 30 this.Eval(interpreter.FloatStack, 2, values => values[0] - values[1]); 24 protected override string InitStringRepresentation() { return "FLOAT.-"; } 25 26 public override void Eval(IInterpreter interpreter) 27 { 28 Eval(interpreter.FloatStack, 2, values => values[0] - values[1]); 31 29 } 32 30 } … … 37 35 public class FloatMultiplyExpression : PushResultExpression<double> 38 36 { 39 public FloatMultiplyExpression() : base("FLOAT.*") 40 { } 41 42 public override void Eval(IInterpreter interpreter) 43 { 44 this.Eval(interpreter.FloatStack, 2, values => values[0] * values[1]); 37 protected override string InitStringRepresentation() { return "FLOAT.*"; } 38 39 public override void Eval(IInterpreter interpreter) 40 { 41 Eval(interpreter.FloatStack, 2, values => values[0] * values[1]); 45 42 } 46 43 } … … 52 49 public class FloatDivideExpression : PushResultExpression<double> 53 50 { 54 public FloatDivideExpression() : base("FLOAT./") 55 { } 56 57 public override void Eval(IInterpreter interpreter) 58 { 59 this.Eval(interpreter.FloatStack, 2, values => values[0] / values[1], 0); 51 protected override string InitStringRepresentation() { return "FLOAT./"; } 52 53 public override void Eval(IInterpreter interpreter) 54 { 55 Eval(interpreter.FloatStack, 2, values => values[0] / values[1], 0); 60 56 } 61 57 } … … 69 65 public class FloatModuloExpression : PushResultExpression<double> 70 66 { 71 public FloatModuloExpression() : base("FLOAT.%") 72 { } 73 74 public override void Eval(IInterpreter interpreter) 75 { 76 this.Eval(interpreter.FloatStack, 2, values => values[0] % values[1], 0); 67 protected override string InitStringRepresentation() { return "FLOAT.%"; } 68 69 public override void Eval(IInterpreter interpreter) 70 { 71 Eval(interpreter.FloatStack, 2, values => values[0] % values[1], 0); 77 72 } 78 73 } … … 83 78 public class FloatMinExpression : PushResultExpression<double> 84 79 { 85 public FloatMinExpression() : base("FLOAT.MIN") 86 { } 87 88 public override void Eval(IInterpreter interpreter) 89 { 90 this.Eval(interpreter.FloatStack, 2, values => Math.Min(values[0], values[1])); 80 protected override string InitStringRepresentation() { return "FLOAT.MIN"; } 81 82 public override void Eval(IInterpreter interpreter) 83 { 84 Eval(interpreter.FloatStack, 2, values => Math.Min(values[0], values[1])); 91 85 } 92 86 } … … 97 91 public class FloatMaxExpression : PushResultExpression<double> 98 92 { 99 public FloatMaxExpression() : base("FLOAT.MAX") 100 { } 101 102 public override void Eval(IInterpreter interpreter) 103 { 104 this.Eval(interpreter.FloatStack, 2, values => Math.Max(values[0], values[1])); 93 protected override string InitStringRepresentation() { return "FLOAT.MAX"; } 94 95 public override void Eval(IInterpreter interpreter) 96 { 97 Eval(interpreter.FloatStack, 2, values => Math.Max(values[0], values[1])); 105 98 } 106 99 } … … 111 104 public class FloatSmallerThanExpression : PushResultExpression<double> 112 105 { 113 public FloatSmallerThanExpression() : base("FLOAT.<") 114 { } 115 116 public override void Eval(IInterpreter interpreter) 117 { 118 this.Eval( 106 protected override string InitStringRepresentation() { return "FLOAT.<"; } 107 108 public override void Eval(IInterpreter interpreter) 109 { 110 Eval( 119 111 interpreter.FloatStack, 120 112 interpreter.BooleanStack, … … 129 121 public class FloatGreaterThanExpression : PushResultExpression<double> 130 122 { 131 public FloatGreaterThanExpression() : base("FLOAT.>") 132 { } 133 134 public override void Eval(IInterpreter interpreter) 135 { 136 this.Eval( 123 protected override string InitStringRepresentation() { return "FLOAT.>"; } 124 125 public override void Eval(IInterpreter interpreter) 126 { 127 Eval( 137 128 interpreter.FloatStack, 138 129 interpreter.BooleanStack, … … 147 138 public class FloatSineExpression : PushResultExpression<double> 148 139 { 149 public FloatSineExpression() : base("FLOAT.SIN") 150 { } 151 152 public override void Eval(IInterpreter interpreter) 153 { 154 this.Eval( 140 protected override string InitStringRepresentation() { return "FLOAT.SIN"; } 141 142 public override void Eval(IInterpreter interpreter) 143 { 144 Eval( 155 145 interpreter.FloatStack, 156 146 1, … … 164 154 public class FloatCosineExpression : PushResultExpression<double> 165 155 { 166 p ublic FloatCosineExpression() : base("FLOAT.COS") {}167 168 public override void Eval(IInterpreter interpreter) 169 { 170 this.Eval(156 protected override string InitStringRepresentation() { return "FLOAT.COS"; } 157 158 public override void Eval(IInterpreter interpreter) 159 { 160 Eval( 171 161 interpreter.FloatStack, 172 162 1, … … 178 168 /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE. 179 169 /// </summary> 180 public class FloatFromBooleanExpression : Expression181 { 182 p ublic FloatFromBooleanExpression() : base("FLOAT.FROMBOOLEAN") {}170 public class FloatFromBooleanExpression : StatelessExpression 171 { 172 protected override string InitStringRepresentation() { return "FLOAT.FROMBOOLEAN"; } 183 173 184 174 public override void Eval(IInterpreter interpreter) … … 197 187 /// Pushes a floating point version of the top INTEGER. 198 188 /// </summary> 199 public class FloatFromIntegerExpression : Expression 200 { 201 public FloatFromIntegerExpression() : base("FLOAT.FROMINTEGER") 202 { 203 } 189 public class FloatFromIntegerExpression : StatelessExpression 190 { 191 protected override string InitStringRepresentation() { return "FLOAT.FROMINTEGER"; } 204 192 205 193 public override void Eval(IInterpreter interpreter) -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/FlushExpressions.cs
r14392 r14398 8 8 /// </summary> 9 9 /// <typeparam name="T">Stacktype</typeparam> 10 public abstract class FlushExpression<T> : Expression10 public abstract class FlushExpression<T> : StatelessExpression 11 11 { 12 public FlushExpression(string stringRepresentation) : base(stringRepresentation)13 { }14 15 12 public void Eval(IStack<T> stack) 16 13 { … … 21 18 public class IntegerFlushExpression : FlushExpression<long> 22 19 { 23 public IntegerFlushExpression() : base("INTEGER.FLUSH") 24 { } 20 protected override string InitStringRepresentation() { return "INTEGER.FLUSH"; } 25 21 26 22 public override void Eval(IInterpreter interpreter) 27 23 { 28 this.Eval(interpreter.IntegerStack);24 Eval(interpreter.IntegerStack); 29 25 } 30 26 } … … 32 28 public class FloatFlushExpression : FlushExpression<double> 33 29 { 34 public FloatFlushExpression() : base("FLOAT.FLUSH") 35 { } 30 protected override string InitStringRepresentation() { return "FLOAT.FLUSH"; } 36 31 37 32 public override void Eval(IInterpreter interpreter) 38 33 { 39 this.Eval(interpreter.FloatStack);34 Eval(interpreter.FloatStack); 40 35 } 41 36 } … … 43 38 public class BooleanFlushExpression : FlushExpression<bool> 44 39 { 45 public BooleanFlushExpression() : base("BOOLEAN.FLUSH") 46 { } 40 protected override string InitStringRepresentation() { return "BOOLEAN.FLUSH"; } 47 41 48 42 public override void Eval(IInterpreter interpreter) 49 43 { 50 this.Eval(interpreter.BooleanStack);44 Eval(interpreter.BooleanStack); 51 45 } 52 46 } … … 54 48 public class NameFlushExpression : FlushExpression<string> 55 49 { 56 public NameFlushExpression() : base("NAME.FLUSH") 57 { } 50 protected override string InitStringRepresentation() { return "NAME.FLUSH"; } 58 51 59 52 public override void Eval(IInterpreter interpreter) 60 53 { 61 this.Eval(interpreter.NameStack);54 Eval(interpreter.NameStack); 62 55 } 63 56 } … … 65 58 public class ExecFlushExpression : FlushExpression<Expression> 66 59 { 67 public ExecFlushExpression() : base("EXEC.FLUSH") 68 { } 60 protected override string InitStringRepresentation() { return "EXEC.FLUSH"; } 69 61 70 62 public override void Eval(IInterpreter interpreter) 71 63 { 72 this.Eval(interpreter.ExecStack);64 Eval(interpreter.ExecStack); 73 65 } 74 66 } … … 76 68 public class CodeFlushExpression : FlushExpression<Expression> 77 69 { 78 public CodeFlushExpression() : base("CODE.FLUSH") 79 { } 70 protected override string InitStringRepresentation() { return "CODE.FLUSH"; } 80 71 81 72 public override void Eval(IInterpreter interpreter) 82 73 { 83 this.Eval(interpreter.CodeStack);74 Eval(interpreter.CodeStack); 84 75 } 85 76 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/IntegerExpressions.cs
r14392 r14398 9 9 public class IntegerAddExpression : PushResultExpression<long> 10 10 { 11 p ublic IntegerAddExpression() : base("INTEGER.+") {}11 protected override string InitStringRepresentation() { return "INTEGER.+"; } 12 12 13 13 public override void Eval(IInterpreter interpreter) 14 14 { 15 this.Eval(interpreter.IntegerStack, 2, values => values[0] + values[1]);15 Eval(interpreter.IntegerStack, 2, values => values[0] + values[1]); 16 16 } 17 17 } … … 22 22 public class IntegerSubtractExpression : PushResultExpression<long> 23 23 { 24 p ublic IntegerSubtractExpression() : base("INTEGER.-") {}24 protected override string InitStringRepresentation() { return "INTEGER.-"; } 25 25 26 26 public override void Eval(IInterpreter interpreter) 27 27 { 28 this.Eval(interpreter.IntegerStack, 2, values => values[0] - values[1]);28 Eval(interpreter.IntegerStack, 2, values => values[0] - values[1]); 29 29 } 30 30 } … … 35 35 public class IntegerMultiplyExpression : PushResultExpression<long> 36 36 { 37 p ublic IntegerMultiplyExpression() : base("INTEGER.*") {}37 protected override string InitStringRepresentation() { return "INTEGER.*"; } 38 38 39 39 public override void Eval(IInterpreter interpreter) 40 40 { 41 this.Eval(interpreter.IntegerStack, 2, values => values[0] * values[1]);41 Eval(interpreter.IntegerStack, 2, values => values[0] * values[1]); 42 42 } 43 43 } … … 49 49 public class IntegerDivideExpression : PushResultExpression<long> 50 50 { 51 p ublic IntegerDivideExpression() : base("INTEGER./") {}51 protected override string InitStringRepresentation() { return "INTEGER./"; } 52 52 53 53 public override void Eval(IInterpreter interpreter) 54 54 { 55 this.Eval(interpreter.IntegerStack, 2, values => values[0] / values[1], 0);55 Eval(interpreter.IntegerStack, 2, values => values[0] / values[1], 0); 56 56 } 57 57 } … … 65 65 public class IntegerModuloExpression : PushResultExpression<long> 66 66 { 67 p ublic IntegerModuloExpression() : base("INTEGER.%") {}67 protected override string InitStringRepresentation() { return "INTEGER.%"; } 68 68 69 69 public override void Eval(IInterpreter interpreter) 70 70 { 71 this.Eval(interpreter.IntegerStack, 2, values => values[0] % values[1], 0);71 Eval(interpreter.IntegerStack, 2, values => values[0] % values[1], 0); 72 72 } 73 73 } … … 78 78 public class IntegerMinExpression : PushResultExpression<long> 79 79 { 80 p ublic IntegerMinExpression() : base("INTEGER.MIN") {}80 protected override string InitStringRepresentation() { return "INTEGER.MIN"; } 81 81 82 82 public override void Eval(IInterpreter interpreter) 83 83 { 84 this.Eval(interpreter.IntegerStack, 2, values => Math.Min(values[0], values[1]));84 Eval(interpreter.IntegerStack, 2, values => Math.Min(values[0], values[1])); 85 85 } 86 86 } … … 91 91 public class IntegerMaxExpression : PushResultExpression<long> 92 92 { 93 p ublic IntegerMaxExpression() : base("INTEGER.MAX") {}93 protected override string InitStringRepresentation() { return "INTEGER.MAX"; } 94 94 95 95 public override void Eval(IInterpreter interpreter) 96 96 { 97 this.Eval(interpreter.IntegerStack, 2, values => Math.Max(values[0], values[1]));97 Eval(interpreter.IntegerStack, 2, values => Math.Max(values[0], values[1])); 98 98 } 99 99 } … … 104 104 public class IntegerSmallerThanExpression : PushResultExpression<long> 105 105 { 106 p ublic IntegerSmallerThanExpression() : base("INTEGER.<") {}106 protected override string InitStringRepresentation() { return "INTEGER.<"; } 107 107 108 108 public override void Eval(IInterpreter interpreter) 109 109 { 110 this.Eval(110 Eval( 111 111 interpreter.IntegerStack, 112 112 interpreter.BooleanStack, … … 121 121 public class IntegerGreaterThanExpression : PushResultExpression<long> 122 122 { 123 p ublic IntegerGreaterThanExpression() : base("INTEGER.>") {}123 protected override string InitStringRepresentation() { return "INTEGER.>"; } 124 124 125 125 public override void Eval(IInterpreter interpreter) 126 126 { 127 this.Eval(127 Eval( 128 128 interpreter.IntegerStack, 129 129 interpreter.BooleanStack, … … 136 136 /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE. 137 137 /// </summary> 138 public class IntegerFromBooleanExpression : Expression138 public class IntegerFromBooleanExpression : StatelessExpression 139 139 { 140 p ublic IntegerFromBooleanExpression() : base("INTEGER.FROMBOOLEAN") {}140 protected override string InitStringRepresentation() { return "INTEGER.FROMBOOLEAN"; } 141 141 142 142 public override void Eval(IInterpreter interpreter) … … 155 155 /// Pushes the result of truncating the top FLOAT. 156 156 /// </summary> 157 public class IntegerFromFloatExpression : Expression157 public class IntegerFromFloatExpression : StatelessExpression 158 158 { 159 p ublic IntegerFromFloatExpression() : base("INTEGER.FROMFLOAT") {}159 protected override string InitStringRepresentation() { return "INTEGER.FROMFLOAT"; } 160 160 161 161 public override void Eval(IInterpreter interpreter) -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/LoopExpression.cs
r14392 r14398 1 1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 using HeuristicLab.Algorithms.PushGP.Stack; 3 using HeuristicLab.Common; 3 4 4 5 namespace HeuristicLab.Algorithms.PushGP.Expressions 5 6 { 6 public abstract class LoopExpression : Expression7 public abstract class LoopExpression : StatefullExpression 7 8 { 8 public long destinationIndex; 9 public long currentIndex; 10 public long incrementor; 11 public bool pushCurrentIndex = false; 12 public Expression body; 9 protected long destinationIndex; 10 protected long currentIndex; 11 protected long incrementor; 12 protected Expression body; 13 13 14 public LoopExpression(string stringRepresentation) : base(stringRepresentation) { } 14 public LoopExpression() 15 { } 15 16 16 protected void Eval(IInterpreter interpreter, IStack<Expression> sourceStack) 17 /// <summary> 18 /// Clone constructor 19 /// </summary> 20 protected LoopExpression(LoopExpression origin, Cloner cloner) : base(origin, cloner) 21 { 22 destinationIndex = origin.destinationIndex; 23 currentIndex = origin.currentIndex; 24 incrementor = origin.incrementor; 25 body = origin.body; 26 } 27 28 protected void Eval(IInterpreter interpreter, IStack<Expression> sourceStack, bool pushCurrentIndex = false) 17 29 { 18 30 // if not initialized 19 31 if (body == null) 20 32 { 21 if ( this.HasInsufficientArguments(interpreter, sourceStack))33 if (HasInsufficientArguments(interpreter, sourceStack)) 22 34 { 23 35 return; 24 36 } 25 37 26 this.InitState(interpreter, sourceStack);38 InitState(interpreter, sourceStack); 27 39 28 interpreter.ExecStack.Push(this, this.body);40 interpreter.ExecStack.Push(this, body); 29 41 30 42 return; … … 34 46 if (destinationIndex == currentIndex) 35 47 { 36 this.PushLastIteration(interpreter);37 this.Clear();48 PushLastIteration(interpreter); 49 Clear(); 38 50 39 51 return; 40 52 } 41 53 42 this.PushIteration(interpreter);43 this.currentIndex += this.incrementor;54 PushIteration(interpreter); 55 currentIndex += incrementor; 44 56 } 45 57 46 58 protected virtual void Clear() 47 59 { 48 this.body = null;60 body = null; 49 61 } 50 62 51 63 protected virtual void PushIteration(IInterpreter interpreter) 52 64 { 53 interpreter.IntegerStack.Push( this.currentIndex);54 interpreter.ExecStack.Push(this, this.body);65 interpreter.IntegerStack.Push(currentIndex); 66 interpreter.ExecStack.Push(this, body); 55 67 } 56 68 57 69 protected virtual void PushLastIteration(IInterpreter interpreter) 58 70 { 59 interpreter.IntegerStack.Push( this.currentIndex);60 interpreter.ExecStack.Push( this.body);71 interpreter.IntegerStack.Push(currentIndex); 72 interpreter.ExecStack.Push(body); 61 73 } 62 74 … … 71 83 72 84 var isBodyEqual = 73 ( this.body == null && other.body == null) ||74 ( this.body != null && other.body != null && this.body.Equals(other.body));85 (body == null && other.body == null) || 86 (body != null && other.body != null && body.Equals(other.body)); 75 87 76 88 return isBodyEqual && 77 this.currentIndex == other.currentIndex && 78 this.destinationIndex == other.destinationIndex && 79 this.incrementor == other.incrementor; 89 currentIndex == other.currentIndex && 90 destinationIndex == other.destinationIndex && 91 incrementor == other.incrementor; 92 } 93 94 public override int GetHashCode() 95 { 96 return base.GetHashCode(); 97 } 98 99 protected override int InitId() 100 { 101 unchecked 102 { 103 var hash = 19; 104 105 if (body != null) 106 { 107 hash *= 31 + body.GetHashCode(); 108 } 109 110 hash *= 31 + currentIndex.GetHashCode(); 111 hash *= 31 + destinationIndex.GetHashCode(); 112 hash *= 31 + incrementor.GetHashCode(); 113 114 return hash; 115 } 80 116 } 81 117 -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/NameExpressions.cs
r14392 r14398 1 using System.Linq; 1 using System; 2 using System.Linq; 2 3 using HeuristicLab.Algorithms.PushGP.Interpreter; 4 using HeuristicLab.Common; 3 5 using HeuristicLab.Core; 4 6 using HeuristicLab.Random; … … 6 8 namespace HeuristicLab.Algorithms.PushGP.Expressions 7 9 { 8 public class NameDefineXExecExpression : Expression10 public class NameDefineXExecExpression : StatefullExpression 9 11 { 10 public NameDefineXExecExpression(string name) : base(name) { } 12 private readonly string name; 13 public NameDefineXExecExpression(string name) 14 { 15 this.name = name; 16 } 17 18 public NameDefineXExecExpression(NameDefineXExecExpression origin, Cloner cloner) : base(origin, cloner) 19 { 20 name = origin.name; 21 } 11 22 12 23 public override void Eval(IInterpreter interpreter) … … 14 25 Expression expression; 15 26 if (!interpreter.IsNameQuoteFlagSet && 16 interpreter.CustomExpressions.TryGetValue( this.StringRepresentation, out expression))27 interpreter.CustomExpressions.TryGetValue(name, out expression)) 17 28 { 18 29 interpreter.ExecStack.Push(expression); … … 20 31 else 21 32 { 22 interpreter.NameStack.Push( this.StringRepresentation);33 interpreter.NameStack.Push(name); 23 34 interpreter.IsNameQuoteFlagSet = false; 24 35 } … … 32 43 } 33 44 34 return this.StringRepresentation.Equals(obj.ToString()); 45 var other = obj as NameDefineXExecExpression; 46 47 return name.Equals(other.name); 48 } 49 50 public override int GetHashCode() 51 { 52 return base.GetHashCode(); 53 } 54 55 protected override string InitStringRepresentation() 56 { 57 return name; 58 } 59 60 protected override int InitId() 61 { 62 return name.GetHashCode(); 63 } 64 65 public override IDeepCloneable Clone(Cloner cloner) 66 { 67 throw new NotImplementedException(); 35 68 } 36 69 } … … 41 74 /// onto the NAME stack the flag will be cleared (whether or not the pushed name had a definition). 42 75 /// </summary> 43 public class NameQuoteExpression : Expression76 public class NameQuoteExpression : StatelessExpression 44 77 { 45 public NameQuoteExpression() : base("NAME.QUOTE") { } 78 protected override string InitStringRepresentation() 79 { 80 return "NAME.QUOTE"; 81 } 46 82 47 83 public override void Eval(IInterpreter interpreter) … … 54 90 /// Pushes a randomly selected NAME that already has a definition. 55 91 /// </summary> 56 public class NameRandBoundNameExpression : Expression92 public class NameRandBoundNameExpression : StatelessExpression 57 93 { 58 94 private static IRandom rand = new FastRandom(); 59 95 60 public NameRandBoundNameExpression() : base("NAME.RANDBOUNDNAME") { } 96 protected override string InitStringRepresentation() 97 { 98 return "NAME.RANDBOUNDNAME"; 99 } 61 100 62 101 public override void Eval(IInterpreter interpreter) -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/PopExpressions.cs
r14392 r14398 4 4 namespace HeuristicLab.Algorithms.PushGP.Expressions 5 5 { 6 public abstract class PopExpression<T> : Expression6 public abstract class PopExpression<T> : StatelessExpression 7 7 { 8 public PopExpression(string stringRepresentation) : base(stringRepresentation)9 {10 }11 12 8 public void Eval(IStack<T> stack) 13 9 { … … 21 17 public class IntegerPopExpression : PopExpression<long> 22 18 { 23 public IntegerPopExpression() : base("INTEGER.POP") 24 { 25 } 19 protected override string InitStringRepresentation() { return "INTEGER.POP"; } 26 20 27 21 public override void Eval(IInterpreter interpreter) 28 22 { 29 this.Eval(interpreter.IntegerStack);23 Eval(interpreter.IntegerStack); 30 24 } 31 25 } … … 33 27 public class FloatPopExpression : PopExpression<double> 34 28 { 35 public FloatPopExpression() : base("FLOAT.POP") 36 { 37 } 29 protected override string InitStringRepresentation() { return "FLOAT.POP"; } 38 30 39 31 public override void Eval(IInterpreter interpreter) 40 32 { 41 this.Eval(interpreter.FloatStack);33 Eval(interpreter.FloatStack); 42 34 } 43 35 } … … 45 37 public class BooleanPopExpression : PopExpression<bool> 46 38 { 47 public BooleanPopExpression() : base("BOOLEAN.POP") 48 { 49 } 39 protected override string InitStringRepresentation() { return "BOOLEAN.POP"; } 50 40 51 41 public override void Eval(IInterpreter interpreter) 52 42 { 53 this.Eval(interpreter.BooleanStack);43 Eval(interpreter.BooleanStack); 54 44 } 55 45 } … … 57 47 public class NamePopExpression : PopExpression<string> 58 48 { 59 public NamePopExpression() : base("NAME.POP") 60 { 61 } 49 protected override string InitStringRepresentation() { return "NAME.POP"; } 62 50 63 51 public override void Eval(IInterpreter interpreter) 64 52 { 65 this.Eval(interpreter.NameStack);53 Eval(interpreter.NameStack); 66 54 } 67 55 } … … 69 57 public class ExecPopExpression : PopExpression<Expression> 70 58 { 71 public ExecPopExpression() : base("EXEC.POP") 72 { 73 } 59 protected override string InitStringRepresentation() { return "EXEC.POP"; } 74 60 75 61 public override void Eval(IInterpreter interpreter) 76 62 { 77 this.Eval(interpreter.ExecStack);63 Eval(interpreter.ExecStack); 78 64 } 79 65 } … … 81 67 public class CodePopExpression : PopExpression<Expression> 82 68 { 83 public CodePopExpression() : base("CODE.POP") 84 { 85 } 69 protected override string InitStringRepresentation() { return "CODE.POP"; } 86 70 87 71 public override void Eval(IInterpreter interpreter) 88 72 { 89 this.Eval(interpreter.CodeStack);73 Eval(interpreter.CodeStack); 90 74 } 91 75 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/PushExpressions.cs
r14392 r14398 5 5 namespace HeuristicLab.Algorithms.PushGP.Expressions 6 6 { 7 public abstract class PushExpression<T> : Expression7 public abstract class PushExpression<T> : StatefullExpression 8 8 { 9 9 protected readonly T value; 10 10 11 public PushExpression(T value) : base(value.ToString(), value.GetHashCode())11 public PushExpression(T value) 12 12 { 13 13 this.value = value; … … 17 17 public PushExpression(PushExpression<T> expression, Cloner cloner) : base(expression, cloner) 18 18 { 19 this.value = expression.value; 19 value = expression.value; 20 } 21 22 protected override int InitId() 23 { 24 return this.value.GetHashCode(); 25 } 26 27 protected override string InitStringRepresentation() 28 { 29 return this.value.ToString(); 20 30 } 21 31 22 32 protected void Eval(IStack<T> stack) 23 33 { 24 stack.Push( this.value);34 stack.Push(value); 25 35 } 26 36 … … 34 44 var other = obj as PushExpression<T>; 35 45 36 return this.value.Equals(value); 46 return value.Equals(value); 47 } 48 49 public override int GetHashCode() 50 { 51 return base.GetHashCode(); 37 52 } 38 53 } … … 49 64 public override void Eval(IInterpreter interpreter) 50 65 { 51 this.Eval(interpreter.IntegerStack);66 Eval(interpreter.IntegerStack); 52 67 } 53 68 … … 69 84 public override void Eval(IInterpreter interpreter) 70 85 { 71 this.Eval(interpreter.FloatStack);86 Eval(interpreter.FloatStack); 72 87 } 73 88 … … 89 104 public override void Eval(IInterpreter interpreter) 90 105 { 91 this.Eval(interpreter.BooleanStack);106 Eval(interpreter.BooleanStack); 92 107 } 93 108 … … 109 124 public override void Eval(IInterpreter interpreter) 110 125 { 111 this.Eval(interpreter.NameStack);126 Eval(interpreter.NameStack); 112 127 } 113 128 … … 129 144 public override void Eval(IInterpreter interpreter) 130 145 { 131 this.Eval(interpreter.ExecStack);146 Eval(interpreter.ExecStack); 132 147 } 133 148 -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/PushResultExpression.cs
r14392 r14398 4 4 namespace HeuristicLab.Algorithms.PushGP.Expressions 5 5 { 6 public abstract class PushResultExpression<T> : Expression6 public abstract class PushResultExpression<T> : StatelessExpression 7 7 { 8 public PushResultExpression(string stringRepresentation) : base(stringRepresentation)9 { }10 11 8 protected void Eval(IStack<T> stack, int count, Func<T[], T> templateFunc) 12 9 { -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/RandExpressions.cs
r14392 r14398 7 7 /// Pushes a random NAME. 8 8 /// </summary> 9 public class NameRandExpression : Expression9 public class NameRandExpression : StatelessExpression 10 10 { 11 public NameRandExpression() : base("NAME.RAND") 12 { 13 } 11 protected override string InitStringRepresentation() { return "NAME.RAND"; } 14 12 15 13 public override void Eval(IInterpreter interpreter) … … 24 22 /// Pushes a random integer. 25 23 /// </summary> 26 public class IntegerRandExpression : Expression24 public class IntegerRandExpression : StatelessExpression 27 25 { 28 public IntegerRandExpression() : base("INTEGER.RAND") 29 { 30 } 26 protected override string InitStringRepresentation() { return "INTEGER.RAND"; } 31 27 32 28 public override void Eval(IInterpreter interpreter) … … 43 39 /// Pushes a random float. 44 40 /// </summary> 45 public class FloatRandExpression : Expression41 public class FloatRandExpression : StatelessExpression 46 42 { 47 public FloatRandExpression() : base("FLOAT.RAND") 48 { 49 } 43 protected override string InitStringRepresentation() { return "FLOAT.RAND"; } 50 44 51 45 public override void Eval(IInterpreter interpreter) … … 62 56 /// Pushes a random boolean. 63 57 /// </summary> 64 public class BooleanRandExpression : Expression58 public class BooleanRandExpression : StatelessExpression 65 59 { 66 public BooleanRandExpression() : base("BOOLEAN.RAND") 67 { 68 } 60 protected override string InitStringRepresentation() { return "BOOLEAN.RAND"; } 69 61 70 62 public override void Eval(IInterpreter interpreter) … … 79 71 /// Pushes random expressions onto the code stack. 80 72 /// </summary> 81 public class CodeRandExpression : Expression73 public class CodeRandExpression : StatelessExpression 82 74 { 83 public CodeRandExpression() : base("CODE.RAND") 84 { 85 } 75 protected override string InitStringRepresentation() { return "CODE.RAND"; } 86 76 87 77 public override void Eval(IInterpreter interpreter) -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/RotateExpressions.cs
r14392 r14398 4 4 namespace HeuristicLab.Algorithms.PushGP.Expressions 5 5 { 6 public abstract class RotateExpression<T> : Expression6 public abstract class RotateExpression<T> : StatelessExpression 7 7 { 8 public RotateExpression(string stringRepresentation) : base(stringRepresentation)9 { }10 11 8 public void Eval(IStack<T> stack) 12 9 { … … 20 17 public class IntegerRotateExpression : RotateExpression<long> 21 18 { 22 public IntegerRotateExpression() : base("INTEGER.ROT") 23 { } 19 protected override string InitStringRepresentation() { return "INTEGER.ROT"; } 24 20 25 21 public override void Eval(IInterpreter interpreter) 26 22 { 27 this.Eval(interpreter.IntegerStack);23 Eval(interpreter.IntegerStack); 28 24 } 29 25 } … … 31 27 public class FloatRotateExpression : RotateExpression<double> 32 28 { 33 public FloatRotateExpression() : base("FLOAT.ROT") 34 { } 29 protected override string InitStringRepresentation() { return "FLOAT.ROT"; } 35 30 36 31 public override void Eval(IInterpreter interpreter) 37 32 { 38 this.Eval(interpreter.FloatStack);33 Eval(interpreter.FloatStack); 39 34 } 40 35 } … … 42 37 public class BooleanRotateExpression : RotateExpression<bool> 43 38 { 44 public BooleanRotateExpression() : base("BOOLEAN.ROT") 45 { } 39 protected override string InitStringRepresentation() { return "BOOLEAN.ROT"; } 46 40 47 41 public override void Eval(IInterpreter interpreter) 48 42 { 49 this.Eval(interpreter.BooleanStack);43 Eval(interpreter.BooleanStack); 50 44 } 51 45 } … … 53 47 public class NameRotateExpression : RotateExpression<string> 54 48 { 55 public NameRotateExpression() : base("NAME.ROT") 56 { } 49 protected override string InitStringRepresentation() { return "NAME.ROT"; } 57 50 58 51 public override void Eval(IInterpreter interpreter) 59 52 { 60 this.Eval(interpreter.NameStack);53 Eval(interpreter.NameStack); 61 54 } 62 55 } … … 64 57 public class ExecRotateExpression : RotateExpression<Expression> 65 58 { 66 public ExecRotateExpression() : base("EXEC.ROT") 67 { } 59 protected override string InitStringRepresentation() { return "EXEC.ROT"; } 68 60 69 61 public override void Eval(IInterpreter interpreter) 70 62 { 71 this.Eval(interpreter.ExecStack);63 Eval(interpreter.ExecStack); 72 64 } 73 65 } … … 75 67 public class CodeRotateExpression : RotateExpression<Expression> 76 68 { 77 public CodeRotateExpression() : base("CODE.ROT") 78 { } 69 protected override string InitStringRepresentation() { return "CODE.ROT"; } 79 70 80 71 public override void Eval(IInterpreter interpreter) 81 72 { 82 this.Eval(interpreter.CodeStack);73 Eval(interpreter.CodeStack); 83 74 } 84 75 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/ShoveExpressions.cs
r14392 r14398 4 4 namespace HeuristicLab.Algorithms.PushGP.Expressions 5 5 { 6 public abstract class ShoveExpression<T> : Expression6 public abstract class ShoveExpression<T> : StatelessExpression 7 7 { 8 public ShoveExpression(string stringRepresentation) : base(stringRepresentation)9 { }10 11 8 public void Eval(IStack<T> stack, IStack<long> integerStack, int count = 1) 12 9 { … … 29 26 public class IntegerShoveExpression : ShoveExpression<long> 30 27 { 31 public IntegerShoveExpression() : base("INTEGER.SHOVE") 32 { } 28 protected override string InitStringRepresentation() { return "INTEGER.SHOVE"; } 33 29 34 30 public override void Eval(IInterpreter interpreter) 35 31 { 36 this.Eval(interpreter.IntegerStack, interpreter.IntegerStack, 2);32 Eval(interpreter.IntegerStack, interpreter.IntegerStack, 2); 37 33 } 38 34 } … … 40 36 public class FloatShoveExpression : ShoveExpression<double> 41 37 { 42 public FloatShoveExpression() : base("FLOAT.SHOVE") 43 { } 38 protected override string InitStringRepresentation() { return "FLOAT.SHOVE"; } 44 39 45 40 public override void Eval(IInterpreter interpreter) 46 41 { 47 this.Eval(interpreter.FloatStack, interpreter.IntegerStack);42 Eval(interpreter.FloatStack, interpreter.IntegerStack); 48 43 } 49 44 } … … 51 46 public class BooleanShoveExpression : ShoveExpression<bool> 52 47 { 53 public BooleanShoveExpression() : base("BOOLEAN.SHOVE") 54 { } 48 protected override string InitStringRepresentation() { return "BOOLEAN.SHOVE"; } 55 49 56 50 public override void Eval(IInterpreter interpreter) 57 51 { 58 this.Eval(interpreter.BooleanStack, interpreter.IntegerStack);52 Eval(interpreter.BooleanStack, interpreter.IntegerStack); 59 53 } 60 54 } … … 62 56 public class NameShoveExpression : ShoveExpression<string> 63 57 { 64 public NameShoveExpression() : base("NAME.SHOVE") 65 { } 58 protected override string InitStringRepresentation() { return "NAME.SHOVE"; } 66 59 67 60 public override void Eval(IInterpreter interpreter) 68 61 { 69 this.Eval(interpreter.NameStack, interpreter.IntegerStack);62 Eval(interpreter.NameStack, interpreter.IntegerStack); 70 63 } 71 64 } … … 73 66 public class ExecShoveExpression : ShoveExpression<Expression> 74 67 { 75 public ExecShoveExpression() : base("EXEC.SHOVE") 76 { } 68 protected override string InitStringRepresentation() { return "EXEC.SHOVE"; } 77 69 78 70 public override void Eval(IInterpreter interpreter) 79 71 { 80 this.Eval(interpreter.ExecStack, interpreter.IntegerStack);72 Eval(interpreter.ExecStack, interpreter.IntegerStack); 81 73 } 82 74 } … … 84 76 public class CodeShoveExpression : ShoveExpression<Expression> 85 77 { 86 public CodeShoveExpression() : base("CODE.SHOVE") 87 { } 78 protected override string InitStringRepresentation() { return "CODE.SHOVE"; } 88 79 89 80 public override void Eval(IInterpreter interpreter) 90 81 { 91 this.Eval(interpreter.CodeStack, interpreter.IntegerStack);82 Eval(interpreter.CodeStack, interpreter.IntegerStack); 92 83 } 93 84 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/StackdepthExpressions.cs
r14392 r14398 8 8 /// </summary> 9 9 /// <typeparam name="T">Stacktype</typeparam> 10 public abstract class StackdepthExpression<T> : Expression10 public abstract class StackdepthExpression<T> : StatelessExpression 11 11 { 12 public StackdepthExpression(string stringRepresentation) : base(stringRepresentation)13 { }14 15 12 public void Eval(IStack<T> stack, IStack<long> integerStack, bool incremental = false) 16 13 { … … 28 25 public class IntegerStackdepthExpression : StackdepthExpression<long> 29 26 { 30 public IntegerStackdepthExpression() : base("INTEGER.STACKDEPTH") 31 { } 27 protected override string InitStringRepresentation() { return "INTEGER.STACKDEPTH"; } 32 28 33 29 public override void Eval(IInterpreter interpreter) 34 30 { 35 this.Eval(interpreter.IntegerStack, interpreter.IntegerStack, true);31 Eval(interpreter.IntegerStack, interpreter.IntegerStack, true); 36 32 } 37 33 } … … 39 35 public class FloatStackdepthExpression : StackdepthExpression<double> 40 36 { 41 public FloatStackdepthExpression() : base("FLOAT.STACKDEPTH") 42 { } 37 protected override string InitStringRepresentation() { return "FLOAT.STACKDEPTH"; } 43 38 44 39 public override void Eval(IInterpreter interpreter) 45 40 { 46 this.Eval(interpreter.FloatStack, interpreter.IntegerStack);41 Eval(interpreter.FloatStack, interpreter.IntegerStack); 47 42 } 48 43 } … … 50 45 public class BooleanStackdepthExpression : StackdepthExpression<bool> 51 46 { 52 public BooleanStackdepthExpression() : base("BOOLEAN.STACKDEPTH") 53 { } 47 protected override string InitStringRepresentation() { return "BOOLEAN.STACKDEPTH"; } 54 48 55 49 public override void Eval(IInterpreter interpreter) 56 50 { 57 this.Eval(interpreter.BooleanStack, interpreter.IntegerStack);51 Eval(interpreter.BooleanStack, interpreter.IntegerStack); 58 52 } 59 53 } … … 61 55 public class NameStackdepthExpression : StackdepthExpression<string> 62 56 { 63 public NameStackdepthExpression() : base("NAME.STACKDEPTH") 64 { } 57 protected override string InitStringRepresentation() { return "NAME.STACKDEPTH"; } 65 58 66 59 public override void Eval(IInterpreter interpreter) 67 60 { 68 this.Eval(interpreter.NameStack, interpreter.IntegerStack);61 Eval(interpreter.NameStack, interpreter.IntegerStack); 69 62 } 70 63 } … … 72 65 public class ExecStackdepthExpression : StackdepthExpression<Expression> 73 66 { 74 public ExecStackdepthExpression() : base("EXEC.STACKDEPTH") 75 { } 67 protected override string InitStringRepresentation() { return "EXEC.STACKDEPTH"; } 76 68 77 69 public override void Eval(IInterpreter interpreter) 78 70 { 79 this.Eval(interpreter.ExecStack, interpreter.IntegerStack);71 Eval(interpreter.ExecStack, interpreter.IntegerStack); 80 72 } 81 73 } … … 83 75 public class CodeStackdepthExpression : StackdepthExpression<Expression> 84 76 { 85 public CodeStackdepthExpression() : base("CODE.STACKDEPTH") 86 { } 77 protected override string InitStringRepresentation() { return "CODE.STACKDEPTH"; } 87 78 88 79 public override void Eval(IInterpreter interpreter) 89 80 { 90 this.Eval(interpreter.CodeStack, interpreter.IntegerStack);81 Eval(interpreter.CodeStack, interpreter.IntegerStack); 91 82 } 92 83 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/SwapExpressions.cs
r14392 r14398 8 8 /// </summary> 9 9 /// <typeparam name="T">Stacktype</typeparam> 10 public abstract class SwapExpression<T> : Expression10 public abstract class SwapExpression<T> : StatelessExpression 11 11 { 12 public SwapExpression(string stringRepresentation) : base(stringRepresentation)13 { }14 15 12 public void Eval(IStack<T> stack) 16 13 { … … 24 21 public class IntegerSwapExpression : SwapExpression<long> 25 22 { 26 public IntegerSwapExpression() : base("INTEGER.SWAP") 27 { } 23 protected override string InitStringRepresentation() { return "INTEGER.SWAP"; } 28 24 29 25 public override void Eval(IInterpreter interpreter) 30 26 { 31 this.Eval(interpreter.IntegerStack);27 Eval(interpreter.IntegerStack); 32 28 } 33 29 } … … 35 31 public class FloatSwapExpression : SwapExpression<double> 36 32 { 37 public FloatSwapExpression() : base("FLOAT.SWAP") 38 { } 33 protected override string InitStringRepresentation() { return "FLOAT.SWAP"; } 39 34 40 35 public override void Eval(IInterpreter interpreter) 41 36 { 42 this.Eval(interpreter.FloatStack);37 Eval(interpreter.FloatStack); 43 38 } 44 39 } … … 46 41 public class BooleanSwapExpression : SwapExpression<bool> 47 42 { 48 public BooleanSwapExpression() : base("BOOLEAN.SWAP") 49 { } 43 protected override string InitStringRepresentation() { return "BOOLEAN.SWAP"; } 50 44 51 45 public override void Eval(IInterpreter interpreter) 52 46 { 53 this.Eval(interpreter.BooleanStack);47 Eval(interpreter.BooleanStack); 54 48 } 55 49 } … … 57 51 public class NameSwapExpression : SwapExpression<string> 58 52 { 59 public NameSwapExpression() : base("NAME.SWAP") 60 { } 53 protected override string InitStringRepresentation() { return "NAME.SWAP"; } 61 54 62 55 public override void Eval(IInterpreter interpreter) 63 56 { 64 this.Eval(interpreter.NameStack);57 Eval(interpreter.NameStack); 65 58 } 66 59 } … … 68 61 public class ExecSwapExpression : SwapExpression<Expression> 69 62 { 70 public ExecSwapExpression() : base("EXEC.SWAP") 71 { } 63 protected override string InitStringRepresentation() { return "EXEC.SWAP"; } 72 64 73 65 public override void Eval(IInterpreter interpreter) 74 66 { 75 this.Eval(interpreter.ExecStack);67 Eval(interpreter.ExecStack); 76 68 } 77 69 } … … 79 71 public class CodeSwapExpression : SwapExpression<Expression> 80 72 { 81 public CodeSwapExpression() : base("CODE.SWAP") 82 { } 73 protected override string InitStringRepresentation() { return "CODE.SWAP"; } 83 74 84 75 public override void Eval(IInterpreter interpreter) 85 76 { 86 this.Eval(interpreter.CodeStack);77 Eval(interpreter.CodeStack); 87 78 } 88 79 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/YankDuplicateExpressions.cs
r14392 r14398 9 9 /// </summary> 10 10 /// <typeparam name="T">Stacktype</typeparam> 11 public abstract class YankDuplicateExpression<T> : Expression11 public abstract class YankDuplicateExpression<T> : StatelessExpression 12 12 { 13 public YankDuplicateExpression(string stringRepresentation) : base(stringRepresentation)14 { }15 16 13 public void Eval(IStack<T> stack, IStack<long> integerStack, int count = 1) 17 14 { … … 35 32 public class IntegerYankDuplicateExpression : YankDuplicateExpression<long> 36 33 { 37 public IntegerYankDuplicateExpression() : base("INTEGER.YANKDUP") 38 { } 34 protected override string InitStringRepresentation() { return "INTEGER.YANKDUP"; } 39 35 40 36 public override void Eval(IInterpreter interpreter) 41 37 { 42 this.Eval(interpreter.IntegerStack, interpreter.IntegerStack, 2);38 Eval(interpreter.IntegerStack, interpreter.IntegerStack, 2); 43 39 } 44 40 } … … 46 42 public class FloatYankDuplicateExpression : YankDuplicateExpression<double> 47 43 { 48 public FloatYankDuplicateExpression() : base("FLOAT.YANKDUP") 49 { } 44 protected override string InitStringRepresentation() { return "FLOAT.YANKDUP"; } 50 45 51 46 public override void Eval(IInterpreter interpreter) 52 47 { 53 this.Eval(interpreter.FloatStack, interpreter.IntegerStack);48 Eval(interpreter.FloatStack, interpreter.IntegerStack); 54 49 } 55 50 } … … 57 52 public class BooleanYankDuplicateExpression : YankDuplicateExpression<bool> 58 53 { 59 public BooleanYankDuplicateExpression() : base("BOOLEAN.YANKDUP") 60 { } 54 protected override string InitStringRepresentation() { return "BOOLEAN.YANKDUP"; } 61 55 62 56 public override void Eval(IInterpreter interpreter) 63 57 { 64 this.Eval(interpreter.BooleanStack, interpreter.IntegerStack);58 Eval(interpreter.BooleanStack, interpreter.IntegerStack); 65 59 } 66 60 } … … 68 62 public class NameYankDuplicateExpression : YankDuplicateExpression<string> 69 63 { 70 public NameYankDuplicateExpression() : base("NAME.YANKDUP") 71 { } 64 protected override string InitStringRepresentation() { return "NAME.YANKDUP"; } 72 65 73 66 public override void Eval(IInterpreter interpreter) 74 67 { 75 this.Eval(interpreter.NameStack, interpreter.IntegerStack);68 Eval(interpreter.NameStack, interpreter.IntegerStack); 76 69 } 77 70 } … … 79 72 public class ExecYankDuplicateExpression : YankDuplicateExpression<Expression> 80 73 { 81 public ExecYankDuplicateExpression() : base("EXEC.YANKDUP") 82 { } 74 protected override string InitStringRepresentation() { return "EXEC.YANKDUP"; } 83 75 84 76 public override void Eval(IInterpreter interpreter) 85 77 { 86 this.Eval(interpreter.ExecStack, interpreter.IntegerStack);78 Eval(interpreter.ExecStack, interpreter.IntegerStack); 87 79 } 88 80 } … … 90 82 public class CodeYankDuplicateExpression : YankDuplicateExpression<Expression> 91 83 { 92 public CodeYankDuplicateExpression() : base("CODE.YANKDUP") 93 { } 84 protected override string InitStringRepresentation() { return "CODE.YANKDUP"; } 94 85 95 86 public override void Eval(IInterpreter interpreter) 96 87 { 97 this.Eval(interpreter.CodeStack, interpreter.IntegerStack);88 Eval(interpreter.CodeStack, interpreter.IntegerStack); 98 89 } 99 90 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/YankExpressions.cs
r14392 r14398 9 9 /// </summary> 10 10 /// <typeparam name="T">Stacktype</typeparam> 11 public abstract class YankExpression<T> : Expression11 public abstract class YankExpression<T> : StatelessExpression 12 12 { 13 public YankExpression(string stringRepresentation) : base(stringRepresentation)14 { }15 16 13 public void Eval(IStack<T> stack, IStack<long> integerStack, int count = 1) 17 14 { … … 33 30 public class IntegerYankExpression : YankExpression<long> 34 31 { 35 public IntegerYankExpression() : base("INTEGER.YANK") 36 { } 32 protected override string InitStringRepresentation() { return "INTEGER.YANK"; } 37 33 38 34 public override void Eval(IInterpreter interpreter) 39 35 { 40 this.Eval(interpreter.IntegerStack, interpreter.IntegerStack, 2);36 Eval(interpreter.IntegerStack, interpreter.IntegerStack, 2); 41 37 } 42 38 } … … 44 40 public class FloatYankExpression : YankExpression<double> 45 41 { 46 public FloatYankExpression() : base("FLOAT.YANK") 47 { } 42 protected override string InitStringRepresentation() { return "FLOAT.YANK"; } 48 43 49 44 public override void Eval(IInterpreter interpreter) 50 45 { 51 this.Eval(interpreter.FloatStack, interpreter.IntegerStack);46 Eval(interpreter.FloatStack, interpreter.IntegerStack); 52 47 } 53 48 } … … 55 50 public class BooleanYankExpression : YankExpression<bool> 56 51 { 57 public BooleanYankExpression() : base("BOOLEAN.YANK") 58 { } 52 protected override string InitStringRepresentation() { return "BOOLEAN.YANK"; } 59 53 60 54 public override void Eval(IInterpreter interpreter) 61 55 { 62 this.Eval(interpreter.BooleanStack, interpreter.IntegerStack);56 Eval(interpreter.BooleanStack, interpreter.IntegerStack); 63 57 } 64 58 } … … 66 60 public class NameYankExpression : YankExpression<string> 67 61 { 68 public NameYankExpression() : base("NAME.YANK") 69 { } 62 protected override string InitStringRepresentation() { return "NAME.YANK"; } 70 63 71 64 public override void Eval(IInterpreter interpreter) 72 65 { 73 this.Eval(interpreter.NameStack, interpreter.IntegerStack);66 Eval(interpreter.NameStack, interpreter.IntegerStack); 74 67 } 75 68 } … … 77 70 public class ExecYankExpression : YankExpression<Expression> 78 71 { 79 public ExecYankExpression() : base("EXEC.YANK") 80 { } 72 protected override string InitStringRepresentation() { return "EXEC.YANK"; } 81 73 82 74 public override void Eval(IInterpreter interpreter) 83 75 { 84 this.Eval(interpreter.ExecStack, interpreter.IntegerStack);76 Eval(interpreter.ExecStack, interpreter.IntegerStack); 85 77 } 86 78 } … … 88 80 public class CodeYankExpression : YankExpression<Expression> 89 81 { 90 public CodeYankExpression() : base("CODE.YANK") 91 { } 82 protected override string InitStringRepresentation() { return "CODE.YANK"; } 92 83 93 84 public override void Eval(IInterpreter interpreter) 94 85 { 95 this.Eval(interpreter.CodeStack, interpreter.IntegerStack);86 Eval(interpreter.CodeStack, interpreter.IntegerStack); 96 87 } 97 88 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/CodeGenerator.cs
r14392 r14398 46 46 if (points == 1) 47 47 { 48 var index = random.Next(0, ExpressionTable.Count + this.interpreter.CustomExpressions.Count - 2);49 return new[] { this.CreateExpression(index) };48 var index = random.Next(0, ExpressionTable.Count + interpreter.CustomExpressions.Count - 2); 49 return new[] { CreateExpression(index) }; 50 50 } 51 51 else … … 73 73 return (index >= 0 && index < ExpressionTable.Count) 74 74 ? ExpressionTable.GetExpression(index) 75 : new NameDefineXExecExpression( this.interpreter.CustomExpressions.ElementAt(index - (ExpressionTable.Count - 1)).Key);75 : new NameDefineXExecExpression(interpreter.CustomExpressions.ElementAt(index - (ExpressionTable.Count - 1)).Key); 76 76 } 77 77 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.csproj
r14397 r14398 52 52 </ItemGroup> 53 53 <ItemGroup> 54 <Compile Include="Data\ExpressionNode.cs" /> 55 <Compile Include="Data\ExpressionTree.cs" /> 54 56 <Compile Include="Exporter\Exporter.cs" /> 55 57 <Compile Include="Expressions\BooleanExpressions.cs" /> … … 63 65 <Compile Include="Expressions\EqualsExpressions.cs" /> 64 66 <Compile Include="Expressions\RandExpressions.cs" /> 65 <Compile Include="Expressions\SetExpressions.cs" /> 67 <Compile Include="Expressions\StatefullExpression.cs" /> 68 <Compile Include="Expressions\StatelessExpression.cs" /> 66 69 <Compile Include="Expressions\YankDuplicateExpressions.cs" /> 67 70 <Compile Include="Expressions\YankExpressions.cs" /> … … 82 85 <Compile Include="Simplifier\ISimplifier.cs" /> 83 86 <Compile Include="Simplifier\RandomSimplifier.cs" /> 84 <Compile Include="StateFullExpressionAttribute.cs" />85 87 <Compile Include="Generators\BooleanGenerator.cs" /> 86 88 <Compile Include="Generators\FloatGenerator.cs" /> -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Interpreter/Configuration.cs
r14397 r14398 8 8 public Configuration() 9 9 { 10 this.IsBooleanStackEnabled = true;11 this.IsIntegerStackEnabled = true;12 this.IsFloatStackEnabled = true;13 this.IsCodeStackEnabled = true;14 this.IsNameStackEnabled = true;10 IsBooleanStackEnabled = true; 11 IsIntegerStackEnabled = true; 12 IsFloatStackEnabled = true; 13 IsCodeStackEnabled = true; 14 IsNameStackEnabled = true; 15 15 16 this.EvalPushLimit = 4096;17 this.MaxPointsInProgram = 128;18 this.MaxPointsInRandomExpression = 1024;16 EvalPushLimit = 4096; 17 MaxPointsInProgram = 128; 18 MaxPointsInRandomExpression = 1024; 19 19 20 this.TopLevelPushCode = true;21 this.TopLevelPopCode = false;20 TopLevelPushCode = true; 21 TopLevelPopCode = false; 22 22 23 this.RandomSeedMax = 30081;24 this.RandomSeedMin = 0;25 this.RandomSeed = null;23 RandomSeedMax = 30081; 24 RandomSeedMin = 0; 25 RandomSeed = null; 26 26 27 this.MinRandomInteger = -128;28 this.MaxRandomInteger = 128;29 this.MinRandomFloat = -128D;30 this.MaxRandomFloat = 128D;31 this.NewErcNameProbability = 0.5;27 MinRandomInteger = -128; 28 MaxRandomInteger = 128; 29 MinRandomFloat = -128D; 30 MaxRandomFloat = 128D; 31 NewErcNameProbability = 0.5; 32 32 } 33 33 -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Interpreter/PushGPInterpreter.cs
r14392 r14398 16 16 public PushGPInterpreter(Configuration config = null) 17 17 { 18 this.Configuration = config ?? new Configuration();19 20 this.CodeStack = new PushGPStack<Expression>(128);21 this.ExecStack = new PushGPStack<Expression>(128);22 this.NameStack = new PushGPStack<string>(16);23 this.BooleanStack = new PushGPStack<bool>(16);24 this.IntegerStack = new PushGPStack<long>(16);25 this.FloatStack = new PushGPStack<double>(16);26 27 this.CustomExpressions = new Dictionary<string, Expression>();28 29 this.CodeGenerator = new CodeGenerator(this);18 Configuration = config ?? new Configuration(); 19 20 CodeStack = new PushGPStack<Expression>(128); 21 ExecStack = new PushGPStack<Expression>(128); 22 NameStack = new PushGPStack<string>(16); 23 BooleanStack = new PushGPStack<bool>(16); 24 IntegerStack = new PushGPStack<long>(16); 25 FloatStack = new PushGPStack<double>(16); 26 27 CustomExpressions = new Dictionary<string, Expression>(); 28 29 CodeGenerator = new CodeGenerator(this); 30 30 } 31 31 … … 35 35 public bool IsPaused { get; private set; } 36 36 public bool IsAborted { get; private set; } 37 public bool IsRunning { get { return this.ExecStack.Count > 0 && !IsPaused && !IsAborted && ExecCounter < Configuration.EvalPushLimit; } }38 public bool IsCompleted { get { return this.ExecStack.Count == 0; } }37 public bool IsRunning { get { return ExecStack.Count > 0 && !IsPaused && !IsAborted && ExecCounter < Configuration.EvalPushLimit; } } 38 public bool IsCompleted { get { return ExecStack.Count == 0; } } 39 39 public bool CanStep { get { return !IsCompleted && !IsAborted && IsPaused; } } 40 40 … … 54 54 public void Reset() 55 55 { 56 this.IsAborted = false;57 this.IsPaused = false;58 this.currentTask = null;59 60 this.Clear();56 IsAborted = false; 57 IsPaused = false; 58 currentTask = null; 59 60 Clear(); 61 61 } 62 62 63 63 public void Clear() 64 64 { 65 this.ExecCounter = 0;66 67 this.ExecStack.Clear();68 this.CodeStack.Clear();69 this.NameStack.Clear();70 this.BooleanStack.Clear();71 this.IntegerStack.Clear();72 this.FloatStack.Clear();73 74 this.CustomExpressions.Clear();65 ExecCounter = 0; 66 67 ExecStack.Clear(); 68 CodeStack.Clear(); 69 NameStack.Clear(); 70 BooleanStack.Clear(); 71 IntegerStack.Clear(); 72 FloatStack.Clear(); 73 74 CustomExpressions.Clear(); 75 75 } 76 76 … … 78 78 { 79 79 var program = Encode(code); 80 this.Interpret(program);80 Interpret(program); 81 81 } 82 82 … … 85 85 var program = Encode(code); 86 86 87 this.currentTask = this.InterpretAsync(program, paused, token);87 currentTask = InterpretAsync(program, paused, token); 88 88 89 89 return currentTask; … … 92 92 public Task InterpretAsync(Expression program, bool paused = false, CancellationToken token = default(CancellationToken)) 93 93 { 94 this.IsPaused = paused;95 this.currentTask = Task.Run(() => this.Interpret(program), token);94 IsPaused = paused; 95 currentTask = Task.Run(() => Interpret(program), token); 96 96 97 97 return currentTask; … … 103 103 * If the top expression is a single statement then the loop has nothing to do 104 104 * Otherwise the expand expression will be evaluated and pushes code onto the EXEC stack */ 105 this.ExecStack.Push(program);106 107 if ( this.Configuration.TopLevelPushCode)108 { 109 this.CodeStack.Insert(0, program);105 ExecStack.Push(program); 106 107 if (Configuration.TopLevelPushCode) 108 { 109 CodeStack.Insert(0, program); 110 110 } 111 111 112 112 // run top expression 113 this.ExecStack.Pop().Eval(this);114 115 this.Interpret();113 ExecStack.Pop().Eval(this); 114 115 Interpret(); 116 116 } 117 117 118 118 private void Interpret() 119 119 { 120 while ( this.IsRunning)121 { 122 this.DoStep();123 this.ExecCounter++;124 } 125 126 this.Finally();120 while (IsRunning) 121 { 122 DoStep(); 123 ExecCounter++; 124 } 125 126 Finally(); 127 127 } 128 128 129 129 private void Finally() 130 130 { 131 if ( this.IsCompleted && this.Configuration.TopLevelPopCode && this.CodeStack.Count > 0)132 { 133 this.CodeStack.Pop();131 if (IsCompleted && Configuration.TopLevelPopCode && CodeStack.Count > 0) 132 { 133 CodeStack.Pop(); 134 134 } 135 135 } … … 137 137 private void DoStep() 138 138 { 139 this.ExecStack.Pop().Eval(this);139 ExecStack.Pop().Eval(this); 140 140 } 141 141 142 142 private Task InterpreteAsync() 143 143 { 144 this.currentTask = Task.Run(() => this.Interpret());145 146 return this.currentTask;144 currentTask = Task.Run(() => Interpret()); 145 146 return currentTask; 147 147 } 148 148 149 149 public async Task AbortAsync() 150 150 { 151 if ( this.IsAborted || this.IsCompleted)151 if (IsAborted || IsCompleted) 152 152 return; 153 153 154 this.IsAborted = true;155 156 if ( this.currentTask != null)157 { 158 await this.currentTask;154 IsAborted = true; 155 156 if (currentTask != null) 157 { 158 await currentTask; 159 159 } 160 160 } … … 162 162 public async Task AbortAndResetAsync() 163 163 { 164 await this.AbortAsync();165 this.Reset();164 await AbortAsync(); 165 Reset(); 166 166 } 167 167 168 168 public async Task PauseAsync() 169 169 { 170 if ( this.IsPaused || this.IsCompleted)170 if (IsPaused || IsCompleted) 171 171 return; 172 172 173 this.IsPaused = true;174 175 if ( this.currentTask != null)176 { 177 await this.currentTask;173 IsPaused = true; 174 175 if (currentTask != null) 176 { 177 await currentTask; 178 178 } 179 179 } … … 181 181 public Task ResumeAsync() 182 182 { 183 if ( this.IsPaused || !this.IsAborted)184 { 185 this.IsPaused = false;186 return this.InterpreteAsync();187 } 188 189 return this.currentTask;183 if (IsPaused || !IsAborted) 184 { 185 IsPaused = false; 186 return InterpreteAsync(); 187 } 188 189 return currentTask; 190 190 } 191 191 192 192 public void Step() 193 193 { 194 if ( this.CanStep)195 { 196 this.DoStep();197 this.Finally();194 if (CanStep) 195 { 196 DoStep(); 197 Finally(); 198 198 } 199 199 } … … 203 203 for (var i = 0; i < count; i++) 204 204 { 205 this.Step();205 Step(); 206 206 } 207 207 } … … 229 229 public void PrintStacks() 230 230 { 231 this.PrintStack("EXEC", this.ExecStack);232 this.PrintStack("CODE", this.CodeStack);233 this.PrintStack("NAME", this.NameStack);234 this.PrintStack("BOOLEAN", this.BooleanStack);235 this.PrintStack("FLOAT", this.FloatStack);236 this.PrintStack("INTEGER", this.IntegerStack);231 PrintStack("EXEC", ExecStack); 232 PrintStack("CODE", CodeStack); 233 PrintStack("NAME", NameStack); 234 PrintStack("BOOLEAN", BooleanStack); 235 PrintStack("FLOAT", FloatStack); 236 PrintStack("INTEGER", IntegerStack); 237 237 } 238 238 -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Simplifier/RandomSimplifier.cs
r14397 r14398 17 17 var clone = program.Clone() as ExecExpandExpression; 18 18 19 for (var i = 0; i < this.Trys; i++)19 for (var i = 0; i < Trys; i++) 20 20 { 21 21 var index = rand.Next(0, clone.TotalCount - 1); 22 22 23 } 23 24 -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Stack/PushGPStack.cs
r14392 r14398 20 20 public PushGPStack(int capacity = 0) 21 21 { 22 this.data = new List<T>();23 } 24 25 public T Top { get { return this.data[Count - 1]; } }26 public T TopOrDefault { get { return Count > 0 ? this.data[Count - 1] : default(T); } }27 public T Bottom { get { return this.data[0]; } }28 public T BottomOrDefault { get { return Count > 0 ? this.data[0] : default(T); } }29 public int Count { get { return this.data.Count; } }30 public int Capacity { get { return this.data.Capacity; } }31 public bool IsEmpty { get { return this.Count == 0; } }22 data = new List<T>(); 23 } 24 25 public T Top { get { return data[Count - 1]; } } 26 public T TopOrDefault { get { return Count > 0 ? data[Count - 1] : default(T); } } 27 public T Bottom { get { return data[0]; } } 28 public T BottomOrDefault { get { return Count > 0 ? data[0] : default(T); } } 29 public int Count { get { return data.Count; } } 30 public int Capacity { get { return data.Capacity; } } 31 public bool IsEmpty { get { return Count == 0; } } 32 32 33 33 public bool IsReadOnly { get { return false; } } … … 35 35 public void Add(T item) 36 36 { 37 this.Push(item);37 Push(item); 38 38 } 39 39 40 40 public void Clear() 41 41 { 42 this.data.Clear();42 data.Clear(); 43 43 } 44 44 45 45 public bool Contains(T item) 46 46 { 47 return this.data.Contains(item);47 return data.Contains(item); 48 48 } 49 49 50 50 public void CopyTo(T[] array, int arrayIndex) 51 51 { 52 this.data.CopyTo(array, arrayIndex);52 data.CopyTo(array, arrayIndex); 53 53 } 54 54 55 55 public IEnumerator<T> GetEnumerator() 56 56 { 57 return this.data.GetEnumerator();57 return data.GetEnumerator(); 58 58 } 59 59 60 60 public T ElementAt(int index) 61 61 { 62 return this.data[index];62 return data[index]; 63 63 } 64 64 … … 66 66 public T ReverseElementAt(int offset) 67 67 { 68 return this.data[this.Count - 1 - offset];68 return data[Count - 1 - offset]; 69 69 } 70 70 71 71 public void SetTop(T value) 72 72 { 73 this.data[Count - 1] = value;73 data[Count - 1] = value; 74 74 } 75 75 76 76 public T this[int key] 77 77 { 78 get { return this.data[key]; }79 set { this.data[key] = value; }78 get { return data[key]; } 79 set { data[key] = value; } 80 80 } 81 81 82 82 public void Swap(int count) 83 83 { 84 var top = this.Top;85 var bottomIndex = this.Count - count;86 87 for (var i = this.Count - 1; i > bottomIndex; i--)88 { 89 this.data[i] = this.data[i - 1];90 } 91 92 this.data[bottomIndex] = top;84 var top = Top; 85 var bottomIndex = Count - count; 86 87 for (var i = Count - 1; i > bottomIndex; i--) 88 { 89 data[i] = data[i - 1]; 90 } 91 92 data[bottomIndex] = top; 93 93 } 94 94 95 95 public void Yank(int index) 96 96 { 97 var item = this.ElementAt(index);98 this.data.RemoveAt(index);99 this.data.Add(item);97 var item = ElementAt(index); 98 data.RemoveAt(index); 99 data.Add(item); 100 100 } 101 101 102 102 public T Pop() 103 103 { 104 var value = this.data[Count - 1];105 this.data.RemoveAt(Count - 1);104 var value = data[Count - 1]; 105 data.RemoveAt(Count - 1); 106 106 107 107 return value; … … 113 113 114 114 var items = new T[count]; 115 this.data.CopyTo(startIndex, items, 0, count);116 this.Remove(count);115 data.CopyTo(startIndex, items, 0, count); 116 Remove(count); 117 117 118 118 return items; … … 121 121 public void Push(T item) 122 122 { 123 this.data.Add(item);123 data.Add(item); 124 124 } 125 125 … … 128 128 for (var i = 0; i < items.Length; i++) 129 129 { 130 this.data.Add((T)items[i]);130 data.Add((T)items[i]); 131 131 } 132 132 } … … 137 137 var items = new T[count]; 138 138 139 this.data.CopyTo(startIndex, items, 0, count);140 this.Remove(count - 1);141 142 this.data[Count - 1] = templateFunc(items);139 data.CopyTo(startIndex, items, 0, count); 140 Remove(count - 1); 141 142 data[Count - 1] = templateFunc(items); 143 143 } 144 144 … … 147 147 for (var i = 0; i < items.Length; i++) 148 148 { 149 this.data.Add(items[i]);149 data.Add(items[i]); 150 150 } 151 151 } … … 153 153 public void Push(IEnumerable<T> items) 154 154 { 155 this.data.AddRange(items);155 data.AddRange(items); 156 156 } 157 157 158 158 public void Insert(int index, T item) 159 159 { 160 this.data.Insert(index, item);160 data.Insert(index, item); 161 161 } 162 162 163 163 public void Insert(int index, params T[] items) 164 164 { 165 this.data.InsertRange(index, items);165 data.InsertRange(index, items); 166 166 } 167 167 168 168 public void Insert(int index, IEnumerable<T> items) 169 169 { 170 this.data.InsertRange(index, items);170 data.InsertRange(index, items); 171 171 } 172 172 … … 174 174 { 175 175 var index = Count - 1; 176 if ( this.Count > 0 && this.data[index].Equals(item))177 { 178 this.data.RemoveAt(index);176 if (Count > 0 && data[index].Equals(item)) 177 { 178 data.RemoveAt(index); 179 179 return true; 180 180 } … … 187 187 public void RemoveTop() 188 188 { 189 this.data.RemoveAt(this.Count - 1);189 data.RemoveAt(Count - 1); 190 190 } 191 191 … … 194 194 for (var i = 0; i < count; i++) 195 195 { 196 this.data.RemoveAt(Count - 1);196 data.RemoveAt(Count - 1); 197 197 } 198 198 } … … 200 200 public void RemoveAt(int index) 201 201 { 202 this.data.RemoveAt(index);202 data.RemoveAt(index); 203 203 } 204 204 205 205 public void RemoveAt(int index, int count) 206 206 { 207 this.data.RemoveRange(index, count);207 data.RemoveRange(index, count); 208 208 } 209 209 210 210 public bool TryPop(out T item) 211 211 { 212 if ( this.Count > 0)213 { 214 item = this.Pop();212 if (Count > 0) 213 { 214 item = Pop(); 215 215 return true; 216 216 } … … 224 224 IEnumerator IEnumerable.GetEnumerator() 225 225 { 226 return ( this.data as IEnumerable).GetEnumerator();226 return (data as IEnumerable).GetEnumerator(); 227 227 } 228 228 229 229 public override string ToString() 230 230 { 231 return string.Join(Delimiter, this.data);231 return string.Join(Delimiter, data); 232 232 } 233 233 }
Note: See TracChangeset
for help on using the changeset viewer.