Free cookie consent management tool by TermsFeed Policy Generator

Changeset 18227


Ignore:
Timestamp:
03/04/22 14:44:05 (2 years ago)
Author:
pfleck
Message:

#3040 Added cached option version of sampling mutator.

File:
1 edited

Legend:

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

    r18217 r18227  
    8484    public ValueParameter<EnumValue<SamplingPointsType>> SamplingPointsParameter => (ValueParameter<EnumValue<SamplingPointsType>>)Parameters["SamplingPoints"];
    8585    public ValueParameter<BoolValue> CountSamplesAsEvaluationsParameter => (ValueParameter<BoolValue>)Parameters["CountSamplesAsEvaluations"];
     86    public ValueParameter<BoolValue> CountCacheHitsAsEvaluationsParameter => (ValueParameter<BoolValue>)Parameters["CountCacheHitsAsEvaluations"];
    8687
    8788    public LookupParameter<IntValue> EvaluatedSolutionsParameter => (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"];
    8889    #endregion
     90
     91    private IDictionary<Tuple<int, int>, double> cache;
    8992
    9093    public SamplingSegmentOptimizationManipulator() {
     
    97100      Parameters.Add(new ValueParameter<EnumValue<SamplingPointsType>>("SamplingPoints", new EnumValue<SamplingPointsType>(SamplingPointsType.BeforeCombinations | SamplingPointsType.AfterCombinations)));
    98101      Parameters.Add(new ValueParameter<BoolValue>("CountSamplesAsEvaluations", new BoolValue(false)));
     102      Parameters.Add(new ValueParameter<BoolValue>("CountCacheHitsAsEvaluations", new BoolValue(true)));
    99103      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions"));
    100104    }
     
    115119      if (!Parameters.ContainsKey("DirectedRangeRange"))
    116120        Parameters.Add(new ValueParameter<IntValue>("DirectedRangeRange", new IntValue(5)));
     121      if (!Parameters.ContainsKey("CountCacheHitsAsEvaluations"))
     122        Parameters.Add(new ValueParameter<BoolValue>("CountCacheHitsAsEvaluations", new BoolValue(true)));
     123
    117124    }
    118125
    119126    protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) {
    120       var indices = CreateIndices(random, new IntegerVector(new [] { integerVector.Min(), integerVector.Max() }), bounds, Dimension, SearchRange, Evaluate, true,
     127      var solution = new IntegerVector(new[] { integerVector.Min(), integerVector.Max() });
     128
     129      var indices = CreateIndices(random, solution, bounds, Dimension, SearchRange, EvaluateCached, true,
    121130        this.DirectedRangeStepSize, this.DirectedRangeRange);
    122131     
     
    126135      var solutions = CreateCombinations(indices[0], indices[1]);
    127136      if (!solutions.Any()) {
    128         if (SamplingPoints.HasFlag(SamplingPointsType.BeforeCombinations))
     137        //if (SamplingPoints.HasFlag(SamplingPointsType.BeforeCombinations))
    129138          return; // no valid combinations -> no mutation
    130         throw new InvalidOperationException("no indices!");
     139        //throw new InvalidOperationException("no indices!");
    131140      }
    132141
     
    134143        solutions = SampleIndices(solutions, Sampling, random, SampleCount);
    135144
    136       if (CountSamplesAsEvaluationsParameter.Value.Value) {
    137         int moves = solutions.Count();
    138         EvaluatedSolutionsParameter.ActualValue.Value += moves - 1;
    139       }
    140 
    141       var best = FindBest(solutions, Evaluate);
     145      //if (CountSamplesAsEvaluationsParameter.Value.Value) {
     146      //  int moves = solutions.Count();
     147      //  EvaluatedSolutionsParameter.ActualValue.Value += moves - 1;
     148      //}
     149
     150      var best = FindBest(solutions, EvaluateCached);
    142151      if (best != null) {
    143152        CopyTo(best.Item1, integerVector);
    144153      }
     154    }
     155
     156    private double EvaluateCached(IntegerVector solution) {
     157      if (cache == null) cache = new Dictionary<Tuple<int, int>, double>();
     158      var key = Tuple.Create(solution.Min(), solution.Max());
     159      if (cache.TryGetValue(key, out double cachedQuality)) {
     160        if (CountSamplesAsEvaluationsParameter.Value.Value && CountCacheHitsAsEvaluationsParameter.Value.Value)
     161          EvaluatedSolutionsParameter.ActualValue.Value += 1;
     162        return cachedQuality;
     163      }
     164      var quality = Evaluate(solution);
     165      if (CountSamplesAsEvaluationsParameter.Value.Value)
     166        EvaluatedSolutionsParameter.ActualValue.Value += 1;
     167      cache.Add(key, quality);
     168      return quality;
    145169    }
    146170   
     
    153177        indices[i] = CreateSingleIndices(integerVector, i, bounds, searchRange, evaluate, minimize, random, directedRangeStepSize, directedRangeRange).ToList();
    154178        if (!indices[i].Any()) {
    155           throw new InvalidOperationException("no indices!");
    156           //indices[i] = new[] { integerVector[i] };
     179          //throw new InvalidOperationException("no indices!");
     180          indices[i] = new[] { integerVector[i] };
    157181        }
    158182      }
     
    193217
    194218          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);
     219          int targetStart = (int)Math.Round(target - range / 2.0);
     220          int targetEnd = (int)Math.Round(target + range / 2.0);
     221
     222          int start = Limit(targetStart, 0, currentIndex + 1);
     223          int end = Limit(targetEnd, currentIndex, length);
    199224          return Enumerable.Range(start, end - start + 1);
    200           }
     225        }
    201226        default:
    202227          throw new ArgumentOutOfRangeException(nameof(searchRange), searchRange, null);
    203228      }
    204229    }
     230
     231    private static double Limit(int x, double min, double max) { return Math.Min(Math.Max(x, min), max); }
     232    private static int Limit(int x, int min, int max) { return Math.Min(Math.Max(x, min), max); }
    205233
    206234    private static double CalculateSlope(IntegerVector position, int dim, Func<IntegerVector, double> evaluate) {
Note: See TracChangeset for help on using the changeset viewer.