- Timestamp:
- 03/14/22 11:37:51 (3 years ago)
- Location:
- branches/3040_VectorBasedGP
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Manipulators/UniformOnePositionManipulator.cs
r18233 r18235 85 85 if (bounds == null || bounds.Rows == 0 || bounds.Columns < 2) throw new ArgumentException("UniformOnePositionManipulator: Invalid bounds specified", "bounds"); 86 86 int min = bounds[index % bounds.Rows, 0], max = bounds[index % bounds.Rows, 1], step = 1; 87 if (min == max ) {87 if (min == max || min == max - 1) { 88 88 vector[index] = min; 89 89 } else { … … 93 93 // but 99 is not a feasible value, so max needs to be adjusted => min = 0, max = 95 94 94 max = FloorFeasible(min, max, step, max - 1); 95 if (min == max) 96 vector[index] = min; 97 else 98 vector[index] = RoundFeasible(min, max, step, random.Next(min, max)); 95 vector[index] = RoundFeasible(min, max, step, random.Next(min, max + 1)); 99 96 } 100 97 } -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/NestedOptimizerSubVectorImprovementManipulator.cs
r18233 r18235 210 210 211 211 protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) { 212 if (MutationTypeParameter.Value.Value == MutationType.AllPosition) { 212 var mutationType = MutationTypeParameter.Value.Value; 213 214 if (mutationType == MutationType.AllPosition) { 213 215 var gradient = CalculateGradient(integerVector, bounds); 214 216 var limitedBounds = LimitBounds(bounds, integerVector, gradient); 215 217 UniformSomePositionsManipulator.Apply(random, integerVector, limitedBounds, probability: 1.0); 216 } else if( MutationTypeParameter.Value.Value == MutationType.SinglePosition) {218 } else if(mutationType == MutationType.SinglePosition) { 217 219 int dim = random.Next(integerVector.Length); 218 220 var gradient = Enumerable.Repeat(0.0, integerVector.Length).ToArray(); … … 226 228 var limitedBounds = new IntMatrix(gradient.Length, 2); 227 229 for (int i = 0; i < gradient.Length; i++) { 228 limitedBounds[i, 0] = gradient[i] < 0 ? position[i] + 0 : bounds[i % bounds.Rows, 0]; 229 limitedBounds[i, 1] = gradient[i] > 0 ? position[i] + 1 : bounds[i % bounds.Rows, 1]; 230 int min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1]; 231 int lower = gradient[i] < 0 ? position[i] - 1 : min; // exclude current 232 int upper = gradient[i] > 0 ? position[i] + 1 : max; // exclude current 233 limitedBounds[i, 0] = RoundFeasible(min, max, 1, lower); 234 limitedBounds[i, 1] = RoundFeasible(min, max, 1, upper); 235 } 236 return limitedBounds; 237 } 238 } 239 240 [Item("GuidedDirectionManipulator", "")] 241 [StorableType("3034E82F-FE7B-4723-90E6-887AE82BB86D")] 242 public class GuidedRangeManipulator : SubVectorGradientMutator { 243 244 [StorableType("560E2F2A-2B34-48CC-B747-DE82119DA652")] 245 public enum MutationType { 246 SinglePosition, 247 AllPosition 248 } 249 250 public IFixedValueParameter<EnumValue<MutationType>> MutationTypeParameter { 251 get { return (IFixedValueParameter<EnumValue<MutationType>>)Parameters["MutationType"]; } 252 } 253 public IFixedValueParameter<DoubleRange> StepSizeParameter { 254 get { return (IFixedValueParameter<DoubleRange>)Parameters["StepSize"]; } 255 } 256 public IFixedValueParameter<IntValue> RangeParameter { 257 get { return (IFixedValueParameter<IntValue>)Parameters["Range"]; } 258 } 259 260 public GuidedRangeManipulator(SubVectorOptimizationProblem problem) 261 : base(problem) { 262 Parameters.Add(new FixedValueParameter<EnumValue<MutationType>>("MutationType", new EnumValue<MutationType>(MutationType.AllPosition))); 263 Parameters.Add(new FixedValueParameter<DoubleRange>("StepSize", new DoubleRange(0.001, 1000.0))); 264 Parameters.Add(new FixedValueParameter<IntValue>("Range", new IntValue(10))); 265 } 266 267 protected GuidedRangeManipulator(GuidedRangeManipulator original, Cloner cloner) 268 : base(original, cloner) { 269 } 270 public override IDeepCloneable Clone(Cloner cloner) { 271 return new GuidedRangeManipulator(this, cloner); 272 } 273 274 [StorableConstructor] 275 protected GuidedRangeManipulator(StorableConstructorFlag _) : base(_) { 276 } 277 [StorableHook(HookType.AfterDeserialization)] 278 private void AfterDeserialization() { 279 } 280 281 protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) { 282 var mutationType = MutationTypeParameter.Value.Value; 283 var stepSizeRange = StepSizeParameter.Value; 284 var range = RangeParameter.Value.Value; 285 286 var stepSize = LogUniformRandom(stepSizeRange, random); 287 288 if (mutationType == MutationType.AllPosition) { 289 var gradient = CalculateGradient(integerVector, bounds); 290 var limitedBounds = LimitBounds(bounds, integerVector, gradient, stepSize, range); 291 UniformSomePositionsManipulator.Apply(random, integerVector, limitedBounds, probability: 1.0); 292 } else if (mutationType == MutationType.SinglePosition) { 293 int dim = random.Next(integerVector.Length); 294 var gradient = Enumerable.Repeat(0.0, integerVector.Length).ToArray(); 295 gradient[dim] = FivePointStencil(integerVector, dim, bounds); 296 var limitedBounds = LimitBounds(bounds, integerVector, gradient, stepSize, range); 297 UniformOnePositionManipulator.Manipulate(random, integerVector, limitedBounds, dim); 298 } 299 } 300 301 private static double LogUniformRandom(DoubleRange range, IRandom random) { 302 double logStart = Math.Log(range.Start); 303 double logEnd = Math.Log(range.End); 304 double logResult = logStart + random.NextDouble() * (logEnd - logStart); 305 return Math.Exp(logResult); 306 } 307 308 private static IntMatrix LimitBounds(IntMatrix bounds, IntegerVector position, double[] gradient, double stepSize, int range) { 309 var limitedBounds = new IntMatrix(gradient.Length, 2); 310 for (int i = 0; i < gradient.Length; i++) { 311 int min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1]; 312 var newPoint = position[i] - gradient[i] * stepSize; 313 var lower = newPoint - range / 2.0; 314 var upper = newPoint + range / 2.0; 315 limitedBounds[i, 0] = RoundFeasible(min, max, 1, lower); 316 limitedBounds[i, 1] = RoundFeasible(min, max, 1, upper); 230 317 } 231 318 return limitedBounds; … … 284 371 gdes.MutatorParameter.ValidValues.Add(gdMutator); 285 372 gdes.Mutator = gdMutator; 373 374 var gres = new EvolutionStrategy() { 375 Problem = problem, 376 PlusSelection = new BoolValue(true), 377 PopulationSize = new IntValue(10), 378 Children = new IntValue(10), 379 MaximumGenerations = new IntValue(100) 380 }; 381 gres.Name = "Guided Range " + gres.Name; 382 var grMutator = new GuidedRangeManipulator(problem); 383 problem.Encoding.ConfigureOperator(grMutator); 384 gres.MutatorParameter.ValidValues.Add(grMutator); 385 gres.Mutator = grMutator; 286 386 287 387 var ga = new GeneticAlgorithm() { … … 309 409 #endregion 310 410 311 var optimizers = new ItemSet<IAlgorithm>() { rs, es, gdes, g a, osga };411 var optimizers = new ItemSet<IAlgorithm>() { rs, es, gdes, gres, ga, osga }; 312 412 313 413 Parameters.Add(new OptionalConstrainedValueParameter<IAlgorithm>("NestedOptimizer", optimizers, rs)); … … 325 425 [StorableHook(HookType.AfterDeserialization)] 326 426 private void AfterDeserialization() { 327 if (Parameters.TryGetValue("NestedOptimizer", out var param)) { 328 if (param is ConstrainedValueParameter<IAlgorithm> constrainedParam) { 329 Parameters.Remove("NestedOptimizer"); 330 Parameters.Add(new OptionalConstrainedValueParameter<IAlgorithm>("NestedOptimizer", 331 new ItemSet<IAlgorithm>(constrainedParam.ValidValues), constrainedParam.Value) 332 ); 333 } 427 if (Parameters.TryGetValue("NestedOptimizer", out var param) && param is ConstrainedValueParameter<IAlgorithm> constrainedParam) { 428 Parameters.Remove("NestedOptimizer"); 429 Parameters.Add(new OptionalConstrainedValueParameter<IAlgorithm>("NestedOptimizer", 430 new ItemSet<IAlgorithm>(constrainedParam.ValidValues), constrainedParam.Value) 431 ); 334 432 } 335 433 } … … 371 469 372 470 int numSelect = (int)Math.Round(subVectorNodes.Count * PercentOptimizedSubVectorNodes.Value); 373 var selectedSubVectorNodes = Enumerable.Range(0, subVectorNodes.Count).SampleRandomWithoutRepetition(random, numSelect) .ToList();374 return selectedSubVectorNodes ;471 var selectedSubVectorNodes = Enumerable.Range(0, subVectorNodes.Count).SampleRandomWithoutRepetition(random, numSelect); 472 return selectedSubVectorNodes.ToList(); 375 473 } 376 474
Note: See TracChangeset
for help on using the changeset viewer.