[14834] | 1 | using System.Collections.Generic;
|
---|
| 2 |
|
---|
| 3 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator {
|
---|
| 4 | using HeuristicLab.Core;
|
---|
| 5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
|
---|
| 6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
|
---|
| 7 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
| 8 | using HeuristicLab.Random;
|
---|
| 9 |
|
---|
| 10 | public class LinearCodeGenerator {
|
---|
| 11 | public static PushProgram RandomProgram(IManagedPool<PushProgram> pool, IManagedPool<PooledList<Expression>> expressionListPool, int maxPoints, IRandom random = null, IReadOnlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) {
|
---|
| 12 | var code = RandomCode(maxPoints, expressionListPool, random, pushGpConfiguration, customExpressions);
|
---|
| 13 |
|
---|
| 14 | return PushProgram.Create(pool, code);
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | public static PushProgram RandomProgram(IManagedPool<PooledList<Expression>> expressionListPool, int maxPoints, IRandom random = null, IReadOnlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) {
|
---|
| 18 | var code = RandomCode(maxPoints, expressionListPool, random, pushGpConfiguration, customExpressions);
|
---|
| 19 |
|
---|
| 20 | return new PushProgram(code);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | public static PushProgram RandomProgram(int maxPoints, IRandom random = null, IReadOnlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) {
|
---|
| 24 | var code = RandomCode(maxPoints, null, random, pushGpConfiguration, customExpressions);
|
---|
| 25 |
|
---|
| 26 | return new PushProgram(code);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public static IReadOnlyList<Expression> RandomCode(
|
---|
| 30 | int maxPoints,
|
---|
| 31 | IManagedPool<PooledList<Expression>> expressionListPool = null,
|
---|
| 32 | IRandom random = null,
|
---|
| 33 | IReadOnlyPushConfiguration pushConfiguration = null,
|
---|
| 34 | IDictionary<string, Expression> customExpressions = null) {
|
---|
| 35 | if (maxPoints == 0)
|
---|
| 36 | return new Expression[0];
|
---|
| 37 |
|
---|
| 38 | random = random ?? new FastRandom();
|
---|
| 39 | pushConfiguration = pushConfiguration ?? new PushConfiguration();
|
---|
| 40 |
|
---|
| 41 | var size = maxPoints <= 1 ? 1 : random.Next(1, maxPoints);
|
---|
| 42 | var expressions = expressionListPool == null ? new List<Expression>(size) : expressionListPool.Get();
|
---|
| 43 |
|
---|
| 44 | for (var i = 0; i < size; i++) {
|
---|
| 45 | var expression = CodeGeneratorUtils.CreateExpressionOrErc(
|
---|
| 46 | random,
|
---|
| 47 | pushConfiguration.EnabledExpressions,
|
---|
| 48 | pushConfiguration.ErcOptions,
|
---|
| 49 | customExpressions);
|
---|
| 50 |
|
---|
| 51 | expressions.Add(expression);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | return expressions;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | }
|
---|
| 59 | }
|
---|