Free cookie consent management tool by TermsFeed Policy Generator

Changeset 18217 for branches


Ignore:
Timestamp:
02/15/22 15:59:24 (3 years ago)
Author:
pfleck
Message:

#3040 Added guided direction and range sampling using approximated gradients.

Location:
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/SamplingSubVectorImprovementManipulator.cs

    r18214 r18217  
    9898          var current = GetIndices(node, length);
    9999
     100          var evaluate = new Func<IntegerVector, double>(integerVector => {
     101            var original = GetIndices(node, length);
     102            UpdateNode(node, integerVector, length);
     103            var quality = evaluator.Evaluate(context, symbolicExpressionTree, problemData, rows);
     104            UpdateNode(node, original, length);
     105            return quality;
     106          });
     107
    100108          var indices = SamplingSegmentOptimizationManipulator.CreateIndices(
    101             random, current, bounds, dimension, searchRange, 0, null);
     109            random, current, bounds, dimension, searchRange, evaluate, !evaluator.Maximization, 0.0, 0);
    102110
    103111          if (samplingPoints.HasFlag(SamplingSegmentOptimizationManipulator.SamplingPointsType.BeforeCombinations))
     
    114122            solutions = SamplingSegmentOptimizationManipulator.SampleIndices(solutions, sampling, random, sampleCount);
    115123
    116           var evaluate = new Func<IntegerVector, double>(integerVector => {
    117             UpdateNode(node, integerVector, length);
    118             return evaluator.Evaluate(context, symbolicExpressionTree, problemData, rows);
    119           });
     124
    120125          var best = SamplingSegmentOptimizationManipulator.FindBest(solutions, evaluate, minimize: !evaluator.Maximization);
    121126          UpdateNode(node, best.Item1, length);
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/SegmentOptimization/SamplingSegmentOptimizationManipulator.cs

    r18214 r18217  
    6868    public DimensionType Dimension { get { return DimensionParameter.Value.Value; } set { DimensionParameter.Value.Value = value; } }
    6969    public SearchRangeType SearchRange { get { return SearchRangeParameter.Value.Value; } set { SearchRangeParameter.Value.Value = value; } }
    70     public int SlopeCalculationRange { get { return SlopeCalculationRangeParameter.Value.Value; } set { SlopeCalculationRangeParameter.Value.Value = value; } }
     70    public double DirectedRangeStepSize { get { return DirectedRangeStepSizeParameter.Value.Value; } set { DirectedRangeStepSizeParameter.Value.Value = value; } }
     71    public int DirectedRangeRange { get { return DirectedRangeRangeParameter.Value.Value; } set { DirectedRangeRangeParameter.Value.Value = value; } }
    7172    public SamplingType Sampling { get { return SamplingParameter.Value.Value; } set { SamplingParameter.Value.Value = value; } }
    7273    public int SampleCount { get { return SampleCountParameter.Value.Value; } set { SampleCountParameter.Value.Value = value; } }
     
    7778    public ValueParameter<EnumValue<DimensionType>> DimensionParameter => (ValueParameter<EnumValue<DimensionType>>)Parameters["Dimension"];
    7879    public ValueParameter<EnumValue<SearchRangeType>> SearchRangeParameter => (ValueParameter<EnumValue<SearchRangeType>>)Parameters["SearchRange"];
    79     public ValueParameter<IntValue> SlopeCalculationRangeParameter => (ValueParameter<IntValue>)Parameters["SlopeCalculationRange"];
     80    public ValueParameter<DoubleValue> DirectedRangeStepSizeParameter => (ValueParameter<DoubleValue>)Parameters["DirectedRangeStepSize"];
     81    public ValueParameter<IntValue> DirectedRangeRangeParameter => (ValueParameter<IntValue>)Parameters["DirectedRangeRange"];
    8082    public ValueParameter<EnumValue<SamplingType>> SamplingParameter => (ValueParameter<EnumValue<SamplingType>>)Parameters["Sampling"];
    8183    public ValueParameter<IntValue> SampleCountParameter => (ValueParameter<IntValue>)Parameters["SampleCount"];
     
    8991      Parameters.Add(new ValueParameter<EnumValue<DimensionType>>("Dimension", new EnumValue<DimensionType>(DimensionType.All)));
    9092      Parameters.Add(new ValueParameter<EnumValue<SearchRangeType>>("SearchRange", new EnumValue<SearchRangeType>(SearchRangeType.Full)));
    91       Parameters.Add(new ValueParameter<IntValue>("SlopeCalculationRange", new IntValue(5)));
     93      Parameters.Add(new ValueParameter<DoubleValue>("DirectedRangeStepSize", new DoubleValue(1.5)));
     94      Parameters.Add(new ValueParameter<IntValue>("DirectedRangeRange", new IntValue(5)));
    9295      Parameters.Add(new ValueParameter<EnumValue<SamplingType>>("Sampling", new EnumValue<SamplingType>(SamplingType.Exhaustive)));
    9396      Parameters.Add(new ValueParameter<IntValue>("SampleCount")); // only used when sampling != Exhaustive
     
    106109    [StorableHook(HookType.AfterDeserialization)]
    107110    private void AfterDeserialization() {
    108       if (!Parameters.ContainsKey("SlopeCalculationRange"))
    109         Parameters.Add(new ValueParameter<IntValue>("SlopeCalculationRange", new IntValue(5)));
     111      if (Parameters.ContainsKey("SlopeCalculationRange"))
     112        Parameters.Remove("SlopeCalculationRange");
     113      if (!Parameters.ContainsKey("DirectedRangeStepSize"))
     114        Parameters.Add(new ValueParameter<DoubleValue>("DirectedRangeStepSize", new DoubleValue(1.5)));
     115      if (!Parameters.ContainsKey("DirectedRangeRange"))
     116        Parameters.Add(new ValueParameter<IntValue>("DirectedRangeRange", new IntValue(5)));
    110117    }
    111118
    112119    protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) {
    113       var indices = CreateIndices(random, new IntegerVector(new [] { integerVector.Min(), integerVector.Max() }), bounds, Dimension, SearchRange, SlopeCalculationRange, DataParameter.ActualValue);
     120      var indices = CreateIndices(random, new IntegerVector(new [] { integerVector.Min(), integerVector.Max() }), bounds, Dimension, SearchRange, Evaluate, true,
     121        this.DirectedRangeStepSize, this.DirectedRangeRange);
    114122     
    115123      if (SamplingPoints.HasFlag(SamplingPointsType.BeforeCombinations))
     
    137145    }
    138146   
    139     public static IEnumerable<int>[] CreateIndices(IRandom random, IntegerVector integerVector, IntMatrix bounds, DimensionType dimension, SearchRangeType indexRange, int gradientRange, DoubleMatrix data) {
     147    public static IEnumerable<int>[] CreateIndices(IRandom random, IntegerVector integerVector, IntMatrix bounds, DimensionType dimension, SearchRangeType indexRange, Func<IntegerVector, double> evaluate, bool minimize,
     148      double directedRangeStepSize, int directedRangeRange) {
    140149      var indices = new IEnumerable<int>[integerVector.Length];
    141150      int targetIndex = random.Next(indices.Length); // first or second index
    142151      for (int i = 0; i < indices.Length; i++) {
    143152        var searchRange = dimension == DimensionType.All || targetIndex == i ? indexRange : SearchRangeType.None;
    144         indices[i] = CreateSingleIndices(bounds[i % bounds.Rows, 1], integerVector[i], searchRange, gradientRange, data, random).ToList();
     153        indices[i] = CreateSingleIndices(integerVector, i, bounds, searchRange, evaluate, minimize, random, directedRangeStepSize, directedRangeRange).ToList();
    145154        if (!indices[i].Any()) {
    146155          throw new InvalidOperationException("no indices!");
     
    151160    }
    152161
    153     public static IEnumerable<int> CreateSingleIndices(int length, int currentIndex, SearchRangeType searchRange, int gradientRange, DoubleMatrix data, IRandom random) {
     162    public static IEnumerable<int> CreateSingleIndices(IntegerVector integerVector, int dim, IntMatrix bounds, SearchRangeType searchRange, Func<IntegerVector, double> evaluate, bool minimize, IRandom random,
     163      double directedRangeStepSize, int directedRangeRange) {
     164      int currentIndex = integerVector[dim];
     165      int length = bounds[dim % bounds.Rows, 1];
    154166      switch (searchRange) {
    155167        case SearchRangeType.Full:
     
    161173            ? Enumerable.Range(0, currentIndex + 1) // left
    162174            : Enumerable.Range(currentIndex, length - currentIndex); // right
    163         case SearchRangeType.RandomRange: // random range around current index, not completely random range
     175        case SearchRangeType.RandomRange: {
     176          // random range around current index, not completely random range
    164177          int start = random.Next(0, currentIndex + 1), end = random.Next(currentIndex, length);
    165178          return Enumerable.Range(start, end - start + 1);
    166         case SearchRangeType.DirectedDirection:
    167           var slope = CalculateSlope(currentIndex, gradientRange, data); // slope of original data, non-aggregated
    168           return slope < 0
     179        }
     180        case SearchRangeType.DirectedDirection: {
     181          var slope = CalculateSlope(integerVector, dim, evaluate);
     182          if (!minimize) slope = -slope;
     183          return slope > 0 // assume minimization: positive slope -> try lower indices to reduce objective
    169184            ? Enumerable.Range(0, currentIndex + 1) // left
    170185            : Enumerable.Range(currentIndex, length - currentIndex); // right
    171         //case SearchRangeType.DirectedRange:
    172         //  var slope = CalculateSlope(currentIndex, gradientRange, data);
     186        }
     187        case SearchRangeType.DirectedRange: {
     188          var slope = CalculateSlope(integerVector, dim, evaluate);
     189          if (!minimize) slope = -slope;
     190         
     191          double stepSize = directedRangeStepSize;
     192          int range = directedRangeRange;
     193
     194          double target = currentIndex - stepSize * slope;
     195          int targetStart = (int)Math.Round(target - range / 2.0), targetEnd = (int)Math.Round(target + range / 2.0);
     196
     197          int start = Math.Min(Math.Max(targetStart, 0), currentIndex + 1);
     198          int end = Math.Min(Math.Max(targetEnd, currentIndex), length);
     199          return Enumerable.Range(start, end - start + 1);
     200          }
    173201        default:
    174202          throw new ArgumentOutOfRangeException(nameof(searchRange), searchRange, null);
     
    176204    }
    177205
    178     private static double CalculateSlope(int position, int range, DoubleMatrix data) {
    179       int begin = Math.Max((int)(position - range / 2.0), 0);
    180       int remaining = data.Columns - begin;
    181       var indices = Enumerable.Range(begin, Math.Min(range, remaining)).ToArray();
    182 
    183       int num = data.Rows * indices.Length;
    184       double[] xs = new double[num], ys = new double[num];
    185 
    186       for (int i = 0; i < data.Rows; i++) {
    187         for (int j = 0; j < indices.Length; j++) {
    188           int idx = i * indices.Length + j;
    189           xs[idx] = indices[j];
    190           ys[idx] = data[i, j];
    191         }
     206    private static double CalculateSlope(IntegerVector position, int dim, Func<IntegerVector, double> evaluate) {
     207      double f(int i) {
     208        var modified = new IntegerVector(position);
     209        modified[dim] = i;
     210        return evaluate(modified);
    192211      }
    193212     
    194       var  coefficients = MathNet.Numerics.LinearRegression.SimpleRegression.Fit(xs, ys);
    195       var slope = coefficients.Item2;
    196      
     213      int x = position[dim];
     214      var slope = ( // five-point stencil with h = 1
     215        + 1 * f(x - 2)
     216        - 8 * f(x - 1)
     217        + 8 * f(x + 1)
     218        - 1 * f(x + 2)
     219      ) / 12;
     220
    197221      return slope;
    198222    }
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SegmentOptimization/SegmentOptimizationProblem.cs

    r18212 r18217  
    305305     
    306306      int removedQualities = 0;
    307       // foreach (var run in content.GetObjectGraphObjects(excludeStaticMembers: true).OfType<IRun>()) {
    308       //   if (run.Results.ContainsKey("Qualities")) {
    309       //     run.Results.Remove("Qualities");
    310       //     removedQualities++;
    311       //   }
    312       // }
     307      foreach (var run in content.GetObjectGraphObjects(excludeStaticMembers: true).OfType<IRun>()) {
     308        if (run.Results.ContainsKey("Qualities")) {
     309          run.Results.Remove("Qualities");
     310          removedQualities++;
     311        }
     312      }
    313313
    314314      return Tuple.Create(overallTests, removedDuplicated, removedQualities);
Note: See TracChangeset for help on using the changeset viewer.