Free cookie consent management tool by TermsFeed Policy Generator

Changeset 18235 for branches


Ignore:
Timestamp:
03/14/22 11:37:51 (3 years ago)
Author:
pfleck
Message:

#3040 Added GuidedRangeManipulator for nested index optimization.

Location:
branches/3040_VectorBasedGP
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Manipulators/UniformOnePositionManipulator.cs

    r18233 r18235  
    8585      if (bounds == null || bounds.Rows == 0 || bounds.Columns < 2) throw new ArgumentException("UniformOnePositionManipulator: Invalid bounds specified", "bounds");
    8686      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) {
    8888        vector[index] = min;
    8989      } else {
     
    9393        // but 99 is not a feasible value, so max needs to be adjusted => min = 0, max = 95
    9494        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));
    9996      }
    10097    }
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/NestedOptimizerSubVectorImprovementManipulator.cs

    r18233 r18235  
    210210
    211211      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) {
    213215          var gradient = CalculateGradient(integerVector, bounds);
    214216          var limitedBounds = LimitBounds(bounds, integerVector, gradient);
    215217          UniformSomePositionsManipulator.Apply(random, integerVector, limitedBounds, probability: 1.0);
    216         } else if(MutationTypeParameter.Value.Value == MutationType.SinglePosition) {
     218        } else if(mutationType == MutationType.SinglePosition) {
    217219          int dim = random.Next(integerVector.Length);
    218220          var gradient = Enumerable.Repeat(0.0, integerVector.Length).ToArray();
     
    226228        var limitedBounds = new IntMatrix(gradient.Length, 2);
    227229        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);
    230317        }
    231318        return limitedBounds;
     
    284371      gdes.MutatorParameter.ValidValues.Add(gdMutator);
    285372      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;
    286386
    287387      var ga = new GeneticAlgorithm() {
     
    309409      #endregion
    310410
    311       var optimizers = new ItemSet<IAlgorithm>() { rs, es, gdes, ga, osga };
     411      var optimizers = new ItemSet<IAlgorithm>() { rs, es, gdes, gres, ga, osga };
    312412
    313413      Parameters.Add(new OptionalConstrainedValueParameter<IAlgorithm>("NestedOptimizer", optimizers, rs));
     
    325425    [StorableHook(HookType.AfterDeserialization)]
    326426    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        );
    334432      }
    335433    }
     
    371469
    372470      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();
    375473    }
    376474
Note: See TracChangeset for help on using the changeset viewer.