Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreterPool.cs @ 15866

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

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

File size: 1.9 KB
Line 
1using System;
2using HeuristicLab.Core;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
5  public class PushInterpreterPool {
6    private readonly ObjectPool<PooledPushInterpreter> pool;
7
8    public readonly ManagedPoolProvider<PushProgram> PushProgramPoolProvider;
9    public readonly ManagedPoolProvider<PooledList<Expression>> ExpressionListPoolProvider;
10
11    public PushInterpreterPool(IReadOnlyPushConfiguration config = null)
12      : this(Environment.ProcessorCount * 4, 1024, null, config) {
13    }
14
15    public PushInterpreterPool(int size, int poolPartitionSize, int? maxPartitionCount = null, IReadOnlyPushConfiguration config = null) {
16      PushConfiguration = config ?? new PushConfiguration();
17
18      PushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(poolPartitionSize, () => new PushProgram(), maxPartitionCount);
19      ExpressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(poolPartitionSize, () => new PooledList<Expression>(), maxPartitionCount);
20
21      pool = new ObjectPool<PooledPushInterpreter>(CreateInterpreter, size);
22    }
23
24    private PooledPushInterpreter CreateInterpreter() {
25      var poolContainer = new InterpreterPoolContainer(PushProgramPoolProvider, ExpressionListPoolProvider);
26      return new PooledPushInterpreter(this, PushConfiguration, poolContainer);
27    }
28
29    public IReadOnlyPushConfiguration PushConfiguration { get; private set; }
30
31    public PooledPushInterpreter Create(IRandom random = null) {
32      var interpreter = pool.Allocate();
33      interpreter.Reset(random);
34
35      return interpreter;
36    }
37
38    public void Free(PooledPushInterpreter interpreter) {
39      interpreter.ClearStacks();
40      pool.Free(interpreter);
41    }
42
43    public void Clear() {
44      PushProgramPoolProvider.Clear();
45      ExpressionListPoolProvider.Clear();
46      pool.Clear();
47    }
48  }
49}
Note: See TracBrowser for help on using the repository browser.