-
Notifications
You must be signed in to change notification settings - Fork 0
/
predictionThread.cpp
251 lines (219 loc) · 9.64 KB
/
predictionThread.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
//
// Created by MacBook Pro on 12/14/20.
//
#include "predictionThread.h"
typedef struct{
int gridrank;
int num_order;
} order_t;
bool operator<(const order_t &order1, const order_t &order2){
return order1.num_order<order2.num_order;
}
void predictionThread::run() {
db=this->window->db;
window->mutex_db.acquire();
qDebug()<<"Begin!";
//QSqlQuery query(db);
db.open();
switch(window->predictboxes[4]->currentIndex()){
case 0:
predictDestination();
break;
case 1:
predictTravelTime();
break;
default:
emit fail(this,-1);
}
window->mutex_db.release();
this->exit(0);
}
pair<int, int> getGridID(float north, float south, float west, float east, float lng, float lat){
int col=9-(lng-west)/((east-west)/10);
int row=9-(lat-south)/((north-south)/10);
if(col<0) col=0;
if(col>9) col=9;
if(row<0) row=0;
if(row>9) row=9;
return pair<int,int>(row,col);
}
void predictionThread::predictDestination() {
//window->predictedit->clear();
if(!(window->fields_onehot[3]&&window->fields_onehot[4]&&window->fields_onehot[5]&&window->fields_onehot[6])){
emit fail(this, 0);
return;
}
QSqlQuery query(db);
db.open();
//QTime time=QTime::fromString(window->timeedit->text()+":00","hh:mm:ss");
QString from_date=window->from_date;
QString to_date=window->to_date;
QString time_point=window->timeedit->text()+":00";
int row=window->predictboxes[1]->currentIndex();
int col=window->predictboxes[0]->currentIndex();
qDebug()<<row;
qDebug()<<col;
QPointF topleft=window->grids[row*10+col].first;
QPointF bottomright=window->grids[row*10+col].second;
QDate fromdate=QDate::fromString(from_date,"yyyyMMdd");
QDate todate=QDate::fromString(to_date,"yyyyMMdd");
qDebug()<<fromdate;
qDebug()<<todate;
QVector<order_t> orders(100);
for(int i=0;i<100;i++) {orders[i].gridrank=i;orders[i].num_order=0;}
pair<int, int> coordinate;
int cnt=0;
for(auto i=fromdate;i<=todate;i=i.addDays(1)){
auto datestring=i.toString("yyyyMMdd");
qDebug()<<datestring;
QDateTime timepoint=QDateTime::fromString(datestring+"-"+time_point,"yyyyMMdd-hh:mm:ss");
auto lowerbound=timepoint.toTime_t()-1800;
auto upperbound=timepoint.toTime_t()+1800;
QString querystr=QString("SELECT dest_lng,dest_lat FROM order_%1 WHERE "
"departure_time>=%2 and departure_time<=%3 "
"and orig_lng >= %4 and orig_lng <%5 "
"and orig_lat >= %6 and orig_lat <%7 ").arg(
datestring,NUMBER(lowerbound),NUMBER(upperbound),
NUMBER(topleft.x()),NUMBER(bottomright.x()),
NUMBER(bottomright.y()),NUMBER(topleft.y())
);
qDebug()<<querystr;
auto r=query.exec(querystr);
if(!r) qDebug()<<query.lastError();
while(query.next()){
cnt++;
coordinate=getGridID(window->grid_north,window->grid_south,window->grid_west,window->grid_east,
query.value(0).toFloat(),query.value(1).toFloat());
qDebug()<<coordinate;
orders[coordinate.first*10+coordinate.second].num_order++;
}
//emit success(this);
//return;
}
qDebug()<<cnt;
sort(orders.begin(),orders.end());
window->predictedit->append(QString("From Grid No.%1(%2 °E ~ %3 °E, %4 °N ~ %5 °N), the most popular 5 destinations are:\n").arg(
NUMBER(row+col*10),
NUMBER(topleft.x()),NUMBER(bottomright.x()),NUMBER(bottomright.y()),NUMBER(topleft.y())));
for(int i=0;i<5;i++){
QPointF topleft=window->grids[orders[orders.size()-1-i].gridrank].first;
QPointF bottomright=window->grids[orders[orders.size()-1-i].gridrank].second;
window->predictedit->append(QString("Grid No.%1(%2 °E ~ %3 °E, %4 °N ~ %5 °N) : %6 orders").arg(
NUMBER(orders[orders.size()-1-i].gridrank),
NUMBER(topleft.x()),NUMBER(bottomright.x()),NUMBER(bottomright.y()),NUMBER(topleft.y()),
NUMBER(orders[orders.size()-1-i].num_order)
));
}
emit success(this);
}
void predictionThread::predictTravelTime() {
if(!(window->fields_onehot[1]&&window->fields_onehot[2]&&window->fields_onehot[3]&&window->fields_onehot[4]&&window->fields_onehot[5]&&window->fields_onehot[6]))
{emit fail(this,1);return;}
QSqlQuery query(db);
db.open();
//QTime time=QTime::fromString(window->timeedit->text()+":00","hh:mm:ss");
QString from_date=window->from_date;
QString to_date=window->to_date;
QString time_point=window->timeedit->text()+":00";
int fromrow=window->predictboxes[1]->currentIndex();
int fromcol=window->predictboxes[0]->currentIndex();
int torow=window->predictboxes[3]->currentIndex();
int tocol=window->predictboxes[2]->currentIndex();
qDebug()<<fromrow;
qDebug()<<fromcol;
qDebug()<<torow;
qDebug()<<tocol;
QPointF fromtopleft=window->grids[fromrow*10+fromcol].first;
QPointF frombottomright=window->grids[fromrow*10+fromcol].second;
QPointF totopleft=window->grids[torow*10+tocol].first;
QPointF tobottomright=window->grids[torow*10+tocol].second;
QDate fromdate=QDate::fromString(from_date,"yyyyMMdd");
QDate todate=QDate::fromString(to_date,"yyyyMMdd");
qDebug()<<fromdate;
qDebug()<<todate;
QVector<int> periods;
for(auto i=fromdate;i<=todate;i=i.addDays(1)){
auto datestring=i.toString("yyyyMMdd");
qDebug()<<datestring;
QDateTime timepoint=QDateTime::fromString(datestring+"-"+time_point,"yyyyMMdd-hh:mm:ss");
auto lowerbound=timepoint.toTime_t()-1800;
auto upperbound=timepoint.toTime_t()+1800;
QString querystr=QString("SELECT departure_time,end_time FROM order_%1 WHERE departure_time>=%2 and departure_time<=%3 ").arg(
datestring,NUMBER(lowerbound),NUMBER(upperbound)
)+
QString(
"and orig_lng >= %1 and orig_lng <%2 "
"and orig_lat >= %3 and orig_lat <%4 "
"and dest_lng >= %5 and dest_lng <%6 "
"and dest_lat >= %7 and dest_lat <%8 ").arg(
NUMBER(fromtopleft.x()),NUMBER(frombottomright.x()),
NUMBER(frombottomright.y()),NUMBER(fromtopleft.y()),
NUMBER(totopleft.x()),NUMBER(tobottomright.x()),
NUMBER(tobottomright.y()),NUMBER(totopleft.y())
);
qDebug()<<querystr;
auto r=query.exec(querystr);
if(!r) qDebug()<<query.lastError();
while(query.next()){
int t=query.value(1).toInt()-query.value(0).toInt();
periods.push_back(t);
}
}
sort(periods.begin(),periods.end());
QString ans=QString("\nFrom Grid No.%1(%2 °E ~ %3 °E, %4 °N ~ %5 °N) ").arg(
NUMBER(fromrow*10+fromcol),
NUMBER(fromtopleft.x()),NUMBER(frombottomright.x()),NUMBER(frombottomright.y()),NUMBER(fromtopleft.y())
)+QString("to Grid No.%1(%2 °E ~ %3 °E, %4 °N ~ %5 °N)").arg(
NUMBER(torow*10+tocol),
NUMBER(totopleft.x()),NUMBER(tobottomright.x()),NUMBER(tobottomright.y()),NUMBER(totopleft.y())
);
window->predictedit->append(ans);
window->predictedit->append(QString("Number of records: %1\n").arg(periods.size()));
qDebug()<<periods.size();
if(periods.size()==0) {emit success(this);return;}
if(periods[periods.size()-1]<=60)
window->predictedit->append(QString("Maximum: %1 s.\n").arg(NUMBER(periods[periods.size()-1])));
else{
int seconds=periods[periods.size()-1];
if(seconds<=3600)
window->predictedit->append(QString("Maximum: %1 min, %2 s.\n").arg(
NUMBER(seconds/60),NUMBER(seconds-60*(seconds/60))));
else{
window->predictedit->append(QString("Maximum: %1 h, %2 min, %3 s.\n").arg(
NUMBER(seconds/3600),
NUMBER((seconds-3600*(seconds/3600))/60),
NUMBER(seconds-60*(seconds/60))));
}
}
if(periods[0]<=60)
window->predictedit->append(QString("Minimum: %1 s.\n").arg(NUMBER(periods[periods.size()-1])));
else{
int seconds=periods[0];
if(periods[periods.size()-1<=3600])
window->predictedit->append(QString("Minimum: %1 min, %2 s.\n").arg(
NUMBER(seconds/60),NUMBER(seconds-60*(seconds/60))));
else{
window->predictedit->append(QString("Minimum: %1 h, %2 min, %3 s.\n").arg(
NUMBER(seconds/3600),
NUMBER((seconds-3600*(seconds/3600))/60),
NUMBER(seconds-60*(seconds/60))));
}
}
if(periods[periods.size()/2]<=60)
window->predictedit->append(QString("Midst: %1 s.\n").arg(NUMBER(periods[periods.size()-1])));
else{
int seconds=periods[periods.size()/2];
if(seconds<=3600)
window->predictedit->append(QString("Midst: %1 min, %2 s.\n").arg(
NUMBER(seconds/60),NUMBER(seconds-60*(seconds/60))));
else{
window->predictedit->append(QString("Midst: %1 h, %2 min, %3 s.\n").arg(
NUMBER(seconds/3600),
NUMBER((seconds-3600*(seconds/3600))/60),
NUMBER(seconds-60*(seconds/60))));
}
}
//window->predictedit->append(QString("Minimum: %1\n").arg(NUMBER(periods[0])));
//window->predictedit->append(QString("Midst: %1\n").arg(NUMBER(periods[periods.size()/2])));
emit success(this);
}