Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14184


Ignore:
Timestamp:
07/22/16 16:37:23 (8 years ago)
Author:
bburlacu
Message:

#2635: Simplified evaluator code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.OSGAEvaluator/HeuristicLab.OSGAEvaluator/SymbolicRegressionSingleObjectiveOSGAEvaluator.cs

    r14104 r14184  
    171171
    172172    private double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, DoubleLimit estimationLimits, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
    173       var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows).ToList();
     173      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows).LimitToRange(estimationLimits.Lower, estimationLimits.Upper);
    174174      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    175       IEnumerator<double> targetValuesEnumerator;
    176 
    177       double alpha = 0, beta = 1;
    178       if (applyLinearScaling) {
    179         var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
    180         targetValuesEnumerator = targetValues.GetEnumerator();
    181         var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
    182         while (targetValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) {
    183           double target = targetValuesEnumerator.Current;
    184           double estimated = estimatedValuesEnumerator.Current;
    185           if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
    186             linearScalingCalculator.Add(estimated, target);
    187         }
    188         if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None && (targetValuesEnumerator.MoveNext() || estimatedValuesEnumerator.MoveNext()))
    189           throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
    190 
    191         alpha = linearScalingCalculator.Alpha;
    192         beta = linearScalingCalculator.Beta;
    193         if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) {
    194           alpha = 0.0;
    195           beta = 1.0;
    196         }
    197       }
    198       var scaledEstimatedValuesEnumerator = estimatedValues.Select(x => x * beta + alpha).LimitToRange(estimationLimits.Lower, estimationLimits.Upper).GetEnumerator();
    199       targetValuesEnumerator = targetValues.GetEnumerator();
    200 
    201       var pearsonRCalculator = new OnlinePearsonsRCalculator();
    202 
    203       var interval = (int)Math.Floor(problemData.TrainingPartition.Size * RelativeFitnessEvaluationIntervalSize);
    204       var i = problemData.TrainingPartition.Start;
    205       var trainingEnd = problemData.TrainingPartition.End;
    206       var qualityPerInterval = new List<double>();
    207       while (targetValuesEnumerator.MoveNext() && scaledEstimatedValuesEnumerator.MoveNext()) {
    208         pearsonRCalculator.Add(targetValuesEnumerator.Current, scaledEstimatedValuesEnumerator.Current);
    209         ++i;
    210         if (i % interval == 0 || i == trainingEnd) {
    211           var q = pearsonRCalculator.ErrorState != OnlineCalculatorError.None ? double.NaN : pearsonRCalculator.R;
    212           qualityPerInterval.Add(q * q);
    213         }
    214       }
    215       var r = pearsonRCalculator.ErrorState != OnlineCalculatorError.None ? double.NaN : pearsonRCalculator.R;
    216       var actualQuality = r * r;
     175
    217176      var parentQualities = ParentQualitiesParameter.ActualValue.Select(x => x.Value);
    218177      var minQuality = parentQualities.Min();
     
    222181      var threshold = parentQuality * RelativeParentChildQualityThreshold;
    223182
    224       bool predictedRejected = false;
    225 
    226       i = 0;
    227       foreach (var q in qualityPerInterval) {
    228         if (double.IsNaN(q) || !(q > threshold)) {
    229           predictedRejected = true;
    230           break;
     183      var pearsonRCalculator = new OnlinePearsonsRCalculator();
     184      var targetValuesEnumerator = targetValues.GetEnumerator();
     185      var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
     186
     187      var i = 0;
     188      var trainingPartitionSize = problemData.TrainingPartition.Size;
     189      var interval = (int)Math.Floor(trainingPartitionSize * RelativeFitnessEvaluationIntervalSize);
     190      while (targetValuesEnumerator.MoveNext() && estimatedValuesEnumerator.MoveNext()) {
     191        pearsonRCalculator.Add(targetValuesEnumerator.Current, estimatedValuesEnumerator.Current);
     192        ++i;
     193        if (i % interval == 0 || i == trainingPartitionSize) {
     194          var q = pearsonRCalculator.ErrorState != OnlineCalculatorError.None ? double.NaN : pearsonRCalculator.R;
     195          var quality = q * q;
     196          if (!(quality > threshold))
     197            return quality;
    231198        }
    232         ++i;
    233199      }
    234 
    235       var actuallyRejected = !(actualQuality > parentQuality);
    236 
    237       if (RejectedStats.Rows == 0 || TotalStats.Rows == 0) {
    238         RejectedStats = new IntMatrix(2, qualityPerInterval.Count);
    239         RejectedStats.RowNames = new[] { "Predicted", "Actual" };
    240         RejectedStats.ColumnNames = Enumerable.Range(1, RejectedStats.Columns).Select(x => string.Format("0-{0}", Math.Min(trainingEnd, x * interval)));
    241         TotalStats = new IntMatrix(2, 2);
    242         TotalStats.RowNames = new[] { "Predicted", "Actual" };
    243         TotalStats.ColumnNames = new[] { "Rejected", "Not Rejected" };
    244       }
    245       // gather some statistics
    246       if (predictedRejected) {
    247         RejectedStats[0, i]++;
    248         TotalStats[0, 0]++;
    249       } else {
    250         TotalStats[0, 1]++;
    251       }
    252       if (actuallyRejected) {
    253         TotalStats[1, 0]++;
    254       } else {
    255         TotalStats[1, 1]++;
    256       }
    257       if (predictedRejected && actuallyRejected) {
    258         RejectedStats[1, i]++;
    259       }
    260       return r * r;
     200      var r = pearsonRCalculator.ErrorState != OnlineCalculatorError.None ? double.NaN : pearsonRCalculator.R;
     201      var actualQuality = r * r;
     202      return actualQuality;
    261203    }
    262204
Note: See TracChangeset for help on using the changeset viewer.