Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreterPool.cs @ 15032

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

#2665 Fixed bias 0 issue, PushExpressionFrequencyAnalyzer, Fixed probability for ERC settings, Fixed enable/disable instructions, Added expression descriptions

File size: 2.1 KB
Line 
1namespace 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<PooledList<Expression>> ExpressionListPoolProvider;
14
15    public PushInterpreterPool(IReadOnlyPushConfiguration config = null)
16      : this(Environment.ProcessorCount * 4, 1024, null, config) {
17    }
18
19    public PushInterpreterPool(int size, int poolPartitionSize, int? maxPartitionCount = null, IReadOnlyPushConfiguration config = null) {
20      PushConfiguration = config ?? new PushConfiguration();
21
22      PushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(poolPartitionSize, () => new PushProgram(), maxPartitionCount);
23      ExpressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(poolPartitionSize, () => new PooledList<Expression>(), maxPartitionCount);
24
25      pool = new ObjectPool<PooledPushInterpreter>(() => {
26        var poolContainer = new InterpreterPoolContainer(PushProgramPoolProvider, ExpressionListPoolProvider);
27        return new PooledPushInterpreter(this, PushConfiguration, poolContainer);
28      }, size);
29    }
30
31    public IReadOnlyPushConfiguration PushConfiguration { get; private set; }
32
33    public PooledPushInterpreter Create(IRandom random = null) {
34      var interpreter = pool.Allocate();
35      interpreter.Random = random ?? new MersenneTwister();
36      interpreter.Reset();
37
38      return interpreter;
39    }
40
41    public void Free(PooledPushInterpreter interpreter) {
42      interpreter.Random = null;
43      pool.Free(interpreter);
44    }
45
46    public void Clear() {
47      PushProgramPoolProvider.Clear();
48      ExpressionListPoolProvider.Clear();
49    }
50  }
51}
Note: See TracBrowser for help on using the repository browser.