Changeset 14323 for branches/PushGP
- Timestamp:
- 10/05/16 13:27:41 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.Algorithms.PushGP
- Files:
-
- 25 added
- 5 deleted
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.Cli/Program.cs
r14320 r14323 1 1 using System; 2 using System.Diagnostics; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using HeuristicLab.Algorithms.PushGP.Generators; 2 6 using HeuristicLab.Algorithms.PushGP.Interpreter; 3 7 … … 8 12 static void Main(string[] args) 9 13 { 10 var interpreter = new PushGPInterpreter(); 11 12 interpreter.IntegerStack.Push(3, 3); 13 interpreter.FloatStack.Push(2.0, 3.0); 14 15 interpreter.Interprete(example17); 16 interpreter.PrintStacks(); 14 //Stepwise().Wait(); 15 PerformanceTest(); 16 //PerformanceTestCodeGenerator(); 17 17 18 18 Console.WriteLine("\nPress any key to continue..."); … … 20 20 } 21 21 22 static string example1 = "( 2 3 INTEGER.* 4.1 5.2 FLOAT.+ TRUE FALSE BOOLEAN.OR )"; 23 static string example2 = "( 5 1.23 INTEGER.+ ( 4 ) INTEGER.- 5.67 FLOAT.* )"; 24 static string example3 = "( 5 INTEGER.DUP INTEGER.+ )"; 25 static string example4 = "5"; 26 static string example5 = "INTEGER.DUP"; 27 static string example6 = "( 5 CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DO )"; 28 static string example7 = "( DOUBLE CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) CODE.DEFINE 5 DOUBLE )"; 29 static string example8 = "( CODE.QUOTE ( INTEGER.DUP INTEGER.+ ) DOUBLE CODE.DEFINE 5 DOUBLE )"; 30 static string example9 = "( DOUBLE EXEC.DEFINE ( INTEGER.DUP INTEGER.+ ) 5 DOUBLE )"; 22 static async Task Stepwise() 23 { 24 var program = PushGPInterpreter.Encode("( 2 3 INTEGER.* 4.1 5.2 FLOAT.+ TRUE FALSE BOOLEAN.OR )"); 25 var interpreter = new PushGPInterpreter(); 31 26 32 // Requires an integer preloaded 33 static string example10 = @"( CODE.QUOTE ( INTEGER.POP 1 ) 34 CODE.QUOTE ( CODE.DUP INTEGER.DUP 1 INTEGER.- CODE.DO INTEGER.* ) 35 INTEGER.DUP 2 INTEGER.< CODE.IF )"; 36 static string example11 = "( 1 INTEGER.MAX 1 EXEC.DO*RANGE INTEGER.* )"; 37 static string example12 = "( 1 INTEGER.MAX 1 EXEC.DO*RANGE ( 2 INTEGER.* ) )"; 38 static string example13 = "( INTEGER.= CODE.QUOTE FLOAT.* CODE.QUOTE FLOAT./ CODE.IF )"; 39 static string example14 = "( INTEGER.= EXEC.IF FLOAT.* FLOAT./ )"; 40 static string example15 = "( EXEC.Y ( ( FALSE 2 INTEGER.* ) EXEC.IF ( ) EXEC.POP ) )"; 41 static string example16 = "( EXEC.Y ( ( 2 INTEGER.* INTEGER.DUP 1000 INTEGER.< ) EXEC.IF ( ) EXEC.POP ) )"; 42 static string example17 = @"( ARG FLOAT.DEFINE 43 EXEC.Y ( 44 ARG FLOAT.* 45 1 INTEGER.- 46 INTEGER.DUP 1 INTEGER.> 47 EXEC.IF ( ) EXEC.POP 48 ) 49 )"; 27 var task = interpreter.InterpreteAsync(program); 28 await interpreter.PauseAsync(); 29 30 while (!interpreter.IsCompleted) 31 { 32 Console.Clear(); 33 interpreter.PrintStacks(); 34 interpreter.Step(); 35 36 var input = Console.ReadKey(); 37 if (input.Key == ConsoleKey.Escape) 38 { 39 break; 40 } 41 else if (input.Key == ConsoleKey.Spacebar) 42 { 43 await interpreter.ResumeAsync(); 44 } 45 } 46 47 Console.Clear(); 48 interpreter.PrintStacks(); 49 } 50 51 static void PerformanceTest() 52 { 53 var program = PushGPInterpreter.Encode("( 5 INTEGER.DUP INTEGER.+ )"); 54 var interpreter = new PushGPInterpreter(); 55 var sw = new Stopwatch(); 56 57 sw.Start(); 58 for (var i = 0; i < 20000000; i++) 59 { 60 interpreter.Interprete(program); 61 interpreter.Reset(); 62 } 63 sw.Stop(); 64 65 Console.WriteLine(sw.Elapsed); 66 } 67 68 static void PerformanceTestCodeGenerator() 69 { 70 var sw = new Stopwatch(); 71 72 sw.Start(); 73 var expressions = CodeGenerator.RandomCode(10000000); 74 sw.Stop(); 75 76 Console.WriteLine($"Count: {expressions.Count()}"); 77 Console.WriteLine(sw.Elapsed); 78 } 50 79 } 51 80 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.sln
r14320 r14323 7 7 EndProject 8 8 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Algorithms.PushGP.Cli", "HeuristicLab.Algorithms.PushGP.Cli\HeuristicLab.Algorithms.PushGP.Cli.csproj", "{293FD718-99E9-4F6A-A0EC-343C92D0B03E}" 9 EndProject 10 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Tests", "HeuristicLab.Tests\HeuristicLab.Tests.csproj", "{117B7D68-ED7D-4350-996B-9CA15259E107}" 9 11 EndProject 10 12 Global … … 22 24 {293FD718-99E9-4F6A-A0EC-343C92D0B03E}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 25 {293FD718-99E9-4F6A-A0EC-343C92D0B03E}.Release|Any CPU.Build.0 = Release|Any CPU 26 {117B7D68-ED7D-4350-996B-9CA15259E107}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 {117B7D68-ED7D-4350-996B-9CA15259E107}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 {117B7D68-ED7D-4350-996B-9CA15259E107}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 {117B7D68-ED7D-4350-996B-9CA15259E107}.Release|Any CPU.Build.0 = Release|Any CPU 24 30 EndGlobalSection 25 31 GlobalSection(SolutionProperties) = preSolution -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/ExpressionCreatorTable.cs
r14320 r14323 2 2 using System.Collections.Generic; 3 3 using HeuristicLab.Algorithms.PushGP.Expressions; 4 5 using LinqExpression = System.Linq.Expressions.Expression; 4 6 5 7 namespace HeuristicLab.Algorithms.PushGP … … 72 74 private static Func<Expression> GetExpressionCreator(Type type) 73 75 { 74 return System.Linq.Expressions.Expression.Lambda<Func<Expression>>(75 System.Linq.Expressions.Expression.New(type.GetConstructor(Type.EmptyTypes))76 76 return LinqExpression.Lambda<Func<Expression>>( 77 LinqExpression.New(type.GetConstructor(Type.EmptyTypes)) 78 ).Compile(); 77 79 } 78 80 … … 83 85 else throw new NotSupportedException("OpCode: " + opCode); 84 86 } 87 88 public static bool TryGetCreator(OpCode opCode, out Func<Expression> creator) 89 { 90 return symbolTable.TryGetValue(opCode, out creator); 91 } 85 92 } 86 93 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Boolean/BooleanPushExpression.cs
r14320 r14323 19 19 public override string ToString() 20 20 { 21 return $"{ base.ToString()}({this.Value})";21 return $"{this.Value.ToString().ToUpper()}"; 22 22 } 23 23 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecExpandExpression.cs
r14320 r14323 23 23 public override string ToString() 24 24 { 25 return $"( {string.Join(", ", this.Expressions.Reverse())})";25 return $"( {string.Join(" ", this.Expressions.Reverse())} )"; 26 26 } 27 27 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Exec/ExecYExpression.cs
r14320 r14323 20 20 }); 21 21 22 interpreterService.ExecStack.Insert( 1, expandExpression);22 interpreterService.ExecStack.Insert(interpreterService.ExecStack.Count - 1, expandExpression); 23 23 } 24 24 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Expression.cs
r14320 r14323 44 44 return; 45 45 46 var value = stack. ElementAt(0);46 var value = stack.Top; 47 47 stack.Push(value); 48 48 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Float/FloatPushExpression.cs
r14320 r14323 19 19 public override string ToString() 20 20 { 21 return $"{ base.ToString()}({this.Value})";21 return $"{this.Value}"; 22 22 } 23 23 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Integer/IntegerPushExpression.cs
r14320 r14323 19 19 public override string ToString() 20 20 { 21 return $"{ base.ToString()}({this.Value})";21 return $"{this.Value}"; 22 22 } 23 23 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP.csproj
r14320 r14323 41 41 </ItemGroup> 42 42 <ItemGroup> 43 <Compile Include=" Data\Configuration.cs" />43 <Compile Include="Interpreter\Configuration.cs" /> 44 44 <Compile Include="Data\Expression.cs" /> 45 45 <Compile Include="Expressions\Boolean\BooleanAndExpression.cs" /> … … 96 96 <Compile Include="Expressions\Name\NameDuplicateExpression.cs" /> 97 97 <Compile Include="Expressions\Name\NameDefineXExecExpression.cs" /> 98 <Compile Include="Generators\CodeGenerator.cs" /> 99 <Compile Include="Generators\Configuration.cs" /> 98 100 <Compile Include="InstructionSets\ExecInstructionSet.cs" /> 99 101 <Compile Include="InstructionSets\CodeInstructionSet.cs" /> … … 106 108 <Compile Include="OpCodeExtensions.cs" /> 107 109 <Compile Include="OperationTable.cs" /> 108 <Compile Include="Parser.cs" /> 110 <Compile Include="Parser\Parser.cs" /> 111 <Compile Include="StaticRandom.cs" /> 109 112 <Compile Include="Stack\IStack.cs" /> 110 <Compile Include="Interpreter .cs" />113 <Compile Include="Interpreter\Interpreter.cs" /> 111 114 <Compile Include="Properties\AssemblyInfo.cs" /> 112 115 <Compile Include="Stack\IInterpreterService.cs" /> 113 116 <Compile Include="Stack\PushGPStack.cs" /> 114 <Compile Include="SymbolTable.cs" /> 115 </ItemGroup> 116 <ItemGroup> 117 <Folder Include="Generator\" /> 117 <Compile Include="Parser\SymbolTable.cs" /> 118 118 </ItemGroup> 119 119 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/InstructionSets/CommonInstructionSet.cs
r14320 r14323 45 45 // return; 46 46 47 // var value = stack. ElementAt(0);47 // var value = stack.Top; 48 48 // stack.Push(value); 49 49 // } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/OpCodeExtensions.cs
r14320 r14323 1 namespace HeuristicLab.Algorithms.PushGP 1 using System; 2 using System.Linq; 3 4 namespace HeuristicLab.Algorithms.PushGP 2 5 { 3 6 public static class OpCodeExtensions 4 7 { 8 public readonly static byte Min = 0; 9 public readonly static byte Max = Enum.GetValues(typeof(OpCode)).Cast<byte>().Last(); 10 5 11 public static bool IsCodeOp(this OpCode opCode) 6 12 { -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Stack/IStack.cs
r14320 r14323 5 5 public interface IStack<T> : IEnumerable<T>, ICollection<T> 6 6 { 7 bool IsEmpty { get; } 7 8 void Push(T item); 8 9 -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Stack/PushGPStack.cs
r14320 r14323 2 2 using System.Collections; 3 3 using System.Collections.Generic; 4 using System.Linq;5 4 6 5 namespace HeuristicLab.Algorithms.PushGP.Stack … … 18 17 private List<T> data = new List<T>(); 19 18 20 public T Top => this.ElementAt(0); 21 19 public T Top => this.data[Count - 1]; 22 20 public int Count => data.Count; 21 public bool IsEmpty => this.Count == 0; 23 22 24 23 public bool IsReadOnly … … 62 61 public T Pop() 63 62 { 64 var value = this.data[0]; 65 this.data.RemoveAt(0); 63 var index = Count - 1; 64 var value = this.data[index]; 65 this.data.RemoveAt(index); 66 66 67 67 return value; … … 70 70 public T[] Pop(int count) 71 71 { 72 var items = this.data.GetRange(0, count).ToArray();73 this.data.RemoveRange(0, count);72 var startIndex = Count - count; 73 var items = this.data.GetRange(startIndex, count); 74 74 75 return items; 75 this.data.RemoveRange(startIndex, count); 76 77 items.Reverse(); 78 return items.ToArray(); 76 79 } 77 80 78 81 public void Push(T item) 79 82 { 80 this.data. Insert(0,item);83 this.data.Add(item); 81 84 } 82 85 83 86 public void Push(params T[] items) 84 87 { 85 this.data. InsertRange(0, items.Reverse());88 this.data.AddRange(items); 86 89 } 87 90 88 91 public void Push(IEnumerable<T> items) 89 92 { 90 this.data. InsertRange(0,items);93 this.data.AddRange(items); 91 94 } 92 95 … … 98 101 public void Insert(int index, params T[] items) 99 102 { 100 this.data.InsertRange(index, items .Reverse());103 this.data.InsertRange(index, items); 101 104 } 102 105 103 106 public void Insert(int index, IEnumerable<T> items) 104 107 { 105 this.data.InsertRange(index, items .Reverse());108 this.data.InsertRange(index, items); 106 109 } 107 110 108 111 public bool Remove(T item) 109 112 { 110 if (this.data.Count > 0 && this.data[0].Equals(item)) 113 var index = Count - 1; 114 if (this.data.Count > 0 && this.data[index].Equals(item)) 111 115 { 112 this.data.RemoveAt( 0);116 this.data.RemoveAt(index); 113 117 return true; 114 118 } … … 123 127 if (this.data.Count > 0) 124 128 { 125 item = this. data[0];129 item = this.Pop(); 126 130 return true; 127 131 }
Note: See TracChangeset
for help on using the changeset viewer.