Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/28/11 13:57:05 (13 years ago)
Author:
mkommend
Message:

#1604: Enabled caching of evaluation results in data analysis solutions.

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationSolution.cs

    r6589 r6606  
    3131  [StorableClass]
    3232  public abstract class ClassificationSolution : ClassificationSolutionBase {
     33    protected readonly Dictionary<int, double> evaluationCache;
     34
    3335    [StorableConstructor]
    34     protected ClassificationSolution(bool deserializing) : base(deserializing) { }
     36    protected ClassificationSolution(bool deserializing)
     37      : base(deserializing) {
     38      evaluationCache = new Dictionary<int, double>();
     39    }
    3540    protected ClassificationSolution(ClassificationSolution original, Cloner cloner)
    3641      : base(original, cloner) {
     42      evaluationCache = new Dictionary<int, double>(original.evaluationCache);
    3743    }
    3844    public ClassificationSolution(IClassificationModel model, IClassificationProblemData problemData)
    3945      : base(model, problemData) {
     46      evaluationCache = new Dictionary<int, double>();
    4047    }
    4148
     
    5158
    5259    public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
    53       return Model.GetEstimatedClassValues(ProblemData.Dataset, rows);
     60      var rowsToEvaluate = rows.Except(evaluationCache.Keys);
     61      var rowsEnumerator = rowsToEvaluate.GetEnumerator();
     62      var valuesEnumerator = Model.GetEstimatedClassValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
     63
     64      while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
     65        evaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
     66      }
     67
     68      return rows.Select(row => evaluationCache[row]);
     69    }
     70
     71    protected override void OnProblemDataChanged() {
     72      evaluationCache.Clear();
     73      base.OnProblemDataChanged();
     74    }
     75
     76    protected override void OnModelChanged() {
     77      evaluationCache.Clear();
     78      base.OnModelChanged();
    5479    }
    5580  }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/DiscriminantFunctionClassificationSolution.cs

    r6604 r6606  
    3333  [Item("DiscriminantFunctionClassificationSolution", "Represents a classification solution that uses a discriminant function and classification thresholds.")]
    3434  public abstract class DiscriminantFunctionClassificationSolution : DiscriminantFunctionClassificationSolutionBase {
     35    protected readonly Dictionary<int, double> valueEvaluationCache;
     36    protected readonly Dictionary<int, double> classValueEvaluationCache;
    3537
    3638    [StorableConstructor]
    37     protected DiscriminantFunctionClassificationSolution(bool deserializing) : base(deserializing) { }
     39    protected DiscriminantFunctionClassificationSolution(bool deserializing)
     40      : base(deserializing) {
     41      valueEvaluationCache = new Dictionary<int, double>();
     42      classValueEvaluationCache = new Dictionary<int, double>();
     43    }
    3844    protected DiscriminantFunctionClassificationSolution(DiscriminantFunctionClassificationSolution original, Cloner cloner)
    3945      : base(original, cloner) {
     46      valueEvaluationCache = new Dictionary<int, double>(original.valueEvaluationCache);
     47      classValueEvaluationCache = new Dictionary<int, double>(original.classValueEvaluationCache);
    4048    }
    4149    protected DiscriminantFunctionClassificationSolution(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData)
    4250      : base(model, problemData) {
     51      valueEvaluationCache = new Dictionary<int, double>();
     52      classValueEvaluationCache = new Dictionary<int, double>();
     53
     54      SetAccuracyMaximizingThresholds();
    4355    }
    4456
     
    5466
    5567    public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
    56       return Model.GetEstimatedClassValues(ProblemData.Dataset, rows);
     68      var rowsToEvaluate = rows.Except(classValueEvaluationCache.Keys);
     69      var rowsEnumerator = rowsToEvaluate.GetEnumerator();
     70      var valuesEnumerator = Model.GetEstimatedClassValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
     71
     72      while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
     73        classValueEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
     74      }
     75
     76      return rows.Select(row => classValueEvaluationCache[row]);
    5777    }
    5878
     
    6989
    7090    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
    71       return Model.GetEstimatedValues(ProblemData.Dataset, rows);
     91      var rowsToEvaluate = rows.Except(valueEvaluationCache.Keys);
     92      var rowsEnumerator = rowsToEvaluate.GetEnumerator();
     93      var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
     94
     95      while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
     96        valueEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
     97      }
     98
     99      return rows.Select(row => valueEvaluationCache[row]);
     100    }
     101
     102    protected override void OnModelChanged() {
     103      valueEvaluationCache.Clear();
     104      classValueEvaluationCache.Clear();
     105      base.OnModelChanged();
     106    }
     107    protected override void OnModelThresholdsChanged(System.EventArgs e) {
     108      classValueEvaluationCache.Clear();
     109      base.OnModelThresholdsChanged(e);
     110    }
     111    protected override void OnProblemDataChanged() {
     112      valueEvaluationCache.Clear();
     113      classValueEvaluationCache.Clear();
     114      base.OnProblemDataChanged();
    72115    }
    73116  }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/DiscriminantFunctionClassificationSolutionBase.cs

    r6604 r6606  
    8686      Add(new Result(TestRSquaredResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleValue()));
    8787
    88       SetAccuracyMaximizingThresholds();
    8988      RegisterEventHandler();
    9089    }
     
    9594    }
    9695
    97     protected override void OnModelChanged(EventArgs e) {
     96    protected override void OnModelChanged() {
    9897      DeregisterEventHandler();
    9998      SetAccuracyMaximizingThresholds();
    10099      RegisterEventHandler();
    101       base.OnModelChanged(e);
     100      base.OnModelChanged();
    102101    }
    103102
     
    149148
    150149    protected virtual void OnModelThresholdsChanged(EventArgs e) {
    151       RecalculateResults();
     150      CalculateResults();
    152151    }
    153152
Note: See TracChangeset for help on using the changeset viewer.