1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Generators {
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
|
---|
8 | using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
|
---|
9 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
10 | using HeuristicLab.Random;
|
---|
11 |
|
---|
12 | public class LinearCodeGenerator {
|
---|
13 | public static PushProgram RandomProgram(IManagedPool<PushProgram> pool, IManagedPool<PooledList<Expression>> expressionListPool, int maxPoints, IRandom random = null, IReadOnlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) {
|
---|
14 | var code = RandomCode(maxPoints, expressionListPool, random, pushGpConfiguration, customExpressions);
|
---|
15 |
|
---|
16 | return PushProgram.Create(pool, code);
|
---|
17 | }
|
---|
18 |
|
---|
19 | public static PushProgram RandomProgram(IManagedPool<PooledList<Expression>> expressionListPool, int maxPoints, IRandom random = null, IReadOnlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) {
|
---|
20 | var code = RandomCode(maxPoints, expressionListPool, random, pushGpConfiguration, customExpressions);
|
---|
21 |
|
---|
22 | return new PushProgram(code);
|
---|
23 | }
|
---|
24 |
|
---|
25 | public static PushProgram RandomProgram(int maxPoints, IRandom random = null, IReadOnlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) {
|
---|
26 | var code = RandomCode(maxPoints, null, random, pushGpConfiguration, customExpressions);
|
---|
27 |
|
---|
28 | return new PushProgram(code);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public static IReadOnlyList<Expression> RandomCode(
|
---|
32 | int maxPoints,
|
---|
33 | IManagedPool<PooledList<Expression>> expressionListPool = null,
|
---|
34 | IRandom random = null,
|
---|
35 | IReadOnlyPushConfiguration pushGpConfiguration = null,
|
---|
36 | IDictionary<string, Expression> customExpressions = null) {
|
---|
37 | if (maxPoints == 0)
|
---|
38 | return new Expression[0];
|
---|
39 |
|
---|
40 | random = random ?? new FastRandom();
|
---|
41 | pushGpConfiguration = pushGpConfiguration ?? new PushConfiguration();
|
---|
42 |
|
---|
43 | var size = maxPoints <= 1 ? 1 : random.Next(1, maxPoints);
|
---|
44 | var expressions = expressionListPool == null ? new List<Expression>(size) : expressionListPool.Get();
|
---|
45 |
|
---|
46 | for (var i = 0; i < size; i++) {
|
---|
47 | var expression = random.NextDouble() <= pushGpConfiguration.ErcProbability
|
---|
48 | ? CreateRandomErcExpression(random, pushGpConfiguration)
|
---|
49 | : CreateExpression(random, pushGpConfiguration, customExpressions);
|
---|
50 |
|
---|
51 | expressions.Add(expression);
|
---|
52 | }
|
---|
53 |
|
---|
54 | return expressions;
|
---|
55 | }
|
---|
56 |
|
---|
57 | private static Expression CreateExpression(IRandom random, IReadOnlyPushConfiguration pushGpConfiguration, IDictionary<string, Expression> customExpressions = null) {
|
---|
58 | var customCount = customExpressions == null ? 0 : customExpressions.Count - 1;
|
---|
59 | var index = random.Next(0, pushGpConfiguration.EnabledExpressions.Count + customCount - 1);
|
---|
60 |
|
---|
61 | if ((index >= 0) && (index < pushGpConfiguration.EnabledExpressions.Count)) {
|
---|
62 | var name = pushGpConfiguration.EnabledExpressions[index];
|
---|
63 | return ExpressionTable.GetExpression(name);
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (customExpressions == null)
|
---|
67 | throw new IndexOutOfRangeException();
|
---|
68 |
|
---|
69 | return customExpressions.ElementAt(index - (pushGpConfiguration.EnabledExpressions.Count - 1)).Value;
|
---|
70 | }
|
---|
71 |
|
---|
72 | private static Expression CreateRandomErcExpression(IRandom random, IReadOnlyPushConfiguration pushGpConfiguration) {
|
---|
73 | var value = random.Next(0, 3);
|
---|
74 |
|
---|
75 | switch (value) {
|
---|
76 | case 0:
|
---|
77 | return new IntegerPushExpression(random.Next(
|
---|
78 | pushGpConfiguration.MinRandomInteger,
|
---|
79 | pushGpConfiguration.MaxRandomInteger));
|
---|
80 |
|
---|
81 | case 1:
|
---|
82 | return new FloatPushExpression(
|
---|
83 | random.NextDouble() *
|
---|
84 | (pushGpConfiguration.MaxRandomFloat - pushGpConfiguration.MinRandomFloat) +
|
---|
85 | pushGpConfiguration.MinRandomFloat);
|
---|
86 |
|
---|
87 | case 2:
|
---|
88 | return new BooleanPushExpression(random.NextDouble() > 0.5);
|
---|
89 |
|
---|
90 | case 3:
|
---|
91 | return new NamePushExpression(NameGenerator.RandomName(random));
|
---|
92 |
|
---|
93 | default:
|
---|
94 | throw new NotImplementedException();
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|