Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Manipulator/UniformMutation.cs @ 15692

Last change on this file since 15692 was 15334, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

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