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 | using HeuristicLab.Algorithms.PushGP.Parser;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Algorithms.PushGP.Cli {
|
---|
10 |
|
---|
11 | using HeuristicLab.Algorithms.PushGP.Data.Random;
|
---|
12 | using HeuristicLab.Algorithms.PushGP.Expressions;
|
---|
13 | using HeuristicLab.Algorithms.PushGP.Simplifier;
|
---|
14 |
|
---|
15 | class Program {
|
---|
16 | static void Main(string[] args) {
|
---|
17 | //SimpleTest();
|
---|
18 | //Stepwise().Wait();
|
---|
19 | //PerformanceTestInterpreter();
|
---|
20 | //PerformanceParallelTestInterpreter();
|
---|
21 | //PerformanceTestCodeGenerator();
|
---|
22 | TestRobustness();
|
---|
23 | //TestPool();
|
---|
24 | //TestPoolPerformance();
|
---|
25 | //TestSimplifier();
|
---|
26 |
|
---|
27 | Console.WriteLine("\nPress any key to continue...");
|
---|
28 | Console.ReadKey();
|
---|
29 | }
|
---|
30 |
|
---|
31 | static void SimpleTest() {
|
---|
32 | var interpreter = new PushGpInterpreter();
|
---|
33 |
|
---|
34 | interpreter.Interpret("5 INTEGER.DUP INTEGER.+");
|
---|
35 |
|
---|
36 | interpreter.PrintStacks();
|
---|
37 | }
|
---|
38 |
|
---|
39 | static async Task Stepwise() {
|
---|
40 | var interpreter = new PushGpInterpreter();
|
---|
41 |
|
---|
42 | interpreter.InterpretAsync("( 0 2 CODE.QUOTE ( 1 INTEGER.+ 0 3 CODE.QUOTE ( 1 INTEGER.+ INTEGER.* ) CODE.DO*RANGE INTEGER.+ ) CODE.DO*RANGE )", true).Wait();
|
---|
43 |
|
---|
44 | while (!interpreter.IsCompleted) {
|
---|
45 | Console.Clear();
|
---|
46 | interpreter.PrintStacks();
|
---|
47 | interpreter.Step();
|
---|
48 |
|
---|
49 | var input = Console.ReadKey();
|
---|
50 | if (input.Key == ConsoleKey.Escape) {
|
---|
51 | break;
|
---|
52 | } else if (input.Key == ConsoleKey.Spacebar) {
|
---|
53 | await interpreter.ResumeAsync();
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | Console.Clear();
|
---|
58 | interpreter.PrintStacks();
|
---|
59 | }
|
---|
60 |
|
---|
61 | static void PerformanceTestInterpreter() {
|
---|
62 | var program = PushGPParser.Parse("( 5 INTEGER.DUP INTEGER.+ )");
|
---|
63 |
|
---|
64 | var interpreter = new PushGpInterpreter();
|
---|
65 | var sw = new Stopwatch();
|
---|
66 |
|
---|
67 | sw.Start();
|
---|
68 | for (var i = 0; i < 60000000; i++) {
|
---|
69 | interpreter.Interpret(program);
|
---|
70 | interpreter.Clear();
|
---|
71 | }
|
---|
72 | sw.Stop();
|
---|
73 |
|
---|
74 | Console.WriteLine(sw.Elapsed);
|
---|
75 | }
|
---|
76 |
|
---|
77 | static void PerformanceParallelTestInterpreter() {
|
---|
78 | var program = PushGPParser.Parse("( 5 INTEGER.DUP INTEGER.+ )");
|
---|
79 | //var program = new ExecExpandExpression(new Expression[]
|
---|
80 | //{
|
---|
81 | // new IntegerPushExpression(5),
|
---|
82 | // new IntegerDuplicateExpression(),
|
---|
83 | // new IntegerAddExpression()
|
---|
84 | //});
|
---|
85 |
|
---|
86 | var sw = new Stopwatch();
|
---|
87 | var iterations = 100;
|
---|
88 | var amount = 600000;
|
---|
89 | var pool = new PushGpInterpreterPool(iterations);
|
---|
90 |
|
---|
91 | sw.Start();
|
---|
92 | Parallel.For(0, iterations, i => {
|
---|
93 | using (var interpreter = pool.GetInstance()) {
|
---|
94 |
|
---|
95 | for (var j = 0; j < amount; j++) {
|
---|
96 | interpreter.Interpret(program);
|
---|
97 | interpreter.Clear();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | });
|
---|
101 | sw.Stop();
|
---|
102 |
|
---|
103 | Console.WriteLine(sw.Elapsed);
|
---|
104 | }
|
---|
105 |
|
---|
106 | static void PerformanceTestCodeGenerator() {
|
---|
107 | var sw = new Stopwatch();
|
---|
108 |
|
---|
109 | sw.Start();
|
---|
110 | var expressions = CodeGenerator.RandomCode(3000000);
|
---|
111 | sw.Stop();
|
---|
112 |
|
---|
113 | Console.WriteLine(string.Format("Generated {0} in {1}", expressions.Count(), sw.Elapsed));
|
---|
114 | }
|
---|
115 |
|
---|
116 | static void TestRobustness() {
|
---|
117 | var sw = new Stopwatch();
|
---|
118 | var parallism = Environment.ProcessorCount;
|
---|
119 | var maxProgramSizeLimit = 100;
|
---|
120 | var partitionSize = 100000;
|
---|
121 | RandomFactory.Seed = 1337;
|
---|
122 |
|
---|
123 | var execCounters = new int[parallism];
|
---|
124 | var config = new Configuration { EvalPushLimit = 500 };
|
---|
125 | var pool = new PushGpInterpreterPool(config);
|
---|
126 |
|
---|
127 | sw.Start();
|
---|
128 | Parallel.For(0, parallism, i => {
|
---|
129 | using (var interpreter = pool.GetInstance()) {
|
---|
130 | for (var j = 0; j < partitionSize; j++) {
|
---|
131 | var program = CodeGenerator.RandomProgram(maxProgramSizeLimit);
|
---|
132 |
|
---|
133 | interpreter.Interpret(program);
|
---|
134 |
|
---|
135 | execCounters[i] += interpreter.ExecCounter;
|
---|
136 | interpreter.Clear();
|
---|
137 | }
|
---|
138 | }
|
---|
139 | });
|
---|
140 | sw.Stop();
|
---|
141 |
|
---|
142 | Console.WriteLine("ExecCount: {0}", execCounters.Sum());
|
---|
143 | Console.WriteLine("Duration: {0}", sw.Elapsed);
|
---|
144 | }
|
---|
145 |
|
---|
146 | static void TestPool() {
|
---|
147 | var pool = new PushGpInterpreterPool();
|
---|
148 | var normal = new PushGpInterpreter();
|
---|
149 | int id;
|
---|
150 |
|
---|
151 | using (var interpreter = pool.GetInstance()) {
|
---|
152 | interpreter.Interpret("( 1 2 INTEGER.+ )");
|
---|
153 |
|
---|
154 | id = interpreter.GetHashCode();
|
---|
155 | if (id == normal.GetHashCode()) Console.WriteLine("equal 1");
|
---|
156 | }
|
---|
157 |
|
---|
158 | using (var interpreter = pool.GetInstance())
|
---|
159 | using (var interpreter2 = pool.GetInstance()) {
|
---|
160 | interpreter.Interpret("( 1 2 INTEGER.+ )");
|
---|
161 | if (id == interpreter.GetHashCode()) Console.WriteLine("equal 2");
|
---|
162 | if (id == interpreter2.GetHashCode()) Console.WriteLine("equal 3");
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | static void TestPoolPerformance() {
|
---|
167 | var sw = new Stopwatch();
|
---|
168 | var iterations = 100000000;
|
---|
169 |
|
---|
170 | sw.Start();
|
---|
171 | for (var i = 0; i < iterations; i++) {
|
---|
172 | var interpreter = new PushGpInterpreter();
|
---|
173 | interpreter.Clear();
|
---|
174 | }
|
---|
175 | sw.Stop();
|
---|
176 |
|
---|
177 | Console.WriteLine(sw.Elapsed);
|
---|
178 |
|
---|
179 | var pool = new PushGpInterpreterPool();
|
---|
180 | sw.Restart();
|
---|
181 | for (var i = 0; i < iterations; i++) {
|
---|
182 | using (var interpreter = pool.GetInstance()) {
|
---|
183 | interpreter.Clear();
|
---|
184 | }
|
---|
185 | }
|
---|
186 | sw.Stop();
|
---|
187 |
|
---|
188 | Console.WriteLine(sw.Elapsed);
|
---|
189 | }
|
---|
190 |
|
---|
191 | static void TestSimplifier() {
|
---|
192 | var interpreter = new PushGpInterpreter();
|
---|
193 | var program = PushGPParser.Parse("( 5 INTEGER.DUP FLOAT.+ INTEGER.+ )") as ExecExpandExpression;
|
---|
194 | var simplifier = new RandomSimplifier() { Trys = 10 };
|
---|
195 |
|
---|
196 | var simplerProgram = simplifier.Simplify(
|
---|
197 | program,
|
---|
198 | solution => {
|
---|
199 | interpreter.Clear();
|
---|
200 | interpreter.Interpret(solution);
|
---|
201 | return interpreter.IntegerStack.Top == 10;
|
---|
202 | });
|
---|
203 |
|
---|
204 | Console.WriteLine(simplerProgram);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | } |
---|