Changeset 15017 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter
- Timestamp:
- 06/01/17 09:28:34 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/Extensions.cs
r14914 r15017 34 34 35 35 foreach (StackTypes type in Enum.GetValues(stackTypesType)) { 36 if (!interpreter.Stacks.ContainsKey(type)) continue; 37 36 38 var stackName = Enum.GetName(stackTypesType, type); 37 39 var stack = interpreter.Stacks[type]; 38 40 39 if (stack.IsEmpty || !stack.IsEnabled) return; 40 Console.WriteLine("--------- {0} ---------\n{1}\n", stackName, stack); 41 if (stack.IsEmpty || !stack.IsEnabled) continue; 42 43 var stackString = string.Join(" ", interpreter.StringifyStack(type).Reverse()); 44 Console.WriteLine("--------- {0} ---------\n{1}\n", stackName, stackString); 41 45 } 42 46 -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/IPushInterpreter.cs
r14914 r15017 20 20 IPushStack<char> CharStack { get; } 21 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; }22 IPushStack<IReadOnlyList<long>> IntegerVectorStack { get; } 23 IPushStack<IReadOnlyList<double>> FloatVectorStack { get; } 24 IPushStack<IReadOnlyList<bool>> BooleanVectorStack { get; } 25 IPushStack<IReadOnlyList<string>> StringVectorStack { get; } 26 26 IPushStack<string> PrintStack { get; } 27 27 IDictionary<string, Expression> CustomExpressions { get; } … … 30 30 void Clear(); 31 31 void Reset(); 32 33 void Run(bool stepwise); 32 34 void Run(string code, bool stepwise = false); 33 35 void Run(Expression expression, bool stepwise = false); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/InterpreterPoolContainer.cs
r14952 r15017 8 8 public class InterpreterPoolContainer { 9 9 private readonly ManagedPoolProvider<PushProgram> pushProgramPoolProvider; 10 private readonly ManagedPoolProvider<LoopState> loopStatePoolProvider;11 10 private readonly ManagedPoolProvider<PooledList<Expression>> expressionListPoolProvider; 12 11 … … 19 18 public InterpreterPoolContainer(int partitionSize = 512, int maxPartitionCount = 1024) { 20 19 pushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(partitionSize, () => new PushProgram(), maxPartitionCount); 21 loopStatePoolProvider = new ManagedPoolProvider<LoopState>(partitionSize, () => new LoopState(), maxPartitionCount);22 20 expressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(partitionSize, () => new PooledList<Expression>(), maxPartitionCount); 23 21 … … 27 25 public InterpreterPoolContainer( 28 26 ManagedPoolProvider<PushProgram> pushProgramPoolProvider, 29 ManagedPoolProvider<LoopState> loopStatePoolProvider,30 27 ManagedPoolProvider<PooledList<Expression>> expressionListPoolProvider) { 31 28 this.pushProgramPoolProvider = pushProgramPoolProvider; 32 this.loopStatePoolProvider = loopStatePoolProvider;33 29 this.expressionListPoolProvider = expressionListPoolProvider; 34 30 … … 43 39 if (pushProgramPool == null) pushProgramPool = pushProgramPoolProvider.CreatePool(); 44 40 return pushProgramPool; 45 }46 }47 48 private IManagedPool<LoopState> loopStatePool;49 public IManagedPool<LoopState> LoopStatePool50 {51 get52 {53 if (loopStatePool == null) loopStatePool = loopStatePoolProvider.CreatePool();54 return loopStatePool;55 41 } 56 42 } … … 96 82 } 97 83 98 if (loopStatePool != null) {99 loopStatePool.Dispose();100 loopStatePool = null;101 }102 103 84 if (expressionListPool != null) { 104 85 expressionListPool.Dispose(); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreter.cs
r14952 r15017 5 5 using System.Threading; 6 6 using System.Threading.Tasks; 7 using Attributes; 7 8 using Configuration; 8 9 using Core; 9 10 using Expressions; 10 11 using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;12 13 11 using Parser; 14 12 using Random; 15 13 using Stack; 16 14 17 #if DEBUG18 using System.Diagnostics;19 using System.Linq;20 #endif21 15 22 16 public class PushInterpreter : IInternalPushInterpreter, IDisposable { … … 24 18 25 19 public PushInterpreter(IReadOnlyPushConfiguration config = null, IRandom random = null, InterpreterPoolContainer poolContainer = null) { 26 Random = random ?? new FastRandom(); 27 20 Random = random ?? new MersenneTwister(); 28 21 Configuration = config ?? new PushConfiguration(); 29 22 … … 59 52 }; 60 53 61 IntegerVectorStack = new PushStack< List<long>> {54 IntegerVectorStack = new PushStack<IReadOnlyList<long>> { 62 55 IsEnabled = Configuration.EnabledStacks[StackTypes.IntegerVector] 63 56 }; 64 57 65 FloatVectorStack = new PushStack< List<double>> {58 FloatVectorStack = new PushStack<IReadOnlyList<double>> { 66 59 IsEnabled = Configuration.EnabledStacks[StackTypes.FloatVector] 67 60 }; 68 61 69 BooleanVectorStack = new PushStack< List<bool>> {62 BooleanVectorStack = new PushStack<IReadOnlyList<bool>> { 70 63 IsEnabled = Configuration.EnabledStacks[StackTypes.BooleanVector] 71 64 }; 72 65 73 StringVectorStack = new PushStack< List<string>> {66 StringVectorStack = new PushStack<IReadOnlyList<string>> { 74 67 IsEnabled = Configuration.EnabledStacks[StackTypes.StringVector] 75 68 }; … … 88 81 { StackTypes.String, StringStack }, 89 82 { StackTypes.Name, NameStack }, 83 { StackTypes.Print, PrintStack }, 90 84 { StackTypes.IntegerVector, IntegerVectorStack }, 91 85 { StackTypes.FloatVector, FloatVectorStack }, 92 86 { StackTypes.BooleanVector, BooleanVectorStack }, 93 87 { StackTypes.StringVector, StringVectorStack }, 94 { StackTypes.Print, PrintStack },95 88 }; 96 89 … … 160 153 public IPushStack<string> StringStack { get; private set; } 161 154 [PushStack(StackTypes.IntegerVector)] 162 public IPushStack< List<long>> IntegerVectorStack { get; private set; }155 public IPushStack<IReadOnlyList<long>> IntegerVectorStack { get; private set; } 163 156 [PushStack(StackTypes.FloatVector)] 164 public IPushStack< List<double>> FloatVectorStack { get; private set; }157 public IPushStack<IReadOnlyList<double>> FloatVectorStack { get; private set; } 165 158 [PushStack(StackTypes.BooleanVector)] 166 public IPushStack< List<bool>> BooleanVectorStack { get; private set; }159 public IPushStack<IReadOnlyList<bool>> BooleanVectorStack { get; private set; } 167 160 [PushStack(StackTypes.StringVector)] 168 public IPushStack< List<string>> StringVectorStack { get; private set; }161 public IPushStack<IReadOnlyList<string>> StringVectorStack { get; private set; } 169 162 [PushStack(StackTypes.Print)] 170 163 public IPushStack<string> PrintStack { get; private set; } … … 186 179 var program = PushParser.Parse(code); 187 180 Run(program, stepwise); 181 } 182 183 public void Run(bool stepwise) { 184 Run(PushProgram.Empty, stepwise); 188 185 } 189 186 … … 322 319 } 323 320 324 #if DEBUG325 private Expression last;326 private bool DoStep() {327 double[] bk;328 if (!FloatStack.IsEmpty) {329 bk = FloatStack.Peek(Math.Min(FloatStack.Count, 3));330 }331 332 var expression = ExecStack.Pop();333 var succ = expression.TryEval(this);334 335 if ((ExecStack.Count > 0 && ExecStack.Top == null) ||336 (CodeStack.Count > 0 && CodeStack.Top == null) ||337 FloatStack.Any(x => double.IsNaN(x) || double.IsInfinity(x)) ||338 StringStack.Count > 0 && StringStack.Any(x => x == null)) {339 Debugger.Break();340 }341 342 ExecCounter++;343 last = expression;344 return succ;345 }346 #else347 321 [MethodImpl(MethodImplOptions.AggressiveInlining)] 348 322 private bool DoStep() { … … 350 324 return ExecStack.Pop().TryEval(this); 351 325 } 352 #endif353 326 354 327 [MethodImpl(MethodImplOptions.AggressiveInlining)] -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreterPool.cs
r14834 r15017 11 11 12 12 public readonly ManagedPoolProvider<PushProgram> PushProgramPoolProvider; 13 public readonly ManagedPoolProvider<LoopState> LoopStatePoolProvider;14 13 public readonly ManagedPoolProvider<PooledList<Expression>> ExpressionListPoolProvider; 15 14 … … 19 18 20 19 public PushInterpreterPool(int size, int poolPartitionSize, int? maxPartitionCount = null, IReadOnlyPushConfiguration config = null) { 21 Push GpConfiguration = config ?? new PushConfiguration();20 PushConfiguration = config ?? new PushConfiguration(); 22 21 23 22 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); 23 ExpressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(poolPartitionSize, () => new PooledList<Expression>(), maxPartitionCount); 26 24 27 25 pool = new ObjectPool<PooledPushInterpreter>(() => { 28 var poolContainer = new InterpreterPoolContainer(PushProgramPoolProvider, LoopStatePoolProvider,ExpressionListPoolProvider);29 return new PooledPushInterpreter(this, Push GpConfiguration, poolContainer);26 var poolContainer = new InterpreterPoolContainer(PushProgramPoolProvider, ExpressionListPoolProvider); 27 return new PooledPushInterpreter(this, PushConfiguration, poolContainer); 30 28 }, size); 31 29 } 32 30 33 public IReadOnlyPushConfiguration Push GpConfiguration { get; private set; }31 public IReadOnlyPushConfiguration PushConfiguration { get; private set; } 34 32 35 33 public PooledPushInterpreter Create(IRandom random = null) { … … 41 39 42 40 public void Free(PooledPushInterpreter interpreter) { 41 interpreter.Random = null; 43 42 pool.Free(interpreter); 43 } 44 45 public void Clear() { 46 PushProgramPoolProvider.Clear(); 47 ExpressionListPoolProvider.Clear(); 44 48 } 45 49 }
Note: See TracChangeset
for help on using the changeset viewer.