Free cookie consent management tool by TermsFeed Policy Generator

source: branches/sluengo/HeuristicLab.Problems.TradeRules/Solution/TradeRulesSolution.cs @ 11999

Last change on this file since 11999 was 9262, checked in by sluengo, 12 years ago
File size: 2.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6using HeuristicLab.Common;
7using HeuristicLab.Problems.DataAnalysis;
8using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
9
10namespace HeuristicLab.Problems.TradeRules
11{
12    /// <summary>
13    /// Represents a regression data analysis solution
14    /// </summary>
15    [StorableClass]
16    public abstract class TradeRulesSolution : TradeRulesSolutionBase
17    {
18       protected readonly Dictionary<int, double> evaluationCache;
19
20    [StorableConstructor]
21    protected TradeRulesSolution(bool deserializing)
22      : base(deserializing) {
23      evaluationCache = new Dictionary<int, double>();
24    }
25    protected TradeRulesSolution(TradeRulesSolution original, Cloner cloner)
26      : base(original, cloner) {
27      evaluationCache = new Dictionary<int, double>(original.evaluationCache);
28    }
29    protected TradeRulesSolution(IRegressionModel model, IRegressionProblemData problemData)
30      : base(model, problemData) {
31      evaluationCache = new Dictionary<int, double>(problemData.Dataset.Rows);
32    }
33
34    protected override void RecalculateResults() {
35      CalculateResults();
36    }
37
38    public override IEnumerable<double> EstimatedValues {
39      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
40    }
41    public override IEnumerable<double> EstimatedTrainingValues {
42      get { return GetEstimatedValues(ProblemData.TrainingIndices); }
43    }
44    public override IEnumerable<double> EstimatedTestValues {
45      get { return GetEstimatedValues(ProblemData.TestIndices); }
46    }
47
48    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
49      var rowsToEvaluate = rows.Except(evaluationCache.Keys);
50      var rowsEnumerator = rowsToEvaluate.GetEnumerator();
51      var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
52
53      while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
54        evaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
55      }
56
57      return rows.Select(row => evaluationCache[row]);
58    }
59
60    protected override void OnProblemDataChanged() {
61      evaluationCache.Clear();
62      base.OnProblemDataChanged();
63    }
64
65    protected override void OnModelChanged() {
66      evaluationCache.Clear();
67      base.OnModelChanged();
68    }
69    }
70}
Note: See TracBrowser for help on using the repository browser.