1 | using HeuristicLab.Common;
|
---|
2 | using HeuristicLab.Core;
|
---|
3 | using HeuristicLab.Data;
|
---|
4 | using HeuristicLab.Operators;
|
---|
5 | using HeuristicLab.Optimization;
|
---|
6 | using HeuristicLab.Parameters;
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
10 | /// <summary>
|
---|
11 | /// Uniformly distributed change of a single position of an integer vector.
|
---|
12 | /// </summary>
|
---|
13 | [Item("UniformMutation", " Uniformly distributed change of a single position of an plush vector.")]
|
---|
14 | [StorableClass]
|
---|
15 | public class UniformMutation : InstrumentedOperator, IPlushManipulator, IStochasticOperator {
|
---|
16 |
|
---|
17 | public UniformMutation() {
|
---|
18 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
|
---|
19 | Parameters.Add(new LookupParameter<PlushVector>("PlushVector", "The vector which should be manipulated."));
|
---|
20 | Parameters.Add(new ValueLookupParameter<IReadOnlyExpressionsConfiguration>("Instructions", "The enabled instructions"));
|
---|
21 | Parameters.Add(new ValueLookupParameter<IReadOnlyErcOptions>("ErcOptions", "ERC options"));
|
---|
22 | Parameters.Add(new ValueLookupParameter<PercentValue>("InInstructionProbability", "The probability of IN Instructions"));
|
---|
23 |
|
---|
24 | Parameters.Add(new FixedValueParameter<PercentValue>("InstructionMutationProbability", "The probability instructions get mutated", new PercentValue(0.8)));
|
---|
25 | Parameters.Add(new FixedValueParameter<PercentValue>("InstructionMutationRate", "The probability an instruction gets mutated", new PercentValue(0.01)));
|
---|
26 | Parameters.Add(new FixedValueParameter<PercentValue>("InstructionConstantTweakRate", "When mutating an instruction, this parameter specifies if mutation is related to the current type (e.g. INTEGER, FLOAT) or not. ", new PercentValue(0)));
|
---|
27 | Parameters.Add(new FixedValueParameter<PercentValue>("CloseMutationProbability", "The probability close gets mutated", new PercentValue(0.2)));
|
---|
28 | Parameters.Add(new FixedValueParameter<PercentValue>("CloseIncrementRate", "When mutating individual, the increment rate specifies if close should be increased or decreased by 1.", new PercentValue(0.1)));
|
---|
29 | }
|
---|
30 |
|
---|
31 | public UniformMutation(bool deserializing) : base(deserializing) {
|
---|
32 | }
|
---|
33 |
|
---|
34 | public UniformMutation(UniformMutation origin, Cloner cloner) : base(origin, cloner) {
|
---|
35 | }
|
---|
36 |
|
---|
37 | public IValueParameter<PercentValue> InstructionMutationProbabilityParameter {
|
---|
38 | get { return (IValueParameter<PercentValue>)Parameters["InstructionMutationProbability"]; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public double InstructionMutationProbability {
|
---|
42 | get { return InstructionMutationProbabilityParameter.Value.Value; }
|
---|
43 | set { InstructionMutationProbabilityParameter.Value.Value = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public IValueParameter<PercentValue> InstructionMutationRateParameter {
|
---|
47 | get { return (IValueParameter<PercentValue>)Parameters["InstructionMutationRate"]; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public double InstructionMutationRate {
|
---|
51 | get { return InstructionMutationRateParameter.Value.Value; }
|
---|
52 | set { InstructionMutationRateParameter.Value.Value = value; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public IValueParameter<PercentValue> InstructionConstantTweakRateParameter {
|
---|
56 | get { return (IValueParameter<PercentValue>)Parameters["InstructionConstantTweakRate"]; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public double InstructionConstantTweakRate {
|
---|
60 | get { return InstructionConstantTweakRateParameter.Value.Value; }
|
---|
61 | set { InstructionConstantTweakRateParameter.Value.Value = value; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public IValueParameter<PercentValue> CloseMutationProbabilityParameter {
|
---|
65 | get { return (IValueParameter<PercentValue>)Parameters["CloseMutationProbability"]; }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public double CloseMutationProbability {
|
---|
69 | get { return CloseMutationProbabilityParameter.Value.Value; }
|
---|
70 | set { CloseMutationProbabilityParameter.Value.Value = value; }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public IValueParameter<PercentValue> CloseIncrementRateParameter {
|
---|
74 | get { return (IValueParameter<PercentValue>)Parameters["CloseIncrementRate"]; }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public double CloseIncrementRate {
|
---|
78 | get { return CloseIncrementRateParameter.Value.Value; }
|
---|
79 | set { CloseIncrementRateParameter.Value.Value = value; }
|
---|
80 | }
|
---|
81 |
|
---|
82 | public override bool CanChangeName {
|
---|
83 | get { return false; }
|
---|
84 | }
|
---|
85 | public ILookupParameter<IRandom> RandomParameter {
|
---|
86 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
87 | }
|
---|
88 | public ILookupParameter<PlushVector> PlushVectorParameter {
|
---|
89 | get { return (ILookupParameter<PlushVector>)Parameters["PlushVector"]; }
|
---|
90 | }
|
---|
91 | public IValueLookupParameter<IReadOnlyExpressionsConfiguration> InstructionsParameter {
|
---|
92 | get { return (IValueLookupParameter<IReadOnlyExpressionsConfiguration>)Parameters["Instructions"]; }
|
---|
93 | }
|
---|
94 | public IValueLookupParameter<IReadOnlyErcOptions> ErcOptionsParameter {
|
---|
95 | get { return (IValueLookupParameter<IReadOnlyErcOptions>)Parameters["ErcOptions"]; }
|
---|
96 | }
|
---|
97 | public IValueLookupParameter<PercentValue> InInstructionProbabilityParameter {
|
---|
98 | get { return (IValueLookupParameter<PercentValue>)Parameters["InInstructionProbability"]; }
|
---|
99 | }
|
---|
100 |
|
---|
101 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
102 | return new UniformMutation(this, cloner);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public sealed override IOperation InstrumentedApply() {
|
---|
106 | Manipulate(
|
---|
107 | RandomParameter.ActualValue,
|
---|
108 | PlushVectorParameter.ActualValue,
|
---|
109 | ErcOptionsParameter.ActualValue,
|
---|
110 | InstructionsParameter.ActualValue,
|
---|
111 | InInstructionProbabilityParameter.ActualValue.Value,
|
---|
112 | CloseMutationProbability,
|
---|
113 | CloseIncrementRate,
|
---|
114 | InstructionMutationProbability,
|
---|
115 | InstructionMutationRate,
|
---|
116 | InstructionConstantTweakRate);
|
---|
117 | return base.InstrumentedApply();
|
---|
118 | }
|
---|
119 |
|
---|
120 | private static void Manipulate(
|
---|
121 | IRandom random,
|
---|
122 | PlushVector plushVector,
|
---|
123 | IReadOnlyErcOptions ercOptions,
|
---|
124 | IReadOnlyExpressionsConfiguration instructions,
|
---|
125 | double inInstructionProbability,
|
---|
126 | double closeMutationProbability,
|
---|
127 | double closeIncrementRate,
|
---|
128 | double instructionMutationProbability,
|
---|
129 | double instructionMutationRate,
|
---|
130 | double constantTweakRate
|
---|
131 | ) {
|
---|
132 |
|
---|
133 | var x = random.NextDouble();
|
---|
134 |
|
---|
135 | // instruction manipulation
|
---|
136 | if (x < instructionMutationProbability) {
|
---|
137 | if (instructionMutationRate == 0) return;
|
---|
138 | for (var i = 0; i < plushVector.Entries.Count; i++) {
|
---|
139 | MutateInstruction(random, ercOptions, instructions, inInstructionProbability, instructionMutationRate, constantTweakRate, plushVector[i]);
|
---|
140 | }
|
---|
141 |
|
---|
142 | plushVector.UpdatePushProgram();
|
---|
143 | return;
|
---|
144 | }
|
---|
145 |
|
---|
146 | // close manipulation
|
---|
147 | if (x < instructionMutationProbability + closeMutationProbability) {
|
---|
148 | if (closeIncrementRate == 0) return;
|
---|
149 | for (var i = 0; i < plushVector.Entries.Count; i++) {
|
---|
150 | MutateClose(random, closeIncrementRate, plushVector[i]);
|
---|
151 | }
|
---|
152 |
|
---|
153 | plushVector.UpdatePushProgram();
|
---|
154 | return;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | private static void MutateClose(IRandom random, double closeIncrementRate, PlushEntry entry) {
|
---|
159 | entry.Close += random.NextDouble() < closeIncrementRate ? 1 : -1;
|
---|
160 |
|
---|
161 | if (entry.Close < 0)
|
---|
162 | entry.Close = 0;
|
---|
163 | }
|
---|
164 |
|
---|
165 | private static void MutateInstruction(
|
---|
166 | IRandom random,
|
---|
167 | IReadOnlyErcOptions ercOptions,
|
---|
168 | IReadOnlyExpressionsConfiguration instructions,
|
---|
169 | double inInstructionProbability,
|
---|
170 | double instructionMutationRate,
|
---|
171 | double constantTweakRate,
|
---|
172 | PlushEntry entry) {
|
---|
173 |
|
---|
174 | if (random.NextDouble() >= instructionMutationRate)
|
---|
175 | return;
|
---|
176 |
|
---|
177 | var entryType = entry.Instruction.GetType();
|
---|
178 |
|
---|
179 | if (constantTweakRate > 0 && random.NextDouble() < constantTweakRate &&
|
---|
180 | entryType.IsSubclass(typeof(PushExpression<>))) {
|
---|
181 | var attribute = ExpressionTable.TypeToAttributeTable[entryType];
|
---|
182 | entry.Instruction = CodeGeneratorUtils.CreateRandomErcExpression(attribute.StackType, random, ercOptions);
|
---|
183 | return;
|
---|
184 | }
|
---|
185 |
|
---|
186 | entry.Instruction = CodeGeneratorUtils.GetRandomExpression(
|
---|
187 | random,
|
---|
188 | ercOptions,
|
---|
189 | instructions,
|
---|
190 | inInstructionProbability);
|
---|
191 | }
|
---|
192 | }
|
---|
193 | } |
---|