Changeset 15736
- Timestamp:
- 02/07/18 20:55:57 (7 years ago)
- Location:
- branches/1614_GeneralizedQAP
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1614_GeneralizedQAP/HeuristicLab.OptimizationExpertSystem.Common/3.3/KnowledgeCenter.cs
r15255 r15736 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 24 using System.Drawing; 25 using System.IO; 26 using System.Linq; 27 using System.Threading; 28 using System.Threading.Tasks; 29 using HeuristicLab.Algorithms.DataAnalysis; 22 30 using HeuristicLab.Analysis; 23 31 using HeuristicLab.Analysis.SelfOrganizingMaps; … … 32 40 using HeuristicLab.Problems.DataAnalysis; 33 41 using HeuristicLab.Random; 34 using System;35 using System.Collections.Generic;36 using System.Drawing;37 using System.IO;38 using System.Linq;39 using System.Threading;40 using System.Threading.Tasks;41 using HeuristicLab.Algorithms.DataAnalysis;42 42 using Algorithm = HeuristicLab.Clients.OKB.Administration.Algorithm; 43 43 using Problem = HeuristicLab.Clients.OKB.Administration.Problem; … … 296 296 297 297 private static readonly HashSet<string> InterestingValueNames = new HashSet<string>() { 298 "QualityPerEvaluations", " Problem Name", "Problem Type", "Algorithm Name", "Algorithm Type", "Maximization", "BestKnownQuality"298 "QualityPerEvaluations", "QualityPerClock", "Problem Name", "Problem Type", "Algorithm Name", "Algorithm Type", "Maximization", "BestKnownQuality" 299 299 }; 300 300 … … 517 517 518 518 IItem bkParam; 519 if (!prob.Parameters.TryGetValue("BestKnownQuality", out bkParam) || !(bkParam is DoubleValue) ) {519 if (!prob.Parameters.TryGetValue("BestKnownQuality", out bkParam) || !(bkParam is DoubleValue) || double.IsNaN(((DoubleValue)bkParam).Value)) { 520 520 var list = kvp.Value.SelectMany(x => x.Value) 521 .Where(x => x.Results.ContainsKey("QualityPerEvaluations")) 522 .Select(x => ((IndexedDataTable<double>)x.Results["QualityPerEvaluations"]).Rows.First().Values.Last().Item2); 521 .Where(x => x.Results.ContainsKey("QualityPerClock")) 522 .Select(x => (IndexedDataTable<double>)x.Results["QualityPerClock"]) 523 .Where(x => x.Rows.Count > 0 && x.Rows.First().Values.Count > 0) 524 .Select(x => x.Rows.First().Values.Last().Item2); 523 525 if (!list.Any()) continue; 524 526 bkParam = new DoubleValue(maximization ? list.Max() : list.Min()); … … 765 767 return knowledgeBase.Where(x => ((StringValue)x.Parameters["Problem Name"]).Value == ((StringValue)problemInstance.Parameters["Problem Name"]).Value) 766 768 .GroupBy(x => algorithmId2AlgorithmInstanceMapping.GetByFirst(algorithmId2RunMapping.GetBySecond(x).Single())) 767 .ToDictionary(x => x.Key, x => ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPer Evaluations", target, Maximization).ExpectedRuntime);769 .ToDictionary(x => x.Key, x => ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerClock", target, Maximization).ExpectedRuntime); 768 770 } 769 771 … … 773 775 return knowledgeBase.Where(x => ((StringValue)x.Parameters["Problem Name"]).Value == ((StringValue)problemInstance.Parameters["Problem Name"]).Value) 774 776 .GroupBy(x => algorithmId2AlgorithmInstanceMapping.GetByFirst(algorithmId2RunMapping.GetBySecond(x).Single())) 775 .ToDictionary(x => x.Key, x => Math.Log10(ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPer Evaluations", target, Maximization).ExpectedRuntime));777 .ToDictionary(x => x.Key, x => Math.Log10(ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerClock", target, Maximization).ExpectedRuntime)); 776 778 } 777 779 … … 806 808 var max = ((BoolValue)prob.Parameters["Maximization"]).Value; 807 809 var bkq = ((DoubleValue)prob.Parameters["BestKnownQuality"]).Value; 808 var ert = ExpectedRuntimeHelper.CalculateErt(pr.ToList(), "QualityPer Evaluations", GetTarget(bkq, target, max), max).ExpectedRuntime;810 var ert = ExpectedRuntimeHelper.CalculateErt(pr.ToList(), "QualityPerClock", GetTarget(bkq, target, max), max).ExpectedRuntime; 809 811 if (double.IsInfinity(ert)) ert = int.MaxValue; 810 812 ds.AddRow(new object[] { pr.Key }.Concat(f.Cast<object>()).Concat(new object[] { ert })); … … 872 874 873 875 var values = pr.GroupBy(x => algorithmId2RunMapping.GetBySecond(x).Single()) 874 .ToDictionary(x => x.Key, x => Math.Log10(ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPer Evaluations", GetTarget(bkq, target, max), max).ExpectedRuntime));876 .ToDictionary(x => x.Key, x => Math.Log10(ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerClock", GetTarget(bkq, target, max), max).ExpectedRuntime)); 875 877 var ranks = ClusteringHelper<long>.Cluster(nClasses, values, x => double.IsInfinity(x.Value)) 876 878 .GetByCluster().ToList(); -
branches/1614_GeneralizedQAP/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders/KNearestNeighborModel.cs
r13878 r15736 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 22 25 using HeuristicLab.Collections; 23 26 using HeuristicLab.Common; 24 27 using HeuristicLab.Optimization; 25 using System;26 using System.Collections.Generic;27 using System.Linq;28 28 29 29 namespace HeuristicLab.OptimizationExpertSystem.Common { … … 55 55 else feature[f] = (feature[f] - means[f]) / sdevs[f]; 56 56 } 57 var dist = features.Select((f, i) => Math.Sqrt(f.Select((g, j) => Math.Sqrt((g - feature[j]) * (g - feature[j]))).Sum())).ToList(); 57 58 var nearestK = features.Select((f, i) => new { ProblemInstanceIndex = i, Feature = f }) 58 .OrderBy(x => x.Feature.Select((f, i) => (f - feature[i]) * (f - feature[i])).Sum());59 .OrderBy(x => dist[x.ProblemInstanceIndex]); 59 60 60 61 var performances = new Dictionary<IAlgorithm, Performance>(); -
branches/1614_GeneralizedQAP/HeuristicLab.OptimizationExpertSystem/3.3/Views/PerformanceModelingView.Designer.cs
r13803 r15736 57 57 this.topNComboBox = new System.Windows.Forms.ComboBox(); 58 58 this.topNlabel = new System.Windows.Forms.Label(); 59 this.ndcgLabel = new System.Windows.Forms.Label(); 59 60 this.kendallsTauLabel = new System.Windows.Forms.Label(); 60 61 this.predictedLabel = new System.Windows.Forms.Label(); 61 62 this.absoluteLogErrorLabel = new System.Windows.Forms.Label(); 62 63 this.absoluteErrorLabel = new System.Windows.Forms.Label(); 64 this.ncdgView = new HeuristicLab.Data.Views.StringConvertibleValueView(); 63 65 this.kendallsTauView = new HeuristicLab.Data.Views.StringConvertibleValueView(); 64 66 this.absoluteLogErrorView = new HeuristicLab.Data.Views.StringConvertibleValueView(); … … 68 70 this.minTargetView = new HeuristicLab.Data.Views.StringConvertibleValueView(); 69 71 this.minimumTargetLabel = new System.Windows.Forms.Label(); 70 this. ncdgView = new HeuristicLab.Data.Views.StringConvertibleValueView();71 this. ndcgLabel = new System.Windows.Forms.Label();72 this.spearmansRhoView = new HeuristicLab.Data.Views.StringConvertibleValueView(); 73 this.spearmansRhoLabel = new System.Windows.Forms.Label(); 72 74 this.tabControl.SuspendLayout(); 73 75 this.characteristicsTabPage.SuspendLayout(); … … 186 188 this.crossvalidationTabPage.Controls.Add(this.topNlabel); 187 189 this.crossvalidationTabPage.Controls.Add(this.ndcgLabel); 190 this.crossvalidationTabPage.Controls.Add(this.spearmansRhoLabel); 188 191 this.crossvalidationTabPage.Controls.Add(this.kendallsTauLabel); 189 192 this.crossvalidationTabPage.Controls.Add(this.predictedLabel); … … 191 194 this.crossvalidationTabPage.Controls.Add(this.absoluteErrorLabel); 192 195 this.crossvalidationTabPage.Controls.Add(this.ncdgView); 196 this.crossvalidationTabPage.Controls.Add(this.spearmansRhoView); 193 197 this.crossvalidationTabPage.Controls.Add(this.kendallsTauView); 194 198 this.crossvalidationTabPage.Controls.Add(this.absoluteLogErrorView); … … 223 227 this.topNlabel.Text = "Evaluate only best ranked:"; 224 228 // 229 // ndcgLabel 230 // 231 this.ndcgLabel.AutoSize = true; 232 this.ndcgLabel.Location = new System.Drawing.Point(519, 165); 233 this.ndcgLabel.Margin = new System.Windows.Forms.Padding(3); 234 this.ndcgLabel.Name = "ndcgLabel"; 235 this.ndcgLabel.Size = new System.Drawing.Size(106, 13); 236 this.ndcgLabel.TabIndex = 3; 237 this.ndcgLabel.Text = "Mean NDCG (top N):"; 238 // 225 239 // kendallsTauLabel 226 240 // … … 263 277 this.absoluteErrorLabel.TabIndex = 3; 264 278 this.absoluteErrorLabel.Text = "Mean Absolute Error (top N):"; 279 // 280 // ncdgView 281 // 282 this.ncdgView.Caption = "StringConvertibleValue View"; 283 this.ncdgView.Content = null; 284 this.ncdgView.LabelVisible = false; 285 this.ncdgView.Location = new System.Drawing.Point(503, 184); 286 this.ncdgView.Name = "ncdgView"; 287 this.ncdgView.ReadOnly = true; 288 this.ncdgView.Size = new System.Drawing.Size(194, 21); 289 this.ncdgView.TabIndex = 2; 265 290 // 266 291 // kendallsTauView … … 339 364 this.minimumTargetLabel.Text = "Target:"; 340 365 // 341 // ncdgView342 // 343 this. ncdgView.Caption = "StringConvertibleValue View";344 this. ncdgView.Content = null;345 this. ncdgView.LabelVisible = false;346 this. ncdgView.Location = new System.Drawing.Point(503, 184);347 this. ncdgView.Name = "ncdgView";348 this. ncdgView.ReadOnly = true;349 this. ncdgView.Size = new System.Drawing.Size(194, 21);350 this. ncdgView.TabIndex = 2;351 // 352 // ndcgLabel353 // 354 this. ndcgLabel.AutoSize = true;355 this. ndcgLabel.Location = new System.Drawing.Point(519, 165);356 this. ndcgLabel.Margin = new System.Windows.Forms.Padding(3);357 this. ndcgLabel.Name = "ndcgLabel";358 this. ndcgLabel.Size = new System.Drawing.Size(106, 13);359 this. ndcgLabel.TabIndex = 3;360 this. ndcgLabel.Text = "Mean NDCG (top N):";366 // spearmansRhoView 367 // 368 this.spearmansRhoView.Caption = "StringConvertibleValue View"; 369 this.spearmansRhoView.Content = null; 370 this.spearmansRhoView.LabelVisible = false; 371 this.spearmansRhoView.Location = new System.Drawing.Point(503, 303); 372 this.spearmansRhoView.Name = "spearmansRhoView"; 373 this.spearmansRhoView.ReadOnly = true; 374 this.spearmansRhoView.Size = new System.Drawing.Size(194, 21); 375 this.spearmansRhoView.TabIndex = 2; 376 // 377 // spearmansRhoLabel 378 // 379 this.spearmansRhoLabel.AutoSize = true; 380 this.spearmansRhoLabel.Location = new System.Drawing.Point(519, 284); 381 this.spearmansRhoLabel.Margin = new System.Windows.Forms.Padding(3); 382 this.spearmansRhoLabel.Name = "spearmansRhoLabel"; 383 this.spearmansRhoLabel.Size = new System.Drawing.Size(118, 13); 384 this.spearmansRhoLabel.TabIndex = 3; 385 this.spearmansRhoLabel.Text = "Mean Spearman\'s Rho:"; 361 386 // 362 387 // PerformanceModelingView … … 409 434 private System.Windows.Forms.Label ndcgLabel; 410 435 private Data.Views.StringConvertibleValueView ncdgView; 436 private System.Windows.Forms.Label spearmansRhoLabel; 437 private Data.Views.StringConvertibleValueView spearmansRhoView; 411 438 } 412 439 } -
branches/1614_GeneralizedQAP/HeuristicLab.OptimizationExpertSystem/3.3/Views/PerformanceModelingView.cs
r13878 r15736 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 25 using System.Threading.Tasks; 22 26 using HeuristicLab.Analysis; 23 27 using HeuristicLab.Common.Resources; … … 28 32 using HeuristicLab.OptimizationExpertSystem.Common; 29 33 using HeuristicLab.PluginInfrastructure; 30 using System;31 using System.Collections.Generic;32 using System.Linq;33 using System.Threading.Tasks;34 34 35 35 namespace HeuristicLab.OptimizationExpertSystem { … … 181 181 var tau = 0.0; 182 182 var tauCnt = 0; 183 var rho = 0.0; 184 var rhoCnt = 0; 183 185 var ndcg = 0.0; 184 186 var ndcgCnt = 0; … … 207 209 } 208 210 var kt = KendallsTau(observed, predicted); 209 if (!double.IsNaN(kt) ) {211 if (!double.IsNaN(kt) && !double.IsInfinity(kt)) { 210 212 tau += kt; 211 213 tauCnt++; 214 } 215 var sr = SpearmansRho(observed, predicted); 216 if (!double.IsNaN(sr)) { 217 rho += sr; 218 rhoCnt++; 212 219 } 213 220 var gain = NDCG(observed, model.GetRanking(pi).Take(topN).Select(x => x.Key).ToList()); … … 216 223 ndcgCnt++; 217 224 } 218 // mean reciprocal rank219 // optional: expected reciprocal rank220 225 progress.ProgressValue = ++count / (double)trainingSet.Length; 221 226 } … … 223 228 absLogErr /= absLogErrCnt; 224 229 tau /= tauCnt; 230 rho /= rhoCnt; 225 231 ndcg /= ndcgCnt; 226 232 … … 230 236 confusionMatrixView.Content = new IntMatrix(confMatrix) {ColumnNames = description}; 231 237 kendallsTauView.Content = new DoubleValue(tau); 238 spearmansRhoView.Content = new DoubleValue(rho); 232 239 ncdgView.Content = new DoubleValue(ndcg); 233 240 } finally { progress.Finish(); } … … 272 279 } 273 280 281 private static double SpearmansRho(Dictionary<IAlgorithm, double> performance, Dictionary<IAlgorithm, double> ranking) { 282 var commonKeys = performance.Keys.Intersect(ranking.Keys).ToList(); 283 var n = commonKeys.Count; 284 if (n == 0) return double.NaN; 285 286 var actualRanks = commonKeys.Select((x, i) => new { Alg = x, Index = i, Perf = performance[x] }) 287 .OrderBy(x => x.Perf).Select((x, i) => new { Index = x.Index, Rank = double.IsPositiveInfinity(x.Perf) ? int.MaxValue : i }) 288 .OrderBy(x => x.Index).Select(x => (double)x.Rank).ToArray(); 289 var predictedRanks = commonKeys.Select((x, i) => new { Alg = x, Index = i, Perf = ranking[x] }) 290 .OrderBy(x => x.Perf).Select((x, i) => new { Index = x.Index, Rank = double.IsPositiveInfinity(x.Perf) ? int.MaxValue : i }) 291 .OrderBy(x => x.Index).Select(x => (double)x.Rank).ToArray(); 292 293 return alglib.spearmancorr2(actualRanks, predictedRanks); 294 } 295 274 296 private static double KendallsTau(Dictionary<IAlgorithm, double> performance, Dictionary<IAlgorithm, double> ranking) { 275 297 var commonKeys = performance.Keys.Intersect(ranking.Keys).ToList(); … … 278 300 279 301 var actualRanks = commonKeys.Select((x, i) => new { Alg = x, Index = i, Perf = performance[x] }) 280 .OrderBy(x => x.Perf).Select((x, i) => new { Index = x.Index, Rank = i })302 .OrderBy(x => x.Perf).Select((x, i) => new { Index = x.Index, Rank = double.IsPositiveInfinity(x.Perf) ? int.MaxValue : i }) 281 303 .OrderBy(x => x.Index).Select(x => x.Rank).ToList(); 282 304 var predictedRanks = commonKeys.Select((x, i) => new { Alg = x, Index = i, Perf = ranking[x] }) 283 .OrderBy(x => x.Perf).Select((x, i) => new { Index = x.Index, Rank = i })305 .OrderBy(x => x.Perf).Select((x, i) => new { Index = x.Index, Rank = double.IsPositiveInfinity(x.Perf) ? int.MaxValue : i }) 284 306 .OrderBy(x => x.Index).Select(x => x.Rank).ToList(); 285 307 … … 292 314 || paired[i].Actual < paired[j].Actual && paired[i].Predicted < paired[j].Predicted) 293 315 concordant++; 294 else discordant++; 316 else if (paired[i].Actual > paired[j].Actual && paired[i].Predicted < paired[j].Predicted 317 || paired[i].Actual < paired[j].Actual && paired[i].Predicted > paired[j].Predicted) 318 discordant++; 295 319 } 296 return (2.0 * (concordant - discordant)) / (n * (n - 1)); 320 321 var ti = performance.GroupBy(x => x.Value).Sum(x => x.Count() * (x.Count() - 1)); 322 var uj = ranking.GroupBy(x => x.Value).Sum(x => x.Count() * (x.Count() - 1)); 323 return (2.0 * (concordant - discordant)) / Math.Sqrt( (n * (n - 1) - ti * (ti - 1)) * (n * (n - 1) - uj * (uj - 1)) ); 297 324 } 298 325
Note: See TracChangeset
for help on using the changeset viewer.