using System; using HeuristicLab.Core; using HeuristicLab.Encodings.IntegerVectorEncoding; namespace HeuristicLab.Problems.ProgramSynthesis { public class ListCheck { public PooledList List { get; set; } public IManagedPool> Pool { get; set; } public IndividualMapper Mapper { get; set; } } public class IndividualMapper : IDisposable { private static readonly ObjectPool MapperPool = new ObjectPool(() => new IndividualMapper()); private static readonly ManagedPoolProvider PushProgramPoolProvider = new ManagedPoolProvider(1000, () => new PushProgram()); private static readonly ManagedPoolProvider> ExpressionListPoolProvider = new ManagedPoolProvider>(1000, () => new PooledList()); private readonly IManagedPool pushProgramPool = PushProgramPoolProvider.CreatePool(); private readonly IManagedPool> expressionListPool = ExpressionListPoolProvider.CreatePool(); private IndividualMapper() { } public PushProgram FromPlush( IntegerVector vector, ref int currentIndex, ref int close, int depth, IReadOnlyPushConfiguration config, IRandom random) { if (currentIndex >= vector.Length) return PushProgram.Empty; var expressions = expressionListPool.Get(); for (; currentIndex < vector.Length; currentIndex++) { var expression = CodeGeneratorUtils.MapToExpression( vector[currentIndex] % config.EnabledExpressions.Count, random, config.ErcOptions, config); var expressionType = expression.GetType(); if (config.CloseBiasLevel > 0) { close += random.NextBiased(0, config.MaxClose, config.CloseBiasLevel); // check if expression requires additional blocks if (ExpressionTable.TypeToAttributeTable.ContainsKey(expressionType)) { var attr = ExpressionTable.TypeToAttributeTable[expressionType]; for (var blockIdx = 0u; blockIdx < attr.RequiredBlockCount && currentIndex < vector.Length; blockIdx++) { if (close != 0) { close--; expressions.Add(PushProgram.Empty); } else { currentIndex++; var subProgram = FromPlush(vector, ref currentIndex, ref close, depth + 1, config, random); var subExpression = subProgram.Count == 1 ? subProgram.Expressions[0] : subProgram; expressions.Add(subExpression); } } } } expressions.Add(expression); if (close > 0 && depth > 0) { close--; break; } if (depth == 0) { close = 0; } } return PushProgram.Create(pushProgramPool, expressions); } public static IndividualMapper Create() { return MapperPool.Allocate(); } public static void Reset() { foreach (var mapper in MapperPool.Items) { mapper.expressionListPool.Release(); mapper.pushProgramPool.Release(); } } public static void Clear() { Reset(); PushProgramPoolProvider.Clear(); ExpressionListPoolProvider.Clear(); MapperPool.Clear(); } public void Dispose() { MapperPool.Free(this); } } }