- Timestamp:
- 03/11/17 20:07:13 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push
- Files:
-
- 1 added
- 2 deleted
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/CodeExpressions.cs
r14745 r14746 108 108 var second = interpreter.CodeStack.Top; 109 109 110 var isFirstList = first. CanExpand;111 var isSecondList = second. CanExpand;112 113 Expression[] result = null;110 var isFirstList = first.IsProgram; 111 var isSecondList = second.IsProgram; 112 113 PushProgram result; 114 114 115 115 if (isFirstList) { 116 var expand1 = first as PushProgram;116 var program1 = first as PushProgram; 117 117 118 118 if (isSecondList) { 119 var expand2 = second as PushProgram;120 var size = expand2.State.Length + expand1.State.Length;119 var program2 = second as PushProgram; 120 var size = program2.Expressions.Count + program1.Expressions.Count; 121 121 122 122 // if size > maxPointsInProgram this expressions results in a NOOP 123 123 if (size > interpreter.Configuration.MaxPointsInProgram) return false; 124 124 125 result = new Expression[size]; 126 127 Array.Copy(expand2.State, result, expand2.State.Length); 128 Array.Copy( 129 expand1.State, 130 0, 131 result, 132 expand2.State.Length, 133 expand1.State.Length); 125 result = PushProgram.Merge(interpreter.PushProgramPool, program2, program1); 134 126 } else { 135 var size = expand1.State.Length+ 1;127 var size = program1.Expressions.Count + 1; 136 128 137 129 // if size > maxPointsInProgram this expressions results in a NOOP 138 130 if (size > interpreter.Configuration.MaxPointsInProgram) return false; 139 131 140 result = new Expression[size]; 141 result[0] = second; 142 143 Array.Copy(expand1.State, 0, result, 1, expand1.State.Length); 132 result = PushProgram.Merge(interpreter.PushProgramPool, second, program1); 144 133 } 145 134 } else if (isSecondList) { 146 var expand2 = second as PushProgram; 147 148 var size = expand2.State.Length + 1; 135 var program2 = second as PushProgram; 136 var size = program2.Expressions.Count + 1; 149 137 150 138 // if size > maxPointsInProgram this expressions results in a NOOP 151 139 if (size > interpreter.Configuration.MaxPointsInProgram) return false; 152 140 153 result = new Expression[size]; 154 result[0] = first; 155 156 Array.Copy(expand2.State as Expression[], 0, result, 1, expand2.State.Length); 141 result = PushProgram.Merge(interpreter.PushProgramPool, first, program2); 157 142 } else { 158 result = new[] { second, first };143 result = PushProgram.Create(interpreter.PushProgramPool, second, first); 159 144 } 160 145 161 var expression = new PushProgram(result); 162 163 interpreter.CodeStack.SetTop(expression); 146 interpreter.CodeStack.SetTop(result); 164 147 165 148 return true; … … 177 160 178 161 var expression = interpreter.CodeStack.Pop(); 179 var isExpandExpression = expression. CanExpand;162 var isExpandExpression = expression.IsProgram; 180 163 181 164 interpreter.BooleanStack.Push(!isExpandExpression); … … 199 182 200 183 if (expand.IsEmpty) return false; 201 var first = expand. State[expand.State.Length- 1];184 var first = expand.Expressions[expand.Expressions.Count - 1]; 202 185 203 186 interpreter.CodeStack.SetTop(first); … … 217 200 if (interpreter.CodeStack.Count == 0) return false; 218 201 219 PushProgram expandExpression;202 PushProgram result; 220 203 var top = interpreter.CodeStack.Top; 221 204 222 if (top.CanExpand) { 223 var expand = (PushProgram)top; 224 225 if (expand.IsEmpty) return false; 226 var length = expand.State.Length - 1; 227 var newExpressions = new Expression[length]; 228 229 Array.Copy((Expression[])expand.State, 0, newExpressions, 0, length); 230 231 expandExpression = new PushProgram(newExpressions); 205 if (top.IsProgram) { 206 var program = (PushProgram)top; 207 208 if (program.IsEmpty) return false; 209 210 result = program.Copy(interpreter.PushProgramPool, 0, program.Expressions.Count - 1); 232 211 } else { 233 expandExpression= PushProgram.Empty;212 result = PushProgram.Empty; 234 213 } 235 214 236 interpreter.CodeStack.SetTop( expandExpression);215 interpreter.CodeStack.SetTop(result); 237 216 return true; 238 217 } … … 252 231 PushProgram result; 253 232 254 if (interpreter.CodeStack.Top. CanExpand) {233 if (interpreter.CodeStack.Top.IsProgram) { 255 234 var first = (PushProgram)interpreter.CodeStack.Pop(); 256 var size = first. State.Length+ 1;235 var size = first.Expressions.Count + 1; 257 236 258 237 if (size > interpreter.Configuration.MaxPointsInProgram) return false; … … 260 239 var expressions = new Expression[size]; 261 240 262 expressions[first.State.Length] = interpreter.CodeStack.Top;263 Array.Copy(first.State as Expression[], expressions, first.State.Length);264 265 result = new PushProgram(expressions);241 first.CopyExpressionsTo(expressions); 242 expressions[first.Expressions.Count] = interpreter.CodeStack.Top; 243 244 result = PushProgram.Create(interpreter.PushProgramPool, expressions); 266 245 } else { 267 result = new PushProgram(interpreter.CodeStack.Pop(), interpreter.CodeStack.Top);246 result = PushProgram.Create(interpreter.PushProgramPool, interpreter.CodeStack.Top); 268 247 } 269 248 … … 303 282 304 283 var subContainer = 305 current. State.Where(e => e.CanExpand)284 current.Expressions.Where(e => e.IsProgram) 306 285 .Select(e => GetContainer(e as PushProgram, target)) 307 286 .Where(e => e != null); … … 309 288 var execExpandExpressions = subContainer as IList<PushProgram> ?? subContainer.ToList(); 310 289 return execExpandExpressions.Any() 311 ? execExpandExpressions.OrderBy(c => c. State.Length).LastOrDefault()312 : current. State.Contains(target)290 ? execExpandExpressions.OrderBy(c => c.Expressions.Count).LastOrDefault() 291 : current.Expressions.Contains(target) 313 292 ? current 314 293 : null; … … 324 303 public override bool Eval(IPushInterpreter interpreter) { 325 304 if (interpreter.CodeStack.Count < 2 || 326 !interpreter.CodeStack[interpreter.CodeStack.Count - 2]. CanExpand)305 !interpreter.CodeStack[interpreter.CodeStack.Count - 2].IsProgram) 327 306 return false; 328 307 329 308 var values = interpreter.CodeStack.Pop(2); 330 309 var second = (PushProgram)values[0]; 331 var contains = second. State.Contains(values[1]);310 var contains = second.Expressions.Contains(values[1]); 332 311 333 312 interpreter.BooleanStack.Push(contains); … … 413 392 414 393 private void DetermineUniqueItems(Expression source, IDictionary<int, int> items) { 415 if (source.CanExpand) { 416 var expand = source as PushProgram; 417 418 foreach (var e in expand.State) { 419 var id = e.GetHashCode(); 394 if (source.IsProgram) { 395 var program = source as PushProgram; 396 var expressions = program.Expressions; 397 398 for (var i = 0; i < expressions.Count; i++) { 399 var id = expressions[i].GetHashCode(); 420 400 if (!items.ContainsKey(id)) items.Add(id, 1); 421 401 else items[id]++; … … 442 422 if (interpreter.IntegerStack.Count == 0 || 443 423 interpreter.CodeStack.Count == 0 || 444 !interpreter.CodeStack.Top. CanExpand)424 !interpreter.CodeStack.Top.IsProgram) 445 425 return false; 446 426 … … 532 512 if (interpreter.IntegerStack.Count == 0 || 533 513 interpreter.CodeStack.Count < 2 || 534 !interpreter.CodeStack.Top. CanExpand)514 !interpreter.CodeStack.Top.IsProgram) 535 515 return false; 536 516 … … 540 520 541 521 Expression[] newExpressions; 542 if ( target.State.Length > 0) {543 index = target. State.Length - 1 - Math.Abs(index % target.State.Length);522 if (!target.IsEmpty) { 523 index = target.Expressions.Count - 1 - Math.Abs(index % target.Expressions.Count); 544 524 545 525 newExpressions = target.CopyExpressions(); 546 newExpressions[index] = source. CanExpand547 ? ((PushProgram)source).Copy( )526 newExpressions[index] = source.IsProgram 527 ? ((PushProgram)source).Copy(interpreter.PushProgramPool) 548 528 : source; 549 529 } else { … … 551 531 } 552 532 553 var result = new PushProgram(newExpressions);533 var result = PushProgram.Create(interpreter.PushProgramPool, newExpressions); 554 534 interpreter.CodeStack.SetTop(result); 555 535 … … 572 552 var count = 1; 573 553 574 if (expression. CanExpand)575 count = ((PushProgram)expression). State.Length;554 if (expression.IsProgram) 555 count = ((PushProgram)expression).Expressions.Count; 576 556 577 557 interpreter.IntegerStack.Push(count); … … 591 571 var first = interpreter.CodeStack.Pop(); 592 572 var second = interpreter.CodeStack.Top; 593 var expandExpression = new PushProgram(second, first);573 var expandExpression = PushProgram.Create(interpreter.PushProgramPool, second, first); 594 574 595 575 interpreter.CodeStack.SetTop(expandExpression); … … 610 590 var expressions = interpreter.CodeStack.Pop(2); 611 591 612 var contains = expressions[1]. CanExpand613 ? ((PushProgram)expressions[1]). State.Contains(expressions[0])592 var contains = expressions[1].IsProgram 593 ? ((PushProgram)expressions[1]).Expressions.Contains(expressions[0]) 614 594 : expressions[1].Equals(expressions[0]); 615 595 … … 635 615 Expression nthExpression; 636 616 637 if (expression. CanExpand) {617 if (expression.IsProgram) { 638 618 var subExpression = (PushProgram)expression; 639 619 … … 641 621 nthExpression = PushProgram.Empty; 642 622 } else { 643 var index = (int)(subExpression.State.Length - 1 - 644 Math.Abs(n % subExpression.State.Length)); 645 646 nthExpression = subExpression.State[index]; 623 var index = (int)(subExpression.Expressions.Count - 1 - Math.Abs(n % subExpression.Expressions.Count)); 624 625 nthExpression = subExpression.Expressions[index]; 647 626 } 648 627 } else { … … 674 653 Expression nthExpression; 675 654 676 if (expression. CanExpand) {677 var subExpressions = ((PushProgram)expression). State;678 679 if (subExpressions. Length< 2) {655 if (expression.IsProgram) { 656 var subExpressions = ((PushProgram)expression).Expressions; 657 658 if (subExpressions.Count < 2) { 680 659 nthExpression = PushProgram.Empty; 681 660 } else { 682 var index = (int)(subExpressions. Length - 2 - Math.Abs(n % (subExpressions.Length- 1)));661 var index = (int)(subExpressions.Count - 2 - Math.Abs(n % (subExpressions.Count - 1))); 683 662 684 663 nthExpression = subExpressions[index]; … … 723 702 724 703 var position = -1; 725 if (first.CanExpand) { 726 var expand = (PushProgram)first; 727 position = expand.State.Length - 1 728 - Array.FindIndex((Expression[])expand.State, e => e.Equals(second)); 704 if (first.IsProgram) { 705 var program = (PushProgram)first; 706 position = program.Expressions.Count - 1 - program.IndexOf(second); 729 707 } else if (first.Equals(second)) { 730 708 position = 0; … … 747 725 748 726 var expression = interpreter.CodeStack.Pop(); 749 var points = expression. CanExpand750 ? ((PushProgram)expression). State.Length727 var points = expression.IsProgram 728 ? ((PushProgram)expression).Expressions.Count 751 729 : 1; 752 730 … … 773 751 var second = expressions[0]; 774 752 var first = expressions[1] as PushProgram; 775 var newExpressions = new Expression[first.State.Length]; 776 777 for (var i = 0; i < first.State.Length; i++) 778 newExpressions[i] = first.State[i].Equals(third) 753 var firstExpressions = first.Expressions; 754 var newExpressions = new Expression[firstExpressions.Count]; 755 756 for (var i = 0; i < firstExpressions.Count; i++) 757 newExpressions[i] = firstExpressions[i].Equals(third) 779 758 ? second 780 759 /* no cloning needed here because first is removed and therefore newExpression is the only container of sub expressions */ 781 : first .State[i];782 783 var result = new PushProgram(newExpressions);760 : firstExpressions[i]; 761 762 var result = PushProgram.Create(interpreter.PushProgramPool, newExpressions); 784 763 785 764 interpreter.CodeStack.SetTop(result); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExecExpressions.cs
r14745 r14746 41 41 var top = interpreter.ExecStack.Top; 42 42 var execYExpression = ExpressionTable.GetStatelessExpression<ExecYExpression>(); 43 44 var result = new PushProgram(top, execYExpression); 43 var result = PushProgram.Create(interpreter.PushProgramPool, top, execYExpression); 45 44 46 45 interpreter.ExecStack.SetTop(result); … … 81 80 var c = interpreter.ExecStack.Top; 82 81 83 var newTop = new PushProgram(c, b);82 var newTop = PushProgram.Create(interpreter.PushProgramPool, c, b); 84 83 85 84 interpreter.ExecStack.SetTop(newTop); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/Expression.cs
r14745 r14746 7 7 using Interpreter; 8 8 9 [Serializable] 9 10 public abstract class Expression { 10 public bool CanExpand{ get { return this.GetType() == typeof(PushProgram); } }11 public bool IsProgram { get { return this.GetType() == typeof(PushProgram); } } 11 12 12 13 public static readonly IReadOnlyCollection<Expression> EmptyContainer = new Expression[0]; -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExpressionTable.cs
r14745 r14746 5 5 6 6 using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes; 7 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool; 7 8 using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; 8 9 … … 123 124 } 124 125 125 public static PushProgram GetProgram( int[] index) {126 public static PushProgram GetProgram(IManagedPool<PushProgram> pool, int[] index) { 126 127 var expressions = new Expression[index.Length]; 127 128 … … 130 131 } 131 132 132 return new PushProgram(expressions);133 return PushProgram.Create(pool, expressions); 133 134 } 134 135 -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PushProgram.bk.cs
r14745 r14746 88 88 // public IEnumerable<Expression> DepthFirst() { 89 89 // foreach (var expr in this.Expressions) { 90 // if (expr. CanExpand) {90 // if (expr.IsProgram) { 91 91 // var expandExpr = (PushProgram)expr; 92 92 … … 114 114 115 115 // private int CalcDepth() { 116 // //var subExpressions = Expressions.Where(e => e. CanExpand);116 // //var subExpressions = Expressions.Where(e => e.IsProgram); 117 117 // //var depth = subExpressions.Any() 118 118 // // ? subExpressions.Select(e => e as PushProgram).Max(e => e.State.Depth) + 1 … … 124 124 // for (var i = 0; i < Expressions.Count; i++) { 125 125 // var expression = Expressions[i]; 126 // if (!expression. CanExpand) continue;126 // if (!expression.IsProgram) continue; 127 127 // var expandExpression = (PushProgram)expression; 128 128 … … 151 151 // local_treeIndex[i] = next; 152 152 153 // if (subExpression. CanExpand) {153 // if (subExpression.IsProgram) { 154 154 // next += ((PushProgram)subExpression).State.TotalCount; 155 155 // } else { … … 184 184 185 185 // for (var i = 0; i < current.State.Expressions.Count; i++) { 186 // if (current.State.Expressions[i]. CanExpand)186 // if (current.State.Expressions[i].IsProgram) 187 187 // continue; 188 188 -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PushProgram.cs
r14745 r14746 6 6 using System.Diagnostics; 7 7 8 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool; 9 8 10 using Interpreter; 9 11 10 public class PushProgram : StatefulExpression<Expression[]> { 12 [Serializable] 13 public sealed class PushProgram : Expression { 14 private static readonly Expression[] EmptyExpressions = new Expression[0]; 11 15 public static readonly PushProgram Empty = new PushProgram(); 12 16 … … 17 21 private const string Delimiter = " "; 18 22 19 public readonly bool IsEmpty; 20 21 public PushProgram(params Expression[] state) : base(state) { 22 IsEmpty = state.Length == 0; 23 } 24 25 public PushProgram(IEnumerable<Expression> state) : this(state.ToArray()) { 26 // TODO: lazy evaluation of ToArray() 23 private Expression[] expressions; 24 25 public PushProgram() : this(EmptyExpressions) { 26 } 27 28 public PushProgram(Expression[] expressions) { 29 this.expressions = expressions; 30 } 31 32 public bool IsEmpty { get { return Count == 0; } } 33 public int Count { get { return expressions.Length; } } 34 35 public IReadOnlyList<Expression> Expressions { get { return this.expressions; } } 36 37 public static PushProgram Create(IManagedPool<PushProgram> pool, params Expression[] expressions) { 38 var program = pool.Get(); 39 40 program.expressions = expressions; 41 program.Reset(); 42 43 return program; 44 } 45 46 private void Reset() { 47 stringRepresentation = null; 48 depth = null; 49 treeIndex = null; 50 hashCode = null; 51 } 52 53 public static PushProgram Merge(IManagedPool<PushProgram> pool, PushProgram first, Expression second) { 54 var program = pool.Get(); 55 56 program.Reset(); 57 program.expressions = new Expression[first.Expressions.Count + 1]; 58 first.CopyExpressionsTo(program.expressions); 59 program.expressions[0] = first; 60 61 return program; 62 } 63 64 public static PushProgram Merge(IManagedPool<PushProgram> pool, Expression first, PushProgram second) { 65 var program = pool.Get(); 66 67 program.Reset(); 68 69 program.expressions = new Expression[second.Expressions.Count + 1]; 70 program.expressions[0] = first; 71 second.CopyExpressionsTo(program.expressions, 0, 1, second.Count); 72 73 return program; 74 } 75 76 public static PushProgram Merge(IManagedPool<PushProgram> pool, PushProgram first, PushProgram second) { 77 var program = pool.Get(); 78 79 program.Reset(); 80 program.expressions = new Expression[first.Count + second.Count]; 81 82 first.CopyExpressionsTo(program.expressions); 83 second.CopyExpressionsTo(program.expressions, 0, first.Count, second.Count); 84 85 return program; 86 } 87 88 public int IndexOf(Expression expression) { 89 return Array.IndexOf(this.expressions, expression); 27 90 } 28 91 … … 32 95 get 33 96 { 34 if (string.IsNullOrEmpty(stringRepresentation)) 35 stringRepresentation = BuildString(); 97 if (stringRepresentation == null) stringRepresentation = BuildString(); 36 98 return stringRepresentation; 37 99 } 38 100 } 39 101 40 private int depth = -1; 102 103 private int? depth; 41 104 public int Depth 42 105 { 43 106 get 44 107 { 45 if (depth == -1) depth = CalcDepth();46 return depth ;108 if (depth == null) depth = CalcDepth(); 109 return depth.Value; 47 110 } 48 111 } … … 53 116 get 54 117 { 55 return treeIndex ?? (treeIndex = BuildTreeIndex()); 56 } 57 } 58 59 private int hashCode; 60 private int HashCode 61 { 62 get 63 { 64 if (hashCode == default(int)) hashCode = HashExpressions(); 65 return hashCode; 66 } 118 if (treeIndex == null) treeIndex = BuildTreeIndex(); 119 return treeIndex; 120 } 121 } 122 123 private int? hashCode; 124 public override int GetHashCode() { 125 if (hashCode == null) hashCode = HashExpressions(); 126 return hashCode.Value; 67 127 } 68 128 69 129 //public PushProgram Copy() { 70 130 // // as the empty list is a static stateless expression, no copy required 71 // return ReferenceEquals(this, Empty) || this. State.IsEmpty131 // return ReferenceEquals(this, Empty) || this.expressions.IsEmpty 72 132 // ? Empty 73 // : new PushProgram(this. State.Copy());133 // : new PushProgram(this.expressions.Copy()); 74 134 //} 75 135 76 136 public override bool Eval(IPushInterpreter interpreter) { 77 interpreter.ExecStack.Push(this. State);137 interpreter.ExecStack.Push(this.expressions); 78 138 return true; 79 139 } 80 140 81 public override int GetHashCode() {82 return HashCode;83 }84 85 141 public override string ToString() { 86 return this.StringRepresentation;142 return StringRepresentation; 87 143 } 88 144 … … 103 159 104 160 public IEnumerable<Expression> DepthFirst() { 105 foreach (var expr in this. State) {106 if (expr. CanExpand) {161 foreach (var expr in this.expressions) { 162 if (expr.IsProgram) { 107 163 var expandExpr = (PushProgram)expr; 108 164 … … 114 170 115 171 public PushProgram Copy() { 116 return IsEmpty ? this : new PushProgram(State) { 172 if (IsEmpty) return this; 173 174 var program = new PushProgram(this.expressions) { 117 175 stringRepresentation = this.stringRepresentation, 118 176 hashCode = this.hashCode, … … 120 178 treeIndex = this.treeIndex 121 179 }; 122 } 123 124 public Expression[] CopyExpressions() 125 { 126 if (IsEmpty) return Empty.State; 127 128 var copy = new Expression[State.Length]; 129 State.CopyTo(copy, 0); 180 181 return program; 182 } 183 184 public PushProgram Copy(IManagedPool<PushProgram> pool) { 185 if (IsEmpty) return this; 186 187 var program = pool.Get(); 188 program.expressions = this.expressions; 189 program.stringRepresentation = stringRepresentation; 190 program.hashCode = hashCode; 191 program.depth = depth; 192 program.treeIndex = treeIndex; 193 194 return program; 195 } 196 197 public PushProgram Copy(IManagedPool<PushProgram> pool, int startIndex, int length) { 198 if (IsEmpty) return this; 199 200 var program = pool.Get(); 201 program.expressions = CopyExpressions(startIndex, length); 202 203 program.Reset(); 204 205 return program; 206 } 207 208 public Expression[] CopyExpressions() { 209 return CopyExpressions(Count); 210 } 211 212 public Expression[] CopyExpressions(int length) { 213 if (IsEmpty) return EmptyExpressions; 214 215 var copy = new Expression[length]; 216 this.expressions.CopyTo(copy, 0); 130 217 131 218 return copy; 219 } 220 221 public Expression[] CopyExpressions(int startIndex, int length) { 222 if (IsEmpty) return EmptyExpressions; 223 224 var copy = new Expression[length]; 225 CopyExpressionsTo(copy, startIndex, 0, length); 226 227 return copy; 228 } 229 230 public void CopyExpressionsTo(Expression[] destination, int? sourceIndex = null, int? destinationIndex = null, int? length = null) { 231 if (IsEmpty) return; 232 233 Array.Copy(this.expressions, sourceIndex ?? 0, destination, destinationIndex ?? 0, length ?? Count); 132 234 } 133 235 134 236 private int CalcDepth() { 135 237 var maxDepth = 1; 136 for (var i = 0; i < State.Length; i++) {137 var expression = State[i];138 if (!expression. CanExpand) continue;238 for (var i = 0; i < Count; i++) { 239 var expression = this.expressions[i]; 240 if (!expression.IsProgram) continue; 139 241 var expandExpression = (PushProgram)expression; 140 242 … … 149 251 // prevent too big string 150 252 return this.TotalCount > 50 151 ? PrefixReduced + this. State.Length+ PostfixReduced152 : Prefix + string.Join(Delimiter, this. State.Where(e => !string.IsNullOrWhiteSpace(e.StringRepresentation))) + Postfix;253 ? PrefixReduced + this.Count + PostfixReduced 254 : Prefix + string.Join(Delimiter, this.expressions.Where(e => !string.IsNullOrWhiteSpace(e.StringRepresentation))) + Postfix; 153 255 } 154 256 155 257 private int[] BuildTreeIndex() { 156 var local_treeIndex = new int[ State.Length];258 var local_treeIndex = new int[Count]; 157 259 158 260 var next = 1; 159 261 160 for (var i = State.Length- 1; i >= 0; i--) {161 var subExpression = State[i];262 for (var i = Count - 1; i >= 0; i--) { 263 var subExpression = this.expressions[i]; 162 264 163 265 local_treeIndex[i] = next; 164 266 165 if (subExpression. CanExpand) {267 if (subExpression.IsProgram) { 166 268 next += ((PushProgram)subExpression).TotalCount; 167 269 } else { … … 176 278 var hash = 19 * 31 + this.GetType().FullName.GetHashCode(); 177 279 178 for (var i = 0; i < State.Length; i++) {179 hash = hash * 31 + State[i].GetHashCode();280 for (var i = 0; i < Count; i++) { 281 hash = hash * 31 + this.expressions[i].GetHashCode(); 180 282 } 181 283 … … 195 297 } 196 298 197 for (var i = 0; i < current. State.Length; i++) {198 if (current. State[i].CanExpand)299 for (var i = 0; i < current.Count; i++) { 300 if (current.expressions[i].IsProgram) 199 301 continue; 200 302 201 var subExpression = current. State[i] as PushProgram;303 var subExpression = current.expressions[i] as PushProgram; 202 304 203 305 if (ReferenceEquals(target, subExpression)) { … … 231 333 if (index == 0) return resolver(parent, parent, this, 0, parentIndex); 232 334 233 var min = State.Length- 1;335 var min = Count - 1; 234 336 var max = 0; 235 337 var mid = (min + max) / 2; … … 243 345 244 346 return TreeIndex[mid] == index 245 ? resolver(parent, this, State[mid], mid, parentIndex)246 : ( State[mid + 1] as PushProgram).GetFromTree(index - TreeIndex[mid + 1], mid + 1,347 ? resolver(parent, this, this.expressions[mid], mid, parentIndex) 348 : (this.expressions[mid + 1] as PushProgram).GetFromTree(index - TreeIndex[mid + 1], mid + 1, 247 349 this, resolver); 248 350 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/RandExpressions.cs
r14744 r14746 69 69 70 70 var size = (int)(interpreter.IntegerStack.Pop() % interpreter.Configuration.MaxPointsInRandomExpression); 71 var program = CodeGenerator.RandomExpandExpression( 71 var program = CodeGenerator.RandomProgram( 72 interpreter.PushProgramPool, 72 73 size, 73 74 interpreter.Random, -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/StatefulExpression.cs
r14745 r14746 2 2 public abstract class StatefulExpression<T> : Expression { 3 3 public readonly T State; 4 5 private int hashCode; 6 private int HashCode 7 { 8 get 9 { 10 if (hashCode == default(int)) hashCode = CalcHashCode(); 11 return hashCode; 12 } 13 } 4 private int? hashCode; 14 5 15 6 protected StatefulExpression(T state) { … … 34 25 } 35 26 27 public bool Equals(StatefulExpression<T> obj) { 28 if (ReferenceEquals(this, obj)) 29 return true; 30 31 if (GetType() != obj.GetType()) 32 return false; 33 34 return ReferenceEquals(State, obj.State) || 35 GetHashCode() == obj.GetHashCode(); 36 } 37 36 38 public override int GetHashCode() { 37 return HashCode; 39 if (hashCode == null) hashCode = CalcHashCode(); 40 return hashCode.Value; 38 41 } 39 42 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Generators/CodeGenerator.cs
r14745 r14746 5 5 using HeuristicLab.Core; 6 6 using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; 7 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool; 7 8 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 8 9 using HeuristicLab.Random; … … 18 19 public static class CodeGenerator { 19 20 20 public static PushProgram RandomProgram( int maxPoints, IRandom random = null, IReadonlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) {21 public static PushProgram RandomProgram(IManagedPool<PushProgram> pool, int maxPoints, IRandom random = null, IReadonlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) { 21 22 var code = RandomCode(maxPoints, random, pushGpConfiguration, customExpressions); 22 23 23 return new PushProgram(code.ToArray()); 24 } 25 26 public static PushProgram RandomExpandExpression(int maxPoints, IRandom random = null, IReadonlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) { 27 var program = RandomProgram(maxPoints, random, pushGpConfiguration, customExpressions); 28 29 return new PushProgram(program); 24 return PushProgram.Create(pool, code.ToArray()); 30 25 } 31 26 -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/IPushInterpreter.cs
r14745 r14746 7 7 8 8 using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; 9 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool; 9 10 10 11 using Stack; … … 23 24 24 25 IReadonlyPushConfiguration Configuration { get; } 26 27 IManagedPool<PushProgram> PushProgramPool { get; } 25 28 26 29 bool IsNameQuoteFlagSet { get; set; } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreter.cs
r14745 r14746 2 2 using System; 3 3 using System.Collections.Generic; 4 #if DEBUG 5 using System.Linq; 6 #endif 4 7 using System.Runtime.CompilerServices; 5 8 using System.Threading; … … 7 10 using HeuristicLab.Core; 8 11 using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; 12 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool; 9 13 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 10 14 using HeuristicLab.Problems.ProgramSynthesis.Push.Parser; … … 82 86 } 83 87 88 public IManagedPool<PushProgram> PushProgramPool { get; set; } 89 84 90 public IReadonlyPushConfiguration Configuration { get; protected set; } 85 91 … … 249 255 } 250 256 257 #if DEBUG 258 private Expression last; 259 private bool DoStep() { 260 var expression = ExecStack.Pop(); 261 262 if (ExecStack.Any(e => e == null)) { 263 throw new InvalidProgramException(); 264 } 265 266 var succ = expression.Eval(this); 267 last = expression; 268 269 return succ; 270 } 271 #else 251 272 [MethodImpl(MethodImplOptions.AggressiveInlining)] 252 273 private bool DoStep() { 253 274 return ExecStack.Pop().Eval(this); 254 275 } 276 #endif 255 277 256 278 private Task InterpretAsync() { -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushEncoding.cs
r14744 r14746 42 42 43 43 public static PushProgram MapToPushProgram(this IntegerVector vector, IReadOnlyList<string> enabledExpressions) { 44 var expressions = vector 45 .Select(i => ExpressionTable.GetExpression(enabledExpressions[i])) 46 .ToArray(); 44 //var expressions = vector 45 // .Select(i => ExpressionTable.GetExpression(enabledExpressions[i])) 46 // .ToArray(); 47 48 var expressions = new Expression[vector.Length]; 49 for (var i = 0; i < vector.Length; i++) 50 { 51 expressions[i] = ExpressionTable.GetExpression(enabledExpressions[vector[i]]); 52 } 47 53 48 54 return new Expressions.PushProgram(expressions); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushProblem.cs
r14745 r14746 12 12 using HeuristicLab.BenchmarkSuite; 13 13 using HeuristicLab.Data; 14 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool; 15 14 16 using Instances; 15 17 using Interpreter; … … 26 28 private readonly PushConfiguration config; 27 29 private PushInterpreterPool pool; 30 private ManagedPoolProvider<PushProgram> pushProgramPoolProvider; 28 31 29 32 public PushProblem() { 30 33 config = new PushConfiguration(); 31 34 pool = new PushInterpreterPool(config); 35 36 pushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(1024); 37 pushProgramPoolProvider.InitDummyPartition(() => new PushProgram()); 32 38 33 39 InitEvents(); … … 56 62 pool = new PushInterpreterPool(config); 57 63 Instructions = config; 64 65 pushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(1024); 66 pushProgramPoolProvider.InitDummyPartition(() => new PushProgram()); 58 67 59 68 InitEvents(); … … 404 413 405 414 public override double Evaluate(Individual individual, IRandom random) { 415 if (DataBounds[0, 1] <= 0) return default(double); 406 416 407 417 var program = individual.PushProgram(config.EnabledExpressions as IReadOnlyList<string>); 408 var expandExpression = new PushProgram(program); 409 var results = new List<double>(); 418 var result = 0d; 410 419 411 420 using (var interpreter = pool.GetInstance(random)) { 412 421 for (var i = DataBounds[0, 0]; i < DataBounds[0, 1]; i++) { 413 var example = this.DataDescriptor.Examples[i];422 var example = DataDescriptor.Examples[i]; 414 423 415 424 interpreter.BooleanStack.Push(example.InputBoolean); … … 417 426 interpreter.FloatStack.Push(example.InputFloat); 418 427 419 interpreter.Run(expandExpression); 420 421 var diff = GetDiff(example.OutputInt, interpreter.IntegerStack, this.DataDescriptor.WorstResult, LongDiffer) + 422 GetDiff(example.OutputFloat, interpreter.FloatStack, this.DataDescriptor.WorstResult, DoubleDiffer) + 423 GetDiff(example.OutputBoolean, interpreter.BooleanStack, this.DataDescriptor.WorstResult, BooleanDiffer); 424 425 results.Add(diff); 428 using (interpreter.PushProgramPool = pushProgramPoolProvider.CreatePool()) { 429 interpreter.Run(program); 430 } 431 432 result += GetDiff(example.OutputInt, interpreter.IntegerStack, DataDescriptor.WorstResult, LongDiffer) 433 + GetDiff(example.OutputFloat, interpreter.FloatStack, DataDescriptor.WorstResult, DoubleDiffer) 434 + GetDiff(example.OutputBoolean, interpreter.BooleanStack, DataDescriptor.WorstResult, BooleanDiffer); 435 426 436 interpreter.Clear(); 427 437 } 428 438 } 429 439 430 return result s.Count == 0 ? 0d : results.Average();440 return result / DataBounds[0, 1]; 431 441 } 432 442 -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Simplifier/RandomSimplifier.cs
r14745 r14746 1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Simplifier {2 using System;3 using HeuristicLab.Core;4 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;1 //namespace HeuristicLab.Problems.ProgramSynthesis.Push.Simplifier { 2 // using System; 3 // using HeuristicLab.Core; 4 // using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 5 5 6 public class RandomSimplifier : ISimplifier {7 public int Trys { get; set; }6 // public class RandomSimplifier : ISimplifier { 7 // public int Trys { get; set; } 8 8 9 public PushProgram Simplify(PushProgram program, IRandom random, Predicate<PushProgram> isBetter) {9 // public PushProgram Simplify(PushProgram program, IRandom random, Predicate<PushProgram> isBetter) { 10 10 11 if (program.TotalCount == 1) {12 return isBetter(PushProgram.Empty) ? PushProgram.Empty : program;13 }11 // if (program.TotalCount == 1) { 12 // return isBetter(PushProgram.Empty) ? PushProgram.Empty : program; 13 // } 14 14 15 var copy = program.Copy();16 var maxTries = Math.Min(Trys, program.TotalCount - 2);17 var successfulRemoves = 0;15 // var copy = program.Copy(); 16 // var maxTries = Math.Min(Trys, program.TotalCount - 2); 17 // var successfulRemoves = 0; 18 18 19 for (var i = 0; i < maxTries; i++) {20 var rndIndex = random.Next(1, program.TotalCount - 1 - successfulRemoves);21 var node = copy.GetFromTree(22 rndIndex,23 (super, parent, child, childIndex, parentIndex) => new {24 Super = super,25 Parent = parent,26 ChildIndex = childIndex,27 ParentIndex = parentIndex28 });19 // for (var i = 0; i < maxTries; i++) { 20 // var rndIndex = random.Next(1, program.TotalCount - 1 - successfulRemoves); 21 // var node = copy.GetFromTree( 22 // rndIndex, 23 // (super, parent, child, childIndex, parentIndex) => new { 24 // Super = super, 25 // Parent = parent, 26 // ChildIndex = childIndex, 27 // ParentIndex = parentIndex 28 // }); 29 29 30 var oldParentExpressions = node.Parent.State;31 var newParentExpressions = RemoveAt(oldParentExpressions, node.ChildIndex);32 var newParent = new PushProgram(newParentExpressions);30 // var oldParentExpressions = node.Parent.State; 31 // var newParentExpressions = RemoveAt(oldParentExpressions, node.ChildIndex); 32 // var newParent = new PushProgram(newParentExpressions); 33 33 34 var superExpressions = node.Super == null ? copy.State : node.Super.State;35 superExpressions[node.ParentIndex] = newParent;34 // var superExpressions = node.Super == null ? copy.State : node.Super.State; 35 // superExpressions[node.ParentIndex] = newParent; 36 36 37 if (isBetter(copy)) {38 successfulRemoves++;39 } else {40 superExpressions[node.ParentIndex] = node.Parent;41 }42 }37 // if (isBetter(copy)) { 38 // successfulRemoves++; 39 // } else { 40 // superExpressions[node.ParentIndex] = node.Parent; 41 // } 42 // } 43 43 44 return copy;45 }44 // return copy; 45 // } 46 46 47 private static T[] RemoveAt<T>(T[] source, int index) {48 var dest = new T[source.Length - 1];49 if (index > 0)50 Array.Copy(source, 0, dest, 0, index);47 // private static T[] RemoveAt<T>(T[] source, int index) { 48 // var dest = new T[source.Length - 1]; 49 // if (index > 0) 50 // Array.Copy(source, 0, dest, 0, index); 51 51 52 if (index < source.Length - 1)53 Array.Copy(source, index + 1, dest, index, source.Length - index - 1);52 // if (index < source.Length - 1) 53 // Array.Copy(source, index + 1, dest, index, source.Length - index - 1); 54 54 55 return dest;56 }57 }58 }55 // return dest; 56 // } 57 // } 58 //}
Note: See TracChangeset
for help on using the changeset viewer.