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