-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
333 lines (250 loc) · 7.23 KB
/
main.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <cmath>
#include <ctime>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
const int length_user = 35190;
const int length_item = 7335;
const int trainLineCount = 1123345;
const int topUserCount = 130;
class tmp_obj {
public:
int user_id, item_id;
float rating;
void set_obj(int Userid, int Itemid, float Rating) {
user_id = Userid;
item_id = Itemid;
rating = Rating;
}
tmp_obj() {
user_id = 0;
item_id = 0;
rating = 0;
};
};
// ---------- Heapify ----------
void heapify(tmp_obj array[], int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && array[left].user_id > array[largest].user_id) {
largest = left;
}
if (right < n && array[right].user_id > array[largest].user_id) {
largest = right;
}
if (largest != i) {
swap(array[i], array[largest]);
heapify(array, n, largest);
}
}
void heap_sort(tmp_obj array[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(array, n, i);
for (int i = n - 1; i >= 0; i--) {
swap(array[0], array[i]);
heapify(array, i, 0);
}
}
template <typename T>
class storage {
public:
T* array;
int last_obj_index;
explicit storage(int size) {
array = new T[size];
last_obj_index = 0;
}
void next_obj_index(int id) {
if (last_obj_index != id) {
array[last_obj_index].next_object_index = id;
last_obj_index = id;
}
}
void add_item_id_to_rating_list(int item_id, float rating) {
next_obj_index(item_id);
array[item_id].rate = rating;
}
void add_rating_to_user(int user_id, int item_id, float rating) {
next_obj_index(user_id);
array[user_id].add_rating(item_id, rating);
}
~storage() {
delete[] array;
}
};
class rating_class {
public:
float rate;
int next_object_index;
explicit rating_class(float rating = -1.0) {
rate = rating;
next_object_index = 0;
}
};
class user {
public:
int next_object_index;
storage<rating_class> rating_array_class = storage<rating_class>(length_item);
user() {
next_object_index = 0;
}
void add_rating(int item_id, float rating) {
rating_array_class.add_item_id_to_rating_list(item_id, rating);
}
};
storage<user> user_list = storage<user>(length_user);
string file = "ID,Predicted\n";
class compare_obj {
public:
double similarity;
int common;
double rating;
compare_obj() {
similarity = -1;
common = -1;
rating = -1;
}
void set_obj(double sim, int com, double rat) {
similarity = sim;
common = com;
rating = rat;
}
};
void min_heapify(compare_obj array[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && array[left].similarity > array[largest].similarity)
largest = left;
if (right < n && array[right].similarity > array[largest].similarity)
largest = right;
if (largest != i) {
swap(array[i], array[largest]);
min_heapify(array, n, largest);
}
}
void min_heap_sort(compare_obj array[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
min_heapify(array, n, i);
for (int i = n - 1; i >= 0; i--) {
swap(array[0], array[i]);
min_heapify(array, i, 0);
}
}
struct similarityOutput {
double similarity;
int common;
};
struct similarityOutput calculate_manhattan(int question_user_id, int current_user_id, int* item_ids, int id_array_size) {
struct similarityOutput output { 2147483647, 0 };
int common = 0;
double number = 0;
for (int i = 0; i < id_array_size; i++) {
double c_rat = user_list.array[current_user_id].rating_array_class.array[item_ids[i]].rate;
if (c_rat != -1) {
common++;
double q_rat = user_list.array[question_user_id].rating_array_class.array[item_ids[i]].rate;
number += abs(c_rat - q_rat);
}
}
if (common != 0) {
output.similarity = number / (5 * common);
output.common = common;
}
return output;
};
void set_next_id(int* user_id, int item_id) {
int next_index = user_list.array[*user_id].next_object_index;
if (next_index == 0) *user_id = -1;
else if (next_index == item_id) {
*user_id = next_index;
set_next_id(user_id, item_id);
}
else *user_id = next_index;
}
void predict(int qid, int user_id, int item_id) {
int first = 0;
int second = 0;
int item_ids[1633];
while (true) {
if (user_list.array[user_id].rating_array_class.array[first].rate != -1) {
item_ids[second] = first;
second++;
}
if (user_list.array[user_id].rating_array_class.array[first].next_object_index == 0) break;
else first = user_list.array[user_id].rating_array_class.array[first].next_object_index;
}
int id_arr_len = second;
compare_obj* results = new compare_obj[length_user];
first = 0;
second = 0;
while (first != -1) {
double current_rating = user_list.array[first].rating_array_class.array[item_id].rate;
if (current_rating != -1) {
struct similarityOutput output = calculate_manhattan(user_id, first, item_ids, id_arr_len);
results[second].set_obj(output.similarity, output.common, current_rating);
second++;
}
set_next_id(&first, item_id);
}
min_heap_sort(results, second);
long double result = 0;
int total = 0;
for (int i = 0; i < second; i++) {
if (i >= topUserCount)break;
result += results[i].rating * results[i].common * results[i].common;
total += results[i].common * results[i].common;
}
result = result / total;
file += to_string(qid);
file += ",";
file += to_string(result);
file += "\n";
delete[] results;
}
int main() {
tmp_obj* tmp_array = new tmp_obj[trainLineCount];
string line;
string user_id;
string item_id;
string rate;
ifstream train("TXT_TRAIN_FILE_PATH");
int i = 0;
getline(train, line);
while (getline(train, line)) {
stringstream strstream(line);
getline(strstream, user_id, ',');
getline(strstream, item_id, ',');
getline(strstream, rate, ',');
tmp_array[i].set_obj(stoi(user_id), stoi(item_id), stof(rate));
i++;
}
train.close();
heap_sort(tmp_array, i);
for (int a = 0; a < i; a++) {
user_list.add_rating_to_user(tmp_array[a].user_id, tmp_array[a].item_id, tmp_array[a].rating);
}
delete[] tmp_array;
ifstream test("TXT_TEST_FILE_PATH");
string qid;
getline(test, line);
while (getline(test, line)) {
stringstream strstream(line);
getline(strstream, qid, ',');
getline(strstream, user_id, ',');
getline(strstream, item_id, ',');
predict(stoi(qid), stoi(user_id), stoi(item_id));
}
train.close();
std::ofstream out_file("C:/submission.csv");
out_file << file;
out_file.close();
std::cout << "Added successfully " << std::endl;
return 0;
}