Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/InterpreterPoolContainer.cs @ 14952

Last change on this file since 14952 was 14952, checked in by pkimmesw, 7 years ago

#2665 Added IsNoop to Expression, Made Expressions storable, Fixed Debugger, Fixed and improved problem data and result visualisation, Added custom ErcOption view, Added problem difficulty to problem data name

File size: 4.5 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter {
2  using System;
3  using System.Collections.Generic;
4
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
7
8  public class InterpreterPoolContainer {
9    private readonly ManagedPoolProvider<PushProgram> pushProgramPoolProvider;
10    private readonly ManagedPoolProvider<LoopState> loopStatePoolProvider;
11    private readonly ManagedPoolProvider<PooledList<Expression>> expressionListPoolProvider;
12
13    private readonly IDictionary<Type, ManagedPoolProvider<Expression>> statefulExpressionPoolProviders
14      = new Dictionary<Type, ManagedPoolProvider<Expression>>();
15
16    private readonly IDictionary<Type, IManagedPool<Expression>> statefulExpressionPools
17      = new Dictionary<Type, IManagedPool<Expression>>();
18
19    public InterpreterPoolContainer(int partitionSize = 512, int maxPartitionCount = 1024) {
20      pushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(partitionSize, () => new PushProgram(), maxPartitionCount);
21      loopStatePoolProvider = new ManagedPoolProvider<LoopState>(partitionSize, () => new LoopState(), maxPartitionCount);
22      expressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(partitionSize, () => new PooledList<Expression>(), maxPartitionCount);
23
24      InitStatefulExpressionPools(partitionSize, maxPartitionCount);
25    }
26
27    public InterpreterPoolContainer(
28      ManagedPoolProvider<PushProgram> pushProgramPoolProvider,
29      ManagedPoolProvider<LoopState> loopStatePoolProvider,
30      ManagedPoolProvider<PooledList<Expression>> expressionListPoolProvider) {
31      this.pushProgramPoolProvider = pushProgramPoolProvider;
32      this.loopStatePoolProvider = loopStatePoolProvider;
33      this.expressionListPoolProvider = expressionListPoolProvider;
34
35      InitStatefulExpressionPools(1024, 2048);
36    }
37
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    }
47
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) {
69      foreach (var type in ExpressionTable.StatefulExpressionTypes) {
70        statefulExpressionPoolProviders.Add(type, new ManagedPoolProvider<Expression>(
71          partitionSize,
72          ExpressionTable.StatefulExpressionFactory[type],
73          maxPartitionCount));
74
75        statefulExpressionPools.Add(type, null);
76      }
77    }
78
79    public T GetStatefulExpression<T>() where T : Expression {
80      var type = typeof(T);
81      if (statefulExpressionPools[type] == null)
82        statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool();
83
84      return (T)statefulExpressionPools[type].Get();
85    }
86
87    public IManagedPool<Expression> GetStatefulExpressionPool<T>() where T : Expression {
88      var type = typeof(T);
89      return statefulExpressionPools[type] ?? (statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool());
90    }
91
92    public void DisposePools() {
93      if (pushProgramPool != null) {
94        pushProgramPool.Dispose();
95        pushProgramPool = null;
96      }
97
98      if (loopStatePool != null) {
99        loopStatePool.Dispose();
100        loopStatePool = null;
101      }
102
103      if (expressionListPool != null) {
104        expressionListPool.Dispose();
105        expressionListPool = null;
106      }
107
108      for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Count; i++) {
109        var type = ExpressionTable.StatefulExpressionTypes[i];
110        var pool = statefulExpressionPools[type];
111
112        if (pool != null) {
113          pool.Dispose();
114          statefulExpressionPools[type] = null;
115        }
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.