Changeset 13787 for branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders
- Timestamp:
- 04/24/16 10:03:52 (9 years ago)
- Location:
- branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders
- Files:
-
- 3 added
- 3 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders/DistanceWeightedRecommender.cs
r13786 r13787 35 35 [StorableClass] 36 36 public class DistanceWeightedRecommender : ParameterizedNamedItem, IAlgorithmInstanceRecommender { 37 private KnowledgeCenter okc;38 39 private IFixedValueParameter<EnumValue<ProblemInstanceProximityType>> ProximityTypeParameter {40 get { return (IFixedValueParameter<EnumValue<ProblemInstanceProximityType>>)Parameters["ProximityType"]; }41 }42 37 43 38 private IFixedValueParameter<DoubleValue> NeighborhoodFactorParameter { 44 39 get { return (IFixedValueParameter<DoubleValue>)Parameters["NeighborhoodFactor"]; } 45 }46 47 public ProblemInstanceProximityType ProximityType {48 get { return ProximityTypeParameter.Value.Value; }49 set { ProximityTypeParameter.Value.Value = value; }50 40 } 51 41 … … 59 49 private DistanceWeightedRecommender(DistanceWeightedRecommender original, Cloner cloner) 60 50 : base(original, cloner) { } 61 public DistanceWeightedRecommender(KnowledgeCenter okc) { 62 this.okc = okc; 63 Parameters.Add(new FixedValueParameter<EnumValue<ProblemInstanceProximityType>>("ProximityType", "The type of neighbor proximity.", new EnumValue<ProblemInstanceProximityType>(ProblemInstanceProximityType.FeatureSpace))); 51 public DistanceWeightedRecommender() { 64 52 Parameters.Add(new FixedValueParameter<DoubleValue>("NeighborhoodFactor", "Penalize neighbors that are far away.", new DoubleValue(5))); 65 53 } … … 69 57 } 70 58 71 public IEnumerable<IAlgorithm> GetRanking() { 72 if (okc.Problem.ProblemId == -1) yield break; 73 74 var piDistances = okc.GetProblemDistances(ProximityType); 59 public IRecommendationModel TrainModel(KnowledgeCenter okc, string[] characteristics) { 60 var piDistances = okc.GetProblemDistances(characteristics); 75 61 var maxDist = piDistances.Max(x => x.Value); 76 62 var instances = new SortedList<double, IAlgorithm>(); … … 109 95 avgERT += new System.Random().NextDouble(); 110 96 } 111 instances.Add(avgERT, algorithm);97 instances.Add(avgERT, (IAlgorithm)algorithm.Clone()); 112 98 } 113 99 114 foreach (var alg in instances.Select(x => (IAlgorithm)x.Value.Clone())) 115 yield return alg; 100 return new FixedRankModel(instances.Select(x => Tuple.Create(x.Value, x.Key))); 116 101 } 117 102 } -
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders/KNearestNeighborRecommender.cs
r13786 r13787 23 23 using HeuristicLab.Core; 24 24 using HeuristicLab.Data; 25 using HeuristicLab.Optimization;26 25 using HeuristicLab.Parameters; 27 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using System.Collections.Generic;29 using System.Linq;30 27 31 28 namespace HeuristicLab.OptimizationExpertSystem.Common { … … 33 30 [StorableClass] 34 31 public sealed class KNearestNeighborRecommender : ParameterizedNamedItem, IAlgorithmInstanceRecommender { 35 private KnowledgeCenter okc; 36 37 private IFixedValueParameter<EnumValue<ProblemInstanceProximityType>> ProximityTypeParameter { 38 get { return (IFixedValueParameter<EnumValue<ProblemInstanceProximityType>>)Parameters["ProximityType"]; } 39 } 40 41 public ProblemInstanceProximityType ProximityType { 42 get { return ProximityTypeParameter.Value.Value; } 43 set { ProximityTypeParameter.Value.Value = value; } 44 } 45 32 46 33 private IFixedValueParameter<IntValue> KParameter { 47 34 get { return (IFixedValueParameter<IntValue>)Parameters["K"]; } … … 52 39 private KNearestNeighborRecommender(KNearestNeighborRecommender original, Cloner cloner) 53 40 : base(original, cloner) { } 54 public KNearestNeighborRecommender(KnowledgeCenter okc) { 55 this.okc = okc; 56 Parameters.Add(new FixedValueParameter<EnumValue<ProblemInstanceProximityType>>("ProximityType", "The type of neighbor proximity.", new EnumValue<ProblemInstanceProximityType>(ProblemInstanceProximityType.FeatureSpace))); 41 public KNearestNeighborRecommender() { 57 42 Parameters.Add(new FixedValueParameter<IntValue>("K", "The number of nearest neighbors to consider.", new IntValue(5))); 58 43 } … … 62 47 } 63 48 64 public IEnumerable<IAlgorithm> GetRanking() { 65 if (okc.Problem.ProblemId == -1) yield break; 66 67 var distances = okc.GetProblemDistances(ProximityType); 68 var K = KParameter.Value.Value; 69 var performances = new Dictionary<IAlgorithm, List<double>>(); 70 for (var k = 0; k < K; k++) { 71 if (distances.Count == 0) break; 72 var min = distances.MinItems(x => x.Value).First(); 73 // lookup algorithm performances in min 74 var perfs = okc.GetAlgorithmPerformance(min.Key); 75 if (perfs.Count == 0) { 76 k--; 77 continue; 78 } 79 foreach (var p in perfs) { 80 var ert = p.Value; 81 if (double.IsNaN(ert)) ert = int.MaxValue; 82 List<double> erts; 83 if (!performances.TryGetValue(p.Key, out erts)) { 84 performances[p.Key] = new List<double>() { ert }; ; 85 } else erts.Add(ert); 86 } 87 distances.Remove(min.Key); 88 } 89 foreach (var alg in performances.Select(x => new { Alg = x.Key, Perf = x.Value.Average() }) 90 .OrderBy(x => x.Perf) 91 .Select(x => x.Alg)) 92 yield return alg; 49 public IRecommendationModel TrainModel(KnowledgeCenter kc, string[] characteristics) { 50 return new KNearestNeighborModel(KParameter.Value.Value, characteristics); 93 51 } 94 52 } -
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders/OverallBestRecommender.cs
r13786 r13787 35 35 [StorableClass] 36 36 public class OverallBestRecommender : ParameterizedNamedItem, IAlgorithmInstanceRecommender { 37 private KnowledgeCenter okc;38 39 private IFixedValueParameter<EnumValue<ProblemInstanceProximityType>> ProximityTypeParameter {40 get { return (IFixedValueParameter<EnumValue<ProblemInstanceProximityType>>)Parameters["ProximityType"]; }41 }42 37 43 38 private IFixedValueParameter<DoubleValue> NeighborhoodFactorParameter { 44 39 get { return (IFixedValueParameter<DoubleValue>)Parameters["NeighborhoodFactor"]; } 45 }46 47 public ProblemInstanceProximityType ProximityType {48 get { return ProximityTypeParameter.Value.Value; }49 set { ProximityTypeParameter.Value.Value = value; }50 40 } 51 41 … … 59 49 private OverallBestRecommender(OverallBestRecommender original, Cloner cloner) 60 50 : base(original, cloner) { } 61 public OverallBestRecommender(KnowledgeCenter okc) { 62 this.okc = okc; 63 Parameters.Add(new FixedValueParameter<EnumValue<ProblemInstanceProximityType>>("ProximityType", "The type of neighbor proximity.", new EnumValue<ProblemInstanceProximityType>(ProblemInstanceProximityType.FeatureSpace))); 51 public OverallBestRecommender() { 64 52 Parameters.Add(new FixedValueParameter<DoubleValue>("NeighborhoodFactor", "Penalize neighbors that are far away.", new DoubleValue(5))); 65 53 } … … 69 57 } 70 58 71 public IEnumerable<IAlgorithm> GetRanking() { 72 if (okc.Problem.ProblemId == -1) yield break; 73 59 public IRecommendationModel TrainModel(KnowledgeCenter kc, string[] characteristics) { 74 60 var instances = new List<Tuple<IAlgorithm, double>>(); 75 foreach (var relevantRuns in okc.GetKnowledgeBaseByAlgorithm()) {61 foreach (var relevantRuns in kc.GetKnowledgeBaseByAlgorithm()) { 76 62 var algorithm = relevantRuns.Key; 77 63 var pis = relevantRuns.Value.Select(x => ((StringValue)x.Parameters["Problem Name"]).Value).Distinct() 78 .Select(x => Tuple.Create(x, okc.ProblemInstances.SingleOrDefault(y => ((StringValue)y.Parameters["Problem Name"]).Value == x)))64 .Select(x => Tuple.Create(x, kc.ProblemInstances.SingleOrDefault(y => ((StringValue)y.Parameters["Problem Name"]).Value == x))) 79 65 .Where(x => x.Item2 != null) 80 66 .Select(x => Tuple.Create(x.Item1, ((DoubleValue)x.Item2.Parameters["BestKnownQuality"]).Value)) … … 84 70 foreach (var problemRuns in relevantRuns.Value.GroupBy(x => ((StringValue)x.Parameters["Problem Name"]).Value)) { 85 71 var bkq = pis[problemRuns.Key]; 86 var ert = ExpectedRuntimeHelper.CalculateErt(problemRuns.ToList(), "QualityPerEvaluations", ( okc.Maximization ? (1 - okc.MinimumTarget.Value) : (1 + okc.MinimumTarget.Value)) * bkq, okc.Maximization).ExpectedRuntime;72 var ert = ExpectedRuntimeHelper.CalculateErt(problemRuns.ToList(), "QualityPerEvaluations", (kc.Maximization ? (1 - kc.MinimumTarget.Value) : (1 + kc.MinimumTarget.Value)) * bkq, kc.Maximization).ExpectedRuntime; 87 73 if (double.IsNaN(ert)) ert = int.MaxValue; 88 74 avgERT += ert; … … 90 76 } 91 77 avgERT /= count; 92 instances.Add(Tuple.Create( algorithm, avgERT));78 instances.Add(Tuple.Create((IAlgorithm)algorithm.Clone(), avgERT)); 93 79 } 94 80 95 foreach (var alg in instances.OrderBy(x => x.Item2).Select(x => (IAlgorithm)x.Item1.Clone())) 96 yield return alg; 81 return new FixedRankModel(instances.OrderBy(x => x.Item2)); 97 82 } 98 83 }
Note: See TracChangeset
for help on using the changeset viewer.