1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Threading.Tasks;
|
---|
5 | using HeuristicLab.Algorithms.PushGP.Generators;
|
---|
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 | {
|
---|
14 | Stepwise().Wait();
|
---|
15 | //PerformanceTestInterpreter();
|
---|
16 | //PerformanceTestCodeGenerator();
|
---|
17 |
|
---|
18 | Console.WriteLine("\nPress any key to continue...");
|
---|
19 | Console.ReadKey();
|
---|
20 | }
|
---|
21 |
|
---|
22 | static async Task Stepwise()
|
---|
23 | {
|
---|
24 | var interpreter = new PushGPInterpreter();
|
---|
25 |
|
---|
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();
|
---|
27 |
|
---|
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();
|
---|
46 | interpreter.PrintStacks();
|
---|
47 | }
|
---|
48 |
|
---|
49 | static void PerformanceTestInterpreter()
|
---|
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 | {
|
---|
58 | interpreter.Interpret(program);
|
---|
59 | interpreter.Clear();
|
---|
60 | }
|
---|
61 | sw.Stop();
|
---|
62 |
|
---|
63 | Console.WriteLine(sw.Elapsed);
|
---|
64 | }
|
---|
65 |
|
---|
66 | static void PerformanceTestCodeGenerator()
|
---|
67 | {
|
---|
68 | var sw = new Stopwatch();
|
---|
69 |
|
---|
70 | var interpreter = new PushGPInterpreter();
|
---|
71 | var generator = new CodeGenerator(interpreter);
|
---|
72 |
|
---|
73 | sw.Start();
|
---|
74 | var expressions = generator.RandomCode(3000000);
|
---|
75 | sw.Stop();
|
---|
76 |
|
---|
77 | Console.WriteLine(string.Format("Generated {0} in {1}", expressions.Count(), sw.Elapsed));
|
---|
78 | }
|
---|
79 | }
|
---|
80 | } |
---|