Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Manipulator/UniformMutation.cs @ 15275

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

#2665 Added PlushEncoding, ZeroErrorDistributionAnalzer

File size: 5.9 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Manipulator {
2  using HeuristicLab.Common;
3  using HeuristicLab.Core;
4  using HeuristicLab.Data;
5  using HeuristicLab.Operators;
6  using HeuristicLab.Optimization;
7  using HeuristicLab.Parameters;
8  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc;
10  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
11  using HeuristicLab.Problems.ProgramSynthesis.Push.Encoding;
12  using HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator;
13  using HeuristicLab.Random;
14
15  /// <summary>
16  /// Uniformly distributed change of a single position of an integer vector.
17  /// </summary>
18  [Item("UniformMutation", " Uniformly distributed change of a single position of an plush vector.")]
19  [StorableClass]
20  public class UniformMutation : InstrumentedOperator, IPlushManipulator, IStochasticOperator {
21
22    public UniformMutation() {
23      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
24      Parameters.Add(new LookupParameter<PlushVector>("PlushVector", "The vector which should be manipulated."));
25      Parameters.Add(new ValueLookupParameter<IntValue>("MinLength", "The min length of the vector."));
26      Parameters.Add(new ValueLookupParameter<IntValue>("MaxLength", "The max length of the vector."));
27      Parameters.Add(new ValueLookupParameter<IReadOnlyExpressionsConfiguration>("Instructions", "The enabled instructions"));
28      Parameters.Add(new ValueLookupParameter<IReadOnlyErcOptions>("ErcOptions", "ERC options"));
29      Parameters.Add(new ValueLookupParameter<PercentValue>("InInstructionProbability", "The probability of IN Instructions"));
30
31      Parameters.Add(new FixedValueParameter<PercentValue>("InstructionMutationProbability", "The probability a value gets mutated", new PercentValue(0.02)));
32      Parameters.Add(new FixedValueParameter<DoubleValue>("CloseMutationDeviation", "When mutating individual, the amount of which the close marker is incremented or decrement is based on a random sample from a normal distribution with mean 0 and standard deviation set by the close mutation deviation parameter", new DoubleValue(1.0)));
33    }
34
35    public UniformMutation(bool deserializing) : base(deserializing) {
36    }
37
38    public UniformMutation(UniformMutation origin, Cloner cloner) : base(origin, cloner) {
39    }
40
41    public IValueParameter<PercentValue> InstructionMutationProbabilityParameter
42    {
43      get { return (IValueParameter<PercentValue>)Parameters["InstructionMutationProbability"]; }
44    }
45
46    public double InstructionMutationProbability
47    {
48      get { return InstructionMutationProbabilityParameter.Value.Value; }
49      set { InstructionMutationProbabilityParameter.Value.Value = value; }
50    }
51
52    public IValueParameter<PercentValue> CloseMutationDeviationParameter
53    {
54      get { return (IValueParameter<PercentValue>)Parameters["CloseMutationDeviation"]; }
55    }
56
57    public double CloseMutationDeviation
58    {
59      get { return CloseMutationDeviationParameter.Value.Value; }
60      set { CloseMutationDeviationParameter.Value.Value = value; }
61    }
62
63    public override bool CanChangeName
64    {
65      get { return false; }
66    }
67    public ILookupParameter<IRandom> RandomParameter
68    {
69      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
70    }
71    public ILookupParameter<PlushVector> PlushVectorParameter
72    {
73      get { return (ILookupParameter<PlushVector>)Parameters["PlushVector"]; }
74    }
75    public IValueLookupParameter<IntValue> MinLengthParameter
76    {
77      get { return (IValueLookupParameter<IntValue>)Parameters["MinLength"]; }
78    }
79    public IValueLookupParameter<IntValue> MaxLengthParameter
80    {
81      get { return (IValueLookupParameter<IntValue>)Parameters["MaxLength"]; }
82    }
83    public IValueLookupParameter<IReadOnlyExpressionsConfiguration> InstructionsParameter
84    {
85      get { return (IValueLookupParameter<IReadOnlyExpressionsConfiguration>)Parameters["Instructions"]; }
86    }
87    public IValueLookupParameter<IReadOnlyErcOptions> ErcOptionsParameter
88    {
89      get { return (IValueLookupParameter<IReadOnlyErcOptions>)Parameters["ErcOptions"]; }
90    }
91    public IValueLookupParameter<PercentValue> InInstructionProbabilityParameter
92    {
93      get { return (IValueLookupParameter<PercentValue>)Parameters["InInstructionProbability"]; }
94    }
95
96    public override IDeepCloneable Clone(Cloner cloner) {
97      return new UniformMutation(this, cloner);
98    }
99
100    public sealed override IOperation InstrumentedApply() {
101      Manipulate(RandomParameter.ActualValue, PlushVectorParameter.ActualValue);
102      return base.InstrumentedApply();
103    }
104
105    private void Manipulate(IRandom random, PlushVector plushVector) {
106      var normalDistributedRandom = new NormalDistributedRandom(random, 0, CloseMutationDeviation);
107      var ercOptions = ErcOptionsParameter.ActualValue;
108      var instructions = InstructionsParameter.ActualValue;
109      var inInstructionProbability = InInstructionProbabilityParameter.ActualValue.Value;
110      var instructionMutationProbability = InstructionMutationProbability;
111
112      for (var i = 0; i < plushVector.Entries.Count; i++) {
113        var entry = plushVector[i];
114        var x = random.NextDouble();
115
116        if (x < instructionMutationProbability) {
117          var instruction = CodeGeneratorUtils.GetRandomExpression(
118            random,
119            ercOptions,
120            instructions,
121            inInstructionProbability);
122          entry.Instruction = instruction;
123        }
124
125        entry.Close += normalDistributedRandom.Next();
126      }
127
128      // specifies that the program cached within entry must be updated before reuse
129      plushVector.UpdatePushProgram();
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.