Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/03/14 16:21:07 (11 years ago)
Author:
bburlacu
Message:

#1837: Performance and usability improvements.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SlidingWindow/SlidingWindowBestSolutionsCollection.cs

    r10720 r10721  
    3535    [Storable]
    3636    private List<SlidingWindowRange> slidingWindowRanges;
     37
    3738    public List<SlidingWindowRange> SlidingWindowRanges {
    3839      get { return slidingWindowRanges; }
     
    5556    [Storable]
    5657    private Dictionary<SlidingWindowRange, ISymbolicExpressionTree> slidingWindowBestSolutions;
     58
    5759    public Dictionary<SlidingWindowRange, ISymbolicExpressionTree> SlidingWindowBestSolutions {
    5860      get { return slidingWindowBestSolutions; }
     
    6264    [Storable]
    6365    private IDataAnalysisProblemData problemData;
     66
    6467    public IDataAnalysisProblemData ProblemData {
    6568      get { return problemData; }
     
    6972    [Storable]
    7073    private ISymbolicDataAnalysisExpressionTreeInterpreter interpreter;
     74
    7175    public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter {
    7276      get { return interpreter; }
     
    7680    [Storable]
    7781    private bool applyLinearScaling;
     82
    7883    public bool ApplyLinearScaling {
    7984      get { return applyLinearScaling; }
     
    98103
    99104    private QualityMeasures qualityMeasure;
     105
    100106    public QualityMeasures QualityMeasure {
    101107      get { return qualityMeasure; }
     
    122128    }
    123129
     130    public event EventHandler QualitiesCalculationStarted;
     131
     132    private void OnQualitiesCalculationStarted(object sender, EventArgs e) {
     133      var started = QualitiesCalculationStarted;
     134      if (started != null) {
     135        started(sender, e);
     136      }
     137    }
     138
    124139    public event EventHandler QualitiesUpdated;
    125140    private void OnQualitiesUpdated(object sender, EventArgs e) {
    126141      var updated = QualitiesUpdated;
    127       if (updated != null) { updated(sender, e); }
     142      if (updated != null) {
     143        updated(sender, e);
     144      }
    128145    }
    129146
    130147    [StorableConstructor]
    131     protected SlidingWindowBestSolutionsCollection(bool deserializing) : base(deserializing) { }
     148    protected SlidingWindowBestSolutionsCollection(bool deserializing)
     149      : base(deserializing) {
     150    }
     151
    132152    protected SlidingWindowBestSolutionsCollection(SlidingWindowBestSolutionsCollection original, Cloner cloner)
    133153      : base(original, cloner) {
     
    137157      this.applyLinearScaling = original.ApplyLinearScaling;
    138158    }
     159
    139160    protected SlidingWindowBestSolutionsCollection() {
    140161      slidingWindowBestSolutions = new Dictionary<SlidingWindowRange, ISymbolicExpressionTree>();
     
    154175
    155176    public ISymbolicExpressionTree this[SlidingWindowRange key] {
    156       get {
    157         return slidingWindowBestSolutions[key];
    158       }
     177      get { return slidingWindowBestSolutions[key]; }
    159178      set {
    160179        AddSolution(key, value); // this should be fast so there's no need for a background worker
     
    177196    }
    178197
    179     public abstract ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    180      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue);
    181 
    182     public abstract ISymbolicDataAnalysisSolution CreateSolution(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData);
     198    public abstract ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree,
     199      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     200      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue);
     201
     202    public abstract ISymbolicDataAnalysisSolution CreateSolution(ISymbolicDataAnalysisModel model,
     203      IDataAnalysisProblemData problemData);
    183204
    184205    private void AddSolution(SlidingWindowRange range, ISymbolicExpressionTree solution) {
    185       // add a solution and update the qualities matrix
    186206      Add(range, solution);
    187207
     
    193213      var trainingIndices = problemData.TrainingIndices.ToList();
    194214      var matrix = new double[nRows, nCols];
    195       List<int> rows;
     215
    196216      // copy old qualities into the new matrix
    197217      for (int i = 0; i < nRows - 1; ++i) {
     
    201221      }
    202222      // copy qualities of new solution into the new matrix
     223      var rows = Enumerable.Range(slidingWindowRanges.First().Start, slidingWindowRanges.Last().End - slidingWindowRanges.First().Start).ToList();
     224      var estimatedValues = Interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows).ToList();
     225      var originalValues = ProblemData.Dataset.GetDoubleValues(GetTargetVariable(ProblemData), rows).ToList();
    203226      for (int i = 0; i < nCols; ++i) {
    204         rows = i == nCols - 1 ? trainingIndices : Enumerable.Range(slidingWindowRanges[i].Start, slidingWindowRanges[i].Size).ToList();
    205         matrix[nRows - 1, i] = CalculateQuality(solution, rows);
     227        if (i == nCols - 1) {
     228          matrix[nRows - 1, i] = CalculateQuality(solution, trainingIndices);
     229        } else {
     230          var indices = Enumerable.Range(slidingWindowRanges[i].Start, slidingWindowRanges[i].Size).ToList();
     231          var estimated = indices.Select(x => estimatedValues[x]);
     232          var original = indices.Select(x => originalValues[x]);
     233          matrix[nRows - 1, i] = CalculateQuality(estimated, original);
     234        }
    206235      }
    207236      // shift old training qualities one column to the right
     
    223252      }
    224253
     254      OnQualitiesCalculationStarted(this, EventArgs.Empty);
     255
    225256      var ranges = SlidingWindowRanges;
    226257      var solutions = ranges.Select(x => SlidingWindowBestSolutions[x]).ToList();
    227258
    228       int rows = solutions.Count;
    229       int columns = ranges.Count + 1;
    230 
    231       SlidingWindowQualities = new double[rows, columns];
    232 
    233       for (int i = 0; i < rows; ++i) {
     259      int nRows = solutions.Count;
     260      int nCols = ranges.Count + 1;
     261
     262      SlidingWindowQualities = new double[nRows, nCols];
     263      var rows = Enumerable.Range(ranges.First().Start, ranges.Last().End - ranges.First().Start).ToList();
     264      var originalValues = ProblemData.Dataset.GetDoubleValues(GetTargetVariable(ProblemData), rows).ToList();
     265
     266      for (int i = 0; i < nRows; ++i) {
    234267        if (worker.CancellationPending) {
    235268          e.Cancel = true;
     
    238271
    239272        var solution = solutions[i];
    240         for (int j = 0; j < columns; ++j) {
    241           var range = (j == columns - 1) ? ProblemData.TrainingIndices : Enumerable.Range(ranges[j].Start, ranges[j].Size);
    242           var q = CalculateQuality(solution, range);
     273        var estimatedValues = Interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows).ToList();
     274
     275        for (int j = 0; j < nCols; ++j) {
     276          double q;
     277          if (j == nCols - 1) {
     278            q = CalculateQuality(solution, problemData.TrainingIndices);
     279          } else {
     280            var range = ranges[j];
     281            var indices = Enumerable.Range(range.Start, range.Size).ToList();
     282            var estimated = indices.Select(x => estimatedValues[x]);
     283            var original = indices.Select(x => originalValues[x]);
     284
     285            q = CalculateQuality(estimated, original);
     286          }
    243287
    244288          SlidingWindowQualities[i, j] = q;
    245289        }
    246290
    247         worker.ReportProgress((int)Math.Round(i * 100.0 / rows));
     291        worker.ReportProgress((int)Math.Round(i * 100.0 / nRows));
    248292      }
    249293    }
     
    259303      if (classificationProblemData != null) return classificationProblemData.TargetVariable;
    260304      throw new NotSupportedException();
     305    }
     306
     307    private double CalculateQuality(IEnumerable<double> estimatedValues, IEnumerable<double> originalValues) {
     308      var errorState = OnlineCalculatorError.None;
     309      double quality = 0.0;
     310      switch (QualityMeasure) {
     311        case QualityMeasures.PEARSON:
     312          quality = OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState);
     313          break;
     314        case QualityMeasures.MSE:
     315          quality = OnlineMeanSquaredErrorCalculator.Calculate(estimatedValues, originalValues, out errorState);
     316          break;
     317      }
     318      return errorState == OnlineCalculatorError.None ? quality : double.NaN;
    261319    }
    262320
Note: See TracChangeset for help on using the changeset viewer.