-
Notifications
You must be signed in to change notification settings - Fork 11
/
answer.py
181 lines (133 loc) · 4.32 KB
/
answer.py
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
import csv
import math
import pandas as pd
from sklearn.cluster import KMeans
import numpy as np
import sys
MAX_DELIVERIES = 10
DRIVE_SPEED_KMPH = 20
PRICE_PER_TRUCK = 100
PRICE_PER_KM = 0.061
MAX_TIME_PER_TRUCK = 10
TOTAL_CLUSTERS = 51
def distance(x1, y1, x2, y2):
return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
class Truck:
id = 0
def __init__(self):
self.capacity_left = MAX_DELIVERIES
self.x = 0
self.y = 0
self.current_time = 0
self.id = Truck.id
self.deliveries = [] # [truck_id, order_id, seq_no]
self.current_order = 1
self.delivered = False
Truck.id += 1
def can_deliver_order(self, order):
"""
Checks whether the truck can deliver the order and reach the depot in
time.
"""
if self.capacity_left == 0:
return False
time_to_reach_order = distance(
self.x, self.y, order.x, order.y
) / DRIVE_SPEED_KMPH
if self.current_time + time_to_reach_order >= order.end_time:
return False
time_to_reach_depot = distance(
order.x, order.y, 0, 0
) / DRIVE_SPEED_KMPH
if (
max(self.current_time + time_to_reach_order, order.start_time)
+ time_to_reach_depot > MAX_TIME_PER_TRUCK
):
return False
print("True")
return True
def deliver_order(self, order):
dist = distance(self.x, self.y, order.x, order.y)
# update current_time
self.current_time = max(
self.current_time + (dist / DRIVE_SPEED_KMPH),
order.start_time
)
# update capacity
self.capacity_left -= 1
# update location
self.x = order.x
self.y = order.y
# add order to deliveries
self.deliveries.append([self.id, order.id, self.current_order])
self.current_order += 1
def deliver(self):
self.delivered = True
return self.deliveries[::]
def has_delivered(self):
return self.delivered
class Order:
def __init__(self, id, x, y, start_time):
self.id = id
self.x = x
self.y = y
self.start_time = start_time
self.end_time = self.start_time + 1
def bruteforce(orders):
deliveries = []
current_truck = Truck()
while orders:
top = orders.pop()
if current_truck.can_deliver_order(top):
current_truck.deliver_order(top)
else:
deliveries.extend(current_truck.deliveries[::])
current_truck = Truck()
current_truck.deliver_order(top)
if len(deliveries) < 500:
deliveries.extend(current_truck.deliveries[::])
return deliveries
def clustered(orders, kmeans):
clusters = {
i: np.where(kmeans.labels_ == i)[0]
for i in range(kmeans.n_clusters)
}
deliveries = []
current_truck = Truck()
# loop over each cluster
for cluster in clusters.values():
curr_len = len(deliveries)
orders_in_cluster = [orders[idx] for idx in cluster]
for order in sorted(orders_in_cluster, key=lambda x: x.end_time):
if current_truck.can_deliver_order(order):
current_truck.deliver_order(order)
else:
deliveries.extend(current_truck.deliver())
current_truck = Truck()
current_truck.deliver_order(order)
if not current_truck.has_delivered():
deliveries.extend(current_truck.deliver())
return deliveries
def answer():
df = pd.read_csv("orders.csv")
orders_to_cluster = []
orders = []
for _, row in df.iterrows():
orders_to_cluster.append([row["x"], row["y"]])
order = Order(
row["order_id"], row["x"], row["y"], row["time_window_start"]
)
orders.append(order)
kmeans = KMeans(n_clusters=TOTAL_CLUSTERS, random_state=0)
kmeans.fit(orders_to_cluster)
# ans_list = bruteforce(orders)
ans_list = clustered(orders, kmeans)
print(len(ans_list))
with open("answer.txt", 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["truck_id", "order_id", "sequence_number"])
for row in ans_list:
writer.writerow(row)
if len(sys.argv) > 1:
TOTAL_CLUSTERS = int(sys.argv[1])
answer()