Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Individual/InduvidualMapper.cs @ 15289

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

#2665 Started Plush Encoding, Added Zero Error Individual Count Analyzer

File size: 3.6 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Individual {
2  using System;
3  using Configuration;
4  using Core;
5  using Data.Pool;
6  using Encodings.IntegerVectorEncoding;
7  using Expressions;
8  using Extensions;
9  using Generators.CodeGenerator;
10
11  public class ListCheck {
12    public PooledList<Expression> List { get; set; }
13    public IManagedPool<PooledList<Expression>> Pool { get; set; }
14    public IndividualMapper Mapper { get; set; }
15  }
16
17  public class IndividualMapper : IDisposable {
18    private static readonly ObjectPool<IndividualMapper> MapperPool = new ObjectPool<IndividualMapper>(() => new IndividualMapper());
19    private static readonly ManagedPoolProvider<PushProgram> PushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(1000, () => new PushProgram());
20    private static readonly ManagedPoolProvider<PooledList<Expression>> ExpressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(1000, () => new PooledList<Expression>());
21
22    private readonly IManagedPool<PushProgram> pushProgramPool = PushProgramPoolProvider.CreatePool();
23    private readonly IManagedPool<PooledList<Expression>> expressionListPool = ExpressionListPoolProvider.CreatePool();
24
25    private IndividualMapper() { }
26
27    public PushProgram FromPlush(
28      IntegerVector vector,
29      ref int currentIndex,
30      ref int close,
31      int depth,
32      IReadOnlyPushConfiguration config,
33      IRandom random) {
34
35      if (currentIndex >= vector.Length)
36        return PushProgram.Empty;
37
38      var expressions = expressionListPool.Get();
39
40      for (; currentIndex < vector.Length; currentIndex++) {
41        var expression = CodeGeneratorUtils.MapToExpression(
42          vector[currentIndex] % config.EnabledExpressions.Count,
43          random,
44          config.ErcOptions,
45          config);
46
47        var expressionType = expression.GetType();
48
49        if (config.ParenthesesCloseBiasLevel > 0) {
50          close += random.NextBiased(0, config.MaxParenthesesClose, config.ParenthesesCloseBiasLevel);
51
52          // check if expression requires additional blocks
53          if (ExpressionTable.TypeToAttributeTable.ContainsKey(expressionType)) {
54            var attr = ExpressionTable.TypeToAttributeTable[expressionType];
55
56            for (var blockIdx = 0u; blockIdx < attr.ExecIn && currentIndex < vector.Length; blockIdx++) {
57              if (close != 0) {
58                close--;
59                expressions.Add(PushProgram.Empty);
60              } else {
61                currentIndex++;
62                var subProgram = FromPlush(vector, ref currentIndex, ref close, depth + 1, config, random);
63                var subExpression = subProgram.Count == 1 ? subProgram.Expressions[0] : subProgram;
64
65                expressions.Add(subExpression);
66              }
67            }
68          }
69        }
70
71        expressions.Add(expression);
72
73        if (close > 0 && depth > 0) {
74          close--;
75          break;
76        }
77
78        if (depth == 0) {
79          close = 0;
80        }
81      }
82
83      return PushProgram.Create(pushProgramPool, expressions);
84    }
85
86    public static IndividualMapper Create() {
87      return MapperPool.Allocate();
88    }
89
90    public static void Reset() {
91      foreach (var mapper in MapperPool.Items) {
92        mapper.expressionListPool.Release();
93        mapper.pushProgramPool.Release();
94      }
95    }
96
97    public static void Clear() {
98      Reset();
99
100      PushProgramPoolProvider.Clear();
101      ExpressionListPoolProvider.Clear();
102
103      MapperPool.Clear();
104    }
105
106    public void Dispose() {
107      MapperPool.Free(this);
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.