Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/KNearestNeighborRecommender.cs @ 13774

Last change on this file since 13774 was 13774, checked in by abeham, 8 years ago

#2457: worked on recommendation algorithms

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using System.Collections.Generic;
29using System.Linq;
30
31namespace HeuristicLab.OptimizationExpertSystem.Common {
32  [Item("K-Nearest Neighbor Recommender", "")]
33  [StorableClass]
34  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
46    private IFixedValueParameter<IntValue> KParameter {
47      get { return (IFixedValueParameter<IntValue>)Parameters["K"]; }
48    }
49
50    [StorableConstructor]
51    private KNearestNeighborRecommender(bool deserializing) : base(deserializing) { }
52    private KNearestNeighborRecommender(KNearestNeighborRecommender original, Cloner cloner)
53      : 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)));
57      Parameters.Add(new FixedValueParameter<IntValue>("K", "The number of nearest neighbors to consider.", new IntValue(5)));
58    }
59
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new KNearestNeighborRecommender(this, cloner);
62    }
63
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;
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.