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 |
|
---|
22 | using HeuristicLab.Collections;
|
---|
23 | using HeuristicLab.Optimization;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Linq;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.OptimizationExpertSystem.Common {
|
---|
28 | public class KNearestNeighborModel : IRecommendationModel {
|
---|
29 | private readonly int K;
|
---|
30 | private readonly string[] characteristics;
|
---|
31 | private readonly Dictionary<IRun, Dictionary<IAlgorithm, double>> performance;
|
---|
32 | private readonly BidirectionalDictionary<int, IRun> problemInstanceMap;
|
---|
33 | private readonly double[] medianValues;
|
---|
34 |
|
---|
35 | public KNearestNeighborModel(int k, Dictionary<IRun, Dictionary<IAlgorithm, double>> perfData, string[] characteristics) {
|
---|
36 | this.K = k;
|
---|
37 | this.performance = perfData;
|
---|
38 | this.characteristics = characteristics;
|
---|
39 | problemInstanceMap = new BidirectionalDictionary<int, IRun>();
|
---|
40 | var i = 0;
|
---|
41 | foreach (var pi in perfData.Keys) {
|
---|
42 | problemInstanceMap.Add(i++, pi);
|
---|
43 | }
|
---|
44 | this.medianValues = KnowledgeCenter.GetMedianValues(perfData.Keys.OrderBy(problemInstanceMap.GetBySecond).ToArray(), characteristics);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public IEnumerable<KeyValuePair<IAlgorithm, double>> GetRanking(IRun problemInstance) {
|
---|
48 | var features = KnowledgeCenter.GetFeatures(performance.Keys.OrderBy(problemInstanceMap.GetBySecond).ToArray(), characteristics, medianValues);
|
---|
49 | var feature = KnowledgeCenter.GetFeatures(new [] { problemInstance }, characteristics, medianValues)[0];
|
---|
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);
|
---|
53 |
|
---|
54 | var performances = new Dictionary<IAlgorithm, List<double>>();
|
---|
55 | foreach (var next in nearestK) {
|
---|
56 | var perfs = performance[problemInstanceMap.GetByFirst(next.ProblemInstanceIndex)];
|
---|
57 | if (perfs.Count == 0) continue;
|
---|
58 |
|
---|
59 | foreach (var p in perfs) {
|
---|
60 | 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);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | return performances.Select(x => new { Alg = x.Key, Perf = x.Value.Average() })
|
---|
70 | .OrderBy(x => x.Perf)
|
---|
71 | .Select(x => new KeyValuePair<IAlgorithm, double>(x.Alg, x.Perf));
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|