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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
#ifndef DATAPROV_H
#define DATAPROV_H
#include<Eigen/Dense>
#include "../tools/easylogging++.h"
#include<vector>
#include<math.h>
// TODO decide how to construct training data
// One possible way for training data:
// Matrix composed of an array of feature vectors
// Labels are composed of linked list, such as
// 6,3,4,0,5,0,0
// => 0->6 | 1->3 | 2->4->5
// How to compensate for non exhaustive labeling?
// Use -1 to indicate not yet labeled data
// -1s will be excluded from training
typedef struct DataEntry{
std::string qid;
double rank;
Eigen::VectorXd feature;
} DataEntry;
class DataList{
private:
int n;
std::vector<DataEntry*> data;
public:
unsigned long getSize(){return data.size();}
void addEntry(DataEntry* d){data.push_back(d);}
void setfSize(int fsize){n=fsize;}
int getfSize(){return n;}
void clear(){
for (int i=0;i<data.size();++i)
delete data[i];
data.clear();
}
static DataEntry* copyEntry(DataEntry* d)
{
DataEntry* dat = new DataEntry;
dat->rank = d->rank;
dat->qid = d->qid;
dat->feature.resize(d->feature.rows());
for (int i=0;i<d->feature.rows();++i)
{
dat->feature(i)=d->feature(i);
}
return dat;
}
inline std::vector<DataEntry*>& getData(){
return data;
}
~DataList(){
clear();
}
};
class RidList{
private:
int n;
std::vector<DataEntry*> uniq;
std::vector<DataEntry*> other;
std::vector<DataEntry*> all;
public:
static bool single;
void clear(){
uniq.clear();
other.clear();
all.clear();
}
void setfSize(int fsize){n=fsize;}
inline int getfSize(){return n;}
void addEntry(DataEntry* d){
int ext=false;
all.push_back(d);
if (d->qid=="-1") {
other.push_back(d);
return;
}
for (int i=0;i<uniq.size();++i)
if (uniq[i]->qid==d->qid)
{
ext = true;
d->rank = i;
}
if (ext)
other.push_back(d);
else
{
uniq.push_back(d);
d->rank=uniq.size()-1;
}
}
inline DataEntry* getU(int x)
{
return uniq[x];
}
inline DataEntry* getO(int x)
{
return other[x];
}
inline DataEntry* getAll(int x)
{
return all[x];
}
inline std::string getQid(int x)
{
int a,b,q=getqSize();
a=x/q;
if (single)
return getU(a)->qid;
return getAll(a)->qid;
}
inline int getqSize()
{
if (single)
return (int)other.size();
return (int)(all.size()-1);
}
inline int getuSize()
{
if (single)
return (int)uniq.size();
return (int)all.size();
}
inline int getSize()
{
return getuSize()*getqSize();
}
inline double getBha(int x){
int a,b,q=getqSize();
a=x/q;
b=x%q;
double res = 0;
Eigen::VectorXd *id,*oth;
if (single)
{
id = &(uniq[a]->feature);
oth = &(other[b]->feature);
}
else {
id = &(all[a]->feature);
if (b<a)
oth = &(all[b]->feature);
else
oth = &(all[b+1]->feature);
}
for (int i=0;i<n;++i)
{
double acc=0;
for (int j=0;j<16;++j,++i)
acc += sqrt((*id)[i] * (*oth)[i]);
res-=log(acc+1e-30);
}
return res;
}
inline double getHell(int x){
int a,b,q=getqSize();
a=x/q;
b=x%q;
double res = 0;
Eigen::VectorXd *id,*oth;
if (single)
{
id = &(uniq[a]->feature);
oth = &(other[b]->feature);
}
else {
id = &(all[a]->feature);
if (b<a)
oth = &(all[b]->feature);
else
oth = &(all[b+1]->feature);
}
for (int i=0;i<n;++i)
{
double acc=0;
for (int j=0;j<16;++j,++i)
acc += sqrt((*id)[i] * (*oth)[i]);
res+=sqrt(1-acc);
}
return res;
}
inline double cal(Eigen::VectorXd *id,Eigen::VectorXd *oth,int i) {
return fabs((*id)[i] - (*oth)[i]);
}
inline Eigen::VectorXd getVec(int x){
int a,b,q=getqSize();
a=x/q;
b=x%q;
Eigen::VectorXd *id,*oth;
if (single)
{
id = &(uniq[a]->feature);
oth = &(other[b]->feature);
}
else {
id = &(all[a]->feature);
if (b<a)
oth = &(all[b]->feature);
else
oth = &(all[b+1]->feature);
}
Eigen::VectorXd res(n);
for (int i=0;i<n;++i)
res(i)=cal(id,oth,i);
return res;
};
inline double getVecDot(int x,const Eigen::VectorXd &w)
{
int a,b,q=getqSize();
a=x/q;
b=x%q;
double res = 0;
Eigen::VectorXd *id,*oth;
if (single)
{
id = &(uniq[a]->feature);
oth = &(other[b]->feature);
}
else {
id = &(all[a]->feature);
if (b<a)
oth = &(all[b]->feature);
else
oth = &(all[b+1]->feature);
}
for (int i=0;i<n;++i)
res += cal(id,oth,i)*w[i];
return res;
}
inline void addVecw(int x,double w,Eigen::VectorXd &X)
{
int a,b,q=getqSize();
a=x/q;
b=x%q;
Eigen::VectorXd *id,*oth;
if (single)
{
id = &(uniq[a]->feature);
oth = &(other[b]->feature);
}
else {
id = &(all[a]->feature);
if (b<a)
oth = &(all[b]->feature);
else
oth = &(all[b+1]->feature);
}
for (int i=0;i<n;++i)
X[i] += cal(id,oth,i)*w;
}
inline double getL(int x){
int a,b,q=getqSize();
a=x/q;
b=x%q;
if (single)
{
if (fabs(other[b]->rank - a) < 1e-5)
return 1;
return -1;
}
double id,oth;
id = all[a]->rank;
if (b<a)
oth = all[b]->rank;
else
oth = all[b+1]->rank;
if (fabs(oth - id) < 1e-5)
return 1;
return -1;
};
};
class DataProvider //Virtual base class for data input
{
protected:
bool eof;
public:
DataProvider():eof(false){};
bool EOFile(){return eof;}
virtual void getAllDataSet(RidList &out) = 0;
virtual int getDataSet(DataList &out) = 0;
virtual int open()=0;
virtual int close()=0;
};
#endif
|