Changeset 14302 for branches/HeuristicLab.OSGAEvaluator
- Timestamp:
- 09/23/16 09:47:25 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.OSGAEvaluator/HeuristicLab.OSGAEvaluator/SymbolicRegressionSingleObjectiveOSGAEvaluator.cs
r14301 r14302 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Diagnostics; 24 25 using System.Linq; 25 26 using HeuristicLab.Common; … … 195 196 196 197 private double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, DoubleLimit estimationLimits, IRegressionProblemData problemData, IEnumerable<int> rows) { 197 var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows) .LimitToRange(estimationLimits.Lower, estimationLimits.Upper);198 var targetValues = problemData.Dataset.Get ReadOnlyDoubleValues(problemData.TargetVariable);198 var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 199 var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows).ToList(); 199 200 200 201 var parentQualities = ParentQualitiesParameter.ActualValue.Select(x => x.Value); … … 204 205 var parentQuality = minQuality + (maxQuality - minQuality) * comparisonFactor; 205 206 206 var e = estimatedValues.GetEnumerator(); 207 207 208 208 209 #region fixed intervals 209 210 if (UseFixedEvaluationIntervals) { 211 var e = estimatedValues.GetEnumerator(); 210 212 double threshold = parentQuality * RelativeParentChildQualityThreshold; 211 213 if (UseAdaptiveQualityThreshold) { … … 290 292 return actualQuality; 291 293 } 292 294 #endregion 293 295 } else { 294 var trainingPartitionSize = problemData.TrainingPartition.Size;295 var interval = (int)Math.Floor(trainingPartitionSize * RelativeFitnessEvaluationIntervalSize);296 double quality = double.NaN;297 var estimated = new List<double>(); // save estimated values in a list so we don't re-evaluate298 // use the actual estimated values for the first i * interval rows of the training partition and and assume the remaining rows are perfectly correlated299 // if the quality of the individual still falls below the parent quality, then we can reject it sooner, otherwise as i increases the whole estimated series will be used300 296 var lsc = new OnlineLinearScalingParameterCalculator(); 301 297 var rcalc = new OnlinePearsonsRCalculator(); 302 var actualQuality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, SymbolicExpressionTreeParameter.ActualValue, estimationLimits.Lower, estimationLimits.Upper, problemData, problemData.TrainingIndices, false); 303 for (int i = 0; i < trainingPartitionSize; i += interval) { 304 var start = problemData.TrainingPartition.Start; 305 int end = Math.Min(trainingPartitionSize, i + interval); 306 // cache estimated values 307 // scale target values to the range of the estimated values 308 for (int j = i; j < end && e.MoveNext(); ++j) { 309 estimated.Add(e.Current); 310 var index = j + start; 311 // in the context of the linear scaling calculator, the target value becomes the "original" 312 // while the estimated value becomes the "target" (because we want to scale the target in the range of the estimated) 313 lsc.Add(targetValues[index], e.Current); 314 } 315 var a = lsc.Alpha; // additive scaling term 316 var b = lsc.Beta; // multiplicative scaling factor 317 // calculate the quality 318 for (int j = i; j < end; ++j) { 319 var index = j + start; 320 rcalc.Add(estimated[j], targetValues[index]); 321 } 322 var rcalc2 = (OnlinePearsonsRCalculator)rcalc.Clone(); 323 for (int j = end; j < trainingPartitionSize; ++j) { 324 var index = j + start; 325 var v = targetValues[index] * b + a; 326 rcalc2.Add(v, targetValues[index]); 327 } 328 var r = rcalc2.ErrorState == OnlineCalculatorError.None ? rcalc2.R : double.NaN; 298 var actualQuality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, SymbolicExpressionTreeParameter.ActualValue, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, true); 299 300 var values = estimatedValues.Zip(targetValues, (es, t) => new { Estimated = es, Target = t }); 301 int calculatedRows = 0; 302 double quality = 0.0; 303 304 foreach (var value in values) { 305 lsc.Add(value.Estimated, value.Target); 306 rcalc.Add(value.Estimated, value.Target); 307 calculatedRows++; 308 309 if (calculatedRows % 5 == 0) { 310 var alpha = lsc.Alpha; 311 var beta = lsc.Beta; 312 313 OnlinePearsonsRCalculator calc = (OnlinePearsonsRCalculator)rcalc.Clone(); 314 foreach (var t in targetValues.Skip(calculatedRows)) { 315 var scaledTarget = (t - alpha) / beta; 316 calc.Add(scaledTarget, t); 317 } 318 319 var r = calc.ErrorState == OnlineCalculatorError.None ? calc.R : double.NaN; 320 quality = r * r; 321 322 if (quality < parentQuality && actualQuality > parentQuality) { 323 Debugger.Break(); 324 } 325 if (quality < parentQuality) return quality; 326 } 327 } 328 329 //calculate quality for all rows 330 { 331 var r = rcalc.ErrorState == OnlineCalculatorError.None ? rcalc.R : double.NaN; 329 332 quality = r * r; 330 bool falseReject = false; 331 if (!(quality > parentQuality)) { 332 if (actualQuality > parentQuality) 333 falseReject = true; 334 } 335 // if (!(quality > parentQuality)) 336 // break; 333 if (quality < parentQuality && actualQuality > parentQuality) { 334 Debugger.Break(); 335 } 336 if (double.IsNaN(quality)) quality = 0.0; 337 if (quality != actualQuality) Debugger.Break(); 338 339 //necessary due to rounding errors and diff in the range of 10E-8 340 quality = actualQuality; 337 341 } 338 342
Note: See TracChangeset
for help on using the changeset viewer.