-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsh.c
184 lines (122 loc) · 6.18 KB
/
lsh.c
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
//
// lsh.c
//
// Christina-Theano Kylafi
// AM: 1115201200077
#include "lsh.h"
int lsh_p_range(struct simple_list* p_neighbs, struct multipoints* Mpointlist, point_num_type totalpointsin, int k, int L, point_num_type tablesize, struct bucket* table[], struct hfun* hfarray[][k], int r[], int metric, int dimension_of_coords, int p_nearest, struct users* users[], long long users_num, int coins_num, int prob)
{
// if(print_msgs_on == 1)
// printf("\nlsh_p range made it!\n");
//print_hashtable(tablesize, L, table);
//p neighbours
//struct simple_list* rlist = NULL;
struct multipoints* templist = NULL;
templist = Mpointlist;
long long point_counter = 0;
//printf("\nApplying LSH for user: ");
while(templist!=NULL)
{
//printf("\nlsh to point: %s", get_name_multipoints(templist));
struct simple_list* rlist = NULL;
// if(print_msgs_recom_on == 1)
// printf("%lld ", point_counter);
rlist = LSH_algo_all_neighbours( rlist, totalpointsin, templist, k, L, Mpointlist, tablesize, table, hfarray, r, metric, dimension_of_coords);
//print_rlist(rlist);
//sort
sort_rlist_p(rlist, p_nearest);
//print_rlist(rlist);
//update ratings of unrated coins
update_ratings(templist, rlist, Mpointlist, users, users_num, coins_num, p_nearest, 2, dimension_of_coords, users_num , prob);
//find best
//rlist = NULL;
delete_rlist(rlist);
templist = get_next_multipoints(templist);
point_counter++;
}
return 88;
}
point_num_type point_assignment_lsh( struct multipoints* MpointsList, point_num_type totalpointsin, int k, int L, point_num_type tablesize, struct bucket* table[], struct hfun* hfarray[][k], int r[], int metric, struct cluster* clusters_ptr[], int clusters_num , int dimension_of_coords)
{
struct multipoints* centroid = NULL;
double range = 0.0, mindist = 0.0;
mindist = min_dist_centroids(clusters_ptr, clusters_num, metric, dimension_of_coords);
range = (double)mindist / ((double)2); //initialize range
double init_range = range;
int clusters_with_no_new_points = 0;
point_num_type points_assigned = 0;
while ( range < ( range_factor *init_range) )
{
//printf("\nclusters_with_no_new_points: %d(range: %lf)\n", clusters_with_no_new_points, range );
clusters_with_no_new_points = 0;
for( int i = 0; i < clusters_num; i++) //for each cluster for each range I run the LSH algorithm to find the NN-Range and save them in cluster
{
centroid = get_cluster_centroid(i, clusters_ptr);
//printClusters(clusters_ptr, clusters_num);
//save the clusters with no new points for later
clusters_with_no_new_points += LSH_algo( totalpointsin, centroid, k, L, MpointsList, tablesize, table, hfarray, r, metric, range, i, clusters_ptr, clusters_num, dimension_of_coords);
//printClusters(clusters_ptr, clusters_num);
//print_cluster_size(clusters_ptr, clusters_num);
}
//lock all the points in cluster structure for this range - so as not to change in next range
points_assigned += lock_points(clusters_ptr, clusters_num);
printf("\nPoints_Assigned_sofar: %lld(range: %lf) - clusters_with_no_new_points: %d\n", points_assigned, range, clusters_with_no_new_points);
if(points_assigned == totalpointsin) //if all the points have been assigned, we are finished
{
break;
}
//printf("\nclusters_with_no_new_points: %d(range: %lf)\n", clusters_with_no_new_points, range );
int clusters_with_no_new_points_max = ceil( (double)clusters_num / (double)2 ) ;
//if more than half of the clusters haven't assigned a new point then exit the loop
if((clusters_with_no_new_points > clusters_with_no_new_points_max ) && (range >= range_factor * init_range ) )
{
//printf("\nclusters_with_no_new_points: %d(range: %lf)\n", clusters_with_no_new_points, range );
break;
}
//else, double the range and search again
range = 2*(double)range;
}
return points_assigned;
}
int lsh_range(struct multipoints* MpointsList, point_num_type totalpointsin, int k, int L, int metric, int r[], struct hfun* hfarray[][k], struct bucket* hashtablesarray[], point_num_type* tablesize_to_return, int dimension_of_coords)
{
//seed for srand for later
struct timeval time;
gettimeofday(&time,NULL);
srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
//check list
//printList(MpointsList);
//printf("\n\nTotal points of input: %lld\n", totalpointsin);
//hashtable's size
point_num_type tablesize = 0;
if (metric == 1)
{
tablesize = ceil((double)totalpointsin/(double)tablesize_division_factor);
}
else if (metric == 2 )
{
tablesize = pow(2,k);
}
(*tablesize_to_return) = tablesize;
//printf("\n> We will create %d H functions and %d hashtables(g hashfunctions) with size: %lld\n", k, L, tablesize );
// creates an array of H functions
//struct hfun *hfarray[L][k];
//printhfuns(k, L, hfarray );
//creates L hashtables
// struct bucket *hashtablesarray[L];
//creates k random int values for the formula ( to create the g functions )
// int r[k];
// memset((void*)r, 0, k*sizeof(int));
hashing(r, k, L, hfarray, MpointsList, tablesize, hashtablesarray, metric, dimension_of_coords);
//print_hashtable(tablesize, L, hashtablesarray);
// point_num_type points_assigned = 0;
// points_assigned = point_assignment_lsh(MpointsList, totalpointsin, k, L, tablesize, hashtablesarray, hfarray, r, metric, clusters_ptr, clusters_num );
// //assign points_left
// assign_points_left(MpointsList, totalpointsin, clusters_ptr, clusters_num, metric, points_assigned);
// //free memory
//delete_all_lsh(k, L, hfarray, tablesize, hashtablesarray, metric);
//calculate size
//calculate_memory_lsh(k, L, tablesize, totalpointsin, r_neighbours);
//printf("\n\nLSH OUT!\n\n");
return 65;
}