- Timestamp:
- 03/11/17 12:42:31 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis
- Files:
-
- 1 added
- 10 deleted
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/HeuristicLab.Problems.ProgramSynthesis.csproj
r14744 r14745 138 138 <Compile Include="Push\Expressions\NameExpressions.cs" /> 139 139 <Compile Include="Push\Expressions\PopExpressions.cs" /> 140 <Compile Include="Push\Expressions\PushProgram.bk.cs" /> 141 <Compile Include="Push\Expressions\PushExpressions.cs" /> 140 142 <Compile Include="Push\Expressions\PushProgram.cs" /> 141 <Compile Include="Push\Expressions\PushExpressions.cs" />142 143 <Compile Include="Push\Expressions\PushResultExpression.cs" /> 143 144 <Compile Include="Push\Expressions\RandExpressions.cs" /> -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/CodeExpressions.cs
r14744 r14745 114 114 115 115 if (isFirstList) { 116 var expand1 = first as ExecExpandExpression;116 var expand1 = first as PushProgram; 117 117 118 118 if (isSecondList) { 119 var expand2 = second as ExecExpandExpression;120 var size = expand2.State. Expressions.Count + expand1.State.Expressions.Count;119 var expand2 = second as PushProgram; 120 var size = expand2.State.Length + expand1.State.Length; 121 121 122 122 // if size > maxPointsInProgram this expressions results in a NOOP … … 125 125 result = new Expression[size]; 126 126 127 Array.Copy(expand2.State .Expressions as Expression[], result, expand2.State.Expressions.Count);127 Array.Copy(expand2.State, result, expand2.State.Length); 128 128 Array.Copy( 129 expand1.State .Expressions as Expression[],129 expand1.State, 130 130 0, 131 131 result, 132 expand2.State. Expressions.Count,133 expand1.State. Expressions.Count);132 expand2.State.Length, 133 expand1.State.Length); 134 134 } else { 135 var size = expand1.State. Expressions.Count+ 1;135 var size = expand1.State.Length + 1; 136 136 137 137 // if size > maxPointsInProgram this expressions results in a NOOP … … 141 141 result[0] = second; 142 142 143 Array.Copy(expand1.State .Expressions as Expression[], 0, result, 1, expand1.State.Expressions.Count);143 Array.Copy(expand1.State, 0, result, 1, expand1.State.Length); 144 144 } 145 145 } else if (isSecondList) { 146 var expand2 = second as ExecExpandExpression;147 148 var size = expand2.State. Expressions.Count+ 1;146 var expand2 = second as PushProgram; 147 148 var size = expand2.State.Length + 1; 149 149 150 150 // if size > maxPointsInProgram this expressions results in a NOOP … … 154 154 result[0] = first; 155 155 156 Array.Copy(expand2.State .Expressions as Expression[], 0, result, 1, expand2.State.Expressions.Count);156 Array.Copy(expand2.State as Expression[], 0, result, 1, expand2.State.Length); 157 157 } else { 158 158 result = new[] { second, first }; 159 159 } 160 160 161 var expression = new ExecExpandExpression(result);161 var expression = new PushProgram(result); 162 162 163 163 interpreter.CodeStack.SetTop(expression); … … 194 194 public override bool Eval(IPushInterpreter interpreter) { 195 195 if ((interpreter.CodeStack.Count == 0) || 196 (interpreter.CodeStack.Top.GetType() != typeof( ExecExpandExpression))) return false;197 198 var expand = interpreter.CodeStack.Top as ExecExpandExpression;199 200 if (expand. State.IsEmpty) return false;201 var first = expand.State .Expressions[expand.State.Expressions.Count- 1];196 (interpreter.CodeStack.Top.GetType() != typeof(PushProgram))) return false; 197 198 var expand = interpreter.CodeStack.Top as PushProgram; 199 200 if (expand.IsEmpty) return false; 201 var first = expand.State[expand.State.Length - 1]; 202 202 203 203 interpreter.CodeStack.SetTop(first); … … 217 217 if (interpreter.CodeStack.Count == 0) return false; 218 218 219 ExecExpandExpressionexpandExpression;219 PushProgram expandExpression; 220 220 var top = interpreter.CodeStack.Top; 221 221 222 222 if (top.CanExpand) { 223 var expand = ( ExecExpandExpression)top;224 225 if (expand. State.IsEmpty) return false;226 var length = expand.State. Expressions.Count- 1;223 var expand = (PushProgram)top; 224 225 if (expand.IsEmpty) return false; 226 var length = expand.State.Length - 1; 227 227 var newExpressions = new Expression[length]; 228 228 229 Array.Copy((Expression[])expand.State .Expressions, 0, newExpressions, 0, length);230 231 expandExpression = new ExecExpandExpression(newExpressions);229 Array.Copy((Expression[])expand.State, 0, newExpressions, 0, length); 230 231 expandExpression = new PushProgram(newExpressions); 232 232 } else { 233 expandExpression = ExecExpandExpression.Empty;233 expandExpression = PushProgram.Empty; 234 234 } 235 235 … … 250 250 return false; 251 251 252 ExecExpandExpressionresult;252 PushProgram result; 253 253 254 254 if (interpreter.CodeStack.Top.CanExpand) { 255 var first = ( ExecExpandExpression)interpreter.CodeStack.Pop();256 var size = first.State. Expressions.Count+ 1;255 var first = (PushProgram)interpreter.CodeStack.Pop(); 256 var size = first.State.Length + 1; 257 257 258 258 if (size > interpreter.Configuration.MaxPointsInProgram) return false; … … 260 260 var expressions = new Expression[size]; 261 261 262 expressions[first.State. Expressions.Count] = interpreter.CodeStack.Top;263 Array.Copy(first.State .Expressions as Expression[], expressions, first.State.Expressions.Count);264 265 result = new ExecExpandExpression(expressions);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); 266 266 } else { 267 result = new ExecExpandExpression(interpreter.CodeStack.Pop(), interpreter.CodeStack.Top);267 result = new PushProgram(interpreter.CodeStack.Pop(), interpreter.CodeStack.Top); 268 268 } 269 269 … … 286 286 if ((interpreter.CodeStack.Count < 2) || 287 287 (interpreter.CodeStack[interpreter.CodeStack.Count - 2].GetType() != 288 typeof( ExecExpandExpression))) return false;288 typeof(PushProgram))) return false; 289 289 290 290 var target = interpreter.CodeStack.Pop(); 291 291 var source = interpreter.CodeStack.Top; 292 292 293 var container = GetContainer(source as ExecExpandExpression, target);294 var result = container ?? ExecExpandExpression.Empty;293 var container = GetContainer(source as PushProgram, target); 294 var result = container ?? PushProgram.Empty; 295 295 296 296 interpreter.CodeStack.SetTop(result); … … 298 298 } 299 299 300 private static ExecExpandExpression GetContainer(ExecExpandExpressioncurrent, Expression target) {300 private static PushProgram GetContainer(PushProgram current, Expression target) { 301 301 if (current == target) 302 302 return null; 303 303 304 304 var subContainer = 305 current.State. Expressions.Where(e => e.CanExpand)306 .Select(e => GetContainer(e as ExecExpandExpression, target))305 current.State.Where(e => e.CanExpand) 306 .Select(e => GetContainer(e as PushProgram, target)) 307 307 .Where(e => e != null); 308 308 309 var execExpandExpressions = subContainer as IList< ExecExpandExpression> ?? subContainer.ToList();309 var execExpandExpressions = subContainer as IList<PushProgram> ?? subContainer.ToList(); 310 310 return execExpandExpressions.Any() 311 ? execExpandExpressions.OrderBy(c => c.State. Expressions.Count).LastOrDefault()312 : current.State. Expressions.Contains(target)311 ? execExpandExpressions.OrderBy(c => c.State.Length).LastOrDefault() 312 : current.State.Contains(target) 313 313 ? current 314 314 : null; … … 328 328 329 329 var values = interpreter.CodeStack.Pop(2); 330 var second = ( ExecExpandExpression)values[0];331 var contains = second.State. Expressions.Contains(values[1]);330 var second = (PushProgram)values[0]; 331 var contains = second.State.Contains(values[1]); 332 332 333 333 interpreter.BooleanStack.Push(contains); … … 414 414 private void DetermineUniqueItems(Expression source, IDictionary<int, int> items) { 415 415 if (source.CanExpand) { 416 var expand = source as ExecExpandExpression;417 418 foreach (var e in expand.State .Expressions) {416 var expand = source as PushProgram; 417 418 foreach (var e in expand.State) { 419 419 var id = e.GetHashCode(); 420 420 if (!items.ContainsKey(id)) items.Add(id, 1); … … 445 445 return false; 446 446 447 var expression = ( ExecExpandExpression)interpreter.CodeStack.Top;448 var index = (int)Math.Abs(interpreter.IntegerStack.Pop() % expression. State.TotalCount);447 var expression = (PushProgram)interpreter.CodeStack.Top; 448 var index = (int)Math.Abs(interpreter.IntegerStack.Pop() % expression.TotalCount); 449 449 var result = expression.GetFromTree(index); 450 450 … … 536 536 537 537 var source = interpreter.CodeStack[interpreter.CodeStack.Count - 2]; 538 var target = ( ExecExpandExpression)interpreter.CodeStack.Pop();538 var target = (PushProgram)interpreter.CodeStack.Pop(); 539 539 var index = (int)interpreter.IntegerStack.Pop(); 540 540 541 541 Expression[] newExpressions; 542 if (target.State. Expressions.Count> 0) {543 index = target.State. Expressions.Count - 1 - Math.Abs(index % target.State.Expressions.Count);544 545 newExpressions = target. State.CopyExpressions();542 if (target.State.Length > 0) { 543 index = target.State.Length - 1 - Math.Abs(index % target.State.Length); 544 545 newExpressions = target.CopyExpressions(); 546 546 newExpressions[index] = source.CanExpand 547 ? (( ExecExpandExpression)source).Copy()547 ? ((PushProgram)source).Copy() 548 548 : source; 549 549 } else { … … 551 551 } 552 552 553 var result = new ExecExpandExpression(newExpressions);553 var result = new PushProgram(newExpressions); 554 554 interpreter.CodeStack.SetTop(result); 555 555 … … 573 573 574 574 if (expression.CanExpand) 575 count = (( ExecExpandExpression)expression).State.Expressions.Count;575 count = ((PushProgram)expression).State.Length; 576 576 577 577 interpreter.IntegerStack.Push(count); … … 591 591 var first = interpreter.CodeStack.Pop(); 592 592 var second = interpreter.CodeStack.Top; 593 var expandExpression = new ExecExpandExpression(second, first);593 var expandExpression = new PushProgram(second, first); 594 594 595 595 interpreter.CodeStack.SetTop(expandExpression); … … 611 611 612 612 var contains = expressions[1].CanExpand 613 ? (( ExecExpandExpression)expressions[1]).State.Expressions.Contains(expressions[0])613 ? ((PushProgram)expressions[1]).State.Contains(expressions[0]) 614 614 : expressions[1].Equals(expressions[0]); 615 615 … … 636 636 637 637 if (expression.CanExpand) { 638 var subExpression = ( ExecExpandExpression)expression;639 640 if (subExpression. State.IsEmpty) {641 nthExpression = ExecExpandExpression.Empty;638 var subExpression = (PushProgram)expression; 639 640 if (subExpression.IsEmpty) { 641 nthExpression = PushProgram.Empty; 642 642 } else { 643 var index = (int)(subExpression.State. Expressions.Count- 1 -644 Math.Abs(n % subExpression.State. Expressions.Count));645 646 nthExpression = subExpression.State .Expressions[index];643 var index = (int)(subExpression.State.Length - 1 - 644 Math.Abs(n % subExpression.State.Length)); 645 646 nthExpression = subExpression.State[index]; 647 647 } 648 648 } else { … … 672 672 var expression = interpreter.CodeStack.Top; 673 673 674 Expression nthExpression = null;674 Expression nthExpression; 675 675 676 676 if (expression.CanExpand) { 677 var subExpressions = (( ExecExpandExpression)expression).State.Expressions;678 679 if (subExpressions. Count< 2) {680 nthExpression = ExecExpandExpression.Empty;677 var subExpressions = ((PushProgram)expression).State; 678 679 if (subExpressions.Length < 2) { 680 nthExpression = PushProgram.Empty; 681 681 } else { 682 var index = (int)(subExpressions. Count - 2 - Math.Abs(n % (subExpressions.Count- 1)));682 var index = (int)(subExpressions.Length - 2 - Math.Abs(n % (subExpressions.Length - 1))); 683 683 684 684 nthExpression = subExpressions[index]; … … 702 702 if (interpreter.CodeStack.Count == 0) return false; 703 703 704 var result = interpreter.CodeStack.Pop().Equals( ExecExpandExpression.Empty);704 var result = interpreter.CodeStack.Pop().Equals(PushProgram.Empty); 705 705 interpreter.BooleanStack.Push(result); 706 706 … … 724 724 var position = -1; 725 725 if (first.CanExpand) { 726 var expand = ( ExecExpandExpression)first;727 position = expand.State. Expressions.Count- 1728 - Array.FindIndex((Expression[])expand.State .Expressions, e => e.Equals(second));726 var expand = (PushProgram)first; 727 position = expand.State.Length - 1 728 - Array.FindIndex((Expression[])expand.State, e => e.Equals(second)); 729 729 } else if (first.Equals(second)) { 730 730 position = 0; … … 748 748 var expression = interpreter.CodeStack.Pop(); 749 749 var points = expression.CanExpand 750 ? (( ExecExpandExpression)expression).State.Expressions.Count750 ? ((PushProgram)expression).State.Length 751 751 : 1; 752 752 … … 767 767 public override bool Eval(IPushInterpreter interpreter) { 768 768 if ((interpreter.CodeStack.Count < 3) || (interpreter.CodeStack.Top.GetType() != 769 typeof( ExecExpandExpression))) return false;769 typeof(PushProgram))) return false; 770 770 771 771 var expressions = interpreter.CodeStack.Pop(2); 772 772 var third = interpreter.CodeStack.Top; 773 773 var second = expressions[0]; 774 var first = expressions[1] as ExecExpandExpression;775 var newExpressions = new Expression[first.State. Expressions.Count];776 777 for (var i = 0; i < first.State. Expressions.Count; i++)778 newExpressions[i] = first.State .Expressions[i].Equals(third)774 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) 779 779 ? second 780 780 /* no cloning needed here because first is removed and therefore newExpression is the only container of sub expressions */ 781 : first.State .Expressions[i];782 783 var result = new ExecExpandExpression(newExpressions);781 : first.State[i]; 782 783 var result = new PushProgram(newExpressions); 784 784 785 785 interpreter.CodeStack.SetTop(result); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExecExpressions.cs
r14744 r14745 1 1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { 2 using System;3 using System.Collections.Generic;4 using System.Linq;5 2 6 3 using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes; … … 8 5 9 6 using Interpreter; 10 11 public class ExecExpandExpression : StatefulExpression<PushProgram> {12 public static readonly ExecExpandExpression Empty = new ExecExpandExpression(PushProgram.Empty);13 14 public ExecExpandExpression(PushProgram program)15 : base(program) {16 }17 18 public ExecExpandExpression(params Expression[] state) : this(new PushProgram(state)) {19 }20 21 public ExecExpandExpression(IEnumerable<Expression> state) : this(state.ToArray()) {22 // TODO: lazy evaluation of ToArray()23 }24 25 public override string StringRepresentation { get { return this.State.ToString(); } }26 27 public ExecExpandExpression Copy() {28 // as the empty list is a static stateless expression, no copy required29 return ReferenceEquals(this, Empty) || this.State.IsEmpty30 ? Empty31 : new ExecExpandExpression(this.State.Copy());32 }33 34 public override bool Eval(IPushInterpreter interpreter) {35 interpreter.ExecStack.Push(this.State.Expressions);36 return true;37 }38 39 /// <summary>40 /// Returns the element with the given index whereby the tree is indexed in depth first manner41 /// </summary>42 /// <param name="index">The index of the required element</param>43 /// <returns>The required element</returns>44 public Expression GetFromTree(int index) {45 return GetFromTree(index, (root, parent, child, childIndex, parentIndex) => child);46 }47 48 /// <returns>The required element</returns>49 public T GetFromTree<T>(int index,50 Func<ExecExpandExpression, ExecExpandExpression, Expression, int, int, T> resolver) {51 return GetFromTree(index, 0, null, resolver);52 }53 54 private T GetFromTree<T>(int index, int parentIndex, ExecExpandExpression parent,55 Func<ExecExpandExpression, ExecExpandExpression, Expression, int, int, T> resolver) {56 if (index == 0) return resolver(parent, parent, this, 0, parentIndex);57 58 var min = State.Expressions.Count - 1;59 var max = 0;60 var mid = (min + max) / 2;61 62 while ((max <= min) && (this.State.TreeIndex[mid] != index)) {63 if (this.State.TreeIndex[mid] > index) max = mid + 1;64 else min = mid - 1;65 66 mid = (min + max) / 2;67 }68 69 return this.State.TreeIndex[mid] == index70 ? resolver(parent, this, State.Expressions[mid], mid, parentIndex)71 : (State.Expressions[mid + 1] as ExecExpandExpression).GetFromTree(index - this.State.TreeIndex[mid + 1], mid + 1,72 this, resolver);73 }74 }75 7 76 8 /// <summary> … … 110 42 var execYExpression = ExpressionTable.GetStatelessExpression<ExecYExpression>(); 111 43 112 var result = new ExecExpandExpression(top, execYExpression);44 var result = new PushProgram(top, execYExpression); 113 45 114 46 interpreter.ExecStack.SetTop(result); … … 149 81 var c = interpreter.ExecStack.Top; 150 82 151 var newTop = new ExecExpandExpression(c, b);83 var newTop = new PushProgram(c, b); 152 84 153 85 interpreter.ExecStack.SetTop(newTop); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/Expression.cs
r14744 r14745 8 8 9 9 public abstract class Expression { 10 public bool CanExpand { get { return this.GetType() == typeof( ExecExpandExpression); } }10 public bool CanExpand { get { return this.GetType() == typeof(PushProgram); } } 11 11 12 12 public static readonly IReadOnlyCollection<Expression> EmptyContainer = new Expression[0]; -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExpressionTable.cs
r14744 r14745 123 123 } 124 124 125 public static ExecExpandExpressionGetProgram(int[] index) {125 public static PushProgram GetProgram(int[] index) { 126 126 var expressions = new Expression[index.Length]; 127 127 … … 130 130 } 131 131 132 var program = new PushProgram(expressions); 133 return new ExecExpandExpression(program); 132 return new PushProgram(expressions); 134 133 } 135 134 -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PushProgram.cs
r14744 r14745 1 using System.Collections.Generic; 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 2 4 3 5 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { 4 6 using System.Diagnostics; 5 using System.Linq; 6 7 public class PushProgram { 7 8 using Interpreter; 9 10 public class PushProgram : StatefulExpression<Expression[]> { 11 public static readonly PushProgram Empty = new PushProgram(); 8 12 9 13 private const string Prefix = "("; … … 13 17 private const string Delimiter = " "; 14 18 15 public static readonly PushProgram Empty = new PushProgram(new Expression[0]);16 17 public readonly IReadOnlyList<Expression> Expressions;18 public bool IsEmpty { get; private set; }19 20 public PushProgram(Expression[] expressions) { 21 this.Expressions = expressions;22 this.IsEmpty = expressions.Length == 0;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 27 } 24 28 25 29 private string stringRepresentation; 26 p rivate string StringRepresentation30 public override string StringRepresentation 27 31 { 28 32 get … … 35 39 36 40 private int depth = -1; 37 p rivateint Depth41 public int Depth 38 42 { 39 43 get … … 44 48 } 45 49 46 private int[] treeIndex = null;47 p ublicint[] TreeIndex48 { 49 get 50 { 51 return t his.treeIndex ?? (this.treeIndex = this.BuildTreeIndex());50 private int[] treeIndex; 51 private int[] TreeIndex 52 { 53 get 54 { 55 return treeIndex ?? (treeIndex = BuildTreeIndex()); 52 56 } 53 57 } … … 61 65 return hashCode; 62 66 } 67 } 68 69 //public PushProgram Copy() { 70 // // as the empty list is a static stateless expression, no copy required 71 // return ReferenceEquals(this, Empty) || this.State.IsEmpty 72 // ? Empty 73 // : new PushProgram(this.State.Copy()); 74 //} 75 76 public override bool Eval(IPushInterpreter interpreter) { 77 interpreter.ExecStack.Push(this.State); 78 return true; 63 79 } 64 80 … … 87 103 88 104 public IEnumerable<Expression> DepthFirst() { 89 foreach (var expr in this. Expressions) {105 foreach (var expr in this.State) { 90 106 if (expr.CanExpand) { 91 var expandExpr = ( ExecExpandExpression)expr;92 93 foreach (var sub in expandExpr. State.DepthFirst())107 var expandExpr = (PushProgram)expr; 108 109 foreach (var sub in expandExpr.DepthFirst()) 94 110 yield return sub; 95 111 } else yield return expr; … … 98 114 99 115 public PushProgram Copy() { 100 return IsEmpty ? this : new PushProgram( (Expression[])Expressions) {116 return IsEmpty ? this : new PushProgram(State) { 101 117 stringRepresentation = this.stringRepresentation, 102 118 hashCode = this.hashCode, … … 106 122 } 107 123 108 public Expression[] CopyExpressions() { 109 var copy = new Expression[this.Expressions.Count]; 110 ((Expression[])this.Expressions).CopyTo(copy, Expressions.Count); 124 public Expression[] CopyExpressions() 125 { 126 if (IsEmpty) return Empty.State; 127 128 var copy = new Expression[State.Length]; 129 State.CopyTo(copy, 0); 111 130 112 131 return copy; … … 114 133 115 134 private int CalcDepth() { 116 //var subExpressions = Expressions.Where(e => e.CanExpand);117 //var depth = subExpressions.Any()118 // ? subExpressions.Select(e => e as ExecExpandExpression).Max(e => e.State.Depth) + 1119 // : 1;120 121 ////return depth;122 123 135 var maxDepth = 1; 124 for (var i = 0; i < Expressions.Count; i++) {125 var expression = Expressions[i];136 for (var i = 0; i < State.Length; i++) { 137 var expression = State[i]; 126 138 if (!expression.CanExpand) continue; 127 var expandExpression = ( ExecExpandExpression)expression;128 129 if (expandExpression. State.Depth > maxDepth)130 maxDepth = expandExpression. State.Depth;139 var expandExpression = (PushProgram)expression; 140 141 if (expandExpression.Depth > maxDepth) 142 maxDepth = expandExpression.Depth; 131 143 } 132 144 … … 137 149 // prevent too big string 138 150 return this.TotalCount > 50 139 ? PrefixReduced + this. Expressions.Count+ PostfixReduced140 : Prefix + string.Join(Delimiter, this. Expressions.Where(e => !string.IsNullOrWhiteSpace(e.StringRepresentation))) + Postfix;151 ? PrefixReduced + this.State.Length + PostfixReduced 152 : Prefix + string.Join(Delimiter, this.State.Where(e => !string.IsNullOrWhiteSpace(e.StringRepresentation))) + Postfix; 141 153 } 142 154 143 155 private int[] BuildTreeIndex() { 144 var local_treeIndex = new int[ this.Expressions.Count];156 var local_treeIndex = new int[State.Length]; 145 157 146 158 var next = 1; 147 159 148 for (var i = this.Expressions.Count- 1; i >= 0; i--) {149 var subExpression = this.Expressions[i];160 for (var i = State.Length - 1; i >= 0; i--) { 161 var subExpression = State[i]; 150 162 151 163 local_treeIndex[i] = next; 152 164 153 165 if (subExpression.CanExpand) { 154 next += (( ExecExpandExpression)subExpression).State.TotalCount;166 next += ((PushProgram)subExpression).TotalCount; 155 167 } else { 156 168 next++; … … 164 176 var hash = 19 * 31 + this.GetType().FullName.GetHashCode(); 165 177 166 for (var i = 0; i < this.Expressions.Count; i++) {167 hash = hash * 31 + this.Expressions[i].GetHashCode();178 for (var i = 0; i < State.Length; i++) { 179 hash = hash * 31 + State[i].GetHashCode(); 168 180 } 169 181 … … 171 183 } 172 184 173 private static void CheckIfNested(int level, ExecExpandExpression target, ExecExpandExpressioncurrent,174 IList< ExecExpandExpression> visited) {185 private static void CheckIfNested(int level, PushProgram target, PushProgram current, 186 IList<PushProgram> visited) { 175 187 if (visited.Any(x => ReferenceEquals(x, current))) 176 188 Debugger.Break(); … … 183 195 } 184 196 185 for (var i = 0; i < current.State. Expressions.Count; i++) {186 if (current.State .Expressions[i].CanExpand)197 for (var i = 0; i < current.State.Length; i++) { 198 if (current.State[i].CanExpand) 187 199 continue; 188 200 189 var subExpression = current.State .Expressions[i] as ExecExpandExpression;201 var subExpression = current.State[i] as PushProgram; 190 202 191 203 if (ReferenceEquals(target, subExpression)) { … … 199 211 visited.RemoveAt(visited.Count - 1); 200 212 } 213 214 /// <summary> 215 /// Returns the element with the given index whereby the tree is indexed in depth first manner 216 /// </summary> 217 /// <param name="index">The index of the required element</param> 218 /// <returns>The required element</returns> 219 public Expression GetFromTree(int index) { 220 return GetFromTree(index, (root, parent, child, childIndex, parentIndex) => child); 221 } 222 223 /// <returns>The required element</returns> 224 public T GetFromTree<T>(int index, 225 Func<PushProgram, PushProgram, Expression, int, int, T> resolver) { 226 return GetFromTree(index, 0, null, resolver); 227 } 228 229 private T GetFromTree<T>(int index, int parentIndex, PushProgram parent, 230 Func<PushProgram, PushProgram, Expression, int, int, T> resolver) { 231 if (index == 0) return resolver(parent, parent, this, 0, parentIndex); 232 233 var min = State.Length - 1; 234 var max = 0; 235 var mid = (min + max) / 2; 236 237 while ((max <= min) && (TreeIndex[mid] != index)) { 238 if (TreeIndex[mid] > index) max = mid + 1; 239 else min = mid - 1; 240 241 mid = (min + max) / 2; 242 } 243 244 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, 247 this, resolver); 248 } 201 249 } 202 250 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/StatefulExpression.cs
r14744 r14745 1 1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { 2 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool; 3 4 public abstract class StatefulExpression<T> : Expression 5 { 2 public abstract class StatefulExpression<T> : Expression { 6 3 public readonly T State; 7 4 -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Generators/CodeGenerator.cs
r14744 r14745 24 24 } 25 25 26 public static ExecExpandExpressionRandomExpandExpression(int maxPoints, IRandom random = null, IReadonlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) {26 public static PushProgram RandomExpandExpression(int maxPoints, IRandom random = null, IReadonlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) { 27 27 var program = RandomProgram(maxPoints, random, pushGpConfiguration, customExpressions); 28 28 29 return new ExecExpandExpression(program);29 return new PushProgram(program); 30 30 } 31 31 -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/IPushInterpreter.cs
r14744 r14745 28 28 void Clear(); 29 29 void Run(string code, bool stepwise = false); 30 void Run(Expression program, bool stepwise = false);31 void Run(PushProgram program, bool stepwise = false);32 Task RunAsync(PushProgram program, bool paused = false, CancellationToken token = default(CancellationToken));33 Task RunAsync(Expression program, bool paused = false, CancellationToken token = default(CancellationToken));30 void Run(Expression expression, bool stepwise = false); 31 //void Run(PushProgram program, bool stepwise = false); 32 //Task RunAsync(PushProgram program, bool paused = false, CancellationToken token = default(CancellationToken)); 33 Task RunAsync(Expression expression, bool paused = false, CancellationToken token = default(CancellationToken)); 34 34 Task RunAsync(string code, bool paused = false, CancellationToken token = default(CancellationToken)); 35 35 Task AbortAndResetAsync(); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreter.cs
r14744 r14745 7 7 using HeuristicLab.Core; 8 8 using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; 9 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;10 9 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 11 10 using HeuristicLab.Problems.ProgramSynthesis.Push.Parser; … … 101 100 public bool IsNameQuoteFlagSet { get; set; } 102 101 102 public Task RunAsync(string code, bool stepwise = false, CancellationToken token = default(CancellationToken)) { 103 var program = PushParser.Parse(code); 104 105 currentTask = RunAsync(program, stepwise, token); 106 107 return currentTask; 108 } 109 public async Task RunAsync( 110 Expression expression, 111 bool stepwise = false, 112 CancellationToken token = default(CancellationToken)) { 113 await Task.Run(() => Run(expression, stepwise), token); 114 } 115 103 116 public void Run(string code, bool stepwise = false) { 104 117 var program = PushParser.Parse(code); … … 106 119 } 107 120 108 public Task RunAsync(string code, bool stepwise = false, CancellationToken token = default(CancellationToken)) {109 var program = PushParser.Parse(code);110 111 currentTask = RunAsync(program, stepwise, token);112 113 return currentTask;114 }115 116 public async Task RunAsync(117 PushProgram program,118 bool stepwise = false,119 CancellationToken token = default(CancellationToken)) {120 var expression = new ExecExpandExpression(program);121 122 await RunAsync(expression, stepwise, token);123 }124 125 public async Task RunAsync(126 Expression program,127 bool stepwise = false,128 CancellationToken token = default(CancellationToken)) {129 await Task.Run(() => Run(program, stepwise), token);130 }131 132 public void Run(PushProgram program, bool stepwise = false) {133 var expression = new ExecExpandExpression(program);134 135 Run(expression, stepwise);136 }137 121 138 122 public void Run(Expression program, bool stepwise = false) { -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Parser/PushParser.cs
r14744 r14745 16 16 private static readonly char[] symbolTrim = { '\r', '\n' }; 17 17 18 public static ExpressionParse(string source, int startIndex = 0) {18 public static PushProgram Parse(string source, int startIndex = 0) { 19 19 var symbols = source.Split(delimiter); 20 20 … … 23 23 } 24 24 25 private static ExpressionParse(string[] symbols, int startIndex, out int endIndex) {25 private static PushProgram Parse(string[] symbols, int startIndex, out int endIndex) { 26 26 var expressions = new List<Expression>(); 27 27 … … 39 39 case closeBrace: 40 40 endIndex = i; 41 return new ExecExpandExpression(expressions.ToArray());41 return new PushProgram(expressions.ToArray()); 42 42 } 43 43 … … 62 62 endIndex = symbols.Length - 1; 63 63 64 return expressions.Count == 1 65 ? expressions[0] 66 : new ExecExpandExpression(expressions.ToArray()); 64 return new PushProgram(expressions.ToArray()); 67 65 } 68 66 -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushProblem.cs
r14744 r14745 406 406 407 407 var program = individual.PushProgram(config.EnabledExpressions as IReadOnlyList<string>); 408 var expandExpression = new ExecExpandExpression(program);408 var expandExpression = new PushProgram(program); 409 409 var results = new List<double>(); 410 410 -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Simplifier/ISimplifier.cs
r14727 r14745 6 6 7 7 public interface ISimplifier { 8 ExecExpandExpression Simplify(ExecExpandExpression program, IRandom random, Predicate<ExecExpandExpression> isBetter);8 PushProgram Simplify(PushProgram program, IRandom random, Predicate<PushProgram> isBetter); 9 9 } 10 10 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Simplifier/RandomSimplifier.cs
r14744 r14745 7 7 public int Trys { get; set; } 8 8 9 public ExecExpandExpression Simplify(ExecExpandExpression program, IRandom random, Predicate<ExecExpandExpression> isBetter) {9 public PushProgram Simplify(PushProgram program, IRandom random, Predicate<PushProgram> isBetter) { 10 10 11 if (program. State.TotalCount == 1) {12 return isBetter( ExecExpandExpression.Empty) ? ExecExpandExpression.Empty : program;11 if (program.TotalCount == 1) { 12 return isBetter(PushProgram.Empty) ? PushProgram.Empty : program; 13 13 } 14 14 15 15 var copy = program.Copy(); 16 var maxTries = Math.Min(Trys, program. State.TotalCount - 2);16 var maxTries = Math.Min(Trys, program.TotalCount - 2); 17 17 var successfulRemoves = 0; 18 18 19 19 for (var i = 0; i < maxTries; i++) { 20 var rndIndex = random.Next(1, program. State.TotalCount - 1 - successfulRemoves);20 var rndIndex = random.Next(1, program.TotalCount - 1 - successfulRemoves); 21 21 var node = copy.GetFromTree( 22 22 rndIndex, … … 28 28 }); 29 29 30 var oldParentExpressions = node.Parent.State .Expressions as Expression[];30 var oldParentExpressions = node.Parent.State; 31 31 var newParentExpressions = RemoveAt(oldParentExpressions, node.ChildIndex); 32 var newParent = new ExecExpandExpression(newParentExpressions);32 var newParent = new PushProgram(newParentExpressions); 33 33 34 var superExpressions = (node.Super == null ? copy.State.Expressions : node.Super.State.Expressions) as Expression[];34 var superExpressions = node.Super == null ? copy.State : node.Super.State; 35 35 superExpressions[node.ParentIndex] = newParent; 36 36
Note: See TracChangeset
for help on using the changeset viewer.