Changeset 18227 for branches/3040_VectorBasedGP
- Timestamp:
- 03/04/22 14:44:05 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/SegmentOptimization/SamplingSegmentOptimizationManipulator.cs
r18217 r18227 84 84 public ValueParameter<EnumValue<SamplingPointsType>> SamplingPointsParameter => (ValueParameter<EnumValue<SamplingPointsType>>)Parameters["SamplingPoints"]; 85 85 public ValueParameter<BoolValue> CountSamplesAsEvaluationsParameter => (ValueParameter<BoolValue>)Parameters["CountSamplesAsEvaluations"]; 86 public ValueParameter<BoolValue> CountCacheHitsAsEvaluationsParameter => (ValueParameter<BoolValue>)Parameters["CountCacheHitsAsEvaluations"]; 86 87 87 88 public LookupParameter<IntValue> EvaluatedSolutionsParameter => (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; 88 89 #endregion 90 91 private IDictionary<Tuple<int, int>, double> cache; 89 92 90 93 public SamplingSegmentOptimizationManipulator() { … … 97 100 Parameters.Add(new ValueParameter<EnumValue<SamplingPointsType>>("SamplingPoints", new EnumValue<SamplingPointsType>(SamplingPointsType.BeforeCombinations | SamplingPointsType.AfterCombinations))); 98 101 Parameters.Add(new ValueParameter<BoolValue>("CountSamplesAsEvaluations", new BoolValue(false))); 102 Parameters.Add(new ValueParameter<BoolValue>("CountCacheHitsAsEvaluations", new BoolValue(true))); 99 103 Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions")); 100 104 } … … 115 119 if (!Parameters.ContainsKey("DirectedRangeRange")) 116 120 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 117 124 } 118 125 119 126 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, 121 130 this.DirectedRangeStepSize, this.DirectedRangeRange); 122 131 … … 126 135 var solutions = CreateCombinations(indices[0], indices[1]); 127 136 if (!solutions.Any()) { 128 if (SamplingPoints.HasFlag(SamplingPointsType.BeforeCombinations))137 //if (SamplingPoints.HasFlag(SamplingPointsType.BeforeCombinations)) 129 138 return; // no valid combinations -> no mutation 130 throw new InvalidOperationException("no indices!");139 //throw new InvalidOperationException("no indices!"); 131 140 } 132 141 … … 134 143 solutions = SampleIndices(solutions, Sampling, random, SampleCount); 135 144 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); 142 151 if (best != null) { 143 152 CopyTo(best.Item1, integerVector); 144 153 } 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; 145 169 } 146 170 … … 153 177 indices[i] = CreateSingleIndices(integerVector, i, bounds, searchRange, evaluate, minimize, random, directedRangeStepSize, directedRangeRange).ToList(); 154 178 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] }; 157 181 } 158 182 } … … 193 217 194 218 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); 199 224 return Enumerable.Range(start, end - start + 1); 200 225 } 201 226 default: 202 227 throw new ArgumentOutOfRangeException(nameof(searchRange), searchRange, null); 203 228 } 204 229 } 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); } 205 233 206 234 private static double CalculateSlope(IntegerVector position, int dim, Func<IntegerVector, double> evaluate) {
Note: See TracChangeset
for help on using the changeset viewer.