-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster_calculation_v2.php
More file actions
192 lines (149 loc) · 6.01 KB
/
cluster_calculation_v2.php
File metadata and controls
192 lines (149 loc) · 6.01 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
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
<?php
ini_set('xdebug.max_nesting_level', 2000);
$minNumberOfBookingsRequired = 10;
$maxNumberOfBookingsRequired = 30;
$selectedYear = '2014';
$expediaTableName = 'expedia-all-bookings-'.$selectedYear.'-reduced';
$expediaTableNameResult = 'expedia-user-hotel-probability-'.$selectedYear;
$expediaTableNameRelevantUsers = 'expedia-relevant-users-'.$selectedYear;
$numberOfHotels = 100; // Based on Expedia, there is a max of hotels of 100.
/**
Remote (server) database connection.
*/
function connectToDb()
{
global $link;
$link = new mysqli("localhost","home-no-pw","", "expedia");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
/**
Local database connection.
*/
function connectToDbLocal()
{
global $link;
$link = new mysqli("localhost","root","", "expedia-test");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
/**
Truncate the database.
*/
function preparations()
{
global $link, $expediaTableNameResult, $expediaTableNameRelevantUsers;
$link->query('TRUNCATE `'.$expediaTableNameResult.'`');
$link->query('TRUNCATE `'.$expediaTableNameRelevantUsers.'`');
}
/**
Function elaborates all users who have conducted a
min amount of bookings and do not exceed a max amount
of bookings.
*/
function findAllRelevantUsers()
{
global $link, $minNumberOfBookingsRequired, $maxNumberOfBookingsRequired, $expediaTableName, $expediaTableNameRelevantUsers;
printf('[findAllRelevantUsers]: Start.'.PHP_EOL);
$result = $link->query('SELECT count(user_id) AS `numberOfBookings`, user_id FROM `'.$expediaTableName.'` GROUP BY `user_id`; ');
printf('[findAllRelevantUsers]: Number of Elements: '.mysqli_num_rows($result).PHP_EOL);
while ($userObject = $result->fetch_object()) {
if($userObject->numberOfBookings >= $minNumberOfBookingsRequired && $userObject->numberOfBookings <= $maxNumberOfBookingsRequired) {
$link->query('INSERT INTO `'.$expediaTableNameRelevantUsers.'` (`id`, `user_id`) VALUES (NULL, '.$userObject->user_id.')');
}
}
}
/**
Base function to call in order to start the probability
calculation for every hotel cluster.
*/
function calculateProbability()
{
global $link, $numberOfHotels, $expediaTableNameRelevantUsers;
printf('[calculateProbability]: Start.'.PHP_EOL);
$users = $link->query('SELECT `user_id` FROM `'.$expediaTableNameRelevantUsers.'`;');
$numberOfHotelsIncrement = 1;
while ($numberOfHotels >= $numberOfHotelsIncrement) {
$usersWhoLikeHotel = findUsersWhoLikeHotel($numberOfHotelsIncrement);
if(count($usersWhoLikeHotel)>0) {
findReferralProbability($usersWhoLikeHotel, $numberOfHotelsIncrement);
}
$numberOfHotelsIncrement++;
}
}
/**
Return list LH = {u1, ... ,un} of all users who do like hotel_id
*/
function findUsersWhoLikeHotel($hotel_id)
{
global $link, $numberOfHotels, $expediaTableName, $expediaTableNameRelevantUsers;
printf('[findUsersWhoLikeHotel]: Start.'.PHP_EOL);
$bookings = $link->query('SELECT `user_id` FROM `'.$expediaTableName.'` WHERE `hotel_cluster`='.$hotel_id.';');
$usersWhoLikeHotel = Array();
if($bookings) {
while ($bookingObject = $bookings->fetch_object()) {
$users = $link->query('SELECT `user_id` FROM `'.$expediaTableNameRelevantUsers.'` WHERE `user_id`='.$bookingObject->user_id.';');
if($users) {
if(mysqli_num_rows($users) > 0 && mysqli_num_rows($users) < 2) {
array_push($usersWhoLikeHotel, $users->fetch_row());
} else if(mysqli_num_rows($users) > 1) {
printf('The list `'.$expediaTableNameRelevantUsers.'` is not clean. User '.$bookingObject->user_id.' is present multiple times.'.PHP_EOL);
}
}
}
}
return $usersWhoLikeHotel;
}
/**
Calculates the probability value of a successful referral taking
place.
*/
function findReferralProbability($usersWhoLikeHotel, $hotel_id)
{
global $link, $numberOfHotels, $expediaTableNameResult;
printf('[findHotelsOfUser]: Start.'.PHP_EOL);
$numberOfHotelsIncrement = 1;
while ($numberOfHotels >= $numberOfHotelsIncrement) {
if($hotel_id !== $numberOfHotelsIncrement) {
$likes = findUsersWhoLikeHotel($numberOfHotelsIncrement);
$probability = count($likes) / count($usersWhoLikeHotel);
if($probability > 1) {$probability=1; printf('Probability is greater than 1: '.$numberOfHotelsIncrement.' '.$hotel_id.' '.count($usersWhoLikeHotel));}
$link->query('INSERT INTO `'.$expediaTableNameResult.'` (`id`, `hotel_id`, `probability`, `like_hotel`) VALUES (NULL, '.$hotel_id.', '.$probability.', '.$numberOfHotelsIncrement.')');
}
$numberOfHotelsIncrement++;
}
}
function compareProbabilities()
{
global $link, $numberOfHotels, $expediaTableNameResult;
printf('[compareProbabilities]: Start.'.PHP_EOL);
$link->query('TRUNCATE `expedia-user-hotel-probability-comparison-2013-2014`');
$numberOfHotelsIncrement = 1;
while ($numberOfHotels >= $numberOfHotelsIncrement) {
$numberOfHotelsIncrementSub = 1;
while ($numberOfHotels >= $numberOfHotelsIncrementSub) {
$probabilities_2013 = $link->query('SELECT * FROM `expedia-user-hotel-probability-2013` WHERE hotel_id='.$numberOfHotelsIncrement.' AND like_hotel='.$numberOfHotelsIncrementSub);
$probabilities_2014 = $link->query('SELECT * FROM `expedia-user-hotel-probability-2014` WHERE hotel_id='.$numberOfHotelsIncrement.' AND like_hotel='.$numberOfHotelsIncrementSub);
if($probabilities_2013 && $probabilities_2014) {
$p2013 = $probabilities_2013->fetch_row();
$p2014 = $probabilities_2014->fetch_row();
//var_dump($p2013);
$volatility = abs(floatval($p2013[2]) - floatval($p2014[2]));
$link->query('INSERT INTO `expedia-user-hotel-probability-comparison-2013-2014` (id, hotel_id, volatility, like_hotel) VALUES (NULL, '.$numberOfHotelsIncrement.', '.$volatility.', '.$numberOfHotelsIncrementSub.');');
}
$numberOfHotelsIncrementSub++;
}
$numberOfHotelsIncrement++;
}
}
// Function call stack
connectToDbLocal();
//preparations();
//findAllRelevantUsers();
//calculateProbability();
compareProbabilities();
?>