1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter {
|
---|
2 | using System;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
|
---|
5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
|
---|
6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
7 | using HeuristicLab.Random;
|
---|
8 |
|
---|
9 | public class PushInterpreterPool {
|
---|
10 | private readonly ObjectPool<PooledPushInterpreter> pool;
|
---|
11 | private ManagedPoolProvider<PushProgram> pushProgramPoolProvider;
|
---|
12 |
|
---|
13 | public PushInterpreterPool(PushConfiguration config = null)
|
---|
14 | : this(Environment.ProcessorCount * 2, 1024, null, config) {
|
---|
15 | }
|
---|
16 |
|
---|
17 | public PushInterpreterPool(int size, int pushProgramPoolPartitionSize, int? maxPartitionCount = null, PushConfiguration config = null) {
|
---|
18 | PushGpConfiguration = config ?? new PushConfiguration();
|
---|
19 |
|
---|
20 | pushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(pushProgramPoolPartitionSize, maxPartitionCount);
|
---|
21 | pushProgramPoolProvider.InitDummyPartition(() => new PushProgram());
|
---|
22 |
|
---|
23 | pool = new ObjectPool<PooledPushInterpreter>(() => new PooledPushInterpreter(this, PushGpConfiguration, pushProgramPoolProvider), size);
|
---|
24 | }
|
---|
25 |
|
---|
26 | public PushConfiguration PushGpConfiguration { get; private set; }
|
---|
27 |
|
---|
28 | public PooledPushInterpreter GetInstance(IRandom random = null) {
|
---|
29 | var interpreter = this.pool.Allocate();
|
---|
30 | interpreter.Random = random ?? new FastRandom();
|
---|
31 |
|
---|
32 | return interpreter;
|
---|
33 | }
|
---|
34 |
|
---|
35 | public void Free(PooledPushInterpreter interpreter) {
|
---|
36 | this.pool.Free(interpreter);
|
---|
37 | }
|
---|
38 | }
|
---|
39 | }
|
---|