Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10721


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

#1837: Performance and usability improvements.

Location:
branches/Sliding Window GP
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SlidingWindowBestSolutionsCollectionHeatMapView.Designer.cs

    r10688 r10721  
    1919 */
    2020#endregion
     21
     22using System.Windows.Forms;
    2123
    2224namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
     
    7678      this.comboBox1.Text = "Pearson R2";
    7779      this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
     80      this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
    7881      //
    7982      // label1
  • branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SlidingWindowBestSolutionsCollectionHeatMapView.cs

    r10720 r10721  
    4242      base.OnContentChanged();
    4343      if (Content != null) {
     44        // set the dropdown value to the correct value
     45        switch (Content.QualityMeasure) {
     46          case SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON:
     47            comboBox1.SelectedIndex = comboBox1.Items.IndexOf("Pearson R2");
     48            break;
     49          case SlidingWindowBestSolutionsCollection.QualityMeasures.MSE:
     50            comboBox1.SelectedIndex = comboBox1.Items.IndexOf("Mean Squared Error");
     51            break;
     52        }
    4453        if (Content.SlidingWindowQualities != null) {
    4554          SetHeatmapContent(Content.SlidingWindowQualities);
    4655        } else if (!Content.QualitiesCalculationInProgress) {
    47           progress.Start("Calculating best solution qualities...");
    4856          Content.CalculateQualities();
    4957        }
     
    6472      Content.QualitiesCalculationProgress -= bestSolutionCollectionQualitiesCalculationProgress;
    6573      Content.QualitiesUpdated -= bestSolutionCollectionQualitiesUpdated;
     74      Content.QualitiesCalculationStarted -= bestSolutionsCollectionQualtiesCalculationStarted;
    6675      base.DeregisterContentEvents();
    6776    }
     
    7382      Content.QualitiesCalculationProgress += bestSolutionCollectionQualitiesCalculationProgress;
    7483      Content.QualitiesUpdated += bestSolutionCollectionQualitiesUpdated;
     84      Content.QualitiesCalculationStarted += bestSolutionsCollectionQualtiesCalculationStarted;
    7585    }
    7686
     
    90100    private void SetHeatmapContent(double[,] values) {
    91101      double min = 0, max = 0;
    92       foreach (var q in values) {
    93         if (min > q) min = q;
    94         if (max < q) max = q;
     102      if (Content.QualityMeasure == SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON) {
     103        max = 1.0;
     104      } else {
     105        foreach (var q in values) {
     106          if (min > q) min = q;
     107          if (max < q) max = q;
     108        }
    95109      }
    96110      int rows = values.GetLength(0);
     
    103117      }
    104118      heatMapView.Content = new HeatMap(flipped, "Best Sliding Window Solutions", min, max);
     119    }
     120
     121    private void bestSolutionsCollectionQualtiesCalculationStarted(object sender, EventArgs e) {
     122      progress.Start("Calculating best solutions qualities...");
    105123    }
    106124
  • branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SlidingWindowBestSolutionsCollectionView.Designer.cs

    r10688 r10721  
    1919 */
    2020#endregion
     21
     22using System.Windows.Forms;
    2123
    2224namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
     
    8082      this.comboBox1.Text = "Pearson R2";
    8183      this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
     84      this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
    8285      //
    8386      // label1
  • branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SlidingWindowBestSolutionsCollectionView.cs

    r10720 r10721  
    4343      base.OnContentChanged();
    4444      if (Content != null) {
     45        switch (Content.QualityMeasure) {
     46          case SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON:
     47            comboBox1.SelectedIndex = comboBox1.Items.IndexOf("Pearson R2");
     48            break;
     49          case SlidingWindowBestSolutionsCollection.QualityMeasures.MSE:
     50            comboBox1.SelectedIndex = comboBox1.Items.IndexOf("Mean Squared Error");
     51            break;
     52        }
    4553        if (Content.SlidingWindowQualities != null) {
    4654          SetMatrixContent(Content.SlidingWindowQualities);
    4755        } else if (!Content.QualitiesCalculationInProgress) {
    48           progress.Start("Calculating best solution qualities...");
    4956          Content.CalculateQualities();
    5057        }
     
    6067      Content.QualitiesCalculationProgress -= bestSolutionCollectionQualitiesCalculationProgress;
    6168      Content.QualitiesUpdated -= bestSolutionCollectionQualitiesUpdated;
     69      Content.QualitiesCalculationStarted -= bestSolutionsCollectionQualtiesCalculationStarted;
    6270      base.DeregisterContentEvents();
    6371    }
     
    7078      Content.QualitiesCalculationProgress += bestSolutionCollectionQualitiesCalculationProgress;
    7179      Content.QualitiesUpdated += bestSolutionCollectionQualitiesUpdated;
     80      Content.QualitiesCalculationStarted += bestSolutionsCollectionQualtiesCalculationStarted;
    7281    }
    7382
     
    7988
    8089    #region Event Handlers (Content)
     90    private void bestSolutionsCollectionQualtiesCalculationStarted(object sender, EventArgs e) {
     91      progress.Start("Calculating best solutions qualities...");
     92    }
     93
    8194    private void bestSolutionsCollectionQualitiesCalculated(object sender, RunWorkerCompletedEventArgs e) {
    8295      if (e.Cancelled || e.Error != null) {
     
    87100      SetMatrixContent(Content.SlidingWindowQualities);
    88101      progress.Finish();
    89     }
    90 
    91     private void SetMatrixContent(double[,] values) {
    92       var qualitiesMap = new DoubleMatrix(values); // add an extra columns for the training data
    93       qualitiesMap.ColumnNames = Enumerable.Range(0, qualitiesMap.Columns - 1).Select(x => Content.SlidingWindowRanges[x].Start + " - " + Content.SlidingWindowRanges[x].End).Concat(new List<string> { "Training" });
    94       qualitiesMap.RowNames = Enumerable.Range(1, qualitiesMap.Rows).Select(x => "M" + x);
    95 
    96       double min = 0, max = 0;
    97       foreach (var q in qualitiesMap) {
    98         if (min > q) min = q;
    99         if (max < q) max = q;
    100       }
    101 
    102       enhancedStringConvertibleMatrixView.Minimum = min;
    103       enhancedStringConvertibleMatrixView.Maximum = max;
    104       enhancedStringConvertibleMatrixView.Content = qualitiesMap;
    105102    }
    106103
     
    117114      base.SetEnabledStateOfControls();
    118115      // TODO: Enable or disable controls based on whether the content is null or the view is set readonly
     116    }
     117
     118    private void SetMatrixContent(double[,] values) {
     119      var qualitiesMap = new DoubleMatrix(values); // add an extra columns for the training data
     120      qualitiesMap.ColumnNames = Enumerable.Range(0, qualitiesMap.Columns - 1).Select(x => Content.SlidingWindowRanges[x].Start + " - " + Content.SlidingWindowRanges[x].End).Concat(new List<string> { "Training" });
     121      qualitiesMap.RowNames = Enumerable.Range(1, qualitiesMap.Rows).Select(x => "M" + x);
     122      double min = 0, max = 0;
     123      if (Content.QualityMeasure == SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON) {
     124        max = 1.0;
     125      } else {
     126        foreach (var q in qualitiesMap) {
     127          if (min > q) min = q;
     128          if (max < q) max = q;
     129        }
     130      }
     131
     132      enhancedStringConvertibleMatrixView.Minimum = min;
     133      enhancedStringConvertibleMatrixView.Maximum = max;
     134      enhancedStringConvertibleMatrixView.Content = qualitiesMap;
    119135    }
    120136
  • 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.