Changeset 10721
- Timestamp:
- 04/03/14 16:21:07 (11 years ago)
- 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 19 19 */ 20 20 #endregion 21 22 using System.Windows.Forms; 21 23 22 24 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views { … … 76 78 this.comboBox1.Text = "Pearson R2"; 77 79 this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); 80 this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; 78 81 // 79 82 // label1 -
branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SlidingWindowBestSolutionsCollectionHeatMapView.cs
r10720 r10721 42 42 base.OnContentChanged(); 43 43 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 } 44 53 if (Content.SlidingWindowQualities != null) { 45 54 SetHeatmapContent(Content.SlidingWindowQualities); 46 55 } else if (!Content.QualitiesCalculationInProgress) { 47 progress.Start("Calculating best solution qualities...");48 56 Content.CalculateQualities(); 49 57 } … … 64 72 Content.QualitiesCalculationProgress -= bestSolutionCollectionQualitiesCalculationProgress; 65 73 Content.QualitiesUpdated -= bestSolutionCollectionQualitiesUpdated; 74 Content.QualitiesCalculationStarted -= bestSolutionsCollectionQualtiesCalculationStarted; 66 75 base.DeregisterContentEvents(); 67 76 } … … 73 82 Content.QualitiesCalculationProgress += bestSolutionCollectionQualitiesCalculationProgress; 74 83 Content.QualitiesUpdated += bestSolutionCollectionQualitiesUpdated; 84 Content.QualitiesCalculationStarted += bestSolutionsCollectionQualtiesCalculationStarted; 75 85 } 76 86 … … 90 100 private void SetHeatmapContent(double[,] values) { 91 101 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 } 95 109 } 96 110 int rows = values.GetLength(0); … … 103 117 } 104 118 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..."); 105 123 } 106 124 -
branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SlidingWindowBestSolutionsCollectionView.Designer.cs
r10688 r10721 19 19 */ 20 20 #endregion 21 22 using System.Windows.Forms; 21 23 22 24 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views { … … 80 82 this.comboBox1.Text = "Pearson R2"; 81 83 this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); 84 this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; 82 85 // 83 86 // label1 -
branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SlidingWindowBestSolutionsCollectionView.cs
r10720 r10721 43 43 base.OnContentChanged(); 44 44 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 } 45 53 if (Content.SlidingWindowQualities != null) { 46 54 SetMatrixContent(Content.SlidingWindowQualities); 47 55 } else if (!Content.QualitiesCalculationInProgress) { 48 progress.Start("Calculating best solution qualities...");49 56 Content.CalculateQualities(); 50 57 } … … 60 67 Content.QualitiesCalculationProgress -= bestSolutionCollectionQualitiesCalculationProgress; 61 68 Content.QualitiesUpdated -= bestSolutionCollectionQualitiesUpdated; 69 Content.QualitiesCalculationStarted -= bestSolutionsCollectionQualtiesCalculationStarted; 62 70 base.DeregisterContentEvents(); 63 71 } … … 70 78 Content.QualitiesCalculationProgress += bestSolutionCollectionQualitiesCalculationProgress; 71 79 Content.QualitiesUpdated += bestSolutionCollectionQualitiesUpdated; 80 Content.QualitiesCalculationStarted += bestSolutionsCollectionQualtiesCalculationStarted; 72 81 } 73 82 … … 79 88 80 89 #region Event Handlers (Content) 90 private void bestSolutionsCollectionQualtiesCalculationStarted(object sender, EventArgs e) { 91 progress.Start("Calculating best solutions qualities..."); 92 } 93 81 94 private void bestSolutionsCollectionQualitiesCalculated(object sender, RunWorkerCompletedEventArgs e) { 82 95 if (e.Cancelled || e.Error != null) { … … 87 100 SetMatrixContent(Content.SlidingWindowQualities); 88 101 progress.Finish(); 89 }90 91 private void SetMatrixContent(double[,] values) {92 var qualitiesMap = new DoubleMatrix(values); // add an extra columns for the training data93 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;105 102 } 106 103 … … 117 114 base.SetEnabledStateOfControls(); 118 115 // 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; 119 135 } 120 136 -
branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SlidingWindow/SlidingWindowBestSolutionsCollection.cs
r10720 r10721 35 35 [Storable] 36 36 private List<SlidingWindowRange> slidingWindowRanges; 37 37 38 public List<SlidingWindowRange> SlidingWindowRanges { 38 39 get { return slidingWindowRanges; } … … 55 56 [Storable] 56 57 private Dictionary<SlidingWindowRange, ISymbolicExpressionTree> slidingWindowBestSolutions; 58 57 59 public Dictionary<SlidingWindowRange, ISymbolicExpressionTree> SlidingWindowBestSolutions { 58 60 get { return slidingWindowBestSolutions; } … … 62 64 [Storable] 63 65 private IDataAnalysisProblemData problemData; 66 64 67 public IDataAnalysisProblemData ProblemData { 65 68 get { return problemData; } … … 69 72 [Storable] 70 73 private ISymbolicDataAnalysisExpressionTreeInterpreter interpreter; 74 71 75 public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { 72 76 get { return interpreter; } … … 76 80 [Storable] 77 81 private bool applyLinearScaling; 82 78 83 public bool ApplyLinearScaling { 79 84 get { return applyLinearScaling; } … … 98 103 99 104 private QualityMeasures qualityMeasure; 105 100 106 public QualityMeasures QualityMeasure { 101 107 get { return qualityMeasure; } … … 122 128 } 123 129 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 124 139 public event EventHandler QualitiesUpdated; 125 140 private void OnQualitiesUpdated(object sender, EventArgs e) { 126 141 var updated = QualitiesUpdated; 127 if (updated != null) { updated(sender, e); } 142 if (updated != null) { 143 updated(sender, e); 144 } 128 145 } 129 146 130 147 [StorableConstructor] 131 protected SlidingWindowBestSolutionsCollection(bool deserializing) : base(deserializing) { } 148 protected SlidingWindowBestSolutionsCollection(bool deserializing) 149 : base(deserializing) { 150 } 151 132 152 protected SlidingWindowBestSolutionsCollection(SlidingWindowBestSolutionsCollection original, Cloner cloner) 133 153 : base(original, cloner) { … … 137 157 this.applyLinearScaling = original.ApplyLinearScaling; 138 158 } 159 139 160 protected SlidingWindowBestSolutionsCollection() { 140 161 slidingWindowBestSolutions = new Dictionary<SlidingWindowRange, ISymbolicExpressionTree>(); … … 154 175 155 176 public ISymbolicExpressionTree this[SlidingWindowRange key] { 156 get { 157 return slidingWindowBestSolutions[key]; 158 } 177 get { return slidingWindowBestSolutions[key]; } 159 178 set { 160 179 AddSolution(key, value); // this should be fast so there's no need for a background worker … … 177 196 } 178 197 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); 183 204 184 205 private void AddSolution(SlidingWindowRange range, ISymbolicExpressionTree solution) { 185 // add a solution and update the qualities matrix186 206 Add(range, solution); 187 207 … … 193 213 var trainingIndices = problemData.TrainingIndices.ToList(); 194 214 var matrix = new double[nRows, nCols]; 195 List<int> rows; 215 196 216 // copy old qualities into the new matrix 197 217 for (int i = 0; i < nRows - 1; ++i) { … … 201 221 } 202 222 // 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(); 203 226 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 } 206 235 } 207 236 // shift old training qualities one column to the right … … 223 252 } 224 253 254 OnQualitiesCalculationStarted(this, EventArgs.Empty); 255 225 256 var ranges = SlidingWindowRanges; 226 257 var solutions = ranges.Select(x => SlidingWindowBestSolutions[x]).ToList(); 227 258 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) { 234 267 if (worker.CancellationPending) { 235 268 e.Cancel = true; … … 238 271 239 272 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 } 243 287 244 288 SlidingWindowQualities[i, j] = q; 245 289 } 246 290 247 worker.ReportProgress((int)Math.Round(i * 100.0 / rows));291 worker.ReportProgress((int)Math.Round(i * 100.0 / nRows)); 248 292 } 249 293 } … … 259 303 if (classificationProblemData != null) return classificationProblemData.TargetVariable; 260 304 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; 261 319 } 262 320
Note: See TracChangeset
for help on using the changeset viewer.