Changeset 14777 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter
- Timestamp:
- 03/23/17 01:11:18 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/IPushInterpreter.cs
r14746 r14777 20 20 IStack<long> IntegerStack { get; } 21 21 IStack<double> FloatStack { get; } 22 IStack<char> CharStack { get; } 23 IStack<string> StringStack { get; } 22 24 23 25 IDictionary<string, Expression> CustomExpressions { get; } 24 26 25 IRead onlyPushConfiguration Configuration { get; }27 IReadOnlyPushConfiguration Configuration { get; } 26 28 27 I ManagedPool<PushProgram> PushProgramPool{ get; }29 InterpreterPoolContainer PoolContainer { get; } 28 30 29 31 bool IsNameQuoteFlagSet { get; set; } … … 32 34 void Run(string code, bool stepwise = false); 33 35 void Run(Expression expression, bool stepwise = false); 34 //void Run(PushProgram program, bool stepwise = false);35 //Task RunAsync(PushProgram program, bool paused = false, CancellationToken token = default(CancellationToken));36 36 Task RunAsync(Expression expression, bool paused = false, CancellationToken token = default(CancellationToken)); 37 37 Task RunAsync(string code, bool paused = false, CancellationToken token = default(CancellationToken)); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PooledPushInterpreter.cs
r14747 r14777 4 4 using HeuristicLab.Core; 5 5 using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; 6 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;7 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;8 6 9 7 public class PooledPushInterpreter : PushInterpreter, IDisposable { … … 11 9 private readonly PushInterpreterPool pool; 12 10 13 public PooledPushInterpreter(PushInterpreterPool pool, PushConfiguration config, ManagedPoolProvider<PushProgram> pushProgramPoolProvider, IRandom random = null)14 : base(config, random, p ushProgramPoolProvider) {11 public PooledPushInterpreter(PushInterpreterPool pool, IReadOnlyPushConfiguration config, InterpreterPoolContainer poolContainer, IRandom random = null) 12 : base(config, random, poolContainer) { 15 13 this.pool = pool; 16 14 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreter.cs
r14747 r14777 3 3 using System.Collections.Generic; 4 4 #if DEBUG 5 using System.Linq;6 5 #endif 7 6 using System.Runtime.CompilerServices; … … 10 9 using HeuristicLab.Core; 11 10 using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; 12 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;13 11 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 14 12 using HeuristicLab.Problems.ProgramSynthesis.Push.Parser; … … 19 17 private Task currentTask; 20 18 21 public PushInterpreter(IReadonlyPushConfiguration config = null, IRandom random = null, ManagedPoolProvider<PushProgram> pushProgramPoolProvider = null) { 19 20 public PushInterpreter(IReadOnlyPushConfiguration config = null, IRandom random = null, InterpreterPoolContainer poolContainer = null) { 22 21 Random = random ?? new FastRandom(); 23 22 … … 43 42 IsEnabled = Configuration.IsFloatStackEnabled 44 43 }; 44 CharStack = new PushStack<char> { 45 IsEnabled = Configuration.IsCharStackEnabled 46 }; 47 StringStack = new PushStack<string> { 48 IsEnabled = Configuration.IsStringStackEnabled 49 }; 45 50 46 51 CustomExpressions = new Dictionary<string, Expression>(); 47 52 48 PushProgramPoolProvider = pushProgramPoolProvider ?? new ManagedPoolProvider<PushProgram>(1024); 49 50 if (pushProgramPoolProvider == null) 51 { 52 PushProgramPoolProvider.InitDummyPartition(() => new PushProgram()); 53 } 54 } 55 56 public PushInterpreter(int seed, IReadonlyPushConfiguration config = null) 53 PoolContainer = poolContainer ?? new InterpreterPoolContainer(); 54 } 55 56 public PushInterpreter(int seed, IReadOnlyPushConfiguration config = null) 57 57 : this(config, new FastRandom(seed)) { 58 58 } … … 93 93 } 94 94 95 private readonly ManagedPoolProvider<PushProgram> PushProgramPoolProvider; 96 public IManagedPool<PushProgram> PushProgramPool { get; private set; } 97 98 public IReadonlyPushConfiguration Configuration { get; protected set; } 95 public InterpreterPoolContainer PoolContainer { get; private set; } 96 97 public IReadOnlyPushConfiguration Configuration { get; protected set; } 99 98 100 99 public IStack<Expression> CodeStack { get; private set; } … … 109 108 110 109 public IStack<double> FloatStack { get; private set; } 110 public IStack<char> CharStack { get; private set; } 111 public IStack<string> StringStack { get; private set; } 111 112 112 113 public IDictionary<string, Expression> CustomExpressions { get; private set; } … … 133 134 } 134 135 135 136 136 public void Run(Expression program, bool stepwise = false) { 137 using (PushProgramPool = PushProgramPoolProvider.CreatePool()) { 138 IsPaused = stepwise; 139 140 /* Push top expression so the loop is able to enter 141 * If the top expression is a single statement then the loop has nothing to do 142 * Otherwise the expand expression will be evaluated and pushes code onto the EXEC stack */ 143 ExecStack.Push(program); 144 145 if (Configuration.TopLevelPushCode) CodeStack.Insert(0, program); 146 147 // run top expression 148 DoStep(); 149 150 Interpret(); 151 } 137 PoolContainer.CreatePools(); 138 139 IsPaused = stepwise; 140 141 /* 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(); 152 153 } 153 154 … … 157 158 IsAborted = true; 158 159 159 if (PushProgramPool != null) PushProgramPool.Dispose();160 PoolContainer.DisposePools(); 160 161 161 162 if (currentTask != null) await currentTask; … … 213 214 PrintStack("INTEGER", IntegerStack); 214 215 215 if (CustomExpressions.Count == 0) return; 216 Console.WriteLine("--------- Custom Expressions ---------"); 217 foreach (var ce in CustomExpressions) { 218 Console.WriteLine("{0}: {1}", ce.Key, ce.Value); 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 } 219 221 } 220 222 } … … 272 274 var expression = ExecStack.Pop(); 273 275 274 if (ExecStack.Any(e => e == null)) {275 throw new InvalidProgramException();276 }276 //if (ExecStack.Any(e => e == null)) { 277 // throw new InvalidProgramException(); 278 //} 277 279 278 280 var succ = expression.Eval(this); 279 last = expression;281 //last = expression; 280 282 281 283 return succ; -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreterPool.cs
r14747 r14777 9 9 public class PushInterpreterPool { 10 10 private readonly ObjectPool<PooledPushInterpreter> pool; 11 private ManagedPoolProvider<PushProgram> pushProgramPoolProvider;12 11 13 public PushInterpreterPool(PushConfiguration config = null) 12 public readonly ManagedPoolProvider<PushProgram> PushProgramPoolProvider; 13 public readonly ManagedPoolProvider<LoopState> LoopStatePoolProvider; 14 public readonly ManagedPoolProvider<PooledList<Expression>> ExpressionListPoolProvider; 15 16 17 public PushInterpreterPool(IReadOnlyPushConfiguration config = null) 14 18 : this(Environment.ProcessorCount * 2, 1024, null, config) { 15 19 } 16 20 17 public PushInterpreterPool(int size, int pushProgramPoolPartitionSize, int? maxPartitionCount = null, PushConfiguration config = null) {21 public PushInterpreterPool(int size, int pushProgramPoolPartitionSize, int? maxPartitionCount = null, IReadOnlyPushConfiguration config = null) { 18 22 PushGpConfiguration = config ?? new PushConfiguration(); 19 23 20 pushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(pushProgramPoolPartitionSize, maxPartitionCount); 21 pushProgramPoolProvider.InitDummyPartition(() => new PushProgram()); 24 PushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(pushProgramPoolPartitionSize, () => new PushProgram(), maxPartitionCount); 25 LoopStatePoolProvider = new ManagedPoolProvider<LoopState>(pushProgramPoolPartitionSize, () => new LoopState(), maxPartitionCount); 26 ExpressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(pushProgramPoolPartitionSize * 2, () => new PooledList<Expression>(), maxPartitionCount); 22 27 23 pool = new ObjectPool<PooledPushInterpreter>(() => new PooledPushInterpreter(this, PushGpConfiguration, pushProgramPoolProvider), size); 28 pool = new ObjectPool<PooledPushInterpreter>(() => { 29 var poolContainer = new InterpreterPoolContainer(PushProgramPoolProvider, LoopStatePoolProvider, ExpressionListPoolProvider); 30 return new PooledPushInterpreter(this, PushGpConfiguration, poolContainer); 31 }, size); 24 32 } 25 33 26 public PushConfiguration PushGpConfiguration { get; private set; }34 public IReadOnlyPushConfiguration PushGpConfiguration { get; private set; } 27 35 28 public PooledPushInterpreter GetInstance(IRandom random = null) {36 public PooledPushInterpreter Create(IRandom random = null) { 29 37 var interpreter = this.pool.Allocate(); 30 38 interpreter.Random = random ?? new FastRandom();
Note: See TracChangeset
for help on using the changeset viewer.