1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.SolutionCreator {
|
---|
2 | using System;
|
---|
3 |
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
8 | using HeuristicLab.Parameters;
|
---|
9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
10 | using HeuristicLab.Problems.ProgramSynthesis.Base.Erc;
|
---|
11 |
|
---|
12 | /// <summary>
|
---|
13 | /// Generates a new random integer vector with each element uniformly distributed in a specified range.
|
---|
14 | /// </summary>
|
---|
15 | [Item("PushSolutionCreator", "An operator which creates a new random int vector with each element uniformly distributed in a specified range and considering Push specific details")]
|
---|
16 | [StorableClass]
|
---|
17 | public class PushSolutionCreator : UniformRandomIntegerVectorCreator {
|
---|
18 |
|
---|
19 | private const string ERC_OPTIONS_PARAMETER_NAME = "ERC options";
|
---|
20 | private const string MIN_LENGTH_PARAMETER_NAME = "Min. Length";
|
---|
21 | private const string MAX_LENGTH_PARAMETER_NAME = "Max. Length";
|
---|
22 | private const string IN_INSTRUCTION_PROBABILITY = "IN Instruction Probability";
|
---|
23 |
|
---|
24 | public PushSolutionCreator() {
|
---|
25 | if (!Parameters.ContainsKey(ERC_OPTIONS_PARAMETER_NAME))
|
---|
26 | Parameters.Add(new ValueParameter<ErcOptions>(ERC_OPTIONS_PARAMETER_NAME));
|
---|
27 |
|
---|
28 | if (!Parameters.ContainsKey(MIN_LENGTH_PARAMETER_NAME))
|
---|
29 | Parameters.Add(new FixedValueParameter<IntValue>(MIN_LENGTH_PARAMETER_NAME, new IntValue(20)));
|
---|
30 |
|
---|
31 | //if (!Parameters.ContainsKey(MAX_LENGTH_PARAMETER_NAME))
|
---|
32 | // Parameters.Add(new FixedValueParameter<IntValue>(MAX_LENGTH_PARAMETER_NAME, new IntValue(100)));
|
---|
33 |
|
---|
34 | if (!Parameters.ContainsKey(IN_INSTRUCTION_PROBABILITY))
|
---|
35 | Parameters.Add(new FixedValueParameter<PercentValue>(IN_INSTRUCTION_PROBABILITY, new PercentValue(0.05)));
|
---|
36 | }
|
---|
37 |
|
---|
38 | [StorableConstructor]
|
---|
39 | public PushSolutionCreator(bool deserializing) : base(deserializing) {
|
---|
40 | }
|
---|
41 |
|
---|
42 | public PushSolutionCreator(PushSolutionCreator origin, Cloner cloner) : base(origin, cloner) { }
|
---|
43 |
|
---|
44 |
|
---|
45 | public IValueParameter<ErcOptions> ErcOptionsParameter
|
---|
46 | {
|
---|
47 | get { return (IValueParameter<ErcOptions>)Parameters[ERC_OPTIONS_PARAMETER_NAME]; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public ErcOptions ErcOptions
|
---|
51 | {
|
---|
52 | get { return ErcOptionsParameter.Value; }
|
---|
53 | set
|
---|
54 | {
|
---|
55 | ErcOptionsParameter.Value = value;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public IValueParameter<IntValue> MinLengthParameter
|
---|
60 | {
|
---|
61 | get { return (IValueParameter<IntValue>)Parameters[MIN_LENGTH_PARAMETER_NAME]; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public int MinLength
|
---|
65 | {
|
---|
66 | get { return MinLengthParameter.Value.Value; }
|
---|
67 | set { MinLengthParameter.Value.Value = value; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | //public IValueParameter<IntValue> MaxLengthParameter
|
---|
71 | //{
|
---|
72 | // get { return LengthParameter; } //(IValueParameter<IntValue>)Parameters[MAX_LENGTH_PARAMETER_NAME]; }
|
---|
73 | //}
|
---|
74 |
|
---|
75 | //public int MaxLength
|
---|
76 | //{
|
---|
77 | // get { return MaxLengthParameter.Value.Value; }
|
---|
78 | // set { MaxLengthParameter.Value.Value = value; }
|
---|
79 | //}
|
---|
80 |
|
---|
81 | public IValueParameter<PercentValue> InInstructionProbabilityParameter
|
---|
82 | {
|
---|
83 | get { return (IValueParameter<PercentValue>)Parameters[IN_INSTRUCTION_PROBABILITY]; }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public double InInstructionProbability
|
---|
87 | {
|
---|
88 | get { return InInstructionProbabilityParameter.Value.Value; }
|
---|
89 | set { InInstructionProbabilityParameter.Value.Value = value; }
|
---|
90 | }
|
---|
91 |
|
---|
92 | protected override IntegerVector Create(IRandom random, IntValue length, IntMatrix bounds) {
|
---|
93 | var len = length.Value;
|
---|
94 | var result = new IntegerVector(len);
|
---|
95 |
|
---|
96 | var variableLength = random.Next(MinLength, len);
|
---|
97 | var contentLength = Math.Min(variableLength, len);
|
---|
98 | var lowerBound = bounds[0, 0];
|
---|
99 | var upperBound = bounds[0, 1];
|
---|
100 | var inInstructionRelativeProbability = ErcOptions.ErcProbability + InInstructionProbability;
|
---|
101 |
|
---|
102 | for (var i = 0; i < contentLength; i++) {
|
---|
103 | var x = random.NextDouble();
|
---|
104 |
|
---|
105 | if (ErcOptions.ErcProbability > 0 &&
|
---|
106 | x <= ErcOptions.ErcProbability)
|
---|
107 | result[i] = PushSolutionEncoding.Erc;
|
---|
108 | else if (InInstructionProbability > 0 &&
|
---|
109 | x <= inInstructionRelativeProbability)
|
---|
110 | result[i] = PushSolutionEncoding.In;
|
---|
111 | else
|
---|
112 | result[i] = random.Next(lowerBound, upperBound);
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (contentLength < len) {
|
---|
116 | result[contentLength] = PushSolutionEncoding.End;
|
---|
117 |
|
---|
118 | for (var i = contentLength + 1; i < len; i++) {
|
---|
119 | result[i] = PushSolutionEncoding.Noop;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | return result;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|