-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRankAggregationAvg.cpp
More file actions
155 lines (116 loc) · 3.89 KB
/
RankAggregationAvg.cpp
File metadata and controls
155 lines (116 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "RankAggregationAvg.h"
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <cmath>
RankAggregationAvg::~RankAggregationAvg() {
}
RankAggregationAvg::RankAggregationAvg() {
}
// output the last item excluding the excluded.
int RankAggregationAvg::last_item(vector<Ranking>* rankings,
unordered_set<int>& excluded) {
int lenght = rankings->at(0).num_elements();
unordered_map<int, int> item_pos;
for (int i = 0; i < rankings->size(); i++) {
assert(rankings->at(i).num_elements() == lenght);
for (int j = 0; j < lenght; j++) {
if (excluded.find(rankings->at(i).at(j)) == excluded.end()) {
item_pos[rankings->at(i).at(j)] += j + 1;
}
}
}
RankedNode last_item_rank;
last_item_rank.node = -1;
last_item_rank.score = -1;
for (const auto& item_pos_pair : item_pos) {
RankedNode other;
other.node = item_pos_pair.first;
other.score = item_pos_pair.second;
if (compareRankedNodes(other, last_item_rank)) {
last_item_rank = other;
}
}
return last_item_rank.node;
}
void RankAggregationAvg::rank_aggregation(vector<Ranking>* rankings,
vector<int>* output) {
int lenght = rankings->at(0).num_elements();
unordered_set<int> excluded;
output->clear();
output->resize(lenght);
for (size_t i = 0; i < lenght; i++) {
int last = last_item(rankings, excluded);
excluded.insert(last);
output->at(lenght - 1 - i) = last;
}
}
// ********************
RankAggregationAvgSimple::~RankAggregationAvgSimple() {
}
RankAggregationAvgSimple::RankAggregationAvgSimple() {
}
void RankAggregationAvgSimple::rank_aggregation(vector<Ranking>* rankings,
vector<int>* output) {
int lenght = rankings->at(0).num_elements();
output->clear();
unordered_map<int, int> item_pos;
for (int i = 0; i < rankings->size(); i++) {
assert(rankings->at(i).num_elements() == lenght);
for (int j = 0; j < lenght; j++) {
item_pos[rankings->at(i).at(j)] += j + 1;
}
}
vector<RankedNode> vec;
for (const auto& item_pos_pair : item_pos) {
RankedNode other;
other.node = item_pos_pair.first;
other.score = -item_pos_pair.second;
vec.push_back(other);
}
sort(vec.begin(), vec.end(), compareRankedNodes);
for (size_t i = 0; i < lenght; i++) {
output->push_back(vec[i].node);
}
}
// ********************
RankAggregationDomination::~RankAggregationDomination() {
}
RankAggregationDomination::RankAggregationDomination() {
}
void RankAggregationDomination::rank_aggregation(vector<Ranking>* rankings,
vector<int>* output) {
int lenght = rankings->at(0).num_elements();
output->clear();
unordered_map<pair<int, int>, int, pairhash> item_dominated;
for (int i = 0; i < rankings->size(); i++) {
assert(rankings->at(i).num_elements() == lenght);
for (int j = 0; j < lenght; j++) {
for (int k = j + 1; k < lenght; k++) {
item_dominated[make_pair(rankings->at(i).at(j),
rankings->at(i).at(k))] += 1;
}
}
}
//cout << "dominated " << endl;
unordered_map<int, int> item_dominated_num;
for (const auto& item_pair_dom : item_dominated) {
if (item_pair_dom.second >= rankings->size()/2){
item_dominated_num[item_pair_dom.first.first]+=1;
}
}
//cout << "dominated count" << endl;
vector<RankedNode> vec;
for (const auto& item_dom : item_dominated_num) {
RankedNode other;
other.node = item_dom.first;
other.score = item_dom.second;
vec.push_back(other);
}
//cout << "sorting" << endl;
sort(vec.begin(), vec.end(), compareRankedNodes);
for (size_t i = 0; i < lenght; i++) {
output->push_back(vec[i].node);
}
}