Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/28/12 16:15:16 (12 years ago)
Author:
gkronber
Message:

#1720 added estimated values caching to RegressionEnsembleSolution

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleSolution.cs

    r8151 r8152  
    5656
    5757    [Storable]
    58     private Dictionary<IRegressionModel, IntRange> trainingPartitions;
     58    private readonly Dictionary<IRegressionModel, IntRange> trainingPartitions;
    5959    [Storable]
    60     private Dictionary<IRegressionModel, IntRange> testPartitions;
     60    private readonly Dictionary<IRegressionModel, IntRange> testPartitions;
    6161
    6262    [StorableConstructor]
     
    156156    #region Evaluation
    157157    public override IEnumerable<double> EstimatedTrainingValues {
    158       get { return GetEstimatedValues(ProblemData.TrainingIndices, (r, m) => RowIsTrainingForModel(r, m) && !RowIsTestForModel(r, m)); }
     158      get {
     159        var rows = ProblemData.TrainingIndices;
     160        var rowsToEvaluate = rows.Except(trainingEstimatedValuesCache.Keys);
     161        var rowsEnumerator = rowsToEvaluate.GetEnumerator();
     162        var valuesEnumerator = GetEstimatedValues(rowsToEvaluate, (r, m) => RowIsTrainingForModel(r, m) && !RowIsTestForModel(r, m)).GetEnumerator();
     163
     164        while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
     165          trainingEstimatedValuesCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
     166        }
     167
     168        return rows.Select(row => trainingEstimatedValuesCache[row]);
     169      }
    159170    }
    160171
    161172    public override IEnumerable<double> EstimatedTestValues {
    162       get { return GetEstimatedValues(ProblemData.TestIndices, RowIsTestForModel); }
     173      get {
     174        var rows = ProblemData.TestIndices;
     175        var rowsToEvaluate = rows.Except(testEstimatedValuesCache.Keys);
     176        var rowsEnumerator = rowsToEvaluate.GetEnumerator();
     177        var valuesEnumerator = GetEstimatedValues(rowsToEvaluate, RowIsTestForModel).GetEnumerator();
     178
     179        while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
     180          testEstimatedValuesCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
     181        }
     182
     183        return rows.Select(row => testEstimatedValuesCache[row]);
     184      }
    163185    }
    164186
     
    191213
    192214    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
    193       return from xs in GetEstimatedValueVectors(ProblemData.Dataset, rows)
    194              select AggregateEstimatedValues(xs);
     215      var rowsToEvaluate = rows.Except(estimatedValuesCache.Keys);
     216      var rowsEnumerator = rowsToEvaluate.GetEnumerator();
     217      var valuesEnumerator = (from xs in GetEstimatedValueVectors(ProblemData.Dataset, rowsToEvaluate)
     218                              select AggregateEstimatedValues(xs))
     219                             .GetEnumerator();
     220
     221      while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
     222        estimatedValuesCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
     223      }
     224
     225      return rows.Select(row => estimatedValuesCache[row]);
    195226    }
    196227
     
    213244
    214245    protected override void OnProblemDataChanged() {
     246      trainingEstimatedValuesCache.Clear();
     247      testEstimatedValuesCache.Clear();
     248      estimatedValuesCache.Clear();
    215249      IRegressionProblemData problemData = new RegressionProblemData(ProblemData.Dataset,
    216250                                                                     ProblemData.AllowedInputVariables,
     
    241275    public void AddRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
    242276      regressionSolutions.AddRange(solutions);
     277
     278      trainingEstimatedValuesCache.Clear();
     279      testEstimatedValuesCache.Clear();
     280      estimatedValuesCache.Clear();
    243281    }
    244282    public void RemoveRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
    245283      regressionSolutions.RemoveRange(solutions);
     284
     285      trainingEstimatedValuesCache.Clear();
     286      testEstimatedValuesCache.Clear();
     287      estimatedValuesCache.Clear();
    246288    }
    247289
     
    265307      trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition;
    266308      testPartitions[solution.Model] = solution.ProblemData.TestPartition;
     309
     310      trainingEstimatedValuesCache.Clear();
     311      testEstimatedValuesCache.Clear();
     312      estimatedValuesCache.Clear();
    267313    }
    268314
     
    272318      trainingPartitions.Remove(solution.Model);
    273319      testPartitions.Remove(solution.Model);
     320
     321      trainingEstimatedValuesCache.Clear();
     322      testEstimatedValuesCache.Clear();
     323      estimatedValuesCache.Clear();
    274324    }
    275325  }
Note: See TracChangeset for help on using the changeset viewer.