Changeset 13878
- Timestamp:
- 06/07/16 16:29:59 (9 years ago)
- Location:
- branches/PerformanceComparison
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/KnowledgeCenter.cs
r13809 r13878 657 657 } 658 658 659 public static double[][] GetFeaturesStandardized(IRun[] problemInstances, string[] characteristics, out double[] means, out double[] sdevs, double[] medianValues = null) { 660 var instances = new double[problemInstances.Length][]; 661 var columns = new List<double>[characteristics.Length]; 662 for (var p = 0; p < problemInstances.Length; p++) { 663 instances[p] = new double[characteristics.Length]; 664 for (var f = 0; f < characteristics.Length; f++) { 665 if (columns[f] == null) { 666 columns[f] = new List<double>(problemInstances.Length); 667 } 668 IItem item; 669 if (problemInstances[p].Results.TryGetValue(characteristics[f], out item)) { 670 double val = 0; 671 var dItem = item as DoubleValue; 672 if (dItem != null) { 673 val = dItem.Value; 674 } else { 675 var iItem = item as IntValue; 676 if (iItem != null) val = iItem.Value; 677 else val = double.NaN; 678 } 679 if (double.IsNaN(val) && medianValues != null) 680 instances[p][f] = medianValues[f]; 681 else instances[p][f] = val; 682 columns[f].Add(instances[p][f]); 683 } else instances[p][f] = medianValues != null ? medianValues[f] : double.NaN; 684 } 685 } 686 687 means = new double[characteristics.Length]; 688 sdevs = new double[characteristics.Length]; 689 for (var f = 0; f < characteristics.Length; f++) { 690 var mean = columns[f].Average(); 691 var dev = columns[f].StandardDeviation(); 692 means[f] = mean; 693 sdevs[f] = dev; 694 for (var p = 0; p < problemInstances.Length; p++) { 695 if (dev.IsAlmost(0)) instances[p][f] = 0; 696 else instances[p][f] = (instances[p][f] - mean) / dev; 697 } 698 } 699 700 return instances; 701 } 702 659 703 public static double[] GetMedianValues(IRun[] problemInstances, string[] characteristics) { 660 704 var values = new List<double>[characteristics.Length]; … … 704 748 .GroupBy(x => algorithmId2AlgorithmInstanceMapping.GetByFirst(algorithmId2RunMapping.GetBySecond(x).Single())) 705 749 .ToDictionary(x => x.Key, x => ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerEvaluations", target, Maximization).ExpectedRuntime); 750 } 751 752 public Dictionary<IAlgorithm, double> GetAlgorithmPerformanceLog10(IRun problemInstance) { 753 if (!problemInstance.Parameters.ContainsKey("BestKnownQuality")) return new Dictionary<IAlgorithm, double>(); 754 var target = GetTarget(((DoubleValue)problemInstance.Parameters["BestKnownQuality"]).Value, MinimumTarget.Value, Maximization); 755 return knowledgeBase.Where(x => ((StringValue)x.Parameters["Problem Name"]).Value == ((StringValue)problemInstance.Parameters["Problem Name"]).Value) 756 .GroupBy(x => algorithmId2AlgorithmInstanceMapping.GetByFirst(algorithmId2RunMapping.GetBySecond(x).Single())) 757 .ToDictionary(x => x.Key, x => Math.Log10(ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerEvaluations", target, Maximization).ExpectedRuntime)); 706 758 } 707 759 … … 802 854 803 855 var values = pr.GroupBy(x => algorithmId2RunMapping.GetBySecond(x).Single()) 804 .ToDictionary(x => x.Key, x => ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerEvaluations", GetTarget(bkq, target, max), max).ExpectedRuntime);856 .ToDictionary(x => x.Key, x => Math.Log10(ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerEvaluations", GetTarget(bkq, target, max), max).ExpectedRuntime)); 805 857 var ranks = ClusteringHelper<long>.Cluster(nClasses, values, x => double.IsInfinity(x.Value)) 806 858 .GetByCluster().ToList(); -
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders/KNearestNeighborModel.cs
r13803 r13878 21 21 22 22 using HeuristicLab.Collections; 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Optimization; 25 using System; 24 26 using System.Collections.Generic; 25 27 using System.Linq; … … 46 48 47 49 public IEnumerable<KeyValuePair<IAlgorithm, double>> GetRanking(IRun problemInstance) { 48 var features = KnowledgeCenter.GetFeatures(performance.Keys.OrderBy(problemInstanceMap.GetBySecond).ToArray(), characteristics, medianValues); 50 double[] means, sdevs; 51 var features = KnowledgeCenter.GetFeaturesStandardized(performance.Keys.OrderBy(problemInstanceMap.GetBySecond).ToArray(), characteristics, out means, out sdevs, medianValues); 49 52 var feature = KnowledgeCenter.GetFeatures(new [] { problemInstance }, characteristics, medianValues)[0]; 53 for (var f = 0; f < feature.Length; f++) { 54 if (sdevs[f].IsAlmost(0)) feature[f] = 0; 55 else feature[f] = (feature[f] - means[f]) / sdevs[f]; 56 } 50 57 var nearestK = features.Select((f, i) => new { ProblemInstanceIndex = i, Feature = f }) 51 58 .OrderBy(x => x.Feature.Select((f, i) => (f - feature[i]) * (f - feature[i])).Sum()); … … 60 67 61 68 foreach (var p in perfs) { 62 var ert = p.Value;69 var ert = Math.Pow(10, p.Value); 63 70 Performance perf; 64 71 if (!performances.TryGetValue(p.Key, out perf)) { -
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders/KNearestNeighborRecommender.cs
r13791 r13878 49 49 50 50 public IRecommendationModel TrainModel(IRun[] problemInstances, KnowledgeCenter kc, string[] characteristics) { 51 var perfData = problemInstances.Select(pi => new { ProblemInstance = pi, Performance = kc.GetAlgorithmPerformance (pi) })51 var perfData = problemInstances.Select(pi => new { ProblemInstance = pi, Performance = kc.GetAlgorithmPerformanceLog10(pi) }) 52 52 .ToDictionary(x => x.ProblemInstance, x => x.Performance); 53 53 return new KNearestNeighborModel(KParameter.Value.Value, perfData, characteristics); -
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/Views/PerformanceModelingView.cs
r13803 r13878 186 186 var count = 0; 187 187 foreach (var pi in trainingSet) { 188 var observed = Content.GetAlgorithmPerformance (pi);188 var observed = Content.GetAlgorithmPerformanceLog10(pi); 189 189 if (observed.Count == 0) continue; 190 190 progress.Status = pi.Name + "..."; 191 191 var model = recommender.TrainModel(trainingSet.Where(x => x != pi).ToArray(), Content, features); 192 var predictedTopN = model.GetRanking(pi).Take(topN).ToDictionary(x => x.Key, x => x.Value);193 var predicted = model.GetRanking(pi).ToDictionary(x => x.Key, x => x.Value);192 var predictedTopN = model.GetRanking(pi).Take(topN).ToDictionary(x => x.Key, x => Math.Log10(x.Value)); 193 var predicted = model.GetRanking(pi).ToDictionary(x => x.Key, x => Math.Log10(x.Value)); 194 194 var ae = AbsoluteError(observed, predictedTopN); 195 195 if (!double.IsNaN(ae)) { … … 240 240 double actual; 241 241 if (!performance.TryGetValue(tuple.Key, out actual)) continue; 242 if (double.IsInfinity(actual)) actual = int.MaxValue;243 error += Math.Abs( actual - tuple.Value);242 if (double.IsInfinity(actual)) actual = Math.Log10(int.MaxValue); 243 error += Math.Abs(Math.Pow(10, actual) - Math.Pow(10, tuple.Value)); 244 244 count++; 245 245 } … … 253 253 double actual; 254 254 if (!performance.TryGetValue(tuple.Key, out actual)) continue; 255 if (double.IsInfinity(actual)) actual = int.MaxValue;256 error += Math.Abs( Math.Log10(actual) - Math.Log10(tuple.Value));255 if (double.IsInfinity(actual)) actual = Math.Log10(int.MaxValue); 256 error += Math.Abs(actual - tuple.Value); 257 257 count++; 258 258 }
Note: See TracChangeset
for help on using the changeset viewer.