Free cookie consent management tool by TermsFeed Policy Generator

Changeset 18233


Ignore:
Timestamp:
03/10/22 09:53:58 (3 years ago)
Author:
pfleck
Message:

#3040 Added Guided Direction Mutation for nested Optimizers.

Location:
branches/3040_VectorBasedGP
Files:
3 edited

Legend:

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

    r17180 r18233  
    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         vector[index] = RoundFeasible(min, max, step, random.Next(min, max));
     95        if (min == max)
     96          vector[index] = min;
     97        else
     98          vector[index] = RoundFeasible(min, max, step, random.Next(min, max));
    9699      }
    97100    }
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/NestedOptimizerSubVectorImprovementManipulator.cs

    r18230 r18233  
    2626    [Item("SubVectorOptimizationProblem", "")]
    2727    [StorableType("EA3D3221-B274-4F2F-8B58-23CB2D091FD7")]
    28     private class SubVectorOptimizationProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding> {
     28    public class SubVectorOptimizationProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding> {
    2929      #region Fixed Problem Parameters
    3030      [Storable]
     
    4545      #endregion
    4646
     47      private IFixedValueParameter<BoolValue> UseCacheParameter {
     48        get { return (IFixedValueParameter<BoolValue>)Parameters["UseCache"]; }
     49      }
     50
     51      public bool UseCache {
     52        get { return UseCacheParameter.Value.Value; }
     53        set { UseCacheParameter.Value.Value = value; }
     54      }
     55
     56      private readonly IDictionary<IntegerVector, double> cache;
     57     
    4758      public override bool Maximization { get { return false; } }
    4859
     
    5061        Encoding = new IntegerVectorEncoding("bounds");
    5162        Parameters.Add(new ResultParameter<IntegerVector>(BestSolutionParameterName, ""));
    52       }
     63
     64        Parameters.Add(new FixedValueParameter<BoolValue>("UseCache", new BoolValue(true)));
     65        cache = new Dictionary<IntegerVector, double>(new IntegerVectorEqualityComparer());
     66      }
     67
    5368      private SubVectorOptimizationProblem(SubVectorOptimizationProblem original, Cloner cloner)
    54         : base(original, cloner) { }
     69        : base(original, cloner) {
     70        this.cache = new Dictionary<IntegerVector, double>(original.cache, new IntegerVectorEqualityComparer());
     71      }
    5572      public override IDeepCloneable Clone(Cloner cloner) {
    5673        return new SubVectorOptimizationProblem(this, cloner);
    5774      }
     75
    5876      [StorableConstructor]
    59       private SubVectorOptimizationProblem(StorableConstructorFlag _) : base(_) { }
     77      private SubVectorOptimizationProblem(StorableConstructorFlag _) : base(_) {
     78        cache = new Dictionary<IntegerVector, double>(new IntegerVectorEqualityComparer());
     79      }
     80      [StorableHook(HookType.AfterDeserialization)]
     81      private void AfterDeserialization() {
     82        if (!Parameters.ContainsKey("UseCache"))
     83          Parameters.Add(new FixedValueParameter<BoolValue>("UseCache", new BoolValue(true)));
     84      }
    6085
    6186      public override double Evaluate(Individual individual, IRandom random) {
    62         var solution = individual.IntegerVector(Encoding.Name);
     87        return Evaluate(individual.IntegerVector(Encoding.Name));
     88      }
     89
     90      public double Evaluate(IntegerVector solution) {
     91        if (UseCache && cache.TryGetValue(solution, out double cachedQuality)) {
     92          return cachedQuality;
     93        }
    6394
    6495        var updatedTree = (ISymbolicExpressionTree)tree.Clone();
    6596        UpdateFromVector(updatedTree, selectedSubVectorNodes, solution, Encoding.Bounds[0, 1]);
    66        
     97
    6798        var quality = evaluator.Evaluate(executionContext, updatedTree, problemData, rows);
    6899        if (evaluator.Maximization)
    69100          quality = -quality;
     101
     102        if (UseCache) {
     103          cache.Add(solution, quality);
     104        }
     105
    70106        return quality;
    71107      }
     
    83119        this.rows = rows;
    84120        this.executionContext = executionContext;
     121        cache.Clear();
    85122      }
    86123      public void SetInstanceData(ISymbolicExpressionTree tree, List<int> selectedSubVectorNodes, int vectorLength) {
     
    88125        this.selectedSubVectorNodes = selectedSubVectorNodes;
    89126        Encoding.Length = selectedSubVectorNodes.Count * 2;
    90         Encoding.Bounds = new IntMatrix(new int[,] { { 0, vectorLength } });
    91       }
    92     }
    93 
     127        Encoding.Bounds = new IntMatrix(new int[,] { { 0, vectorLength + 1 } });
     128        cache.Clear();
     129      }
     130    }
     131
     132    [Item("SubVectorGradientMutator", "")]
     133    [StorableType("DC5EC7CE-AD51-4655-8F75-28601345B4C7")]
     134    public abstract class SubVectorGradientMutator : BoundedIntegerVectorManipulator {
     135
     136      [Storable]
     137      private readonly SubVectorOptimizationProblem problem;
     138
     139      protected SubVectorGradientMutator(SubVectorOptimizationProblem problem) {
     140        this.problem = problem;
     141      }
     142      protected SubVectorGradientMutator(SubVectorGradientMutator original, Cloner cloner)
     143        : base(original, cloner) {
     144        this.problem = cloner.Clone(original.problem);
     145      }
     146
     147      [StorableConstructor]
     148      protected SubVectorGradientMutator(StorableConstructorFlag _) : base(_) {
     149      }
     150      [StorableHook(HookType.AfterDeserialization)]
     151      private void AfterDeserialization() {
     152      }
     153
     154      public double FivePointStencil(IntegerVector position, int dim, IntMatrix bounds, int h = 1) {
     155        double f(int i) {
     156          var modified = new IntegerVector(position);
     157          modified[dim] = FloorFeasible(bounds[dim % bounds.Rows, 0], bounds[dim % bounds.Rows, 1], 1, i);
     158          return problem.Evaluate(modified);
     159        }
     160
     161        int x = position[dim];
     162        var slope = (
     163            + 1 * f(x - 2*h)
     164            - 8 * f(x - 1*h)
     165            + 8 * f(x + 1*h)
     166            - 1 * f(x + 2*h)
     167          ) / 12;
     168
     169        return slope;
     170      }
     171
     172      public double[] CalculateGradient(IntegerVector position, IntMatrix bounds, int h = 1) {
     173        return Enumerable.Range(0, position.Length)
     174          .Select((x, dim) => FivePointStencil(position, dim, bounds, h)).ToArray();
     175      }
     176    }
     177
     178    [Item("GuidedDirectionManipulator", "")]
     179    [StorableType("8781F827-BB46-4041-AAC4-25E76C5EF1F5")]
     180    public class GuidedDirectionManipulator : SubVectorGradientMutator {
     181
     182      [StorableType("AED631BC-C1A3-4408-AA39-18A81018E159")]
     183      public enum MutationType {
     184        SinglePosition,
     185        AllPosition
     186      }
     187
     188      public IFixedValueParameter<EnumValue<MutationType>> MutationTypeParameter {
     189        get { return (IFixedValueParameter<EnumValue<MutationType>>)Parameters["MutationType"]; }
     190      }
     191
     192      public GuidedDirectionManipulator(SubVectorOptimizationProblem problem)
     193      : base (problem) {
     194        Parameters.Add(new FixedValueParameter<EnumValue<MutationType>>("MutationType", new EnumValue<MutationType>(MutationType.AllPosition)));
     195      }
     196
     197      protected GuidedDirectionManipulator(GuidedDirectionManipulator original, Cloner cloner)
     198        : base(original, cloner) {
     199      }
     200      public override IDeepCloneable Clone(Cloner cloner) {
     201        return new GuidedDirectionManipulator(this, cloner);
     202      }
     203
     204      [StorableConstructor]
     205      protected GuidedDirectionManipulator(StorableConstructorFlag _) : base(_) {
     206      }
     207      [StorableHook(HookType.AfterDeserialization)]
     208      private void AfterDeserialization() {
     209      }
     210
     211      protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) {
     212        if (MutationTypeParameter.Value.Value == MutationType.AllPosition) {
     213          var gradient = CalculateGradient(integerVector, bounds);
     214          var limitedBounds = LimitBounds(bounds, integerVector, gradient);
     215          UniformSomePositionsManipulator.Apply(random, integerVector, limitedBounds, probability: 1.0);
     216        } else if(MutationTypeParameter.Value.Value == MutationType.SinglePosition) {
     217          int dim = random.Next(integerVector.Length);
     218          var gradient = Enumerable.Repeat(0.0, integerVector.Length).ToArray();
     219          gradient[dim] = FivePointStencil(integerVector, dim, bounds);
     220          var limitedBounds = LimitBounds(bounds, integerVector, gradient);
     221          UniformOnePositionManipulator.Manipulate(random, integerVector, limitedBounds, dim);
     222        }
     223      }
     224
     225      private static IntMatrix LimitBounds(IntMatrix bounds, IntegerVector position, double[] gradient) {
     226        var limitedBounds = new IntMatrix(gradient.Length, 2);
     227        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        }
     231        return limitedBounds;
     232      }
     233    }
    94234
    95235    #region Parameter Properties
     
    131271      };
    132272      es.Mutator = es.MutatorParameter.ValidValues.OfType<UniformSomePositionsManipulator>().Single();
     273
     274      var gdes = new EvolutionStrategy() {
     275        Problem = problem,
     276        PlusSelection = new BoolValue(true),
     277        PopulationSize = new IntValue(10),
     278        Children = new IntValue(10),
     279        MaximumGenerations = new IntValue(100)
     280      };
     281      gdes.Name = "Guided Direction " + gdes.Name;
     282      var gdMutator = new GuidedDirectionManipulator(problem);
     283      problem.Encoding.ConfigureOperator(gdMutator);
     284      gdes.MutatorParameter.ValidValues.Add(gdMutator);
     285      gdes.Mutator = gdMutator;
    133286
    134287      var ga = new GeneticAlgorithm() {
     
    156309      #endregion
    157310
    158       var optimizers = new ItemSet<IAlgorithm>() { rs, es, ga, osga };
     311      var optimizers = new ItemSet<IAlgorithm>() { rs, es, gdes, ga, osga };
    159312
    160313      Parameters.Add(new OptionalConstrainedValueParameter<IAlgorithm>("NestedOptimizer", optimizers, rs));
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.Instances.DataAnalysis/3.3/SegmentOptimization/SegmentOptimizationInstanceProvider.cs

    r18228 r18233  
    182182  }
    183183
     184  public class SegmentOptimizationLargeNoNoiseFileInstanceProvider : SegmentOptimizationLargeFileInstanceProvider {
     185    public override string Name {
     186      get { return "SOP Large File NoNoise"; }
     187    }
     188
     189    public override string Description {
     190      get { return "SOP Large File NoNoise"; }
     191    }
     192   
     193    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
     194      return base.GetDataDescriptors().Select(id => {
     195        var descriptor = (SOPDataDescriptor)id;
     196        descriptor.Name += "_m";
     197        descriptor.VariableName += "_m";
     198        return descriptor;
     199      });
     200    }
     201  }
     202
    184203  public class SegmentOptimizationCombinationsLargeFileInstanceProvider : SegmentOptimizationFileInstanceProvider {
    185204    public override string Name {
Note: See TracChangeset for help on using the changeset viewer.