Changeset 14834 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter
- Timestamp:
- 04/10/17 00:27:31 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/IPushInterpreter.cs
r14777 r14834 7 7 8 8 using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; 9 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;10 9 11 10 using Stack; … … 13 12 public interface IPushInterpreter { 14 13 IRandom Random { get; set; } 15 16 IStack<Expression> CodeStack { get; } 17 IStack<Expression> ExecStack { get; } 18 IStack<string> NameStack { get; } 19 IStack<bool> BooleanStack { get; } 20 IStack<long> IntegerStack { get; } 21 IStack<double> FloatStack { get; } 22 IStack<char> CharStack { get; } 23 IStack<string> StringStack { get; } 24 14 IPushStack<Expression> CodeStack { get; } 15 IPushStack<Expression> ExecStack { get; } 16 IPushStack<string> NameStack { get; } 17 IPushStack<bool> BooleanStack { get; } 18 IPushStack<long> IntegerStack { get; } 19 IPushStack<double> FloatStack { get; } 20 IPushStack<char> CharStack { get; } 21 IPushStack<string> StringStack { get; } 22 IPushStack<List<long>> IntegerVectorStack { get; } 23 IPushStack<List<double>> FloatVectorStack { get; } 24 IPushStack<List<bool>> BooleanVectorStack { get; } 25 IPushStack<List<string>> StringVectorStack { get; } 25 26 IDictionary<string, Expression> CustomExpressions { get; } 26 27 27 IReadOnlyPushConfiguration Configuration { get; } 28 29 InterpreterPoolContainer PoolContainer { get; }30 31 bool IsNameQuoteFlagSet { get; set; }32 33 28 void Clear(); 34 29 void Run(string code, bool stepwise = false); 35 30 void Run(Expression expression, bool stepwise = false); 36 Task RunAsync(Expression expression, bool paused = false,CancellationToken token = default(CancellationToken));37 Task RunAsync(string code, bool paused = false,CancellationToken token = default(CancellationToken));31 Task RunAsync(Expression expression, CancellationToken token = default(CancellationToken)); 32 Task RunAsync(string code, CancellationToken token = default(CancellationToken)); 38 33 Task AbortAndResetAsync(); 39 34 Task AbortAsync(); … … 41 36 void Resume(); 42 37 Task ResumeAsync(); 43 44 38 bool Step(); 45 void PrintStacks();46 39 } 47 40 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/InterpreterPoolContainer.cs
r14777 r14834 22 22 expressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(partitionSize, () => new PooledList<Expression>(), maxPartitionCount); 23 23 24 InitStatefulExpressionPools( );24 InitStatefulExpressionPools(partitionSize, maxPartitionCount); 25 25 } 26 26 … … 33 33 this.expressionListPoolProvider = expressionListPoolProvider; 34 34 35 InitStatefulExpressionPools( );35 InitStatefulExpressionPools(1024, 2048); 36 36 } 37 37 38 public IManagedPool<PushProgram> PushProgramPool { get; private set; } 39 public IManagedPool<LoopState> LoopStatePool { get; private set; } 40 public IManagedPool<PooledList<Expression>> ExpressionListPool { get; private set; } 38 private IManagedPool<PushProgram> pushProgramPool; 39 public IManagedPool<PushProgram> PushProgramPool 40 { 41 get 42 { 43 if (pushProgramPool == null) pushProgramPool = pushProgramPoolProvider.CreatePool(); 44 return pushProgramPool; 45 } 46 } 41 47 42 private void InitStatefulExpressionPools() { 48 private IManagedPool<LoopState> loopStatePool; 49 public IManagedPool<LoopState> LoopStatePool 50 { 51 get 52 { 53 if (loopStatePool == null) loopStatePool = loopStatePoolProvider.CreatePool(); 54 return loopStatePool; 55 } 56 } 57 58 private IManagedPool<PooledList<Expression>> expressionListPool; 59 public IManagedPool<PooledList<Expression>> ExpressionListPool 60 { 61 get 62 { 63 if (expressionListPool == null) expressionListPool = expressionListPoolProvider.CreatePool(); 64 return expressionListPool; 65 } 66 } 67 68 private void InitStatefulExpressionPools(int partitionSize, int maxPartitionCount) { 43 69 for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Length; i++) { 44 70 var type = ExpressionTable.StatefulExpressionTypes[i]; 45 71 var name = ExpressionTable.TypeToNameTable[type]; 46 var factory = ExpressionTable.StatefulExpressionFactory[name];47 72 48 statefulExpressionPoolProviders.Add(type, new ManagedPoolProvider<Expression>(1024, factory)); 73 statefulExpressionPoolProviders.Add(type, new ManagedPoolProvider<Expression>( 74 partitionSize, 75 ExpressionTable.StatefulExpressionFactory[name], 76 maxPartitionCount)); 49 77 50 78 statefulExpressionPools.Add(type, null); … … 52 80 } 53 81 54 public IManagedPool<Expression> GetStatefulExpressionPool<T, StateT>() where T : StatefulExpression<StateT> { 55 return statefulExpressionPools[typeof(T)]; 82 public T GetStatefulExpression<T>() where T : Expression { 83 var type = typeof(T); 84 if (statefulExpressionPools[type] == null) 85 statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool(); 86 87 return (T)statefulExpressionPools[type].Get(); 56 88 } 57 89 58 public T GetStatefulExpression<T>() where T : Expression { 59 return (T)statefulExpressionPools[typeof(T)].Get(); 60 } 90 public void DisposePools() { 91 if (pushProgramPool != null) { 92 pushProgramPool.Dispose(); 93 pushProgramPool = null; 94 } 61 95 62 public void CreatePools() { 63 PushProgramPool = pushProgramPoolProvider.CreatePool(); 64 LoopStatePool = loopStatePoolProvider.CreatePool(); 65 ExpressionListPool = expressionListPoolProvider.CreatePool(); 96 if (loopStatePool != null) { 97 loopStatePool.Dispose(); 98 loopStatePool = null; 99 } 100 101 if (expressionListPool != null) { 102 expressionListPool.Dispose(); 103 expressionListPool = null; 104 } 66 105 67 106 for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Length; i++) { 68 107 var type = ExpressionTable.StatefulExpressionTypes[i]; 69 statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool(); 70 } 71 } 108 var pool = statefulExpressionPools[type]; 72 109 73 public void DisposePools() { 74 if (PushProgramPool != null) PushProgramPool.Dispose(); 75 if (LoopStatePool != null) LoopStatePool.Dispose(); 76 if (ExpressionListPool != null) ExpressionListPool.Dispose(); 77 78 for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Length; i++) { 79 var type = ExpressionTable.StatefulExpressionTypes[i]; 80 if (statefulExpressionPools[type] != null) statefulExpressionPools[type].Dispose(); 110 if (pool != null) { 111 pool.Dispose(); 112 statefulExpressionPools[type] = null; 113 } 81 114 } 82 115 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PooledPushInterpreter.cs
r14777 r14834 1 1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter { 2 using System;3 2 4 3 using HeuristicLab.Core; 5 4 using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; 6 5 7 public class PooledPushInterpreter : PushInterpreter , IDisposable{6 public class PooledPushInterpreter : PushInterpreter { 8 7 9 8 private readonly PushInterpreterPool pool; … … 14 13 } 15 14 16 public void Dispose() {17 this.Reset();18 this.Configuration = this.Configuration;19 this.pool.Free(this);15 public override void Dispose() { 16 base.Dispose(); 17 Reset(); 18 pool.Free(this); 20 19 } 21 20 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreter.cs
r14777 r14834 2 2 using System; 3 3 using System.Collections.Generic; 4 #if DEBUG5 #endif6 4 using System.Runtime.CompilerServices; 7 5 using System.Threading; 8 6 using System.Threading.Tasks; 9 using HeuristicLab.Core; 10 using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; 11 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 12 using HeuristicLab.Problems.ProgramSynthesis.Push.Parser; 13 using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; 14 using HeuristicLab.Random; 15 16 public class PushInterpreter : IPushInterpreter { 7 using Attributes; 8 using Configuration; 9 using Core; 10 using Expressions; 11 using Parser; 12 using Random; 13 using Stack; 14 15 #if DEBUG 16 using System.Diagnostics; 17 using System.Linq; 18 #endif 19 20 public class PushInterpreter : IInternalPushInterpreter, IDisposable { 17 21 private Task currentTask; 18 22 … … 27 31 28 32 CodeStack = new PushStack<Expression>(Configuration.EvalPushLimit) { 29 IsEnabled = Configuration. IsCodeStackEnabled33 IsEnabled = Configuration.EnabledStacks[StackTypes.Code] 30 34 }; 31 35 32 36 NameStack = new PushStack<string> { 33 IsEnabled = Configuration. IsNameStackEnabled37 IsEnabled = Configuration.EnabledStacks[StackTypes.Code] 34 38 }; 35 39 BooleanStack = new PushStack<bool> { 36 IsEnabled = Configuration. IsBooleanStackEnabled40 IsEnabled = Configuration.EnabledStacks[StackTypes.Boolean] 37 41 }; 38 42 IntegerStack = new PushStack<long> { 39 IsEnabled = Configuration. IsIntegerStackEnabled43 IsEnabled = Configuration.EnabledStacks[StackTypes.Integer] 40 44 }; 41 45 FloatStack = new PushStack<double> { 42 IsEnabled = Configuration. IsFloatStackEnabled46 IsEnabled = Configuration.EnabledStacks[StackTypes.Float] 43 47 }; 44 48 CharStack = new PushStack<char> { 45 IsEnabled = Configuration. IsCharStackEnabled49 IsEnabled = Configuration.EnabledStacks[StackTypes.Char] 46 50 }; 47 51 StringStack = new PushStack<string> { 48 IsEnabled = Configuration.IsStringStackEnabled 52 IsEnabled = Configuration.EnabledStacks[StackTypes.String] 53 }; 54 IntegerVectorStack = new PushStack<List<long>> { 55 IsEnabled = Configuration.EnabledStacks[StackTypes.IntegerVector] 56 }; 57 FloatVectorStack = new PushStack<List<double>> { 58 IsEnabled = Configuration.EnabledStacks[StackTypes.FloatVector] 59 }; 60 BooleanVectorStack = new PushStack<List<bool>> { 61 IsEnabled = Configuration.EnabledStacks[StackTypes.BooleanVector] 62 }; 63 StringVectorStack = new PushStack<List<string>> { 64 IsEnabled = Configuration.EnabledStacks[StackTypes.StringVector] 49 65 }; 50 66 … … 81 97 get 82 98 { 83 return ExecStack. Count == 0;99 return ExecStack.IsEmpty || ExecCounter >= Configuration.EvalPushLimit; 84 100 } 85 101 } … … 97 113 public IReadOnlyPushConfiguration Configuration { get; protected set; } 98 114 99 public IStack<Expression> CodeStack { get; private set; } 100 101 public IStack<Expression> ExecStack { get; private set; } 102 103 public IStack<string> NameStack { get; private set; } 104 105 public IStack<bool> BooleanStack { get; private set; } 106 107 public IStack<long> IntegerStack { get; private set; } 108 109 public IStack<double> FloatStack { get; private set; } 110 public IStack<char> CharStack { get; private set; } 111 public IStack<string> StringStack { get; private set; } 115 [PushStack(StackTypes.Code)] 116 public IPushStack<Expression> CodeStack { get; private set; } 117 118 [PushStack(StackTypes.Exec)] 119 public IPushStack<Expression> ExecStack { get; private set; } 120 121 [PushStack(StackTypes.Name)] 122 public IPushStack<string> NameStack { get; private set; } 123 124 [PushStack(StackTypes.Boolean)] 125 public IPushStack<bool> BooleanStack { get; private set; } 126 127 [PushStack(StackTypes.Integer)] 128 public IPushStack<long> IntegerStack { get; private set; } 129 130 [PushStack(StackTypes.Float)] 131 public IPushStack<double> FloatStack { get; private set; } 132 133 [PushStack(StackTypes.Char)] 134 public IPushStack<char> CharStack { get; private set; } 135 136 [PushStack(StackTypes.String)] 137 public IPushStack<string> StringStack { get; private set; } 138 139 [PushStack(StackTypes.IntegerVector)] 140 public IPushStack<List<long>> IntegerVectorStack { get; private set; } 141 142 [PushStack(StackTypes.FloatVector)] 143 public IPushStack<List<double>> FloatVectorStack { get; private set; } 144 145 [PushStack(StackTypes.BooleanVector)] 146 public IPushStack<List<bool>> BooleanVectorStack { get; private set; } 147 148 [PushStack(StackTypes.StringVector)] 149 public IPushStack<List<string>> StringVectorStack { get; private set; } 112 150 113 151 public IDictionary<string, Expression> CustomExpressions { get; private set; } … … 115 153 public bool IsNameQuoteFlagSet { get; set; } 116 154 117 public Task RunAsync(string code, bool stepwise = false,CancellationToken token = default(CancellationToken)) {155 public Task RunAsync(string code, CancellationToken token = default(CancellationToken)) { 118 156 var program = PushParser.Parse(code); 119 120 currentTask = RunAsync(program, stepwise, token); 121 157 currentTask = RunAsync(program, token); 122 158 return currentTask; 123 159 } 124 public async Task RunAsync( 125 Expression expression, 126 bool stepwise = false, 127 CancellationToken token = default(CancellationToken)) { 128 await Task.Run(() => Run(expression, stepwise), token); 160 public async Task RunAsync(Expression expression, CancellationToken token = default(CancellationToken)) { 161 await Task.Run(() => Run(expression, false), token); 129 162 } 130 163 … … 134 167 } 135 168 136 public void Run(Expression program, bool stepwise = false) { 137 PoolContainer.CreatePools(); 138 169 public void Run(Expression expression, bool stepwise = false) { 139 170 IsPaused = stepwise; 140 171 141 172 /* Push top expression so the loop is able to enter 142 * If the top expression is a single statement then the loop has nothing to do 143 * Otherwise the expand expression will be evaluated and pushes code onto the EXEC stack */ 144 ExecStack.Push(program); 145 146 if (Configuration.TopLevelPushCode) CodeStack.Insert(0, program); 147 148 // run top expression 149 DoStep(); 150 Interpret(); 151 152 PoolContainer.DisposePools(); 173 * If the top expression is a single statement then the loop has nothing to do. 174 * Otherwise the expand expression will be evaluated and pushes code onto the EXEC stack. 175 * Expanding the initial program is not counted */ 176 ExecStack.Push(expression); 177 178 if (Configuration.TopLevelPushCode) CodeStack.Insert(0, expression); 179 180 // expand if program 181 if (expression.IsProgram) 182 DoStep(); 183 184 Run(); 153 185 } 154 186 … … 188 220 189 221 IsPaused = false; 190 Interpret();222 this.Run(); 191 223 } 192 224 … … 196 228 197 229 var successful = DoStep(); 198 Finally(); 230 231 if (IsCompleted) 232 Finally(); 199 233 200 234 return successful; … … 204 238 public void Step(int count) { 205 239 for (var i = 0; i < count; i++) Step(); 206 }207 208 public void PrintStacks() {209 PrintStack("EXEC", ExecStack);210 PrintStack("CODE", CodeStack);211 PrintStack("NAME", NameStack);212 PrintStack("BOOLEAN", BooleanStack);213 PrintStack("FLOAT", FloatStack);214 PrintStack("INTEGER", IntegerStack);215 216 if (CustomExpressions.Count > 0) {217 Console.WriteLine("--------- Custom Expressions ---------");218 foreach (var ce in CustomExpressions) {219 Console.WriteLine("{0}: {1}", ce.Key, ce.Value);220 }221 }222 240 } 223 241 … … 226 244 /// </summary> 227 245 public void Reset() { 246 ExecCounter = 0; 247 228 248 IsAborted = false; 229 249 IsPaused = false; … … 234 254 235 255 /// <summary> 236 /// Clears stacks 256 /// Clears stacks and custom expressions 237 257 /// </summary> 238 258 public void Clear() { 239 ExecCounter = 0;240 241 259 ExecStack.Clear(); 242 260 CodeStack.Clear(); 243 244 261 NameStack.Clear(); 245 262 BooleanStack.Clear(); … … 251 268 252 269 [MethodImpl(MethodImplOptions.AggressiveInlining)] 253 private void Interpret() {254 while (IsRunning) {270 private void Run() { 271 while (IsRunning) 255 272 DoStep(); 256 ExecCounter++; 257 } 258 259 Finally(); 273 274 if (IsCompleted) 275 Finally(); 260 276 } 261 277 262 278 [MethodImpl(MethodImplOptions.AggressiveInlining)] 263 279 private void Finally() { 264 if (!IsCompleted) 265 return; 266 267 if (Configuration.TopLevelPopCode && (CodeStack.Count > 0)) 280 if (Configuration.TopLevelPopCode && !CodeStack.IsEmpty) 268 281 CodeStack.Pop(); 282 283 PoolContainer.DisposePools(); 269 284 } 270 285 … … 272 287 private Expression last; 273 288 private bool DoStep() { 289 double[] bk; 290 if (!FloatStack.IsEmpty) { 291 bk = FloatStack.Peek(Math.Min(FloatStack.Count, 3)); 292 } 293 274 294 var expression = ExecStack.Pop(); 275 276 //if (ExecStack.Any(e => e == null)) {277 // throw new InvalidProgramException();278 //}279 280 295 var succ = expression.Eval(this); 281 //last = expression; 282 296 297 if ((ExecStack.Count > 0 && ExecStack.Top == null) || 298 (CodeStack.Count > 0 && CodeStack.Top == null) || 299 FloatStack.Any(x => double.IsNaN(x) || double.IsInfinity(x)) || 300 StringStack.Count > 0 && StringStack.Any(x => x == null)) { 301 Debugger.Break(); 302 } 303 304 ExecCounter++; 305 last = expression; 283 306 return succ; 284 307 } … … 286 309 [MethodImpl(MethodImplOptions.AggressiveInlining)] 287 310 private bool DoStep() { 311 ExecCounter++; 288 312 return ExecStack.Pop().Eval(this); 289 313 } … … 291 315 292 316 private Task InterpretAsync() { 293 currentTask = Task.Run(() => Interpret());317 currentTask = Task.Run(() => this.Run()); 294 318 295 319 return currentTask; 296 320 } 297 321 298 private void PrintStack<T>(string name, IStack<T> stack) { 299 if (stack.IsEmpty) return; 300 Console.WriteLine("--------- {0} ---------\n{1}\n", name, stack); 322 public virtual void Dispose() { 323 PoolContainer.DisposePools(); 301 324 } 302 325 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreterPool.cs
r14777 r14834 14 14 public readonly ManagedPoolProvider<PooledList<Expression>> ExpressionListPoolProvider; 15 15 16 17 16 public PushInterpreterPool(IReadOnlyPushConfiguration config = null) 18 17 : this(Environment.ProcessorCount * 2, 1024, null, config) { 19 18 } 20 19 21 public PushInterpreterPool(int size, int p ushProgramPoolPartitionSize, int? maxPartitionCount = null, IReadOnlyPushConfiguration config = null) {20 public PushInterpreterPool(int size, int poolPartitionSize, int? maxPartitionCount = null, IReadOnlyPushConfiguration config = null) { 22 21 PushGpConfiguration = config ?? new PushConfiguration(); 23 22 24 PushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(p ushProgramPoolPartitionSize, () => new PushProgram(), maxPartitionCount);25 LoopStatePoolProvider = new ManagedPoolProvider<LoopState>(p ushProgramPoolPartitionSize, () => new LoopState(), maxPartitionCount);26 ExpressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(p ushProgramPoolPartitionSize * 2, () => new PooledList<Expression>(), maxPartitionCount);23 PushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(poolPartitionSize, () => new PushProgram(), maxPartitionCount); 24 LoopStatePoolProvider = new ManagedPoolProvider<LoopState>(poolPartitionSize, () => new LoopState(), maxPartitionCount); 25 ExpressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(poolPartitionSize * 2, () => new PooledList<Expression>(), maxPartitionCount); 27 26 28 27 pool = new ObjectPool<PooledPushInterpreter>(() => { … … 35 34 36 35 public PooledPushInterpreter Create(IRandom random = null) { 37 var interpreter = this.pool.Allocate();38 interpreter.Random = random ?? new FastRandom();36 var interpreter = pool.Allocate(); 37 interpreter.Random = random ?? new MersenneTwister(); 39 38 40 39 return interpreter; … … 42 41 43 42 public void Free(PooledPushInterpreter interpreter) { 44 this.pool.Free(interpreter);43 pool.Free(interpreter); 45 44 } 46 45 }
Note: See TracChangeset
for help on using the changeset viewer.