/
utils.cpp
211 lines (170 loc) · 6.79 KB
/
utils.cpp
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
#include "utils.h"
#include <iostream>
void print_usage() {
fprintf (stdout, "\nExample: main_recommender -t trainFile -q queryFile -s randomseed -m mark \n\n");
fprintf (stdout, " -t: Path to train file in format a b c where 'a' is the first node id, 'b' is the second node id and c is the weight of their link. Weight values must range from 0 to N.\n");
fprintf (stdout, " -q: Path to query file in format a b where 'a' is the first node id of the query and 'b' is the second node id of the query.\n");
fprintf (stdout, " -s: Seed for the random. If you use the same value and number of iterations, you will always obtain the same results.\n");
fprintf (stdout, " -m: Number of different ratings a user can apply. For example, if a node of the second set can be voted from 0 to 4 stars, mark would be 5.\n");
fprintf (stdout, " -n: Number of Monte Carlo steps.\n");
fprintf (stdout, " -h: Display help.\n");
}
void parseArguments(int argc, char **argv, char** inFile, char** qFile, int* stepseed, int* mark, int* iterations, char** algorithm) {
if (argc < 9) {
fprintf (stderr, "Usage: main_recommender -q queryFile -t trainFile -s randomseed -m mark \n");
exit(1);
}
int c;
while ((c = getopt (argc, argv, "a:t:q:s:g:m:n:h")) != -1)
switch (c) {
case 'a':
if (strcmp(optarg, "gibbs") == 0 || strcmp(optarg, "metropolis") == 0)
*algorithm = optarg;
else
fprintf (stderr, "Option -a only allows \"gibbs\" or \"metropolis\" algorithms.\n");
break;
case 't':
*inFile = optarg;
break;
case 'q':
*qFile = optarg;
break;
case 's':
*stepseed = atoi(optarg);
break;
case 'm':
*mark = atoi(optarg);
break;
case 'n':
*iterations = atoi(optarg);
break;
case 'h':
print_usage();
exit(0);
case '?':
if (optopt == 't' || optopt == 'q' || optopt == 'm' || optopt == 's' || optopt == 'g' || optopt == 'n' || optopt == 'a')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
exit(1);
default:
print_usage();
exit(1);
}
}
int ExponentialRootF(const gsl_vector *params, void *points, gsl_vector *f) {
const double a = gsl_vector_get(params, 0);
const double b = gsl_vector_get(params, 1);
double x0 = ((struct pair *)points)->x0;
double y0 = ((struct pair *)points)->y0;
double x1 = ((struct pair *)points)->x1;
double y1 = ((struct pair *)points)->y1;
const double r0 = y0 - a - (1. - a) * exp(-x0 / b);
const double r1 = y1 - a - (1. - a) * exp(-x1 / b);
gsl_vector_set(f, 0, r0);
gsl_vector_set(f, 1, r1);
return GSL_SUCCESS;
}
double getDecay(int nnod, double x1, double x2, double y1, double y2) {
const gsl_multiroot_fsolver_type *T;
gsl_multiroot_fsolver *s;
int status;
size_t i, iter = 0;
const size_t n = 2;
struct pair p = {x1, y1, x2, y2};
gsl_multiroot_function f = {&ExponentialRootF, n, &p};
double x_init[2] = {y2, sqrt(nnod)};
gsl_vector *x = gsl_vector_alloc(n);
double result;
for (i=0; i<n; i++)
gsl_vector_set(x, i, x_init[i]);
T = gsl_multiroot_fsolver_hybrids;
s = gsl_multiroot_fsolver_alloc (T, n);
gsl_multiroot_fsolver_set (s, &f, x);
do {
iter++;
status = gsl_multiroot_fsolver_iterate (s);
if (status) /* check if solver is stuck */
break;
status =gsl_multiroot_test_residual(s->f, 1e-7);
} while (status == GSL_CONTINUE && iter < 1000);
if (strcmp(gsl_strerror(status), "success") != 0)
result = -1;
else
result = gsl_vector_get(s->x, 1);
gsl_multiroot_fsolver_free(s);
gsl_vector_free(x);
return result;
}
double mean(double* data, int N) {
double acc = 0.0;
for (int i=0; i<N; ++i)
acc+=data[i];
return acc/N;
}
double stddev(double *data, int N) {
int i;
double m = 0.0, m2 = 0.0;
double s;
for (i=0; i<N; i++) {
m += data[i];
m2 += data[i] * data[i];
}
m /= (double)(N);
m2 /= (double)(N);
if (m2 - m * m > 0.0)
s = sqrt(m2 - m * m);
else
s = 0.0;
return s;
}
double mutualInfo(Groups *g, Groups *gt, int nnod1, int nnod2){
int S = 0, S1 = 0, S2 = 0, S12 = 0;
double H1 = 0.0, H2 = 0.0, H12 = 0.0;
S = nnod1;
for (Groups::iterator it = (*g).begin(); it != (*g).end(); ++it)
if (it->second.members.size() > 0){
S1 = it->second.members.size();
H1 += (double)S1 * log((double)S1 / (double)S);
}
for (Groups::iterator it = (*gt).begin(); it != (*g).end(); ++it)
if (it->second.members.size() > 0){
S2 = it->second.members.size();
H2 += (double)S2 * log((double)S2 / (double)S);
}
for (Groups::iterator it1 = (*g).begin(); it1 != (*g).end(); ++it1)
for (Groups::iterator it2 = (*gt).begin(); it2 != (*gt).end(); ++it2)
for(GroupNodes::iterator nit1 = it1->second.members.begin(); nit1 != it1->second.members.end(); ++nit1)
for(GroupNodes::iterator nit2 = it2->second.members.begin(); nit2 != it2->second.members.end(); ++nit2)
if (nit1->second == nit2->second)
S12++;
if (S12 > 0)
H12 += (double)S12 * log((double)(S12 * S) / (double)(S1 * S2));
return -2.0 * H12 / (H1 + H2);
}
double* genLogFactList(int size){
double* logFactList = (double*) calloc(size, sizeof(double));
for (int i = 0; i<size; i++)
logFactList[i] = gsl_sf_lnfact(i);
return logFactList;
}
double logFact(int key, int size, double* logFactList){
if (size<key)
return gsl_sf_lnfact(key);
else
return logFactList[key];
}
void printGroups(Groups g, int mark) {
for (Groups::iterator it = (g).begin(); it != (g).end(); ++it){
std::cout << "[Group:" << it->second.getId() << "] ";
for(GroupNodes::iterator it1 = it->second.members.begin(); it1 != it->second.members.end(); ++it1){
std::cout << "[ " << it1->second->getId() << ": " ;
for (Links::iterator nit = it1->second->neighbours.begin(); nit != it1->second->neighbours.end(); ++nit)
std::cout << nit->second.getId() << ", ";
std::cout << " ]";
}
std::cout << "\n";
}
}