Changeset 15334 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Manipulator/UniformMutation.cs
- Timestamp:
- 08/21/17 11:33:53 (7 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP
- Property svn:ignore
-
old new 1 1 *.user 2 packages 3 TestResults
-
- Property svn:ignore
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Manipulator/UniformMutation.cs
r15289 r15334 1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Manipulator { 1 // ReSharper disable CompareOfFloatsByEqualityOperator 2 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Manipulator { 2 3 3 4 using HeuristicLab.Common; … … 9 10 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 10 11 using HeuristicLab.Problems.ProgramSynthesis.Base.Erc; 12 using HeuristicLab.Problems.ProgramSynthesis.Base.Extensions; 11 13 using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; 12 14 using HeuristicLab.Problems.ProgramSynthesis.Push.Encoding; 15 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 13 16 using HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator; 14 17 … … 27 30 Parameters.Add(new ValueLookupParameter<PercentValue>("InInstructionProbability", "The probability of IN Instructions")); 28 31 29 Parameters.Add(new FixedValueParameter<PercentValue>("InstructionMutationProbability", "The probability an instruction gets mutated", new PercentValue(0.02))); 30 Parameters.Add(new FixedValueParameter<PercentValue>("CloseMutationProbability", "The probability close gets mutated", new PercentValue(0.02))); 31 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.5))); 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))); 32 37 } 33 38 … … 47 52 get { return InstructionMutationProbabilityParameter.Value.Value; } 48 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; } 49 76 } 50 77 … … 104 131 RandomParameter.ActualValue, 105 132 PlushVectorParameter.ActualValue, 106 CloseIncrementRate,107 133 ErcOptionsParameter.ActualValue, 108 134 InstructionsParameter.ActualValue, 109 135 InInstructionProbabilityParameter.ActualValue.Value, 110 136 CloseMutationProbability, 111 InstructionMutationProbability); 137 CloseIncrementRate, 138 InstructionMutationProbability, 139 InstructionMutationRate, 140 InstructionConstantTweakRate); 112 141 return base.InstrumentedApply(); 113 142 } … … 116 145 IRandom random, 117 146 PlushVector plushVector, 118 double closeIncrementRate,119 147 IReadOnlyErcOptions ercOptions, 120 148 IReadOnlyExpressionsConfiguration instructions, 121 149 double inInstructionProbability, 122 150 double closeMutationProbability, 123 double instructionMutationProbability 151 double closeIncrementRate, 152 double instructionMutationProbability, 153 double instructionMutationRate, 154 double constantTweakRate 124 155 ) { 125 156 126 for (var i = 0; i < plushVector.Entries.Count; i++) { 127 var entry = plushVector[i]; 128 var x = random.NextDouble(); 129 130 if (x < instructionMutationProbability) { 131 var instruction = CodeGeneratorUtils.GetRandomExpression( 132 random, 133 ercOptions, 134 instructions, 135 inInstructionProbability); 136 entry.Instruction = instruction; 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]); 137 164 } 138 165 139 x = random.NextDouble(); 140 141 if (x < closeMutationProbability) { 142 x = random.NextDouble(); 143 entry.Close += x < closeIncrementRate ? 1 : -1; 144 145 if (entry.Close < 0) 146 entry.Close = 0; 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]); 147 175 } 176 177 plushVector.UpdatePushProgram(); 178 return; 148 179 } 149 150 // specifies that the program cached within entry must be updated before reuse 151 plushVector.UpdatePushProgram(); 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); 152 215 } 153 216 }
Note: See TracChangeset
for help on using the changeset viewer.