Last change
on this file since 14328 was
14328,
checked in by pkimmesw, 8 years ago
|
#2665 Set .NET version to 4.5, C# version to 5.0, Added expression templates and factory
|
File size:
1.6 KB
|
Line | |
---|
1 | using System.Collections.Generic;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Algorithms.PushGP.Expressions;
|
---|
4 | using HeuristicLab.Random;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Algorithms.PushGP.Generators
|
---|
7 | {
|
---|
8 | public static class CodeGenerator
|
---|
9 | {
|
---|
10 | private static FastRandom random = new FastRandom();
|
---|
11 | public static IEnumerable<Expression> RandomCode(int maxPoints)
|
---|
12 | {
|
---|
13 | var actualPoints = random.Next(1, maxPoints);
|
---|
14 |
|
---|
15 | return RandomCodeWithSize(actualPoints).ToArray();
|
---|
16 | }
|
---|
17 |
|
---|
18 | private static IEnumerable<Expression> RandomCodeWithSize(int points)
|
---|
19 | {
|
---|
20 | if (points == 1)
|
---|
21 | {
|
---|
22 | // TODO: If this is an "ephemeral random constant"???? then return a randomly - chosen value of the appropriate type;
|
---|
23 | var opCode = (OpCode)random.Next(OpCodeExtensions.Min, OpCodeExtensions.Max);
|
---|
24 |
|
---|
25 | return new[] { ExpressionFactory.Create(opCode) };
|
---|
26 | }
|
---|
27 | else
|
---|
28 | {
|
---|
29 | var sizesThisLevel = Decompose(points - 1, points - 1);
|
---|
30 |
|
---|
31 | return sizesThisLevel.SelectMany(size => RandomCodeWithSize(size)).Shuffle(random);
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | private static IEnumerable<int> Decompose(int number, int maxParts)
|
---|
36 | {
|
---|
37 | if (number == 1 || maxParts == 1)
|
---|
38 | {
|
---|
39 | return new[] { number };
|
---|
40 | }
|
---|
41 | else
|
---|
42 | {
|
---|
43 | var thisPart = random.Next(1, number - 1);
|
---|
44 |
|
---|
45 | return new[] { thisPart }.Concat(Decompose(number - thisPart, maxParts - 1));
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.