using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Common; using HeuristicLab.Problems.DataAnalysis; using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; namespace HeuristicLab.Problems.TradeRules { /// /// Represents a regression data analysis solution /// [StorableClass] public abstract class TradeRulesSolution : TradeRulesSolutionBase { protected readonly Dictionary evaluationCache; [StorableConstructor] protected TradeRulesSolution(bool deserializing) : base(deserializing) { evaluationCache = new Dictionary(); } protected TradeRulesSolution(TradeRulesSolution original, Cloner cloner) : base(original, cloner) { evaluationCache = new Dictionary(original.evaluationCache); } protected TradeRulesSolution(IRegressionModel model, IRegressionProblemData problemData) : base(model, problemData) { evaluationCache = new Dictionary(problemData.Dataset.Rows); } protected override void RecalculateResults() { CalculateResults(); } public override IEnumerable EstimatedValues { get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); } } public override IEnumerable EstimatedTrainingValues { get { return GetEstimatedValues(ProblemData.TrainingIndices); } } public override IEnumerable EstimatedTestValues { get { return GetEstimatedValues(ProblemData.TestIndices); } } public override IEnumerable GetEstimatedValues(IEnumerable rows) { var rowsToEvaluate = rows.Except(evaluationCache.Keys); var rowsEnumerator = rowsToEvaluate.GetEnumerator(); var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator(); while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) { evaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current); } return rows.Select(row => evaluationCache[row]); } protected override void OnProblemDataChanged() { evaluationCache.Clear(); base.OnProblemDataChanged(); } protected override void OnModelChanged() { evaluationCache.Clear(); base.OnModelChanged(); } } }