Changeset 14328
- Timestamp:
- 10/08/16 12:04:25 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.Algorithms.PushGP
- Files:
-
- 23 added
- 41 deleted
- 31 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.Cli/App.config
r14320 r14328 1 <?xml version="1.0" encoding="utf-8"?>1 <?xml version="1.0" encoding="utf-8"?> 2 2 <configuration> 3 3 <startup> 4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5 .2"/>4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> 5 5 </startup> 6 6 </configuration> -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.Cli/HeuristicLab.Algorithms.PushGP.Cli.csproj
r14320 r14328 10 10 <RootNamespace>HeuristicLab.Algorithms.PushGP.Cli</RootNamespace> 11 11 <AssemblyName>HeuristicLab.Algorithms.PushGP.Cli</AssemblyName> 12 <TargetFrameworkVersion>v4.5 .2</TargetFrameworkVersion>12 <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> 13 13 <FileAlignment>512</FileAlignment> 14 14 <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> 15 <TargetFrameworkProfile /> 15 16 </PropertyGroup> 16 17 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> … … 32 33 <ErrorReport>prompt</ErrorReport> 33 34 <WarningLevel>4</WarningLevel> 35 <LangVersion>5</LangVersion> 34 36 </PropertyGroup> 35 37 <ItemGroup> -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.Cli/Program.cs
r14323 r14328 12 12 static void Main(string[] args) 13 13 { 14 //Stepwise().Wait();15 PerformanceTest();14 Stepwise().Wait(); 15 //PerformanceTestInterpreter(); 16 16 //PerformanceTestCodeGenerator(); 17 17 … … 22 22 static async Task Stepwise() 23 23 { 24 var program = PushGPInterpreter.Encode("( 2 3 INTEGER.* 4.1 5.2 FLOAT.+ TRUE FALSE BOOLEAN.OR )"); 24 var program = PushGPInterpreter.Encode(@"( CODE.QUOTE ( INTEGER.POP 1 ) 25 CODE.QUOTE ( CODE.DUP INTEGER.DUP 1 INTEGER.- CODE.DO INTEGER.* ) 26 INTEGER.DUP 2 INTEGER.< CODE.IF )"); 25 27 var interpreter = new PushGPInterpreter(); 26 28 27 var task = interpreter.InterpreteAsync(program);28 await interpreter.PauseAsync();29 interpreter.IntegerStack.Push(5); 30 interpreter.InterpretAsync(program, true).Wait(); 29 31 30 32 while (!interpreter.IsCompleted) … … 49 51 } 50 52 51 static void PerformanceTest ()53 static void PerformanceTestInterpreter() 52 54 { 53 55 var program = PushGPInterpreter.Encode("( 5 INTEGER.DUP INTEGER.+ )"); … … 58 60 for (var i = 0; i < 20000000; i++) 59 61 { 60 interpreter.Interpret e(program);61 interpreter. Reset();62 interpreter.Interpret(program); 63 interpreter.Clear(); 62 64 } 63 65 sw.Stop(); … … 71 73 72 74 sw.Start(); 73 var expressions = CodeGenerator.RandomCode( 10000000);75 var expressions = CodeGenerator.RandomCode(3000000); 74 76 sw.Stop(); 75 77 76 Console.WriteLine($"Count: {expressions.Count()}"); 77 Console.WriteLine(sw.Elapsed); 78 Console.WriteLine(string.Format("Generated {0} in {1}", expressions.Count(), sw.Elapsed)); 78 79 } 79 80 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Boolean/BooleanPushExpression.cs
r14323 r14328 1 using HeuristicLab.Algorithms.PushGP.Stack; 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 7 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 8 namespace HeuristicLab.Algorithms.PushGP.Expressions.Boolean 4 9 { 5 10 public class BooleanPushExpression : Expression 6 11 { 7 public BooleanPushExpression(bool value) : base(OpCode.BooleanPush) 12 private readonly bool value; 13 public BooleanPushExpression(bool value) 8 14 { 9 this. Value = value;15 this.value = value; 10 16 } 11 17 12 public bool Value { get;}18 public override bool IsCodeOp { get { return false; } } 13 19 14 public override void Eval(IInterpreter Service interpreterService)20 public override void Eval(IInterpreter interpreter) 15 21 { 16 interpreter Service.BooleanStack.Push(Value);22 interpreter.BooleanStack.Push(this.value); 17 23 } 18 24 19 25 public override string ToString() 20 26 { 21 return $"{this.Value.ToString().ToUpper()}";27 return this.value.ToString().ToUpper(); 22 28 } 23 29 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Code/CodeDefineExpression.cs
r14320 r14328 1 using HeuristicLab.Algorithms.PushGP.Stack; 1 using HeuristicLab.Algorithms.PushGP.Expressions.Exec; 2 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 3 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 4 namespace HeuristicLab.Algorithms.PushGP.Expressions.Code 4 5 { 5 6 public class CodeDefineExpression : Expression 6 7 { 7 public CodeDefineExpression() : base(OpCode.CodeDefine) 8 { } 8 public override bool IsCodeOp { get { return true; } } 9 9 10 public override void Eval(IInterpreter Service interpreterService)10 public override void Eval(IInterpreter interpreter) 11 11 { 12 Define( 13 interpreterService.CodeStack, 14 interpreterService.NameStack, 15 interpreterService.CustomExpressions, 16 value => new ExecPushExpression(value)); 12 // not enough arguments on stack 13 if (interpreter.NameStack.Count == 0 || 14 interpreter.CodeStack.Count == 0) 15 return; 17 16 18 // as the interpreter does not pop code expressions 19 interpreterService.CodeStack.Pop(); 17 var name = interpreter.NameStack.Pop(); 18 var expression = new ExecPushExpression(interpreter.CodeStack.Top); 19 20 if (interpreter.CustomExpressions.ContainsKey(name)) 21 { 22 interpreter.CustomExpressions[name] = expression; 23 } 24 else 25 { 26 interpreter.CustomExpressions.Add(name, expression); 27 } 28 29 interpreter.CodeStack.Pop(); 30 } 31 32 public override string ToString() 33 { 34 return Symbols.CodeDefine; 20 35 } 21 36 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Code/CodeDoExpression.cs
r14320 r14328 1 using HeuristicLab.Algorithms.PushGP. Stack;1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 3 namespace HeuristicLab.Algorithms.PushGP.Expressions.Code 4 4 { 5 5 public class CodeDoExpression : Expression 6 6 { 7 public CodeDoExpression() : base(OpCode.CodeDo) 8 { } 7 public override bool IsCodeOp { get { return true; } } 9 8 10 public override void Eval(IInterpreter Service interpreterService)9 public override void Eval(IInterpreter interpreter) 11 10 { 12 11 // not enough arguments on stack 13 if (interpreter Service.CodeStack.Count == 0)12 if (interpreter.CodeStack.Count == 0) 14 13 return; 15 14 16 var expression = interpreterService.CodeStack.Pop(); 17 interpreterService.ExecStack.Push(expression); 15 var expression = interpreter.CodeStack.Pop(); 16 interpreter.ExecStack.Push(expression); 17 } 18 19 public override string ToString() 20 { 21 return Symbols.CodeDo; 18 22 } 19 23 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Code/CodeIfExpression.cs
r14320 r14328 1 using HeuristicLab.Algorithms.PushGP. Stack;1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 3 namespace HeuristicLab.Algorithms.PushGP.Expressions.Code 4 4 { 5 5 public class CodeIfExpression : Expression 6 6 { 7 public CodeIfExpression() : base(OpCode.CodeIf) 8 { } 7 public override bool IsCodeOp { get { return true; } } 9 8 10 public override void Eval(IInterpreter Service interpreterService)9 public override void Eval(IInterpreter interpreter) 11 10 { 12 11 // not enough arguments on stack 13 if (interpreter Service.BooleanStack.Count == 0 ||14 interpreter Service.CodeStack.Count < 2)12 if (interpreter.BooleanStack.Count == 0 || 13 interpreter.CodeStack.Count < 2) 15 14 return; 16 15 17 var condition = interpreter Service.BooleanStack.Pop();18 var expressionFalse = interpreter Service.CodeStack.Pop();19 var expressionTrue = interpreter Service.CodeStack.Pop();16 var condition = interpreter.BooleanStack.Pop(); 17 var expressionFalse = interpreter.CodeStack.Pop(); 18 var expressionTrue = interpreter.CodeStack.Pop(); 20 19 21 interpreter Service.ExecStack.Push(condition20 interpreter.ExecStack.Push(condition 22 21 ? expressionTrue 23 22 : expressionFalse); 24 23 } 24 25 public override string ToString() 26 { 27 return Symbols.CodeIf; 28 } 25 29 } 26 30 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Code/CodeNoopExpression.cs
r14320 r14328 1 using HeuristicLab.Algorithms.PushGP. Stack;1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 3 namespace HeuristicLab.Algorithms.PushGP.Expressions.Code 4 4 { 5 5 public class CodeNoopExpression : Expression 6 6 { 7 public CodeNoopExpression() : base(OpCode.CodeNoop) 7 public override bool IsCodeOp { get { return true; } } 8 9 public override void Eval(IInterpreter interpreter) 8 10 { 9 } 10 11 public override void Eval(IInterpreterService interpreterService) 12 { 13 // nothing to do 11 // do nothing 14 12 } 15 13 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Code/CodeQuoteExpression.cs
r14320 r14328 1 using HeuristicLab.Algorithms.PushGP. Stack;1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 3 namespace HeuristicLab.Algorithms.PushGP.Expressions.Code 4 4 { 5 5 public class CodeQuoteExpression : Expression 6 6 { 7 public CodeQuoteExpression() : base(OpCode.CodeQuote) 8 { } 7 public override bool IsCodeOp { get { return true; } } 9 8 10 public override void Eval(IInterpreter Service interpreterService)9 public override void Eval(IInterpreter interpreter) 11 10 { 12 11 // not enough arguments on stack 13 if (interpreter Service.ExecStack.Count == 0)12 if (interpreter.ExecStack.Count == 0) 14 13 return; 15 14 16 var expression = interpreterService.ExecStack.Pop(); 17 interpreterService.CodeStack.Push(expression); 15 var expression = interpreter.ExecStack.Pop(); 16 interpreter.CodeStack.Push(expression); 17 } 18 19 public override string ToString() 20 { 21 return Symbols.CodeQuote; 18 22 } 19 23 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecDefineExpression.cs
r14320 r14328 1 using HeuristicLab.Algorithms.PushGP.Stack; 1 using HeuristicLab.Algorithms.PushGP.Expressions.Exec; 2 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 3 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 4 namespace HeuristicLab.Algorithms.PushGP.Expressions.Code 4 5 { 5 6 public class ExecDefineExpression : Expression 6 7 { 7 public ExecDefineExpression() : base(OpCode.ExecDefine) 8 { } 8 public override bool IsCodeOp { get { return false; } } 9 9 10 public override void Eval(IInterpreter Service interpreterService)10 public override void Eval(IInterpreter interpreter) 11 11 { 12 Define( 13 interpreterService.ExecStack, 14 interpreterService.NameStack, 15 interpreterService.CustomExpressions, 16 value => new ExecPushExpression(value)); 12 // not enough arguments on stack 13 if (interpreter.NameStack.Count == 0 || 14 interpreter.CodeStack.Count == 0) 15 return; 17 16 18 // as the interpreter does not pop code expressions 19 interpreterService.ExecStack.Pop(); 17 var name = interpreter.NameStack.Pop(); 18 var expression = new ExecPushExpression(interpreter.ExecStack.Top); 19 20 if (interpreter.CustomExpressions.ContainsKey(name)) 21 { 22 interpreter.CustomExpressions[name] = expression; 23 } 24 else 25 { 26 interpreter.CustomExpressions.Add(name, expression); 27 } 28 29 interpreter.ExecStack.Pop(); 30 } 31 32 public override string ToString() 33 { 34 return Symbols.CodeDefine; 20 35 } 21 36 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecDoRangeExpression.cs
r14320 r14328 1 using HeuristicLab.Algorithms.PushGP. Stack;1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 3 namespace HeuristicLab.Algorithms.PushGP.Expressions.Exec 4 4 { 5 5 public class ExecDoRangeExpression : Expression 6 6 { 7 public ExecDoRangeExpression() : base(OpCode.ExecDoXRange) 8 { } 7 public override bool IsCodeOp { get { return false; } } 9 8 10 public override void Eval(IInterpreter Service interpreterService)9 public override void Eval(IInterpreter interpreter) 11 10 { 12 11 // not enough arguments on stack 13 if (interpreter Service.IntegerStack.Count < 2 ||14 interpreter Service.ExecStack.Count == 0)12 if (interpreter.IntegerStack.Count < 2 || 13 interpreter.ExecStack.Count == 0) 15 14 return; 16 15 17 var destinationIndex = interpreter Service.IntegerStack.Pop();18 var currentIndex = interpreter Service.IntegerStack.Pop();19 var loopBody = interpreter Service.ExecStack.Pop();16 var destinationIndex = interpreter.IntegerStack.Pop(); 17 var currentIndex = interpreter.IntegerStack.Pop(); 18 var loopBody = interpreter.ExecStack.Pop(); 20 19 21 20 if (destinationIndex == currentIndex) 22 21 { 23 interpreter Service.IntegerStack.Push(currentIndex);24 interpreter Service.ExecStack.Push(loopBody);22 interpreter.IntegerStack.Push(currentIndex); 23 interpreter.ExecStack.Push(loopBody); 25 24 } 26 25 else … … 30 29 : currentIndex + 1; 31 30 32 interpreter Service.IntegerStack.Push(currentIndex);33 interpreter Service.IntegerStack.Push(nextIndex);34 interpreter Service.IntegerStack.Push(destinationIndex);31 interpreter.IntegerStack.Push(currentIndex); 32 interpreter.IntegerStack.Push(nextIndex); 33 interpreter.IntegerStack.Push(destinationIndex); 35 34 36 35 var expression = new ExecDoRangeExpression(); 37 interpreter Service.ExecStack.Push(loopBody);38 interpreter Service.ExecStack.Push(loopBody);39 interpreter Service.ExecStack.Push(expression);36 interpreter.ExecStack.Push(loopBody); 37 interpreter.ExecStack.Push(loopBody); 38 interpreter.ExecStack.Push(expression); 40 39 } 40 } 41 42 public override string ToString() 43 { 44 return Symbols.ExecDoXRange; 41 45 } 42 46 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecExpandExpression.cs
r14323 r14328 1 using System. Linq;2 using HeuristicLab.Algorithms.PushGP. Stack;1 using System.Text; 2 using HeuristicLab.Algorithms.PushGP.Interpreter; 3 3 4 namespace HeuristicLab.Algorithms.PushGP.Expressions 4 namespace HeuristicLab.Algorithms.PushGP.Expressions.Exec 5 5 { 6 6 public class ExecExpandExpression : Expression 7 7 { 8 public ExecExpandExpression(Expression[] expressions) : base(OpCode.ExecExpand) 8 private const string prefix = "( "; 9 private const string postifx = " )"; 10 private const string delimiter = " "; 11 12 private readonly Expression[] expressions; 13 public ExecExpandExpression(Expression[] expressions) 9 14 { 10 this. Expressions = expressions;15 this.expressions = expressions; 11 16 } 12 17 13 public Expression[] Expressions { get;}18 public override bool IsCodeOp { get { return false; } } 14 19 15 public override void Eval(IInterpreter Service interpreterService)20 public override void Eval(IInterpreter interpreter) 16 21 { 17 foreach (var expression in this.Expressions) 18 { 19 interpreterService.ExecStack.Add(expression); 20 } 22 interpreter.ExecStack.Push(this.expressions); 21 23 } 22 24 23 25 public override string ToString() 24 26 { 25 return $"( {string.Join(" ", this.Expressions.Reverse())} )"; 27 if (this.expressions.Length == 0) 28 { 29 return string.Empty; 30 } 31 32 var sb = new StringBuilder(); 33 sb.Append(prefix); 34 35 for (var i = this.expressions.Length - 1; i > 0; i--) 36 { 37 sb.Append(this.expressions[i] + delimiter); 38 } 39 40 sb.Append(this.expressions[0] + postifx); 41 42 return sb.ToString(); 26 43 } 27 44 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecIfExpression.cs
r14320 r14328 1 using HeuristicLab.Algorithms.PushGP. Stack;1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 3 namespace HeuristicLab.Algorithms.PushGP.Expressions.Exec 4 4 { 5 5 public class ExecIfExpression : Expression 6 6 { 7 public ExecIfExpression() : base(OpCode.ExecIf) 8 { } 7 public override bool IsCodeOp { get { return false; } } 9 8 10 public override void Eval(IInterpreter Service interpreterService)9 public override void Eval(IInterpreter interpreter) 11 10 { 12 11 // not enough arguments on stack 13 if (interpreter Service.BooleanStack.Count == 0 ||14 interpreter Service.ExecStack.Count < 2)12 if (interpreter.BooleanStack.Count == 0 || 13 interpreter.ExecStack.Count < 2) 15 14 return; 16 15 17 var condition = interpreter Service.BooleanStack.Pop();18 var expressionTrue = interpreter Service.ExecStack.Pop();19 var expressionFalse = interpreter Service.ExecStack.Pop();16 var condition = interpreter.BooleanStack.Pop(); 17 var expressionTrue = interpreter.ExecStack.Pop(); 18 var expressionFalse = interpreter.ExecStack.Pop(); 20 19 21 interpreter Service.ExecStack.Push(condition20 interpreter.ExecStack.Push(condition 22 21 ? expressionTrue 23 22 : expressionFalse); 24 23 } 24 25 public override string ToString() 26 { 27 return Symbols.ExecIf; 28 } 25 29 } 26 30 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecPushExpression.cs
r14320 r14328 1 using HeuristicLab.Algorithms.PushGP. Stack;1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 3 namespace HeuristicLab.Algorithms.PushGP.Expressions.Exec 4 4 { 5 5 public class ExecPushExpression : Expression 6 6 { 7 public ExecPushExpression(Expression value) : base(OpCode.ExecPush) 7 private readonly Expression value; 8 public ExecPushExpression(Expression value) 8 9 { 9 this. Value = value;10 this.value = value; 10 11 } 11 12 12 public Expression Value { get;}13 public override bool IsCodeOp { get { return false; } } 13 14 14 public override void Eval(IInterpreter Service interpreterService)15 public override void Eval(IInterpreter interpreter) 15 16 { 16 interpreter Service.ExecStack.Push(Value);17 interpreter.ExecStack.Push(this.value); 17 18 } 18 19 19 20 public override string ToString() 20 21 { 21 return $"{base.ToString()}({this.Value})";22 return this.value.ToString(); 22 23 } 23 24 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecYExpression.cs
r14323 r14328 1 using HeuristicLab.Algorithms.PushGP. Stack;1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 3 namespace HeuristicLab.Algorithms.PushGP.Expressions.Exec 4 4 { 5 5 public class ExecYExpression : Expression 6 6 { 7 public ExecYExpression() : base(OpCode.ExecY) 8 { } 7 public override bool IsCodeOp { get { return false; } } 9 8 10 public override void Eval(IInterpreter Service interpreterService)9 public override void Eval(IInterpreter interpreter) 11 10 { 12 11 // not enough arguments on stack 13 if (interpreter Service.ExecStack.Count == 0)12 if (interpreter.ExecStack.Count == 0) 14 13 return; 15 14 16 15 var expandExpression = new ExecExpandExpression(new[] 17 16 { 18 interpreter Service.ExecStack.Top,17 interpreter.ExecStack.Top, 19 18 new ExecYExpression() 20 19 }); 21 20 22 interpreterService.ExecStack.Insert(interpreterService.ExecStack.Count - 1, expandExpression); 21 interpreter.ExecStack.Insert(interpreter.ExecStack.Count - 1, expandExpression); 22 } 23 24 public override string ToString() 25 { 26 return Symbols.ExecY; 23 27 } 24 28 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Expression.cs
r14323 r14328 1 1 using System; 2 2 using System.Collections.Generic; 3 using HeuristicLab.Algorithms.PushGP.Interpreter; 3 4 using HeuristicLab.Algorithms.PushGP.Stack; 4 5 … … 7 8 public abstract class Expression 8 9 { 9 public Expression(OpCode opCode) 10 { 11 this.OpCode = opCode; 12 } 10 public abstract bool IsCodeOp { get; } 11 public abstract void Eval(IInterpreter interpreter); 13 12 14 public OpCode OpCode { get; } 15 16 public abstract void Eval(IInterpreterService interpreterService); 17 18 public override string ToString() 19 { 20 return SymbolTable.GetSymbol(this.OpCode); 21 } 22 23 protected static void PushResult<T>(IStack<T> stack, int count, Func<T[], T> templateFunc) 13 public void PushResult<T>(IStack<T> stack, int count, Func<T[], T> templateFunc) 24 14 { 25 15 PushResult(stack, stack, count, templateFunc); 26 16 } 27 17 28 p rotected static void PushResult<T, R>(IStack<T> sourceStack, IStack<R> targetStack, int count, Func<T[], R> templateFunc)18 public void PushResult<T, R>(IStack<T> sourceStack, IStack<R> targetStack, int count, Func<T[], R> templateFunc) 29 19 { 30 20 // not enough arguments on stack … … 38 28 } 39 29 40 p rotected static void Duplicate<T>(IStack<T> stack)30 public void Duplicate<T>(IStack<T> stack) 41 31 { 42 32 // not enough arguments on stack -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Float/FloatPushExpression.cs
r14323 r14328 1 using HeuristicLab.Algorithms.PushGP. Stack;1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 3 namespace HeuristicLab.Algorithms.PushGP.Expressions.Float 4 4 { 5 5 public class FloatPushExpression : Expression 6 6 { 7 public FloatPushExpression(double value) : base(OpCode.FloatPush) 7 private readonly double value; 8 public FloatPushExpression(double value) 8 9 { 9 this. Value = value;10 this.value = value; 10 11 } 11 12 12 public double Value { get;}13 public override bool IsCodeOp { get { return false; } } 13 14 14 public override void Eval(IInterpreter Service interpreterService)15 public override void Eval(IInterpreter interpreter) 15 16 { 16 interpreter Service.FloatStack.Push(Value);17 interpreter.FloatStack.Push(this.value); 17 18 } 18 19 19 20 public override string ToString() 20 21 { 21 return $"{this.Value}";22 return this.value.ToString(); 22 23 } 23 24 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Integer/IntegerPushExpression.cs
r14323 r14328 1 using HeuristicLab.Algorithms.PushGP. Stack;1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 3 namespace HeuristicLab.Algorithms.PushGP.Expressions.Integer 4 4 { 5 5 public class IntegerPushExpression : Expression 6 6 { 7 public IntegerPushExpression(long value) : base(OpCode.IntegerPush) 7 private readonly long value; 8 public IntegerPushExpression(long value) 8 9 { 9 this. Value = value;10 this.value = value; 10 11 } 11 12 12 public long Value { get;}13 public override bool IsCodeOp { get { return false; } } 13 14 14 public override void Eval(IInterpreter Service interpreterService)15 public override void Eval(IInterpreter interpreter) 15 16 { 16 interpreter Service.IntegerStack.Push(Value);17 interpreter.IntegerStack.Push(this.value); 17 18 } 18 19 19 20 public override string ToString() 20 21 { 21 return $"{this.Value}";22 return this.value.ToString(); 22 23 } 23 24 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Name/NameDefineXExecExpression.cs
r14320 r14328 1 using HeuristicLab.Algorithms.PushGP. Stack;1 using HeuristicLab.Algorithms.PushGP.Interpreter; 2 2 3 namespace HeuristicLab.Algorithms.PushGP.Expressions 3 namespace HeuristicLab.Algorithms.PushGP.Expressions.Name 4 4 { 5 5 public class NameDefineXExecExpression : Expression 6 6 { 7 public NameDefineXExecExpression(string value) : base(OpCode.NameDefineXExec) 7 private readonly string name; 8 public NameDefineXExecExpression(string name) 8 9 { 9 this. Value = value;10 this.name = name; 10 11 } 11 12 12 public string Value { get;}13 public override bool IsCodeOp { get { return false; } } 13 14 14 public override void Eval(IInterpreter Service interpreterService)15 public override void Eval(IInterpreter interpreter) 15 16 { 16 17 Expression expression; 17 if (interpreter Service.CustomExpressions.TryGetValue(this.Value, out expression))18 if (interpreter.CustomExpressions.TryGetValue(this.name, out expression)) 18 19 { 19 interpreter Service.ExecStack.Push(expression);20 interpreter.ExecStack.Push(expression); 20 21 } 21 22 else 22 23 { 23 interpreter Service.NameStack.Push(this.Value);24 interpreter.NameStack.Push(this.name); 24 25 } 25 26 } … … 27 28 public override string ToString() 28 29 { 29 return this. Value;30 return this.name; 30 31 } 31 32 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/CodeGenerator.cs
r14323 r14328 1 using System; 2 using System.Collections.Generic; 1 using System.Collections.Generic; 3 2 using System.Linq; 4 3 using HeuristicLab.Algorithms.PushGP.Expressions; 4 using HeuristicLab.Random; 5 5 6 6 namespace HeuristicLab.Algorithms.PushGP.Generators … … 8 8 public static class CodeGenerator 9 9 { 10 private static FastRandom random = new FastRandom(); 10 11 public static IEnumerable<Expression> RandomCode(int maxPoints) 11 12 { 12 var actualPoints = StaticRandom.Next(1, maxPoints);13 var actualPoints = random.Next(1, maxPoints); 13 14 14 15 return RandomCodeWithSize(actualPoints).ToArray(); … … 19 20 if (points == 1) 20 21 { 21 OpCode opCode;22 Func<Expression> creator;22 // TODO: If this is an "ephemeral random constant"???? then return a randomly - chosen value of the appropriate type; 23 var opCode = (OpCode)random.Next(OpCodeExtensions.Min, OpCodeExtensions.Max); 23 24 24 do 25 { 26 // TODO: If this is an "ephemeral random constant"???? then return a randomly - chosen value of the appropriate type; 27 opCode = (OpCode)StaticRandom.Next(OpCodeExtensions.Min, OpCodeExtensions.Max); 28 } 29 while (!ExpressionCreatorTable.TryGetCreator(opCode, out creator)); 30 31 return new[] { creator.Invoke() }; 25 return new[] { ExpressionFactory.Create(opCode) }; 32 26 } 33 27 else … … 35 29 var sizesThisLevel = Decompose(points - 1, points - 1); 36 30 37 return sizesThisLevel.SelectMany(size => RandomCodeWithSize(size)) ;31 return sizesThisLevel.SelectMany(size => RandomCodeWithSize(size)).Shuffle(random); 38 32 } 39 33 } … … 47 41 else 48 42 { 49 var thisPart = StaticRandom.Next(1, number - 1);43 var thisPart = random.Next(1, number - 1); 50 44 51 45 return new[] { thisPart }.Concat(Decompose(number - thisPart, maxParts - 1)); -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/Configuration.cs
r14323 r14328 3 3 public class Configuration 4 4 { 5 public Configuration() 6 { 7 this.MinRandomInteger = long.MinValue; 8 this.MaxRandomInteger = long.MaxValue; 9 this.MinRandomFloat = double.MaxValue; 10 this.MaxRandomFloat = double.MaxValue; 11 this.NewErcNameProbability = 0.5; 12 } 13 5 14 /// <summary> 6 15 /// The minimum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND. 7 16 /// </summary> 8 public long MinRandomInteger { get; set; } = long.MinValue;17 public long MinRandomInteger { get; set; } 9 18 10 19 /// <summary> 11 20 /// The maximum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND. 12 21 /// </summary> 13 public long MaxRandomInteger { get; set; } = long.MaxValue;22 public long MaxRandomInteger { get; set; } 14 23 15 24 /// <summary> 16 25 /// The minimum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND. 17 26 /// </summary> 18 public double MinRandomFloa g { get; set; } = double.MinValue;27 public double MinRandomFloat { get; set; } 19 28 20 29 /// <summary> 21 30 /// The maximum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND. 22 31 /// </summary> 23 public double MaxRandomFloa g { get; set; } = double.MaxValue;32 public double MaxRandomFloat { get; set; } 24 33 25 34 /// <summary> … … 28 37 /// (rather than a name that was previously generated). 29 38 /// </summary> 30 public double NewErcNameProbability { get; set; } = 0.5;39 public double NewErcNameProbability { get; set; } 31 40 } 32 41 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.csproj
r14323 r14328 10 10 <RootNamespace>HeuristicLab.Algorithms.PushGP</RootNamespace> 11 11 <AssemblyName>HeuristicLab.Algorithms.PushGP</AssemblyName> 12 <TargetFrameworkVersion>v4.5 .2</TargetFrameworkVersion>12 <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> 13 13 <FileAlignment>512</FileAlignment> 14 <TargetFrameworkProfile /> 14 15 </PropertyGroup> 15 16 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> … … 29 30 <ErrorReport>prompt</ErrorReport> 30 31 <WarningLevel>4</WarningLevel> 32 <LangVersion>5</LangVersion> 31 33 </PropertyGroup> 32 34 <ItemGroup> 35 <Reference Include="HeuristicLab.Common-3.3"> 36 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath> 37 </Reference> 38 <Reference Include="HeuristicLab.Core-3.3"> 39 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath> 40 </Reference> 41 <Reference Include="HeuristicLab.Random-3.3"> 42 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath> 43 </Reference> 33 44 <Reference Include="System" /> 34 45 <Reference Include="System.Core" /> … … 41 52 </ItemGroup> 42 53 <ItemGroup> 43 <Compile Include="Interpreter\Configuration.cs" /> 44 <Compile Include="Data\Expression.cs" /> 45 <Compile Include="Expressions\Boolean\BooleanAndExpression.cs" /> 46 <Compile Include="Expressions\Boolean\BooleanOrExpression.cs" /> 54 <Compile Include="Exporter\Exporter.cs" /> 47 55 <Compile Include="Expressions\Boolean\BooleanPushExpression.cs" /> 48 <Compile Include="Expressions\Boolean\BooleanDuplicateExpression.cs" /> 49 <Compile Include="Expressions\Boolean\BooleanDefineExpression.cs" /> 50 <Compile Include="Expressions\Boolean\BooleanPopExpression.cs" /> 51 <Compile Include="Expressions\Exec\ExecIfExpression.cs" /> 56 <Compile Include="Expressions\Code\CodeDoExpression.cs" /> 57 <Compile Include="Expressions\Code\CodeDefineExpression.cs" /> 52 58 <Compile Include="Expressions\Code\CodeIfExpression.cs" /> 53 <Compile Include="Expressions\Code\Code DoExpression.cs" />59 <Compile Include="Expressions\Code\CodeNoopExpression.cs" /> 54 60 <Compile Include="Expressions\Code\CodeQuoteExpression.cs" /> 55 <Compile Include="Expressions\Code\CodeDuplicateExpression.cs" /> 56 <Compile Include="Expressions\Code\CodeNoopExpression.cs" /> 57 <Compile Include="Expressions\Code\CodePushExpression.cs" /> 58 <Compile Include="Expressions\Code\CodeDefineExpression.cs" /> 61 <Compile Include="Expressions\Exec\ExecDefineExpression.cs" /> 59 62 <Compile Include="Expressions\Exec\ExecYExpression.cs" /> 60 63 <Compile Include="Expressions\Exec\ExecDoRangeExpression.cs" /> 61 <Compile Include="Expressions\Exec\ExecDuplicateExpression.cs" /> 64 <Compile Include="Expressions\Exec\ExecIfExpression.cs" /> 65 <Compile Include="Expressions\Exec\ExecPushExpression.cs" /> 62 66 <Compile Include="Expressions\Exec\ExecExpandExpression.cs" /> 63 <Compile Include="Expressions\Exec\ExecPushExpression.cs" /> 64 <Compile Include="Expressions\Exec\ExecDefineExpression.cs" /> 65 <Compile Include="Expressions\Exec\ExecPopExpression.cs" /> 67 <Compile Include="Expressions\ExpressionFactory.cs" /> 66 68 <Compile Include="Expressions\Expression.cs" /> 67 <Compile Include="ExpressionCreatorTable.cs" />68 <Compile Include="Expressions\Float\FloatAddExpression.cs" />69 69 <Compile Include="Expressions\Float\FloatPushExpression.cs" /> 70 <Compile Include="Expressions\Float\FloatMultiplyExpression.cs" />71 <Compile Include="Expressions\Float\FloatDivideExpression.cs" />72 <Compile Include="Expressions\Float\FloatSubtractExpression.cs" />73 <Compile Include="Expressions\Float\FloatDuplicateExpression.cs" />74 <Compile Include="Expressions\Float\FloatMaxExpression.cs" />75 <Compile Include="Expressions\Float\FloatMinExpression.cs" />76 <Compile Include="Expressions\Float\FloatEqualsExpression.cs" />77 <Compile Include="Expressions\Float\FloatGreaterThanExpression.cs" />78 <Compile Include="Expressions\Float\FloatSmallerThanExpression.cs" />79 <Compile Include="Expressions\Float\FloatDefineExpression.cs" />80 <Compile Include="Expressions\Float\FloatPopExpression.cs" />81 <Compile Include="Expressions\Integer\IntegerPopExpression.cs" />82 <Compile Include="Expressions\Integer\IntegerMinExpression.cs" />83 <Compile Include="Expressions\Integer\IntegerMaxExpression.cs" />84 <Compile Include="Expressions\Integer\IntegerDivideExpression.cs" />85 <Compile Include="Expressions\Integer\IntegerDuplicateExpression.cs" />86 <Compile Include="Expressions\Integer\IntegerEqualsExpression.cs" />87 <Compile Include="Expressions\Integer\IntegerSmallerThanExpression.cs" />88 <Compile Include="Expressions\Integer\IntegerGreaterThanExpression.cs" />89 <Compile Include="Expressions\Integer\IntegerDefineExpression.cs" />90 <Compile Include="Expressions\Integer\IntegerSubtractExpression.cs" />91 <Compile Include="Expressions\Integer\IntegerAddExpression.cs" />92 <Compile Include="Expressions\Integer\IntegerMultiplyExpression.cs" />93 70 <Compile Include="Expressions\Integer\IntegerPushExpression.cs" /> 94 <Compile Include="Expressions\Name\NamePopExpression.cs" />95 <Compile Include="Expressions\Name\NamePushExpression.cs" />96 <Compile Include="Expressions\Name\NameDuplicateExpression.cs" />97 71 <Compile Include="Expressions\Name\NameDefineXExecExpression.cs" /> 72 <Compile Include="Expressions\Templates\DefineExpressionTemplate.cs" /> 73 <Compile Include="Expressions\Templates\DuplicateExpressionTemplate.cs" /> 74 <Compile Include="Expressions\Templates\ExpressionTemplate.cs" /> 75 <Compile Include="Expressions\Templates\PopExpressionTemplate.cs" /> 76 <Compile Include="Expressions\Templates\PushResultExpressionTemplate.cs" /> 77 <Compile Include="Interpreter\Configuration.cs" /> 98 78 <Compile Include="Generators\CodeGenerator.cs" /> 99 79 <Compile Include="Generators\Configuration.cs" /> 100 <Compile Include="InstructionSets\ExecInstructionSet.cs" />101 <Compile Include="InstructionSets\CodeInstructionSet.cs" />102 <Compile Include="InstructionSets\FloatInstructionSet.cs" />103 <Compile Include="InstructionSets\NameInstructionSet.cs" />104 <Compile Include="InstructionSets\IntegerInstructionSet.cs" />105 <Compile Include="InstructionSets\CommonInstructionSet.cs" />106 <Compile Include="InstructionSets\BooleanInstructionSet.cs" />107 80 <Compile Include="OpCode.cs" /> 108 81 <Compile Include="OpCodeExtensions.cs" /> 109 <Compile Include="OperationTable.cs" />110 82 <Compile Include="Parser\Parser.cs" /> 111 <Compile Include="StaticRandom.cs" />112 83 <Compile Include="Stack\IStack.cs" /> 113 84 <Compile Include="Interpreter\Interpreter.cs" /> 114 85 <Compile Include="Properties\AssemblyInfo.cs" /> 115 <Compile Include=" Stack\IInterpreterService.cs" />86 <Compile Include="Interpreter\IInterpreter.cs" /> 116 87 <Compile Include="Stack\PushGPStack.cs" /> 117 88 <Compile Include="Parser\SymbolTable.cs" /> 89 <Compile Include="Symbols.cs" /> 118 90 </ItemGroup> 119 91 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Interpreter/Configuration.cs
r14323 r14328 4 4 namespace HeuristicLab.Algorithms.PushGP.Interpreter 5 5 { 6 public class Configuration 6 public class Configuration : Generators.Configuration 7 7 { 8 public bool IsBooleanStackEnabled { get; set; } = true; 9 public bool IsIntegerStackEnabled { get; set; } = true; 10 public bool IsFloatStackEnabled { get; set; } = true; 11 public bool IsCodeStackEnabled { get; set; } = true; 12 public bool IsNameStackEnabled { get; set; } = true; 8 public Configuration() 9 { 10 this.IsBooleanStackEnabled = true; 11 this.IsIntegerStackEnabled = true; 12 this.IsFloatStackEnabled = true; 13 this.IsCodeStackEnabled = true; 14 this.IsNameStackEnabled = true; 13 15 14 public IList<OpCode> AllowedInstructions { get; set; } 16 this.EvalPushLimit = uint.MaxValue; 17 this.MaxPointsInProgram = uint.MaxValue; 18 this.MaxPointsInRandomExpression = uint.MaxValue; 19 20 this.TopLevelPushCode = true; 21 this.TopLevelPopCode = true; 22 23 this.RandomSeedMax = 30081; 24 this.RandomSeedMin = 0; 25 this.RandomSeed = null; 26 } 27 28 public bool IsBooleanStackEnabled { get; set; } 29 public bool IsIntegerStackEnabled { get; set; } 30 public bool IsFloatStackEnabled { get; set; } 31 public bool IsCodeStackEnabled { get; set; } 32 public bool IsNameStackEnabled { get; set; } 33 34 public IList<OpCode> AllowedInclassions { get; set; } 15 35 16 36 public IList<Expression> PredefinedExpressions { get; set; } 17 37 18 38 /// <summary> 19 /// The minimum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.20 /// </summary>21 public long MinRandomInteger { get; set; } = long.MinValue;22 23 /// <summary>24 /// The maximum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.25 /// </summary>26 public long MaxRandomInteger { get; set; } = long.MaxValue;27 28 /// <summary>29 /// The minimum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.30 /// </summary>31 public double MinRandomFloag { get; set; } = double.MinValue;32 33 /// <summary>34 /// The maximum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.35 /// </summary>36 public double MaxRandomFloag { get; set; } = double.MaxValue;37 38 /// <summary>39 39 /// This is the maximum allowed number of "executions" in a single top-level call to the interpreter. 40 /// The execution of a single Push in struction counts as one execution, as does the processing of a single literal,40 /// The execution of a single Push inclassion counts as one execution, as does the processing of a single literal, 41 41 /// as does the descent into one layer of parentheses (that is, the processing of the "(" counts as one execution). 42 42 /// When this limit is exceeded the interpreter aborts immediately, leaving its stacks in the states they were in prior … … 44 44 /// is up to the calling program. 45 45 /// </summary> 46 public uint EvalPushLimit { get; set; } = uint.MaxValue;46 public uint EvalPushLimit { get; set; } 47 47 48 48 /// <summary> 49 49 /// This is the maximum size of an item on the CODE stack, expressed as a number of points. 50 /// A point is an in struction, a literal, or a pair of parentheses. Any instruction that would cause this limit to be exceeded51 /// should instead act as a NOOP, leaving all stacks in the states that they were in before the execution of the in struction.50 /// A point is an inclassion, a literal, or a pair of parentheses. Any inclassion that would cause this limit to be exceeded 51 /// should instead act as a NOOP, leaving all stacks in the states that they were in before the execution of the inclassion. 52 52 /// </summary> 53 public uint MaxPointsInProgram { get; set; } = uint.MaxValue;53 public uint MaxPointsInProgram { get; set; } 54 54 55 55 /// <summary> 56 /// The maximum number of points in an expression produced by the CODE.RAND in struction.56 /// The maximum number of points in an expression produced by the CODE.RAND inclassion. 57 57 /// </summary> 58 public uint MaxPointsInRandomExpression { get; set; } = uint.MaxValue;58 public uint MaxPointsInRandomExpression { get; set; } 59 59 60 60 /// <summary> … … 62 62 /// will be pushed onto the CODE stack prior to execution. 63 63 /// </summary> 64 public bool TopLevelPushCode { get; set; } = true;64 public bool TopLevelPushCode { get; set; } 65 65 66 66 /// <summary> 67 67 /// When TRUE, the CODE stack will be popped at the end of top level calls to the interpreter. The default is FALSE. 68 68 /// </summary> 69 public bool TopLevelPopCode { get; set; } = true;69 public bool TopLevelPopCode { get; set; } 70 70 71 /// <summary> 72 /// The probability that the selection of the ephemeral 73 /// random NAME constant for inclusion in randomly generated code will produce a new name 74 /// (rather than a name that was previously generated). 75 /// </summary> 76 public double NewErcNameProbability { get; set; } = 0.5; 71 public ushort RandomSeedMax { get; set; } 72 public ushort RandomSeedMin { get; set; } 77 73 78 public ushort RandomSeedMax { get; set; } = 30081; 79 public ushort RandomSeedMin { get; set; } = 0; 80 81 public ushort? RandomSeed { get; set; } = null; 74 public ushort? RandomSeed { get; set; } 82 75 } 83 76 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Interpreter/Interpreter.cs
r14323 r14328 1 using System.Collections.Generic; 1 using System; 2 using System.Collections.Generic; 2 3 using System.Threading; 3 4 using System.Threading.Tasks; … … 7 8 namespace HeuristicLab.Algorithms.PushGP.Interpreter 8 9 { 9 public class PushGPInterpreter : IInterpreter Service10 public class PushGPInterpreter : IInterpreter 10 11 { 11 private static Parser parser = new Parser();12 12 private Task currentTask; 13 13 … … 16 16 this.Configuration = config ?? new Configuration(); 17 17 18 this.CodeStack = new PushGPStack<Expression>(); 19 this.ExecStack = new PushGPStack<Expression>(); 20 this.NameStack = new PushGPStack<string>(); 21 this.BooleanStack = new PushGPStack<bool>(); 22 this.IntegerStack = new PushGPStack<long>(); 23 this.FloatStack = new PushGPStack<double>(); 24 } 25 26 public Configuration Configuration { get; } 27 28 public bool IsPaused { get; private set; } = false; 29 public bool IsAborted { get; private set; } = false; 30 public bool IsRunning => this.ExecStack.Count > 0 && !IsPaused && !IsAborted; 31 public bool IsCompleted => this.ExecStack.Count == 0; 32 public bool CanStep => !IsCompleted && !IsAborted && IsPaused; 33 34 public IStack<Expression> CodeStack { get; } 35 public IStack<Expression> ExecStack { get; } 36 public IStack<string> NameStack { get; } 37 public IStack<bool> BooleanStack { get; } 38 public IStack<long> IntegerStack { get; } 39 public IStack<double> FloatStack { get; } 40 41 public IDictionary<string, Expression> CustomExpressions { get; } = new Dictionary<string, Expression>(); 18 this.CodeStack = new PushGPStack<Expression>(128); 19 this.ExecStack = new PushGPStack<Expression>(128); 20 this.NameStack = new PushGPStack<string>(16); 21 this.BooleanStack = new PushGPStack<bool>(16); 22 this.IntegerStack = new PushGPStack<long>(16); 23 this.FloatStack = new PushGPStack<double>(16); 24 25 this.CustomExpressions = new Dictionary<string, Expression>(); 26 } 27 28 public Configuration Configuration { get; private set; } 29 30 public bool IsPaused { get; private set; } 31 public bool IsAborted { get; private set; } 32 public bool IsRunning { get { return this.ExecStack.Count > 0 && !IsPaused && !IsAborted; } } 33 public bool IsCompleted { get { return this.ExecStack.Count == 0; } } 34 public bool CanStep { get { return !IsCompleted && !IsAborted && IsPaused; } } 35 36 public IStack<Expression> CodeStack { get; private set; } 37 public IStack<Expression> ExecStack { get; private set; } 38 public IStack<string> NameStack { get; private set; } 39 public IStack<bool> BooleanStack { get; private set; } 40 public IStack<long> IntegerStack { get; private set; } 41 public IStack<double> FloatStack { get; private set; } 42 43 public IDictionary<string, Expression> CustomExpressions { get; private set; } 42 44 43 45 public void Reset() … … 47 49 this.currentTask = null; 48 50 51 this.Clear(); 52 } 53 54 public void Clear() 55 { 49 56 this.ExecStack.Clear(); 50 57 this.CodeStack.Clear(); … … 53 60 this.IntegerStack.Clear(); 54 61 this.FloatStack.Clear(); 55 } 56 57 public async void AbortAndReset() 62 63 this.CustomExpressions.Clear(); 64 } 65 66 public void Interpret(string code) 67 { 68 var program = Encode(code); 69 this.Interpret(program); 70 } 71 72 public Task InterpretAsync(string code, bool paused = false, CancellationToken token = default(CancellationToken)) 73 { 74 var program = Encode(code); 75 76 this.currentTask = this.InterpretAsync(program, paused, token); 77 78 return currentTask; 79 } 80 81 public Task InterpretAsync(Expression program, bool paused = false, CancellationToken token = default(CancellationToken)) 82 { 83 this.IsPaused = paused; 84 this.currentTask = Task.Run(() => this.Interpret(program), token); 85 86 return currentTask; 87 } 88 89 public void Interpret(Expression program) 90 { 91 this.IsAborted = false; 92 93 /* Push top expression so the loop is able to enter 94 * If the top expression is a single statement then the loop has nothing to do 95 * Otherwise the expand expression will be evaluated and pushes code onto the EXEC stack */ 96 this.ExecStack.Push(program); 97 this.CodeStack.Push(program); 98 99 // run top expression 100 this.ExecStack.Pop().Eval(this); 101 102 this.Interpret(); 103 } 104 105 private void Interpret() 106 { 107 if (!IsRunning) 108 { 109 return; 110 } 111 112 while (IsRunning) 113 { 114 this.DoStep(); 115 } 116 117 if (this.Configuration.TopLevelPushCode) 118 { 119 this.CodeStack.Pop(); 120 } 121 } 122 123 private void DoStep() 124 { 125 var expression = this.ExecStack.Pop(); 126 127 if (!expression.IsCodeOp) 128 { 129 this.CodeStack.Push(expression); 130 } 131 132 expression.Eval(this); 133 134 if (!expression.IsCodeOp) 135 { 136 CodeStack.Pop(); 137 } 138 } 139 140 private Task InterpreteAsync() 141 { 142 this.currentTask = Task.Run(() => this.Interpret()); 143 144 return this.currentTask; 145 } 146 147 public async Task AbortAsync() 148 { 149 if (this.IsAborted || this.IsCompleted) 150 return; 151 152 this.IsAborted = true; 153 154 if (this.currentTask != null) 155 { 156 await this.currentTask; 157 } 158 } 159 160 public async Task AbortAndReset() 58 161 { 59 162 await this.AbortAsync(); … … 61 164 } 62 165 63 public void Interprete(string code)64 {65 var program = Encode(code);66 this.Interprete(program);67 }68 69 public Task InterpreteAsync(string code, CancellationToken token = default(CancellationToken))70 {71 var program = Encode(code);72 73 this.currentTask = this.InterpreteAsync(program, token);74 75 return currentTask;76 }77 78 public Task InterpreteAsync(Expression program, CancellationToken token = default(CancellationToken))79 {80 this.currentTask = Task.Run(() => this.Interprete(program), token);81 82 return currentTask;83 }84 85 public void Interprete(Expression program)86 {87 this.ExecStack.Push(program);88 this.CodeStack.Push(program);89 90 // run top expression91 this.ExecStack.Pop().Eval(this);92 93 this.Interprete();94 }95 96 private void Interprete()97 {98 while (IsRunning)99 {100 this.DoStep();101 }102 103 if (this.Configuration.TopLevelPushCode)104 {105 this.CodeStack.Pop();106 }107 }108 109 private void DoStep()110 {111 var expression = this.ExecStack.Pop();112 var isNotCodeOp = !expression.OpCode.IsCodeOp();113 114 if (isNotCodeOp)115 {116 this.CodeStack.Push(expression);117 }118 119 expression.Eval(this);120 121 if (isNotCodeOp)122 {123 CodeStack.Pop();124 }125 }126 127 private Task InterpreteAsync()128 {129 this.currentTask = Task.Run(() =>130 {131 this.Interprete();132 });133 134 return this.currentTask;135 }136 137 public async Task AbortAsync()138 {139 if (this.IsAborted || this.IsCompleted)140 return;141 142 this.IsAborted = true;143 144 if (this.currentTask != null)145 {146 await this.currentTask;147 }148 }149 150 166 public async Task PauseAsync() 151 167 { … … 163 179 public Task ResumeAsync() 164 180 { 165 if (this.IsPaused )181 if (this.IsPaused || !this.IsAborted) 166 182 { 167 183 this.IsPaused = false; … … 177 193 { 178 194 DoStep(); 195 196 if (IsCompleted && this.Configuration.TopLevelPopCode) 197 { 198 this.CodeStack.Pop(); 199 } 179 200 } 180 201 } … … 182 203 public static Expression Encode(string code) 183 204 { 184 return parser.Parse(code);205 return Parser.Parse(code); 185 206 } 186 207 … … 212 233 private void PrintStack<T>(string name, IStack<T> stack) 213 234 { 214 System.Console.WriteLine($"--- {name} ---\n{stack}\n---------------------------------------------------"); 235 Console.WriteLine(string.Format( 236 "--- {0} ---\n{1}\n---------------------------------------------------", 237 name, 238 stack)); 215 239 } 216 240 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/OpCode.cs
r14320 r14328 18 18 /// <summary> 19 19 /// BOOLEAN.DEFINE: 20 /// Defines the name on top of the NAME stack as an in struction that will push the top item of the BOOLEAN stack onto the EXEC stack.20 /// Defines the name on top of the NAME stack as an inclassion that will push the top item of the BOOLEAN stack onto the EXEC stack. 21 21 /// </summary> 22 22 BooleanDefine, … … 24 24 /// <summary> 25 25 /// BOOLEAN.DUP: 26 /// Duplicate s the top item on the BOOLEAN stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!)27 /// </summary> 28 BooleanDup ,26 /// Duplicatelicates the top item on the BOOLEAN stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!) 27 /// </summary> 28 BooleanDuplicate, 29 29 30 30 /// <summary> … … 109 109 /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item. The index is taken from the INTEGER stack. 110 110 /// </summary> 111 BooleanYankDup ,111 BooleanYankDuplicate, 112 112 #endregion Boolean 113 113 … … 123 123 /// <summary> 124 124 /// CODE.APPEND: 125 /// Pushes the result of appending the top two pieces of code. If one of the pieces of code is a single in struction or literal (that is, something not surrounded by parentheses) then it is surrounded by parentheses first.125 /// Pushes the result of appending the top two pieces of code. If one of the pieces of code is a single inclassion or literal (that is, something not surrounded by parentheses) then it is surrounded by parentheses first. 126 126 /// </summary> 127 127 CodeAppend, … … 129 129 /// <summary> 130 130 /// CODE.ATOM: 131 /// Pushes TRUE onto the BOOLEAN stack if the top piece of code is a single in struction or a literal, and FALSE otherwise (that is, if it is something surrounded by parentheses).131 /// Pushes TRUE onto the BOOLEAN stack if the top piece of code is a single inclassion or a literal, and FALSE otherwise (that is, if it is something surrounded by parentheses). 132 132 /// </summary> 133 133 CodeAtom, … … 165 165 /// <summary> 166 166 /// CODE.DEFINE: 167 /// Defines the name on top of the NAME stack as an in struction that will push the top item of the CODE stack onto the EXEC stack.167 /// Defines the name on top of the NAME stack as an inclassion that will push the top item of the CODE stack onto the EXEC stack. 168 168 /// </summary> 169 169 CodeDefine, … … 171 171 /// <summary> 172 172 /// CODE.DEFINITION: 173 /// Pushes the definition associated with the top NAME on the NAME stack (if any) onto the CODE stack. This extracts the definition for inspection/manipulation, rather than for immediate execution (although it may then be executed with a call to CODE.DO or a similar in struction).173 /// Pushes the definition associated with the top NAME on the NAME stack (if any) onto the CODE stack. This extracts the definition for inspection/manipulation, rather than for immediate execution (although it may then be executed with a call to CODE.DO or a similar inclassion). 174 174 /// </summary> 175 175 CodeDefinition, … … 178 178 /// CODE.DESCREPANCY: 179 179 /// Pushes a measure of the discrepancy between the top two CODE stack items onto the INTEGER stack. This will be zero if the top two items are equivalent, and will be higher the 'more different' the items are from one another. The calculation is as follows: 180 /// 1. Con structa list of all of the unique items in both of the lists (where uniqueness is determined by equalp). Sub-lists and atoms all count as items.180 /// 1. Conclass a list of all of the unique items in both of the lists (where uniqueness is determined by equalp). Sub-lists and atoms all count as items. 181 181 /// 2. Initialize the result to zero. 182 182 /// 3. For each unique item increment the result by the difference between the number of occurrences of the item in the two pieces of code. … … 199 199 /// <summary> 200 200 /// CODE.DO*COUNT: 201 /// An iteration in struction that performs a loop (the body of which is taken from the CODE stack) the number of times indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the INTEGER stack prior to each execution of the loop body. This should be implemented as a macro that expands into a call to CODE.DO*RANGE. CODE.DO*COUNT takes a single INTEGER argument (the number of times that the loop will be executed) and a single CODE argument (the body of the loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it expands into:201 /// An iteration inclassion that performs a loop (the body of which is taken from the CODE stack) the number of times indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the INTEGER stack prior to each execution of the loop body. This should be implemented as a macro that expands into a call to CODE.DO*RANGE. CODE.DO*COUNT takes a single INTEGER argument (the number of times that the loop will be executed) and a single CODE argument (the body of the loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it expands into: 202 202 /// ( 0 '1 - IntegerArg' CODE.QUOTE <CodeArg> CODE.DO*RANGE ) 203 203 /// </summary> … … 206 206 /// <summary> 207 207 /// CODE.DO*RANGE: 208 /// An iteration in struction that executes the top item on the CODE stack a number of times that depends on the top two integers, while also pushing the loop counter onto the INTEGER stack for possible access during the execution of the body of the loop. The top integer is the "destination index" and the second integer is the "current index." First the code and the integer arguments are saved locally and popped. Then the integers are compared. If the integers are equal then the current index is pushed onto the INTEGER stack and the code (which is the "body" of the loop) is pushed onto the EXEC stack for subsequent execution. If the integers are not equal then the current index will still be pushed onto the INTEGER stack but two items will be pushed onto the EXEC stack -- first a recursive call to CODE.DO*RANGE (with the same code and destination index, but with a current index that has been either incremented or decremented by 1 to be closer to the destination index) and then the body code. Note that the range is inclusive of both endpoints; a call with integer arguments 3 and 5 will cause its body to be executed 3 times, with the loop counter having the values 3, 4, and 5. Note also that one can specify a loop that "counts down" by providing a destination index that is less than the specified current index.208 /// An iteration inclassion that executes the top item on the CODE stack a number of times that depends on the top two integers, while also pushing the loop counter onto the INTEGER stack for possible access during the execution of the body of the loop. The top integer is the "destination index" and the second integer is the "current index." First the code and the integer arguments are saved locally and popped. Then the integers are compared. If the integers are equal then the current index is pushed onto the INTEGER stack and the code (which is the "body" of the loop) is pushed onto the EXEC stack for subsequent execution. If the integers are not equal then the current index will still be pushed onto the INTEGER stack but two items will be pushed onto the EXEC stack -- first a recursive call to CODE.DO*RANGE (with the same code and destination index, but with a current index that has been either incremented or decremented by 1 to be closer to the destination index) and then the body code. Note that the range is inclusive of both endpoints; a call with integer arguments 3 and 5 will cause its body to be executed 3 times, with the loop counter having the values 3, 4, and 5. Note also that one can specify a loop that "counts down" by providing a destination index that is less than the specified current index. 209 209 /// </summary> 210 210 CodeDoXRange, … … 218 218 /// <summary> 219 219 /// CODE.DUP: 220 /// Duplicate s the top item on the CODE stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!).221 /// </summary> 222 CodeDup ,220 /// Duplicatelicates the top item on the CODE stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!). 221 /// </summary> 222 CodeDuplicate, 223 223 224 224 /// <summary> 225 225 /// CODE.EXTRACT: 226 /// Pushes the sub-expression of the top item of the CODE stack that is indexed by the top item of the INTEGER stack. The indexing here counts "points," where each parenthesized expression and each literal/in struction is considered a point, and it proceeds in depth first order. The entire piece of code is at index 0; if it is a list then the first item in the list is at index 1, etc. The integer used as the index is taken modulo the number of points in the overall expression (and its absolute value is taken in case it is negative) to ensure that it is within the meaningful range.226 /// Pushes the sub-expression of the top item of the CODE stack that is indexed by the top item of the INTEGER stack. The indexing here counts "points," where each parenthesized expression and each literal/inclassion is considered a point, and it proceeds in depth first order. The entire piece of code is at index 0; if it is a list then the first item in the list is at index 1, etc. The integer used as the index is taken modulo the number of points in the overall expression (and its absolute value is taken in case it is negative) to ensure that it is within the meaningful range. 227 227 /// </summary> 228 228 CodeExtract, … … 265 265 266 266 /// <summary> 267 /// CODE.INSTRUCTIONS: 268 /// Pushes a list of all active instructions in the interpreter's current configuration. 269 /// </summary> 270 CodeInstructions, 271 272 /// <summary> 267 273 /// CODE.INSERT: 268 274 /// Pushes the result of inserting the second item of the CODE stack into the first item, at the position indexed by the top item of the INTEGER stack (and replacing whatever was there formerly). The indexing is computed as in CODE.EXTRACT. … … 272 278 /// <summary> 273 279 /// CODE.INSTRUCTIONS: 274 /// Pushes a list of all active in structions in the interpreter's current configuration.275 /// </summary> 276 CodeIn structions,280 /// Pushes a list of all active inclassions in the interpreter's current configuration. 281 /// </summary> 282 CodeInclassions, 277 283 278 284 /// <summary> … … 311 317 312 318 /// <summary> 319 /// CODE.NULL: Pushes TRUE onto the BOOLEAN stack if the top item of the CODE stack is an empty list, or FALSE otherwise. 320 /// </summary> 321 CodeNull, 322 323 /// <summary> 324 /// CODE.POSITION: 325 /// Pushes onto the INTEGER stack the position of the second item on the CODE stack within the first item (which is coerced to a list if necessary). Pushes -1 if no match is found. 326 /// </summary> 327 CodePosition, 328 329 /// <summary> 313 330 /// CODE.QUOTE: 314 331 /// Specifies that the next expression submitted for execution will instead be pushed literally onto the CODE stack. This can be implemented by moving the top item on the EXEC stack onto the CODE stack. … … 334 351 335 352 /// <summary> 336 /// CODE.SIZE: Pushes the number of "points" in the top piece of CODE onto the INTEGER stack. Each in struction, literal, and pair of parentheses counts as a point.353 /// CODE.SIZE: Pushes the number of "points" in the top piece of CODE onto the INTEGER stack. Each inclassion, literal, and pair of parentheses counts as a point. 337 354 /// </summary> 338 355 CodeSize, … … 364 381 /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item. The index is taken from the INTEGER stack. 365 382 /// </summary> 366 CodeYankDup ,383 CodeYankDuplicate, 367 384 #endregion Code 368 385 … … 380 397 /// <summary> 381 398 /// EXEC.DEFINE: 382 /// Defines the name on top of the NAME stack as an in struction that will push the top item of the EXEC stack back onto the EXEC stack.399 /// Defines the name on top of the NAME stack as an inclassion that will push the top item of the EXEC stack back onto the EXEC stack. 383 400 /// </summary> 384 401 ExecDefine, … … 386 403 /// <summary> 387 404 /// EXEC.DO*COUNT: 388 /// An iteration in struction that performs a loop (the body of which is taken from the EXEC stack) the number of times indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the INTEGER stack prior to each execution of the loop body. This is similar to CODE.DO*COUNT except that it takes its code argument from the EXEC stack. This should be implemented as a macro that expands into a call to EXEC.DO*RANGE. EXEC.DO*COUNT takes a single INTEGER argument (the number of times that the loop will be executed) and a single EXEC argument (the body of the loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it expands into:405 /// An iteration inclassion that performs a loop (the body of which is taken from the EXEC stack) the number of times indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the INTEGER stack prior to each execution of the loop body. This is similar to CODE.DO*COUNT except that it takes its code argument from the EXEC stack. This should be implemented as a macro that expands into a call to EXEC.DO*RANGE. EXEC.DO*COUNT takes a single INTEGER argument (the number of times that the loop will be executed) and a single EXEC argument (the body of the loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it expands into: 389 406 /// ( 0 <1 - IntegerArg> EXEC.DO*RANGE <ExecArg> ) 390 407 /// </summary> … … 393 410 /// <summary> 394 411 /// EXEC.DO*RANGE: 395 /// An iteration in struction that executes the top item on the EXEC stack a number of times that depends on the top two integers, while also pushing the loop counter onto the INTEGER stack for possible access during the execution of the body of the loop. This is similar to CODE.DO*COUNT except that it takes its code argument from the EXEC stack. The top integer is the "destination index" and the second integer is the "current index." First the code and the integer arguments are saved locally and popped. Then the integers are compared. If the integers are equal then the current index is pushed onto the INTEGER stack and the code (which is the "body" of the loop) is pushed onto the EXEC stack for subsequent execution. If the integers are not equal then the current index will still be pushed onto the INTEGER stack but two items will be pushed onto the EXEC stack -- first a recursive call to EXEC.DO*RANGE (with the same code and destination index, but with a current index that has been either incremented or decremented by 1 to be closer to the destination index) and then the body code. Note that the range is inclusive of both endpoints; a call with integer arguments 3 and 5 will cause its body to be executed 3 times, with the loop counter having the values 3, 4, and 5. Note also that one can specify a loop that "counts down" by providing a destination index that is less than the specified current index.412 /// An iteration inclassion that executes the top item on the EXEC stack a number of times that depends on the top two integers, while also pushing the loop counter onto the INTEGER stack for possible access during the execution of the body of the loop. This is similar to CODE.DO*COUNT except that it takes its code argument from the EXEC stack. The top integer is the "destination index" and the second integer is the "current index." First the code and the integer arguments are saved locally and popped. Then the integers are compared. If the integers are equal then the current index is pushed onto the INTEGER stack and the code (which is the "body" of the loop) is pushed onto the EXEC stack for subsequent execution. If the integers are not equal then the current index will still be pushed onto the INTEGER stack but two items will be pushed onto the EXEC stack -- first a recursive call to EXEC.DO*RANGE (with the same code and destination index, but with a current index that has been either incremented or decremented by 1 to be closer to the destination index) and then the body code. Note that the range is inclusive of both endpoints; a call with integer arguments 3 and 5 will cause its body to be executed 3 times, with the loop counter having the values 3, 4, and 5. Note also that one can specify a loop that "counts down" by providing a destination index that is less than the specified current index. 396 413 /// </summary> 397 414 ExecDoXRange, … … 405 422 /// <summary> 406 423 /// EXEC.DUP: 407 /// Duplicate s the top item on the EXEC stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!). This may be thought of as a "DO TWICE" instruction.408 /// </summary> 409 ExecDup ,424 /// Duplicatelicates the top item on the EXEC stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!). This may be thought of as a "DO TWICE" inclassion. 425 /// </summary> 426 ExecDuplicate, 410 427 411 428 /// <summary> 412 429 /// EXEC.FLUSH: 413 /// Empties the EXEC stack. This may be thought of as a "HALT" in struction.430 /// Empties the EXEC stack. This may be thought of as a "HALT" inclassion. 414 431 /// </summary> 415 432 ExecFlush, … … 428 445 429 446 /// <summary> 430 /// EXEC.POP: Pops the EXEC stack. This may be thought of as a "DONT" in struction.447 /// EXEC.POP: Pops the EXEC stack. This may be thought of as a "DONT" inclassion. 431 448 /// </summary> 432 449 ExecPop, … … 439 456 440 457 /// <summary> 458 /// EXEC.S: 459 /// The Push implementation of the "S combinator". Pops 3 items from the EXEC stack, which we will call A, B, and C (with A being the first one popped). Then pushes a list containing B and C back onto the EXEC stack, followed by another instance of C, followed by another instance of A. 460 /// </summary> 461 ExecS, 462 463 /// <summary> 441 464 /// EXEC.SHOVE: 442 /// Inserts the top EXEC item "deep" in the stack, at the position indexed by the top INTEGER. This may be thought of as a "DO LATER" in struction.465 /// Inserts the top EXEC item "deep" in the stack, at the position indexed by the top INTEGER. This may be thought of as a "DO LATER" inclassion. 443 466 /// </summary> 444 467 ExecShove, … … 464 487 /// <summary> 465 488 /// EXEC.YANK: 466 /// Removes an indexed item from "deep" in the stack and pushes it on top of the stack. The index is taken from the INTEGER stack. This may be thought of as a "DO SOONER" in struction.489 /// Removes an indexed item from "deep" in the stack and pushes it on top of the stack. The index is taken from the INTEGER stack. This may be thought of as a "DO SOONER" inclassion. 467 490 /// </summary> 468 491 ExecYank, … … 472 495 /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item. The index is taken from the INTEGER stack. 473 496 /// </summary> 474 ExecYankDup ,497 ExecYankDuplicate, 475 498 #endregion Exec 476 499 … … 533 556 /// <summary> 534 557 /// FLOAT.DEFINE: 535 /// Defines the name on top of the NAME stack as an in struction that will push the top item of the FLOAT stack onto the EXEC stack.558 /// Defines the name on top of the NAME stack as an inclassion that will push the top item of the FLOAT stack onto the EXEC stack. 536 559 /// </summary> 537 560 FloatDefine, … … 539 562 /// <summary> 540 563 /// FLOAT.DUP: 541 /// Duplicate s the top item on the FLOAT stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!).542 /// </summary> 543 FloatDup ,564 /// Duplicatelicates the top item on the FLOAT stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!). 565 /// </summary> 566 FloatDuplicate, 544 567 545 568 /// <summary> … … 636 659 /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item. The index is taken from the INTEGER stack. 637 660 /// </summary> 638 FloatYankDup ,661 FloatYankDuplicate, 639 662 640 663 #endregion Float … … 692 715 693 716 /// <summary> 694 /// INTEGER.COS:695 /// Pushes the cosine of the top item.696 /// </summary>697 IntegerCos,698 699 /// <summary>700 717 /// INTEGER.DEFINE: 701 /// Defines the name on top of the NAME stack as an in struction that will push the top item of the INTEGER stack onto the EXEC stack.718 /// Defines the name on top of the NAME stack as an inclassion that will push the top item of the INTEGER stack onto the EXEC stack. 702 719 /// </summary> 703 720 IntegerDefine, … … 705 722 /// <summary> 706 723 /// INTEGER.DUP: 707 /// Duplicate s the top item on the INTEGER stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!).708 /// </summary> 709 IntegerDup ,724 /// Duplicatelicates the top item on the INTEGER stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!). 725 /// </summary> 726 IntegerDuplicate, 710 727 711 728 /// <summary> … … 769 786 770 787 /// <summary> 771 /// INTEGER.SIN:772 /// Pushes the sine of the top item.773 /// </summary>774 IntegerSin,775 776 /// <summary>777 788 /// INTEGER.STACKDEPTH: 778 789 /// Pushes the stack depth onto the INTEGER stack. … … 787 798 788 799 /// <summary> 789 /// INTEGER.TAN:790 /// Pushes the tangent of the top item.791 /// </summary>792 IntegerTan,793 794 /// <summary>795 800 /// INTEGER.YANK: 796 801 /// Removes an indexed item from "deep" in the stack and pushes it on top of the stack. The index is taken from the INTEGER stack. … … 802 807 /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item. The index is taken from the INTEGER stack. 803 808 /// </summary> 804 IntegerYankDup ,809 IntegerYankDuplicate, 805 810 806 811 #endregion Integer … … 823 828 /// <summary> 824 829 /// NAME.DUP: 825 /// Duplicate s the top item on the NAME stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!).826 /// </summary> 827 NameDup ,830 /// Duplicatelicates the top item on the NAME stack. Does not pop its argument (which, if it did, would negate the effect of the duplication!). 831 /// </summary> 832 NameDuplicate, 828 833 829 834 /// <summary> … … 890 895 /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item. The index is taken from the INTEGER stack. 891 896 /// </summary> 892 NameYankDup ,897 NameYankDuplicate, 893 898 #endregion Name 894 899 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/OpCodeExtensions.cs
r14323 r14328 6 6 public static class OpCodeExtensions 7 7 { 8 public readonly staticbyte Min = 0;8 public const byte Min = 0; 9 9 public readonly static byte Max = Enum.GetValues(typeof(OpCode)).Cast<byte>().Last(); 10 11 public static bool IsCodeOp(this OpCode opCode)12 {13 return opCode >= OpCode.CodeEquals && opCode < OpCode.CodeYankDup;14 }15 10 } 16 11 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Parser/Parser.cs
r14323 r14328 2 2 using System.Globalization; 3 3 using HeuristicLab.Algorithms.PushGP.Expressions; 4 using HeuristicLab.Algorithms.PushGP.Expressions.Boolean; 5 using HeuristicLab.Algorithms.PushGP.Expressions.Code; 6 using HeuristicLab.Algorithms.PushGP.Expressions.Exec; 7 using HeuristicLab.Algorithms.PushGP.Expressions.Float; 8 using HeuristicLab.Algorithms.PushGP.Expressions.Integer; 9 using HeuristicLab.Algorithms.PushGP.Expressions.Name; 4 10 5 11 namespace HeuristicLab.Algorithms.PushGP 6 12 { 7 public class Parser13 public static class Parser 8 14 { 9 private readonly CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("en-US");15 private static readonly CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("en-US"); 10 16 11 private const string OpenBrace = "("; 12 private const string CloseBrace = ")"; 13 private readonly char[] symbolTrim = new[] { '\r', '\n' }; 17 private const string openBrace = "("; 18 private const string closeBrace = ")"; 19 private const char delimiter = ' '; 20 private static readonly char[] symbolTrim = new[] { '\r', '\n' }; 14 21 15 public Expression Parse(string source, int startIndex = 0)22 public static Expression Parse(string source, int startIndex = 0) 16 23 { 17 var symbols = source.Split( ' ');24 var symbols = source.Split(delimiter); 18 25 19 26 int endIndex; 20 return this.Parse(symbols, 0, out endIndex);27 return Parse(symbols, 0, out endIndex); 21 28 } 22 29 23 private Expression Parse(string[] symbols, int startIndex, out int endIndex)30 private static Expression Parse(string[] symbols, int startIndex, out int endIndex) 24 31 { 25 32 var expressions = new List<Expression>(); … … 28 35 { 29 36 var symbol = symbols[i].TrimEnd(symbolTrim); 37 30 38 if (string.IsNullOrWhiteSpace(symbol)) 31 39 { … … 33 41 } 34 42 35 if (symbol == OpenBrace)43 if (symbol == openBrace) 36 44 { 37 45 var subExpression = Parse(symbols, i + 1, out endIndex); … … 40 48 continue; 41 49 } 42 else if (symbol == CloseBrace)50 else if (symbol == closeBrace) 43 51 { 44 52 endIndex = i; … … 46 54 ? new ExecExpandExpression(expressions.ToArray()) 47 55 : new CodeNoopExpression() as Expression; 48 }49 50 if (string.IsNullOrWhiteSpace(symbol))51 {52 continue;53 56 } 54 57 … … 61 64 } 62 65 63 // instruction66 // expression 64 67 OpCode opCode; 65 68 if (SymbolTable.TryGetOpCode(symbol, out opCode)) 66 69 { 67 expression = Expression CreatorTable.GetCreator(opCode).Invoke();70 expression = ExpressionFactory.Create(opCode); 68 71 expressions.Insert(0, expression); 69 72 continue; 70 73 } 71 74 72 // identifier - custom instruction or named literals75 // identifier - custom expression or named literals 73 76 expressions.Insert(0, new NameDefineXExecExpression(symbol)); 74 77 } … … 80 83 } 81 84 82 private bool TryParseLiteral(string word, out Expression expression)85 private static bool TryParseLiteral(string word, out Expression expression) 83 86 { 84 87 long longValue; -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Parser/SymbolTable.cs
r14323 r14328 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Linq;4 3 5 4 namespace HeuristicLab.Algorithms.PushGP … … 10 9 { 11 10 #region Code 12 { "CODE.NOOP", OpCode.CodeNoop }, 13 { "CODE.PUSH", OpCode.CodePush }, 14 { "CODE.IF", OpCode.CodeIf }, 15 { "CODE.DEFINE", OpCode.CodeDefine }, 16 { "CODE.DUP", OpCode.CodeDup }, 17 { "CODE.QUOTE", OpCode.CodeQuote }, 18 { "CODE.DO", OpCode.CodeDo }, 11 { Symbols.CodeIf, OpCode.CodeIf }, 12 { Symbols.CodeDefine, OpCode.CodeDefine }, 13 { Symbols.CodeDuplicate, OpCode.CodeDuplicate }, 14 { Symbols.CodeQuote, OpCode.CodeQuote }, 15 { Symbols.CodeDo, OpCode.CodeDo }, 16 { Symbols.CodeEquals, OpCode.CodeEquals }, 17 { Symbols.CodeAppend, OpCode.CodeAppend }, 18 { Symbols.CodeAtom, OpCode.CodeAtom }, 19 { Symbols.CodeCar, OpCode.CodeCar }, 20 { Symbols.CodeCdr, OpCode.CodeCdr }, 21 { Symbols.CodeCons, OpCode.CodeCons }, 22 { Symbols.CodeContainer, OpCode.CodeContainer }, 23 { Symbols.CodeContains, OpCode.CodeContains }, 24 { Symbols.CodeDefinition, OpCode.CodeDefinition }, 25 { Symbols.CodeDiscrepancy, OpCode.CodeDiscrepancy }, 26 { Symbols.CodeDoX, OpCode.CodeDoX }, 27 { Symbols.CodeDoXCount, OpCode.CodeDoXCount }, 28 { Symbols.CodeDoXRange, OpCode.CodeDoXRange }, 29 { Symbols.CodeDoXTimes, OpCode.CodeDoXTimes }, 30 { Symbols.CodeExtract, OpCode.CodeExtract }, 31 { Symbols.CodeFlush, OpCode.CodeFlush }, 32 { Symbols.CodeFromFloat, OpCode.CodeFromFloat }, 33 { Symbols.CodeFromInteger, OpCode.CodeFromInteger }, 34 { Symbols.CodeFromName, OpCode.CodeFromName }, 35 { Symbols.CodeInsert, OpCode.CodeInsert }, 36 { Symbols.CodeInstructions, OpCode.CodeInstructions }, 37 { Symbols.CodeLength, OpCode.CodeLength }, 38 { Symbols.CodeList, OpCode.CodeList }, 39 { Symbols.CodeMember, OpCode.CodeMember }, 40 { Symbols.CodeNoop, OpCode.CodeNoop }, 41 { Symbols.CodeNth, OpCode.CodeNth }, 42 { Symbols.CodeNthCdr, OpCode.CodeNthCdr }, 43 { Symbols.CodeNull, OpCode.CodeNull }, 44 { Symbols.CodePosition, OpCode.CodePosition }, 45 { Symbols.CodeRand, OpCode.CodeRand }, 46 { Symbols.CodeRot, OpCode.CodeRot }, 47 { Symbols.CodeShove, OpCode.CodeShove }, 48 { Symbols.CodeSize, OpCode.CodeSize }, 49 { Symbols.CodeStackDepth, OpCode.CodeStackDepth }, 50 { Symbols.CodeSubst, OpCode.CodeSubst }, 51 { Symbols.CodeSwap, OpCode.CodeSwap }, 52 { Symbols.CodeYank, OpCode.CodeYank }, 53 { Symbols.CodeYankDuplicate, OpCode.CodeYankDuplicate }, 19 54 #endregion Code 20 55 21 56 #region Exec 22 { "EXEC.EXPAND", OpCode.ExecExpand }, 23 { "EXEC.PUSH", OpCode.ExecPush }, 24 { "EXEC.IF", OpCode.ExecIf }, 25 { "EXEC.DEFINE", OpCode.ExecDefine }, 26 { "EXEC.DUP", OpCode.ExecDup }, 27 { "EXEC.POP", OpCode.ExecPop }, 28 { "EXEC.Y", OpCode.ExecY }, 29 { "EXEC.DO*RANGE", OpCode.ExecDoXRange }, 57 { Symbols.ExecIf, OpCode.ExecIf }, 58 { Symbols.ExecDefine, OpCode.ExecDefine }, 59 { Symbols.ExecDuplicate, OpCode.ExecDuplicate }, 60 { Symbols.ExecPop, OpCode.ExecPop }, 61 { Symbols.ExecY, OpCode.ExecY }, 62 { Symbols.ExecDoXRange, OpCode.ExecDoXRange }, 63 { Symbols.ExecEquals, OpCode.ExecEquals }, 64 { Symbols.ExecDoXCount, OpCode.ExecDoXCount }, 65 { Symbols.ExecDoXTimes, OpCode.ExecDoXTimes }, 66 { Symbols.ExecFlush, OpCode.ExecFlush }, 67 { Symbols.ExecK, OpCode.ExecK }, 68 { Symbols.ExecRot, OpCode.ExecRot }, 69 { Symbols.ExecS, OpCode.ExecS }, 70 { Symbols.ExecShove, OpCode.ExecShove }, 71 { Symbols.ExecStackDepth, OpCode.ExecStackDepth }, 72 { Symbols.ExecSwap, OpCode.ExecSwap }, 73 { Symbols.ExecYank, OpCode.ExecYank }, 74 { Symbols.ExecYankDuplicate, OpCode.ExecYankDuplicate }, 30 75 #endregion Exec 31 76 32 77 #region Name 33 { "NAME.DEFINE*EXEC", OpCode.NameDefineXExec }, 34 { "NAME.POP", OpCode.NamePop }, 35 { "NAME.DUP", OpCode.NameDup }, 78 { Symbols.NamePop, OpCode.NamePop }, 79 { Symbols.NameDuplicate, OpCode.NameDuplicate }, 80 { Symbols.NameEquals, OpCode.NameEquals }, 81 { Symbols.NameFlush, OpCode.NameFlush }, 82 { Symbols.NameQuote, OpCode.NameQuote }, 83 { Symbols.NameRand, OpCode.NameRand }, 84 { Symbols.NameRandBoundName, OpCode.NameRand }, 85 { Symbols.NameRot, OpCode.NameRot }, 86 { Symbols.NameShove, OpCode.NameShove }, 87 { Symbols.NameStackDepth, OpCode.NameStackDepth }, 88 { Symbols.NameSwap, OpCode.NameSwap }, 89 { Symbols.NameYank, OpCode.NameYank }, 90 { Symbols.NameYankDuplicate, OpCode.NameYankDuplicate }, 36 91 #endregion Name 37 92 38 93 #region Boolean 39 { "BOOLEAN.PUSH", OpCode.BooleanPush }, 40 { "BOOLEAN.POP", OpCode.BooleanPop }, 41 { "BOOLEAN.DUP", OpCode.BooleanDup }, 42 { "BOOLEAN.AND", OpCode.BooleanAnd }, 43 { "BOOLEAN.OR", OpCode.BooleanOr }, 94 { Symbols.BooleanPop, OpCode.BooleanPop }, 95 { Symbols.BooleanDuplicate, OpCode.BooleanDuplicate }, 96 { Symbols.BooleanAnd, OpCode.BooleanAnd }, 97 { Symbols.BooleanOr, OpCode.BooleanOr }, 98 { Symbols.BooleanEquals, OpCode.BooleanEquals }, 99 { Symbols.BooleanFlush, OpCode.BooleanFlush }, 100 { Symbols.BooleanFromFloat, OpCode.BooleanFromFloat }, 101 { Symbols.BooleanFromInteger, OpCode.BooleanFromInteger }, 102 { Symbols.BooleanNot, OpCode.BooleanNot }, 103 { Symbols.BooleanRand, OpCode.BooleanRand }, 104 { Symbols.BooleanRot, OpCode.BooleanRot }, 105 { Symbols.BooleanShove, OpCode.BooleanShove }, 106 { Symbols.BooleanStackDepth, OpCode.BooleanStackDepth }, 107 { Symbols.BooleanSwap, OpCode.BooleanSwap }, 108 { Symbols.BooleanYank, OpCode.BooleanYank }, 109 { Symbols.BooleanYankDuplicate, OpCode.BooleanYankDuplicate }, 44 110 #endregion Boolean 45 111 46 112 #region Integer 47 { "INTEGER.PUSH", OpCode.IntegerPush }, 48 { "INTEGER.POP", OpCode.IntegerPop }, 49 { "INTEGER.DUP", OpCode.IntegerDup }, 50 { "INTEGER.DEFINE", OpCode.IntegerDefine }, 51 { "INTEGER.+", OpCode.IntegerAdd }, 52 { "INTEGER.-", OpCode.IntegerSubtract }, 53 { "INTEGER.*", OpCode.IntegerMultiply }, 54 { "INTEGER./", OpCode.IntegerDivide }, 55 { "INTEGER.<", OpCode.IntegerSmallerThan }, 56 { "INTEGER.>", OpCode.IntegerGreaterThan }, 57 { "INTEGER.=", OpCode.IntegerEquals }, 58 { "INTEGER.MIN", OpCode.IntegerMin }, 59 { "INTEGER.MAX", OpCode.IntegerMax }, 113 { Symbols.IntegerPop, OpCode.IntegerPop }, 114 { Symbols.IntegerDuplicate, OpCode.IntegerDuplicate }, 115 { Symbols.IntegerDefine, OpCode.IntegerDefine }, 116 { Symbols.IntegerAdd, OpCode.IntegerAdd }, 117 { Symbols.IntegerSubtract, OpCode.IntegerSubtract }, 118 { Symbols.IntegerMultiply, OpCode.IntegerMultiply }, 119 { Symbols.IntegerDivide, OpCode.IntegerDivide }, 120 { Symbols.IntegerSmallerThan, OpCode.IntegerSmallerThan }, 121 { Symbols.IntegerGreaterThan, OpCode.IntegerGreaterThan }, 122 { Symbols.IntegerEquals, OpCode.IntegerEquals }, 123 { Symbols.IntegerMin, OpCode.IntegerMin }, 124 { Symbols.IntegerMax, OpCode.IntegerMax }, 125 { Symbols.IntegerModulo, OpCode.IntegerModulo }, 126 { Symbols.IntegerFlush, OpCode.IntegerFlush }, 127 { Symbols.IntegerFromBoolean, OpCode.IntegerFromBoolean }, 128 { Symbols.IntegerFromFloat, OpCode.IntegerFromFloat }, 129 { Symbols.IntegerRand, OpCode.IntegerRand }, 130 { Symbols.IntegerRot, OpCode.IntegerRot }, 131 { Symbols.IntegerShove, OpCode.IntegerShove }, 132 { Symbols.IntegerStackDepth, OpCode.IntegerStackDepth }, 133 { Symbols.IntegerSwap, OpCode.IntegerSwap }, 134 { Symbols.IntegerYank, OpCode.IntegerYank }, 135 { Symbols.IntegerYankDuplicate, OpCode.IntegerYankDuplicate }, 60 136 #endregion Integer 61 137 62 138 #region Float 63 { "FLOAT.PUSH", OpCode.FloatPush }, 64 { "FLOAT.POP", OpCode.FloatPop }, 65 { "FLOAT.DUP", OpCode.FloatDup }, 66 { "FLOAT.DEFINE", OpCode.FloatDefine }, 67 { "FLOAT.+", OpCode.FloatAdd }, 68 { "FLOAT.-", OpCode.FloatSubtract }, 69 { "FLOAT.*", OpCode.FloatMultiply }, 70 { "FLOAT./", OpCode.FloatDivide }, 71 { "FLOAT.<", OpCode.FloatSmallerThan }, 72 { "FLOAT.>", OpCode.FloatGreaterThan }, 73 { "FLOAT.=", OpCode.FloatEquals }, 74 { "FLOAT.MIN", OpCode.FloatMin }, 75 { "FLOAT.MAX", OpCode.FloatMax }, 139 { Symbols.FloatPop, OpCode.FloatPop }, 140 { Symbols.FloatDuplicate, OpCode.FloatDuplicate }, 141 { Symbols.FloatDefine, OpCode.FloatDefine }, 142 { Symbols.FloatAdd, OpCode.FloatAdd }, 143 { Symbols.FloatSubtract, OpCode.FloatSubtract }, 144 { Symbols.FloatMultiply, OpCode.FloatMultiply }, 145 { Symbols.FloatDivide, OpCode.FloatDivide }, 146 { Symbols.FloatSmallerThan, OpCode.FloatSmallerThan }, 147 { Symbols.FloatGreaterThan, OpCode.FloatGreaterThan }, 148 { Symbols.FloatEquals, OpCode.FloatEquals }, 149 { Symbols.FloatMin, OpCode.FloatMin }, 150 { Symbols.FloatMax, OpCode.FloatMax }, 151 { Symbols.FloatCos, OpCode.FloatCos }, 152 { Symbols.FloatSin, OpCode.FloatSin }, 153 { Symbols.FloatTan, OpCode.FloatTan }, 154 { Symbols.FloatFromBoolean, OpCode.FloatFromBoolean }, 155 { Symbols.FloatFromInteger, OpCode.FloatFromInteger }, 156 { Symbols.FloatRand, OpCode.FloatRand }, 157 { Symbols.FloatRot, OpCode.FloatRot }, 158 { Symbols.FloatShove, OpCode.FloatShove }, 159 { Symbols.FloatStackDepth, OpCode.FloatStackDepth }, 160 { Symbols.FloatSwap, OpCode.FloatSwap }, 161 { Symbols.FloatYank, OpCode.FloatYank }, 162 { Symbols.FloatYankDuplicate, OpCode.FloatYankDuplicate }, 76 163 #endregion Float 77 164 }; … … 81 168 OpCode opCode; 82 169 if (symbolTable.TryGetValue(operationName, out opCode)) return opCode; 83 else throw new NotSupportedException(" Symbol: " + operationName);170 else throw new NotSupportedException("Operation not supported: " + operationName); 84 171 } 85 172 … … 88 175 return symbolTable.TryGetValue(operationName, out opCode); 89 176 } 90 91 public static string GetSymbol(OpCode opCode)92 {93 var symbol = symbolTable.SingleOrDefault(pair => pair.Value == opCode);94 95 return !symbol.Equals(default(KeyValuePair<string, OpCode>))96 ? symbol.Key97 : null;98 }99 177 } 100 178 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Stack/PushGPStack.cs
r14323 r14328 2 2 using System.Collections; 3 3 using System.Collections.Generic; 4 using System.Text; 4 5 5 6 namespace HeuristicLab.Algorithms.PushGP.Stack 6 7 { 7 8 /// <summary> 8 /// While Push's stacks are generally treated as genuine stacks---that is, in structions take their arguments from the tops of9 /// the stacks and push their results onto the tops of the stacks---a few in structions (like YANK and SHOVE) do allow direct access9 /// While Push's stacks are generally treated as genuine stacks---that is, inclassions take their arguments from the tops of 10 /// the stacks and push their results onto the tops of the stacks---a few inclassions (like YANK and SHOVE) do allow direct access 10 11 /// to "deep" stack elements by means of integer indices. To this extent the stacks can be used as general, random access memory 11 /// structures. This is one of the features that ensures the Turing-completeness of Push (another being the arbitrary name/value12 /// classures. This is one of the features that ensures the Turing-completeness of Push (another being the arbitrary name/value 12 13 /// bindings supported by the NAME data type and DEFINE methods; see below). 13 14 /// </summary> … … 15 16 public class PushGPStack<T> : IStack<T> 16 17 { 17 private List<T> data = new List<T>(); 18 private readonly List<T> data; 19 private const string delimiter = " "; 18 20 19 public T Top => this.data[Count - 1]; 20 public int Count => data.Count; 21 public bool IsEmpty => this.Count == 0; 21 public PushGPStack(int capacity = 0) 22 { 23 this.data = new List<T>(); 24 } 25 26 public T Top { get { return this.data[Count - 1]; } } 27 public int Count { get { return this.data.Count; } } 28 public int Capacity { get { return this.data.Capacity; } } 29 public bool IsEmpty { get { return this.Count == 0; } } 22 30 23 31 public bool IsReadOnly … … 61 69 public T Pop() 62 70 { 63 var index = Count - 1; 64 var value = this.data[index]; 65 this.data.RemoveAt(index); 71 var value = this.data[Count - 1]; 72 this.data.RemoveAt(Count - 1); 66 73 67 74 return value; … … 75 82 this.data.RemoveRange(startIndex, count); 76 83 77 items.Reverse(); 84 // is faster than remove range?? 85 for (var i = this.Count - 1; i > startIndex - 1; i--) 86 { 87 this.data.RemoveAt(i); 88 } 89 78 90 return items.ToArray(); 79 91 } … … 86 98 public void Push(params T[] items) 87 99 { 88 this.data.AddRange(items); 100 foreach (var item in items) 101 { 102 this.data.Add(item); 103 } 89 104 } 90 105 … … 112 127 { 113 128 var index = Count - 1; 114 if (this. data.Count > 0 && this.data[index].Equals(item))129 if (this.Count > 0 && this.data[index].Equals(item)) 115 130 { 116 131 this.data.RemoveAt(index); … … 125 140 public bool TryPop(out T item) 126 141 { 127 if (this. data.Count > 0)142 if (this.Count > 0) 128 143 { 129 144 item = this.Pop(); … … 144 159 public override string ToString() 145 160 { 146 return string.Join(", ", this.data); 161 if (this.Count == 0) 162 { 163 return string.Empty; 164 } 165 166 var sb = new StringBuilder(); 167 168 for (var i = this.Count - 1; i > 0; i--) 169 { 170 sb.Append(this.data[i] + delimiter); 171 } 172 173 sb.Append(this.data[0]); 174 175 return sb.ToString(); 147 176 } 148 177 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/HeuristicLab.Tests.csproj
r14323 r14328 9 9 <RootNamespace>HeuristicLab.Tests</RootNamespace> 10 10 <AssemblyName>HeuristicLab.Tests</AssemblyName> 11 <TargetFrameworkVersion>v4.5 .2</TargetFrameworkVersion>11 <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> 12 12 <FileAlignment>512</FileAlignment> 13 13 <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> … … 17 17 <IsCodedUITest>False</IsCodedUITest> 18 18 <TestProjectType>UnitTest</TestProjectType> 19 <TargetFrameworkProfile /> 19 20 </PropertyGroup> 20 21 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> … … 34 35 <ErrorReport>prompt</ErrorReport> 35 36 <WarningLevel>4</WarningLevel> 37 <LangVersion>5</LangVersion> 36 38 </PropertyGroup> 37 39 <ItemGroup> … … 51 53 </Choose> 52 54 <ItemGroup> 53 <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\MixedExpressionTests.cs" /> 54 <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\NameExpressionTests.cs" /> 55 <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\FloatExpressionTests.cs" /> 56 <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\BooleanExpressionTests.cs" /> 57 <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\ExecExpressionTests.cs" /> 58 <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\CodeExpressionTests.cs" /> 59 <Compile Include="HeuristicLab.Algorithms.PushGP\Expressions\IntegerExpressionTests.cs" /> 60 <Compile Include="HeuristicLab.Algorithms.PushGP\InterpreterTest.cs" /> 55 <Compile Include="Expressions\ExampleTests.cs" /> 56 <Compile Include="Expressions\NameExpressionTests.cs" /> 57 <Compile Include="Expressions\FloatExpressionTests.cs" /> 58 <Compile Include="Expressions\BooleanExpressionTests.cs" /> 59 <Compile Include="Expressions\ExecExpressionTests.cs" /> 60 <Compile Include="Expressions\CodeExpressionTests.cs" /> 61 <Compile Include="Expressions\IntegerExpressionTests.cs" /> 62 <Compile Include="InterpreterTest.cs" /> 63 <Compile Include="Problems\Checksum.cs" /> 61 64 <Compile Include="Properties\AssemblyInfo.cs" /> 62 65 </ItemGroup> … … 68 71 </ItemGroup> 69 72 <ItemGroup> 70 <Folder Include=" HeuristicLab.Algorithms.PushGP\Problems\" />73 <Folder Include="Problems\Data\" /> 71 74 </ItemGroup> 72 75 <Choose>
Note: See TracChangeset
for help on using the changeset viewer.