1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator {
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using Configuration;
|
---|
6 | using Core;
|
---|
7 | using Data.Pool;
|
---|
8 | using Expressions;
|
---|
9 |
|
---|
10 | using HeuristicLab.Random;
|
---|
11 |
|
---|
12 | /// <summary>
|
---|
13 | /// The following is the standard Push random code generation algorithm, which is used for the CODE.RAND instruction.
|
---|
14 | /// It may also be useful for the initialization of programs in evolutionary computation systems, and it is used for
|
---|
15 | /// this purpose in PushGP.
|
---|
16 | /// It produces a uniform distribution of sizes and what seems to be a reasonable distribution of shapes, in a
|
---|
17 | /// reasonable amount of time.
|
---|
18 | /// </summary>
|
---|
19 | /// <see cref="http://faculty.hampshire.edu/lspector/push3-description.html#RandomCode" />
|
---|
20 | public class RecursiveCodeGenerator {
|
---|
21 |
|
---|
22 | public static PushProgram RandomProgram(IManagedPool<PushProgram> pool, int maxPoints, IRandom random = null, IReadOnlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) {
|
---|
23 | var code = RandomCode(maxPoints, random, pushGpConfiguration, customExpressions);
|
---|
24 |
|
---|
25 | return PushProgram.Create(pool, code.ToList());
|
---|
26 | }
|
---|
27 |
|
---|
28 | public static PushProgram RandomProgram(int maxPoints, IRandom random = null, IReadOnlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) {
|
---|
29 | var code = RandomCode(maxPoints, random, pushGpConfiguration, customExpressions);
|
---|
30 |
|
---|
31 | return new PushProgram(code.ToList());
|
---|
32 | }
|
---|
33 |
|
---|
34 | public static IEnumerable<Expression> RandomCode(int maxPoints, IRandom random = null, IReadOnlyPushConfiguration pushGpConfiguration = null, IDictionary<string, Expression> customExpressions = null) {
|
---|
35 | if (maxPoints == 0)
|
---|
36 | return new Expression[0];
|
---|
37 |
|
---|
38 | random = random ?? new FastRandom();
|
---|
39 | pushGpConfiguration = pushGpConfiguration ?? new PushConfiguration();
|
---|
40 |
|
---|
41 | if (pushGpConfiguration.EnabledExpressions.Count == 0)
|
---|
42 | return new Expression[0];
|
---|
43 |
|
---|
44 | var actualPoints = maxPoints == 1 ? 1 : random.Next(1, Math.Max(1, maxPoints));
|
---|
45 |
|
---|
46 | return RandomCodeWithSize(actualPoints, random, pushGpConfiguration, customExpressions);
|
---|
47 | }
|
---|
48 |
|
---|
49 | private static IEnumerable<Expression> RandomCodeWithSize(int points, IRandom random, IReadOnlyPushConfiguration pushConfiguration, IDictionary<string, Expression> customExpressions = null) {
|
---|
50 | if (points == 1) {
|
---|
51 | return new[] { CodeGeneratorUtils.MapToExpression(
|
---|
52 | random,
|
---|
53 | pushConfiguration.ErcOptions,
|
---|
54 | pushConfiguration,
|
---|
55 | customExpressions)
|
---|
56 | };
|
---|
57 | }
|
---|
58 |
|
---|
59 | var maxParts = points - 1;
|
---|
60 |
|
---|
61 | return Decompose(maxParts, maxParts, random)
|
---|
62 | .SelectMany(size => RandomCodeWithSize(size, random, pushConfiguration, customExpressions));
|
---|
63 | }
|
---|
64 |
|
---|
65 | private static IEnumerable<int> Decompose(int number, int maxParts, IRandom random) {
|
---|
66 | if ((number == 1) || (maxParts == 1))
|
---|
67 | return new[] { number };
|
---|
68 |
|
---|
69 | var thisPart = number == 2 ? 1 : random.Next(1, number - 1);
|
---|
70 |
|
---|
71 | return new[] { thisPart }.Concat(Decompose(number - thisPart, maxParts - 1, random));
|
---|
72 | }
|
---|
73 | }
|
---|
74 | } |
---|