1 | namespace HeuristicLab.Algorithms.PushGP.Interpreter {
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Threading;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using HeuristicLab.Algorithms.PushGP.Expressions;
|
---|
7 | using HeuristicLab.Algorithms.PushGP.Parser;
|
---|
8 | using HeuristicLab.Algorithms.PushGP.Stack;
|
---|
9 |
|
---|
10 | public class PushGpInterpreter : IPushGpInterpreter {
|
---|
11 | private Task currentTask;
|
---|
12 |
|
---|
13 | public PushGpInterpreter(Configuration config = null) {
|
---|
14 | this.Configuration = config ?? new Configuration();
|
---|
15 |
|
---|
16 | // setting the capacity of the stacks to max points ensures that there will be enough memory at runtime
|
---|
17 | this.CodeStack = new PushGPStack<Expression>(this.Configuration.MaxPointsInProgram);
|
---|
18 | this.ExecStack = new PushGPStack<Expression>(this.Configuration.MaxPointsInProgram);
|
---|
19 | this.NameStack = new PushGPStack<string>(16);
|
---|
20 | this.BooleanStack = new PushGPStack<bool>(16);
|
---|
21 | this.IntegerStack = new PushGPStack<long>(16);
|
---|
22 | this.FloatStack = new PushGPStack<double>(16);
|
---|
23 |
|
---|
24 | this.CustomExpressions = new Dictionary<string, Expression>();
|
---|
25 | }
|
---|
26 |
|
---|
27 | public int ExecCounter { get; private set; }
|
---|
28 |
|
---|
29 | public bool IsPaused { get; private set; }
|
---|
30 |
|
---|
31 | public bool IsAborted { get; private set; }
|
---|
32 |
|
---|
33 | public bool IsRunning
|
---|
34 | {
|
---|
35 | get
|
---|
36 | {
|
---|
37 | return !this.ExecStack.IsEmpty &&
|
---|
38 | !this.IsPaused &&
|
---|
39 | !this.IsAborted &&
|
---|
40 | (this.ExecCounter < this.Configuration.EvalPushLimit);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public bool IsCompleted
|
---|
45 | {
|
---|
46 | get
|
---|
47 | {
|
---|
48 | return this.ExecStack.Count == 0;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public bool CanStep
|
---|
53 | {
|
---|
54 | get
|
---|
55 | {
|
---|
56 | return !this.IsCompleted && !this.IsAborted && this.IsPaused;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | public Configuration Configuration { get; protected set; }
|
---|
61 |
|
---|
62 | public IStack<Expression> CodeStack { get; private set; }
|
---|
63 |
|
---|
64 | public IStack<Expression> ExecStack { get; private set; }
|
---|
65 |
|
---|
66 | public IStack<string> NameStack { get; private set; }
|
---|
67 |
|
---|
68 | public IStack<bool> BooleanStack { get; private set; }
|
---|
69 |
|
---|
70 | public IStack<long> IntegerStack { get; private set; }
|
---|
71 |
|
---|
72 | public IStack<double> FloatStack { get; private set; }
|
---|
73 |
|
---|
74 | public IDictionary<string, Expression> CustomExpressions { get; private set; }
|
---|
75 |
|
---|
76 | public bool IsNameQuoteFlagSet { get; set; }
|
---|
77 |
|
---|
78 | public void Interpret(string code) {
|
---|
79 | var program = PushGPParser.Parse(code);
|
---|
80 | this.Interpret(program);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public Task InterpretAsync(string code, bool paused = false, CancellationToken token = default(CancellationToken)) {
|
---|
84 | var program = PushGPParser.Parse(code);
|
---|
85 |
|
---|
86 | this.currentTask = this.InterpretAsync(program, paused, token);
|
---|
87 |
|
---|
88 | return this.currentTask;
|
---|
89 | }
|
---|
90 |
|
---|
91 | public Task InterpretAsync(
|
---|
92 | Expression program,
|
---|
93 | bool paused = false,
|
---|
94 | CancellationToken token = default(CancellationToken)) {
|
---|
95 | this.IsPaused = paused;
|
---|
96 | this.currentTask = Task.Run(() => this.Interpret(program), token);
|
---|
97 |
|
---|
98 | return this.currentTask;
|
---|
99 | }
|
---|
100 |
|
---|
101 | public void Interpret(Expression program) {
|
---|
102 | /* Push top expression so the loop is able to enter
|
---|
103 | * If the top expression is a single statement then the loop has nothing to do
|
---|
104 | * Otherwise the expand expression will be evaluated and pushes code onto the EXEC stack */
|
---|
105 | this.ExecStack.Push(program);
|
---|
106 |
|
---|
107 | if (this.Configuration.TopLevelPushCode) this.CodeStack.Insert(0, program);
|
---|
108 |
|
---|
109 | // run top expression
|
---|
110 | this.DoStep();
|
---|
111 |
|
---|
112 | this.Interpret();
|
---|
113 | }
|
---|
114 |
|
---|
115 | public async Task AbortAsync() {
|
---|
116 | if (this.IsAborted || this.IsCompleted) return;
|
---|
117 |
|
---|
118 | this.IsAborted = true;
|
---|
119 |
|
---|
120 | if (this.currentTask != null) await this.currentTask;
|
---|
121 | }
|
---|
122 |
|
---|
123 | public async Task AbortAndResetAsync() {
|
---|
124 | await this.AbortAsync();
|
---|
125 | this.Reset();
|
---|
126 | }
|
---|
127 |
|
---|
128 | public async Task PauseAsync() {
|
---|
129 | if (this.IsPaused || this.IsCompleted) return;
|
---|
130 |
|
---|
131 | this.IsPaused = true;
|
---|
132 |
|
---|
133 | if (this.currentTask != null) await this.currentTask;
|
---|
134 | }
|
---|
135 |
|
---|
136 | public Task ResumeAsync() {
|
---|
137 | if (this.IsPaused || !this.IsAborted) {
|
---|
138 | this.IsPaused = false;
|
---|
139 | return this.InterpreteAsync();
|
---|
140 | }
|
---|
141 |
|
---|
142 | return this.currentTask;
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void Step() {
|
---|
146 | if (!this.CanStep) return;
|
---|
147 | this.DoStep();
|
---|
148 | this.Finally();
|
---|
149 | }
|
---|
150 |
|
---|
151 | public void Step(int count) {
|
---|
152 | for (var i = 0; i < count; i++) this.Step();
|
---|
153 | }
|
---|
154 |
|
---|
155 | public void PrintStacks() {
|
---|
156 | this.PrintStack("EXEC", this.ExecStack);
|
---|
157 | this.PrintStack("CODE", this.CodeStack);
|
---|
158 | this.PrintStack("NAME", this.NameStack);
|
---|
159 | this.PrintStack("BOOLEAN", this.BooleanStack);
|
---|
160 | this.PrintStack("FLOAT", this.FloatStack);
|
---|
161 | this.PrintStack("INTEGER", this.IntegerStack);
|
---|
162 | }
|
---|
163 |
|
---|
164 | /// <summary>
|
---|
165 | /// Clears stacks and
|
---|
166 | /// </summary>
|
---|
167 | public void Reset() {
|
---|
168 | this.IsAborted = false;
|
---|
169 | this.IsPaused = false;
|
---|
170 | this.currentTask = null;
|
---|
171 |
|
---|
172 | this.Clear();
|
---|
173 | }
|
---|
174 |
|
---|
175 | /// <summary>
|
---|
176 | /// Clears stacks
|
---|
177 | /// </summary>
|
---|
178 | public void Clear() {
|
---|
179 | this.ExecCounter = 0;
|
---|
180 |
|
---|
181 | this.ExecStack.Clear();
|
---|
182 | this.CodeStack.Clear();
|
---|
183 |
|
---|
184 | this.NameStack.Clear();
|
---|
185 | this.BooleanStack.Clear();
|
---|
186 | this.IntegerStack.Clear();
|
---|
187 | this.FloatStack.Clear();
|
---|
188 |
|
---|
189 | this.CustomExpressions.Clear();
|
---|
190 | }
|
---|
191 |
|
---|
192 | private void Interpret() {
|
---|
193 | while (this.IsRunning) {
|
---|
194 | this.DoStep();
|
---|
195 | this.ExecCounter++;
|
---|
196 | }
|
---|
197 |
|
---|
198 | this.Finally();
|
---|
199 | }
|
---|
200 |
|
---|
201 | private void Finally() {
|
---|
202 | if (!this.IsCompleted)
|
---|
203 | return;
|
---|
204 |
|
---|
205 | if (this.Configuration.TopLevelPopCode && (this.CodeStack.Count > 0))
|
---|
206 | this.CodeStack.Pop();
|
---|
207 | }
|
---|
208 |
|
---|
209 | private void DoStep() {
|
---|
210 | this.ExecStack.Pop().Eval(this);
|
---|
211 | }
|
---|
212 |
|
---|
213 | private Task InterpreteAsync() {
|
---|
214 | this.currentTask = Task.Run(() => this.Interpret());
|
---|
215 |
|
---|
216 | return this.currentTask;
|
---|
217 | }
|
---|
218 |
|
---|
219 | private void PrintStack<T>(string name, IStack<T> stack) {
|
---|
220 | Console.WriteLine("--- {0} ---\n{1}\n---------------------------------------------------", name, stack);
|
---|
221 | }
|
---|
222 | }
|
---|
223 | } |
---|