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 |
|
---|
12 | public readonly ManagedPoolProvider<PushProgram> PushProgramPoolProvider;
|
---|
13 | public readonly ManagedPoolProvider<LoopState> LoopStatePoolProvider;
|
---|
14 | public readonly ManagedPoolProvider<PooledList<Expression>> ExpressionListPoolProvider;
|
---|
15 |
|
---|
16 | public PushInterpreterPool(IReadOnlyPushConfiguration config = null)
|
---|
17 | : this(Environment.ProcessorCount * 2, 1024, null, config) {
|
---|
18 | }
|
---|
19 |
|
---|
20 | public PushInterpreterPool(int size, int poolPartitionSize, int? maxPartitionCount = null, IReadOnlyPushConfiguration config = null) {
|
---|
21 | PushGpConfiguration = config ?? new PushConfiguration();
|
---|
22 |
|
---|
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);
|
---|
26 |
|
---|
27 | pool = new ObjectPool<PooledPushInterpreter>(() => {
|
---|
28 | var poolContainer = new InterpreterPoolContainer(PushProgramPoolProvider, LoopStatePoolProvider, ExpressionListPoolProvider);
|
---|
29 | return new PooledPushInterpreter(this, PushGpConfiguration, poolContainer);
|
---|
30 | }, size);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public IReadOnlyPushConfiguration PushGpConfiguration { get; private set; }
|
---|
34 |
|
---|
35 | public PooledPushInterpreter Create(IRandom random = null) {
|
---|
36 | var interpreter = pool.Allocate();
|
---|
37 | interpreter.Random = random ?? new MersenneTwister();
|
---|
38 |
|
---|
39 | return interpreter;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void Free(PooledPushInterpreter interpreter) {
|
---|
43 | pool.Free(interpreter);
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|