Changeset 18217 for branches/3040_VectorBasedGP
- Timestamp:
- 02/15/22 15:59:24 (3 years ago)
- 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 98 98 var current = GetIndices(node, length); 99 99 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 100 108 var indices = SamplingSegmentOptimizationManipulator.CreateIndices( 101 random, current, bounds, dimension, searchRange, 0, null);109 random, current, bounds, dimension, searchRange, evaluate, !evaluator.Maximization, 0.0, 0); 102 110 103 111 if (samplingPoints.HasFlag(SamplingSegmentOptimizationManipulator.SamplingPointsType.BeforeCombinations)) … … 114 122 solutions = SamplingSegmentOptimizationManipulator.SampleIndices(solutions, sampling, random, sampleCount); 115 123 116 var evaluate = new Func<IntegerVector, double>(integerVector => { 117 UpdateNode(node, integerVector, length); 118 return evaluator.Evaluate(context, symbolicExpressionTree, problemData, rows); 119 }); 124 120 125 var best = SamplingSegmentOptimizationManipulator.FindBest(solutions, evaluate, minimize: !evaluator.Maximization); 121 126 UpdateNode(node, best.Item1, length); -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/SegmentOptimization/SamplingSegmentOptimizationManipulator.cs
r18214 r18217 68 68 public DimensionType Dimension { get { return DimensionParameter.Value.Value; } set { DimensionParameter.Value.Value = value; } } 69 69 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; } } 71 72 public SamplingType Sampling { get { return SamplingParameter.Value.Value; } set { SamplingParameter.Value.Value = value; } } 72 73 public int SampleCount { get { return SampleCountParameter.Value.Value; } set { SampleCountParameter.Value.Value = value; } } … … 77 78 public ValueParameter<EnumValue<DimensionType>> DimensionParameter => (ValueParameter<EnumValue<DimensionType>>)Parameters["Dimension"]; 78 79 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"]; 80 82 public ValueParameter<EnumValue<SamplingType>> SamplingParameter => (ValueParameter<EnumValue<SamplingType>>)Parameters["Sampling"]; 81 83 public ValueParameter<IntValue> SampleCountParameter => (ValueParameter<IntValue>)Parameters["SampleCount"]; … … 89 91 Parameters.Add(new ValueParameter<EnumValue<DimensionType>>("Dimension", new EnumValue<DimensionType>(DimensionType.All))); 90 92 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))); 92 95 Parameters.Add(new ValueParameter<EnumValue<SamplingType>>("Sampling", new EnumValue<SamplingType>(SamplingType.Exhaustive))); 93 96 Parameters.Add(new ValueParameter<IntValue>("SampleCount")); // only used when sampling != Exhaustive … … 106 109 [StorableHook(HookType.AfterDeserialization)] 107 110 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))); 110 117 } 111 118 112 119 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); 114 122 115 123 if (SamplingPoints.HasFlag(SamplingPointsType.BeforeCombinations)) … … 137 145 } 138 146 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) { 140 149 var indices = new IEnumerable<int>[integerVector.Length]; 141 150 int targetIndex = random.Next(indices.Length); // first or second index 142 151 for (int i = 0; i < indices.Length; i++) { 143 152 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(); 145 154 if (!indices[i].Any()) { 146 155 throw new InvalidOperationException("no indices!"); … … 151 160 } 152 161 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]; 154 166 switch (searchRange) { 155 167 case SearchRangeType.Full: … … 161 173 ? Enumerable.Range(0, currentIndex + 1) // left 162 174 : 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 164 177 int start = random.Next(0, currentIndex + 1), end = random.Next(currentIndex, length); 165 178 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 169 184 ? Enumerable.Range(0, currentIndex + 1) // left 170 185 : 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 } 173 201 default: 174 202 throw new ArgumentOutOfRangeException(nameof(searchRange), searchRange, null); … … 176 204 } 177 205 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); 192 211 } 193 212 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 197 221 return slope; 198 222 } -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SegmentOptimization/SegmentOptimizationProblem.cs
r18212 r18217 305 305 306 306 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 } 313 313 314 314 return Tuple.Create(overallTests, removedDuplicated, removedQualities);
Note: See TracChangeset
for help on using the changeset viewer.