[14320] | 1 | using System;
|
---|
[14323] | 2 | using System.Diagnostics;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Threading.Tasks;
|
---|
| 5 | using HeuristicLab.Algorithms.PushGP.Generators;
|
---|
[14320] | 6 | using HeuristicLab.Algorithms.PushGP.Interpreter;
|
---|
| 7 |
|
---|
| 8 | namespace HeuristicLab.Algorithms.PushGP.Cli
|
---|
| 9 | {
|
---|
| 10 | class Program
|
---|
| 11 | {
|
---|
| 12 | static void Main(string[] args)
|
---|
| 13 | {
|
---|
[14328] | 14 | Stepwise().Wait();
|
---|
| 15 | //PerformanceTestInterpreter();
|
---|
[14323] | 16 | //PerformanceTestCodeGenerator();
|
---|
| 17 |
|
---|
| 18 | Console.WriteLine("\nPress any key to continue...");
|
---|
| 19 | Console.ReadKey();
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | static async Task Stepwise()
|
---|
| 23 | {
|
---|
[14320] | 24 | var interpreter = new PushGPInterpreter();
|
---|
| 25 |
|
---|
[14392] | 26 | interpreter.InterpretAsync("( 0 2 CODE.QUOTE ( 1 INTEGER.+ 0 3 CODE.QUOTE ( 1 INTEGER.+ INTEGER.* ) CODE.DO*RANGE INTEGER.+ ) CODE.DO*RANGE )", true).Wait();
|
---|
[14320] | 27 |
|
---|
[14323] | 28 | while (!interpreter.IsCompleted)
|
---|
| 29 | {
|
---|
| 30 | Console.Clear();
|
---|
| 31 | interpreter.PrintStacks();
|
---|
| 32 | interpreter.Step();
|
---|
| 33 |
|
---|
| 34 | var input = Console.ReadKey();
|
---|
| 35 | if (input.Key == ConsoleKey.Escape)
|
---|
| 36 | {
|
---|
| 37 | break;
|
---|
| 38 | }
|
---|
| 39 | else if (input.Key == ConsoleKey.Spacebar)
|
---|
| 40 | {
|
---|
| 41 | await interpreter.ResumeAsync();
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | Console.Clear();
|
---|
[14320] | 46 | interpreter.PrintStacks();
|
---|
[14323] | 47 | }
|
---|
[14320] | 48 |
|
---|
[14328] | 49 | static void PerformanceTestInterpreter()
|
---|
[14323] | 50 | {
|
---|
| 51 | var program = PushGPInterpreter.Encode("( 5 INTEGER.DUP INTEGER.+ )");
|
---|
| 52 | var interpreter = new PushGPInterpreter();
|
---|
| 53 | var sw = new Stopwatch();
|
---|
| 54 |
|
---|
| 55 | sw.Start();
|
---|
| 56 | for (var i = 0; i < 20000000; i++)
|
---|
| 57 | {
|
---|
[14328] | 58 | interpreter.Interpret(program);
|
---|
| 59 | interpreter.Clear();
|
---|
[14323] | 60 | }
|
---|
| 61 | sw.Stop();
|
---|
| 62 |
|
---|
| 63 | Console.WriteLine(sw.Elapsed);
|
---|
[14320] | 64 | }
|
---|
| 65 |
|
---|
[14323] | 66 | static void PerformanceTestCodeGenerator()
|
---|
| 67 | {
|
---|
| 68 | var sw = new Stopwatch();
|
---|
[14320] | 69 |
|
---|
[14392] | 70 | var interpreter = new PushGPInterpreter();
|
---|
| 71 | var generator = new CodeGenerator(interpreter);
|
---|
| 72 |
|
---|
[14323] | 73 | sw.Start();
|
---|
[14392] | 74 | var expressions = generator.RandomCode(3000000);
|
---|
[14323] | 75 | sw.Stop();
|
---|
| 76 |
|
---|
[14328] | 77 | Console.WriteLine(string.Format("Generated {0} in {1}", expressions.Count(), sw.Elapsed));
|
---|
[14323] | 78 | }
|
---|
[14320] | 79 | }
|
---|
| 80 | } |
---|