Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6606 for trunk/sources


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
Files:
5 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
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisSolution.cs

    r6588 r6606  
    4848          if (value != null) {
    4949            this[ModelResultName].Value = value;
    50             OnModelChanged(EventArgs.Empty);
     50            OnModelChanged();
    5151          }
    5252        }
     
    6262            this[ProblemDataResultName].Value = value;
    6363            ProblemData.Changed += new EventHandler(ProblemData_Changed);
    64             OnProblemDataChanged(EventArgs.Empty);
     64            OnProblemDataChanged();
    6565          }
    6666        }
     
    8989
    9090    private void ProblemData_Changed(object sender, EventArgs e) {
    91       OnProblemDataChanged(e);
     91      OnProblemDataChanged();
    9292    }
    9393
    9494    public event EventHandler ModelChanged;
    95     protected virtual void OnModelChanged(EventArgs e) {
     95    protected virtual void OnModelChanged() {
    9696      RecalculateResults();
    9797      var listeners = ModelChanged;
    98       if (listeners != null) listeners(this, e);
     98      if (listeners != null) listeners(this, EventArgs.Empty);
    9999    }
    100100
    101101    public event EventHandler ProblemDataChanged;
    102     protected virtual void OnProblemDataChanged(EventArgs e) {
     102    protected virtual void OnProblemDataChanged() {
    103103      RecalculateResults();
    104104      var listeners = ProblemDataChanged;
    105       if (listeners != null) listeners(this, e);
     105      if (listeners != null) listeners(this, EventArgs.Empty);
    106106    }
    107107
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolution.cs

    r6602 r6606  
    3131  [StorableClass]
    3232  public abstract class RegressionSolution : RegressionSolutionBase {
     33    protected readonly Dictionary<int, double> evaluationCache;
     34
    3335    [StorableConstructor]
    34     protected RegressionSolution(bool deserializing) : base(deserializing) { }
     36    protected RegressionSolution(bool deserializing)
     37      : base(deserializing) {
     38      evaluationCache = new Dictionary<int, double>();
     39    }
    3540    protected RegressionSolution(RegressionSolution original, Cloner cloner)
    3641      : base(original, cloner) {
     42      evaluationCache = new Dictionary<int, double>(original.evaluationCache);
    3743    }
    3844    protected RegressionSolution(IRegressionModel model, IRegressionProblemData problemData)
    3945      : base(model, problemData) {
     46      evaluationCache = new Dictionary<int, double>();
    4047    }
    4148
     
    5562
    5663    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
    57       return Model.GetEstimatedValues(ProblemData.Dataset, rows);
     64      var rowsToEvaluate = rows.Except(evaluationCache.Keys);
     65      var rowsEnumerator = rowsToEvaluate.GetEnumerator();
     66      var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
     67
     68      while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
     69        evaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
     70      }
     71
     72      return rows.Select(row => evaluationCache[row]);
     73    }
     74
     75    protected override void OnProblemDataChanged() {
     76      evaluationCache.Clear();
     77      base.OnProblemDataChanged();
     78    }
     79
     80    protected override void OnModelChanged() {
     81      evaluationCache.Clear();
     82      base.OnModelChanged();
    5883    }
    5984  }
Note: See TracChangeset for help on using the changeset viewer.