Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Individual/InduvidualMapper.cs @ 17432

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

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

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