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 config = null,
|
---|
34 | IDictionary<string, Expression> customExpressions = null) {
|
---|
35 | if (maxPoints == 0)
|
---|
36 | return new Expression[0];
|
---|
37 |
|
---|
38 | random = random ?? new MersenneTwister();
|
---|
39 | config = config ?? 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.MapToExpression(
|
---|
46 | random,
|
---|
47 | config.ErcOptions,
|
---|
48 | config,
|
---|
49 | customExpressions);
|
---|
50 |
|
---|
51 | expressions.Add(expression);
|
---|
52 | }
|
---|
53 |
|
---|
54 | return expressions;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | }
|
---|
59 | }
|
---|