Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/InterpreterPoolContainer.cs @ 18242

Last change on this file since 18242 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 3.6 KB
Line 
1using System;
2using System.Collections.Generic;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
5  public class InterpreterPoolContainer {
6    private readonly ManagedPoolProvider<PushProgram> pushProgramPoolProvider;
7    private readonly ManagedPoolProvider<PooledList<Expression>> expressionListPoolProvider;
8
9    private readonly IDictionary<Type, ManagedPoolProvider<Expression>> statefulExpressionPoolProviders
10      = new Dictionary<Type, ManagedPoolProvider<Expression>>();
11
12    private readonly IDictionary<Type, IManagedPool<Expression>> statefulExpressionPools
13      = new Dictionary<Type, IManagedPool<Expression>>();
14
15    public InterpreterPoolContainer(int partitionSize = 512, int maxPartitionCount = 1024) {
16      pushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(partitionSize, () => new PushProgram(), maxPartitionCount);
17      expressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(partitionSize, () => new PooledList<Expression>(), maxPartitionCount);
18
19      InitStatefulExpressionPools(partitionSize, maxPartitionCount);
20    }
21
22    public InterpreterPoolContainer(
23      ManagedPoolProvider<PushProgram> pushProgramPoolProvider,
24      ManagedPoolProvider<PooledList<Expression>> expressionListPoolProvider) {
25      this.pushProgramPoolProvider = pushProgramPoolProvider;
26      this.expressionListPoolProvider = expressionListPoolProvider;
27
28      InitStatefulExpressionPools(1024, 2048);
29    }
30
31    private IManagedPool<PushProgram> pushProgramPool;
32    public IManagedPool<PushProgram> PushProgramPool {
33      get {
34        if (pushProgramPool == null) pushProgramPool = pushProgramPoolProvider.CreatePool();
35        return pushProgramPool;
36      }
37    }
38
39    private IManagedPool<PooledList<Expression>> expressionListPool;
40    public IManagedPool<PooledList<Expression>> ExpressionListPool {
41      get {
42        if (expressionListPool == null) expressionListPool = expressionListPoolProvider.CreatePool();
43        return expressionListPool;
44      }
45    }
46
47    private void InitStatefulExpressionPools(int partitionSize, int maxPartitionCount) {
48      foreach (var type in ExpressionTable.StatefulExpressionTypes) {
49        statefulExpressionPoolProviders.Add(type, new ManagedPoolProvider<Expression>(
50          partitionSize,
51          ExpressionTable.StatefulExpressionFactory[type],
52          maxPartitionCount));
53
54        statefulExpressionPools.Add(type, null);
55      }
56    }
57
58    public T GetStatefulExpression<T>() where T : Expression {
59      var type = typeof(T);
60      if (statefulExpressionPools[type] == null)
61        statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool();
62
63      return (T)statefulExpressionPools[type].Get();
64    }
65
66    public IManagedPool<Expression> GetStatefulExpressionPool<T>() where T : Expression {
67      var type = typeof(T);
68      return statefulExpressionPools[type] ?? (statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool());
69    }
70
71    public void DisposePools() {
72      if (pushProgramPool != null) {
73        pushProgramPool.Dispose();
74        pushProgramPool = null;
75      }
76
77      if (expressionListPool != null) {
78        expressionListPool.Dispose();
79        expressionListPool = null;
80      }
81
82      for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Count; i++) {
83        var type = ExpressionTable.StatefulExpressionTypes[i];
84        var pool = statefulExpressionPools[type];
85
86        if (pool != null) {
87          pool.Dispose();
88          statefulExpressionPools[type] = null;
89        }
90      }
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.