Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/10/17 00:27:31 (8 years ago)
Author:
pkimmesw
Message:

#2665 LexicaseSelector, Performance improvements, UI Fixes, Debugger only shows used stacks, fixed Debugger stepping, Added vector expressions, ERCOptions,

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  
    77
    88  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
    9   using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
    109
    1110  using Stack;
     
    1312  public interface IPushInterpreter {
    1413    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; }
    2526    IDictionary<string, Expression> CustomExpressions { get; }
    26 
    2727    IReadOnlyPushConfiguration Configuration { get; }
    28 
    29     InterpreterPoolContainer PoolContainer { get; }
    30 
    31     bool IsNameQuoteFlagSet { get; set; }
    32 
    3328    void Clear();
    3429    void Run(string code, bool stepwise = false);
    3530    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));
    3833    Task AbortAndResetAsync();
    3934    Task AbortAsync();
     
    4136    void Resume();
    4237    Task ResumeAsync();
    43 
    4438    bool Step();
    45     void PrintStacks();
    4639  }
    4740}
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/InterpreterPoolContainer.cs

    r14777 r14834  
    2222      expressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(partitionSize, () => new PooledList<Expression>(), maxPartitionCount);
    2323
    24       InitStatefulExpressionPools();
     24      InitStatefulExpressionPools(partitionSize, maxPartitionCount);
    2525    }
    2626
     
    3333      this.expressionListPoolProvider = expressionListPoolProvider;
    3434
    35       InitStatefulExpressionPools();
     35      InitStatefulExpressionPools(1024, 2048);
    3636    }
    3737
    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    }
    4147
    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) {
    4369      for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Length; i++) {
    4470        var type = ExpressionTable.StatefulExpressionTypes[i];
    4571        var name = ExpressionTable.TypeToNameTable[type];
    46         var factory = ExpressionTable.StatefulExpressionFactory[name];
    4772
    48         statefulExpressionPoolProviders.Add(type, new ManagedPoolProvider<Expression>(1024, factory));
     73        statefulExpressionPoolProviders.Add(type, new ManagedPoolProvider<Expression>(
     74          partitionSize,
     75          ExpressionTable.StatefulExpressionFactory[name],
     76          maxPartitionCount));
    4977
    5078        statefulExpressionPools.Add(type, null);
     
    5280    }
    5381
    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();
    5688    }
    5789
    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      }
    6195
    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      }
    66105
    67106      for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Length; i++) {
    68107        var type = ExpressionTable.StatefulExpressionTypes[i];
    69         statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool();
    70       }
    71     }
     108        var pool = statefulExpressionPools[type];
    72109
    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        }
    81114      }
    82115    }
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PooledPushInterpreter.cs

    r14777 r14834  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter {
    2   using System;
    32
    43  using HeuristicLab.Core;
    54  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
    65
    7   public class PooledPushInterpreter : PushInterpreter, IDisposable {
     6  public class PooledPushInterpreter : PushInterpreter {
    87
    98    private readonly PushInterpreterPool pool;
     
    1413    }
    1514
    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);
    2019    }
    2120  }
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreter.cs

    r14777 r14834  
    22  using System;
    33  using System.Collections.Generic;
    4 #if DEBUG
    5 #endif
    64  using System.Runtime.CompilerServices;
    75  using System.Threading;
    86  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 {
    1721    private Task currentTask;
    1822
     
    2731
    2832      CodeStack = new PushStack<Expression>(Configuration.EvalPushLimit) {
    29         IsEnabled = Configuration.IsCodeStackEnabled
     33        IsEnabled = Configuration.EnabledStacks[StackTypes.Code]
    3034      };
    3135
    3236      NameStack = new PushStack<string> {
    33         IsEnabled = Configuration.IsNameStackEnabled
     37        IsEnabled = Configuration.EnabledStacks[StackTypes.Code]
    3438      };
    3539      BooleanStack = new PushStack<bool> {
    36         IsEnabled = Configuration.IsBooleanStackEnabled
     40        IsEnabled = Configuration.EnabledStacks[StackTypes.Boolean]
    3741      };
    3842      IntegerStack = new PushStack<long> {
    39         IsEnabled = Configuration.IsIntegerStackEnabled
     43        IsEnabled = Configuration.EnabledStacks[StackTypes.Integer]
    4044      };
    4145      FloatStack = new PushStack<double> {
    42         IsEnabled = Configuration.IsFloatStackEnabled
     46        IsEnabled = Configuration.EnabledStacks[StackTypes.Float]
    4347      };
    4448      CharStack = new PushStack<char> {
    45         IsEnabled = Configuration.IsCharStackEnabled
     49        IsEnabled = Configuration.EnabledStacks[StackTypes.Char]
    4650      };
    4751      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]
    4965      };
    5066
     
    8197      get
    8298      {
    83         return ExecStack.Count == 0;
     99        return ExecStack.IsEmpty || ExecCounter >= Configuration.EvalPushLimit;
    84100      }
    85101    }
     
    97113    public IReadOnlyPushConfiguration Configuration { get; protected set; }
    98114
    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; }
    112150
    113151    public IDictionary<string, Expression> CustomExpressions { get; private set; }
     
    115153    public bool IsNameQuoteFlagSet { get; set; }
    116154
    117     public Task RunAsync(string code, bool stepwise = false, CancellationToken token = default(CancellationToken)) {
     155    public Task RunAsync(string code, CancellationToken token = default(CancellationToken)) {
    118156      var program = PushParser.Parse(code);
    119 
    120       currentTask = RunAsync(program, stepwise, token);
    121 
     157      currentTask = RunAsync(program, token);
    122158      return currentTask;
    123159    }
    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);
    129162    }
    130163
     
    134167    }
    135168
    136     public void Run(Expression program, bool stepwise = false) {
    137       PoolContainer.CreatePools();
    138 
     169    public void Run(Expression expression, bool stepwise = false) {
    139170      IsPaused = stepwise;
    140171
    141172      /* 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();
    153185    }
    154186
     
    188220
    189221      IsPaused = false;
    190       Interpret();
     222      this.Run();
    191223    }
    192224
     
    196228
    197229      var successful = DoStep();
    198       Finally();
     230
     231      if (IsCompleted)
     232        Finally();
    199233
    200234      return successful;
     
    204238    public void Step(int count) {
    205239      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       }
    222240    }
    223241
     
    226244    /// </summary>
    227245    public void Reset() {
     246      ExecCounter = 0;
     247
    228248      IsAborted = false;
    229249      IsPaused = false;
     
    234254
    235255    /// <summary>
    236     /// Clears stacks
     256    /// Clears stacks and custom expressions
    237257    /// </summary>
    238258    public void Clear() {
    239       ExecCounter = 0;
    240 
    241259      ExecStack.Clear();
    242260      CodeStack.Clear();
    243 
    244261      NameStack.Clear();
    245262      BooleanStack.Clear();
     
    251268
    252269    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    253     private void Interpret() {
    254       while (IsRunning) {
     270    private void Run() {
     271      while (IsRunning)
    255272        DoStep();
    256         ExecCounter++;
    257       }
    258 
    259       Finally();
     273
     274      if (IsCompleted)
     275        Finally();
    260276    }
    261277
    262278    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    263279    private void Finally() {
    264       if (!IsCompleted)
    265         return;
    266 
    267       if (Configuration.TopLevelPopCode && (CodeStack.Count > 0))
     280      if (Configuration.TopLevelPopCode && !CodeStack.IsEmpty)
    268281        CodeStack.Pop();
     282
     283      PoolContainer.DisposePools();
    269284    }
    270285
     
    272287    private Expression last;
    273288    private bool DoStep() {
     289      double[] bk;
     290      if (!FloatStack.IsEmpty) {
     291        bk = FloatStack.Peek(Math.Min(FloatStack.Count, 3));
     292      }
     293
    274294      var expression = ExecStack.Pop();
    275 
    276       //if (ExecStack.Any(e => e == null)) {
    277       //  throw new InvalidProgramException();
    278       //}
    279 
    280295      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;
    283306      return succ;
    284307    }
     
    286309    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    287310    private bool DoStep() {
     311      ExecCounter++;
    288312      return ExecStack.Pop().Eval(this);
    289313    }
     
    291315
    292316    private Task InterpretAsync() {
    293       currentTask = Task.Run(() => Interpret());
     317      currentTask = Task.Run(() => this.Run());
    294318
    295319      return currentTask;
    296320    }
    297321
    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();
    301324    }
    302325  }
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreterPool.cs

    r14777 r14834  
    1414    public readonly ManagedPoolProvider<PooledList<Expression>> ExpressionListPoolProvider;
    1515
    16 
    1716    public PushInterpreterPool(IReadOnlyPushConfiguration config = null)
    1817      : this(Environment.ProcessorCount * 2, 1024, null, config) {
    1918    }
    2019
    21     public PushInterpreterPool(int size, int pushProgramPoolPartitionSize, int? maxPartitionCount = null, IReadOnlyPushConfiguration config = null) {
     20    public PushInterpreterPool(int size, int poolPartitionSize, int? maxPartitionCount = null, IReadOnlyPushConfiguration config = null) {
    2221      PushGpConfiguration = config ?? new PushConfiguration();
    2322
    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);
     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);
    2726
    2827      pool = new ObjectPool<PooledPushInterpreter>(() => {
     
    3534
    3635    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();
    3938
    4039      return interpreter;
     
    4241
    4342    public void Free(PooledPushInterpreter interpreter) {
    44       this.pool.Free(interpreter);
     43      pool.Free(interpreter);
    4544    }
    4645  }
Note: See TracChangeset for help on using the changeset viewer.