Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/29/10 15:22:18 (15 years ago)
Author:
gkronber
Message:

Added a special predictor builder for linear scaling tree evaluation and changed default GP engines to use linear scaling evaluators. #823.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/ScalingTreeEvaluator.cs

    r2640 r2722  
    5151      this.sampleIndex = sampleIndex;
    5252      double estimation = EvaluateBakedCode();
    53       if (double.IsPositiveInfinity(estimation)) estimation = UpperEvaluationLimit;
    54       else if (double.IsNegativeInfinity(estimation)) estimation = LowerEvaluationLimit;
    55       else if (double.IsNaN(estimation)) estimation = UpperEvaluationLimit;
     53      //if (double.IsPositiveInfinity(estimation)) estimation = UpperEvaluationLimit;
     54      //else if (double.IsNegativeInfinity(estimation)) estimation = LowerEvaluationLimit;
     55      //else if (double.IsNaN(estimation)) estimation = UpperEvaluationLimit;
    5656      return estimation;
    5757    }
    5858
    5959    public override IEnumerable<double> Evaluate(Dataset dataset, IFunctionTree tree, IEnumerable<int> rows) {
    60       double[] result = base.Evaluate(dataset, tree, rows).ToArray();
    6160      int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
    62       double tMean = dataset.GetMean(targetVariableIndex);
    63       double xMean = Statistics.Mean(result);
    64       double sumXT = 0;
    65       double sumXX = 0;
    66       for (int i = 0; i < result.Length; i++) {
    67         double x = result[i];
    68         double t = dataset.GetValue(rows.ElementAt(i), targetVariableIndex);
    69         sumXT += (x - xMean) * (t - tMean);
    70         sumXX += (x - xMean) * (x - xMean);
     61      // evaluate for all rows
     62      PrepareForEvaluation(dataset, tree);
     63      var result = (from row in rows
     64                    let y = Evaluate(row)
     65                    let y_ = dataset.GetValue(row, targetVariableIndex)
     66                    select new { Row = row, Estimation = y, Target = y_ }).ToArray();
     67
     68      // calculate alpha and beta on the subset of rows with valid values
     69      var filteredResult = result.Where(x => IsValidValue(x.Target) && IsValidValue(x.Estimation));
     70      var target = filteredResult.Select(x => x.Target);
     71      var estimation = filteredResult.Select(x => x.Estimation);
     72      double a, b;
     73      if (filteredResult.Count() > 2) {
     74        double tMean = target.Sum() / target.Count();
     75        double xMean = estimation.Sum() / estimation.Count();
     76        double sumXT = 0;
     77        double sumXX = 0;
     78        foreach (var r in result) {
     79          double x = r.Estimation;
     80          double t = r.Target;
     81          sumXT += (x - xMean) * (t - tMean);
     82          sumXX += (x - xMean) * (x - xMean);
     83        }
     84        b = sumXT / sumXX;
     85        a = tMean - b * xMean;
     86      } else {
     87        b = 1.0;
     88        a = 0.0;
    7189      }
    72       double b = sumXT / sumXX;
    73       double a = tMean - b * xMean;
    74       for (int i = 0; i < result.Length; i++) {
    75         double scaledResult = result[i] * b + a;
    76         scaledResult = Math.Min(Math.Max(scaledResult, LowerEvaluationLimit), UpperEvaluationLimit);
    77         if (double.IsNaN(scaledResult)) scaledResult = UpperEvaluationLimit;
    78         result[i] = scaledResult;
    79       }
    80       return result;
     90      // return scaled results
     91      return from r in result
     92             let scaledR = Math.Min(Math.Max(r.Estimation * b + a, LowerEvaluationLimit), UpperEvaluationLimit)
     93             select double.IsNaN(scaledR) ? UpperEvaluationLimit : scaledR;
     94    }
     95
     96    private bool IsValidValue(double d) {
     97      return !double.IsInfinity(d) && !double.IsNaN(d);
    8198    }
    8299
Note: See TracChangeset for help on using the changeset viewer.