Changeset 13803 for branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders/KNearestNeighborModel.cs
- Timestamp:
- 04/27/16 16:21:07 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders/KNearestNeighborModel.cs
r13794 r13803 49 49 var feature = KnowledgeCenter.GetFeatures(new [] { problemInstance }, characteristics, medianValues)[0]; 50 50 var nearestK = features.Select((f, i) => new { ProblemInstanceIndex = i, Feature = f }) 51 .OrderBy(x => x.Feature.Select((f, i) => (f - feature[i]) * (f - feature[i])).Sum()) 52 .Take(K); 51 .OrderBy(x => x.Feature.Select((f, i) => (f - feature[i]) * (f - feature[i])).Sum()); 53 52 54 var performances = new Dictionary<IAlgorithm, List<double>>(); 53 var performances = new Dictionary<IAlgorithm, Performance>(); 54 55 var k = 0; 55 56 foreach (var next in nearestK) { 57 if (k >= K) break; 56 58 var perfs = performance[problemInstanceMap.GetByFirst(next.ProblemInstanceIndex)]; 57 59 if (perfs.Count == 0) continue; … … 59 61 foreach (var p in perfs) { 60 62 var ert = p.Value; 61 if (double.IsNaN(ert)) ert = int.MaxValue; 62 List<double> erts; 63 if (!performances.TryGetValue(p.Key, out erts)) { 64 performances[p.Key] = new List<double>() { ert }; ; 65 } else erts.Add(ert); 63 Performance perf; 64 if (!performances.TryGetValue(p.Key, out perf)) { 65 perf = new Performance(); 66 performances[p.Key] = perf; 67 } 68 perf.Add(ert); 66 69 } 70 71 k++; 67 72 } 68 73 69 return performances.Select(x => new { Alg = x.Key, Perf = x.Value. Average() })74 return performances.Select(x => new { Alg = x.Key, Perf = x.Value.ExpectedRuntime() }) 70 75 .OrderBy(x => x.Perf) 71 76 .Select(x => new KeyValuePair<IAlgorithm, double>(x.Alg, x.Perf)); 72 77 } 78 79 private class Performance { 80 private readonly List<double> successful; 81 private int runs; 82 public int Fails { get { return runs - successful.Count; } } 83 84 public Performance() { 85 successful = new List<double>(); 86 } 87 88 public void Add(double ert) { 89 if (!double.IsInfinity(ert)) successful.Add(ert); 90 runs++; 91 } 92 93 public double ExpectedRuntime() { 94 if (successful.Count == 0) return int.MaxValue; 95 return successful.Average() / (successful.Count / (double)runs); 96 } 97 } 73 98 } 74 99 }
Note: See TracChangeset
for help on using the changeset viewer.