Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/24/16 10:03:52 (9 years ago)
Author:
abeham
Message:

#2457: worked on performance modeling

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  
    3535  [StorableClass]
    3636  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     }
    4237
    4338    private IFixedValueParameter<DoubleValue> NeighborhoodFactorParameter {
    4439      get { return (IFixedValueParameter<DoubleValue>)Parameters["NeighborhoodFactor"]; }
    45     }
    46 
    47     public ProblemInstanceProximityType ProximityType {
    48       get { return ProximityTypeParameter.Value.Value; }
    49       set { ProximityTypeParameter.Value.Value = value; }
    5040    }
    5141
     
    5949    private DistanceWeightedRecommender(DistanceWeightedRecommender original, Cloner cloner)
    6050      : 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() {
    6452      Parameters.Add(new FixedValueParameter<DoubleValue>("NeighborhoodFactor", "Penalize neighbors that are far away.", new DoubleValue(5)));
    6553    }
     
    6957    }
    7058
    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);
    7561      var maxDist = piDistances.Max(x => x.Value);
    7662      var instances = new SortedList<double, IAlgorithm>();
     
    10995          avgERT += new System.Random().NextDouble();
    11096        }
    111         instances.Add(avgERT, algorithm);
     97        instances.Add(avgERT, (IAlgorithm)algorithm.Clone());
    11298      }
    11399
    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)));
    116101    }
    117102  }
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders/KNearestNeighborRecommender.cs

    r13786 r13787  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Data;
    25 using HeuristicLab.Optimization;
    2625using HeuristicLab.Parameters;
    2726using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    28 using System.Collections.Generic;
    29 using System.Linq;
    3027
    3128namespace HeuristicLab.OptimizationExpertSystem.Common {
     
    3330  [StorableClass]
    3431  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   
    4633    private IFixedValueParameter<IntValue> KParameter {
    4734      get { return (IFixedValueParameter<IntValue>)Parameters["K"]; }
     
    5239    private KNearestNeighborRecommender(KNearestNeighborRecommender original, Cloner cloner)
    5340      : 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() {
    5742      Parameters.Add(new FixedValueParameter<IntValue>("K", "The number of nearest neighbors to consider.", new IntValue(5)));
    5843    }
     
    6247    }
    6348
    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);
    9351    }
    9452  }
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders/OverallBestRecommender.cs

    r13786 r13787  
    3535  [StorableClass]
    3636  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     }
    4237
    4338    private IFixedValueParameter<DoubleValue> NeighborhoodFactorParameter {
    4439      get { return (IFixedValueParameter<DoubleValue>)Parameters["NeighborhoodFactor"]; }
    45     }
    46 
    47     public ProblemInstanceProximityType ProximityType {
    48       get { return ProximityTypeParameter.Value.Value; }
    49       set { ProximityTypeParameter.Value.Value = value; }
    5040    }
    5141
     
    5949    private OverallBestRecommender(OverallBestRecommender original, Cloner cloner)
    6050      : 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() {
    6452      Parameters.Add(new FixedValueParameter<DoubleValue>("NeighborhoodFactor", "Penalize neighbors that are far away.", new DoubleValue(5)));
    6553    }
     
    6957    }
    7058
    71     public IEnumerable<IAlgorithm> GetRanking() {
    72       if (okc.Problem.ProblemId == -1) yield break;
    73 
     59    public IRecommendationModel TrainModel(KnowledgeCenter kc, string[] characteristics) {
    7460      var instances = new List<Tuple<IAlgorithm, double>>();
    75       foreach (var relevantRuns in okc.GetKnowledgeBaseByAlgorithm()) {
     61      foreach (var relevantRuns in kc.GetKnowledgeBaseByAlgorithm()) {
    7662        var algorithm = relevantRuns.Key;
    7763        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)))
    7965                              .Where(x => x.Item2 != null)
    8066                              .Select(x => Tuple.Create(x.Item1, ((DoubleValue)x.Item2.Parameters["BestKnownQuality"]).Value))
     
    8470        foreach (var problemRuns in relevantRuns.Value.GroupBy(x => ((StringValue)x.Parameters["Problem Name"]).Value)) {
    8571          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;
    8773          if (double.IsNaN(ert)) ert = int.MaxValue;
    8874          avgERT += ert;
     
    9076        }
    9177        avgERT /= count;
    92         instances.Add(Tuple.Create(algorithm, avgERT));
     78        instances.Add(Tuple.Create((IAlgorithm)algorithm.Clone(), avgERT));
    9379      }
    9480
    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));
    9782    }
    9883  }
Note: See TracChangeset for help on using the changeset viewer.