211 lines
4.5 KiB
Python
211 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
||
|
||
import math
|
||
import sys
|
||
|
||
print("#hashcode2016")
|
||
|
||
X = 0 # Nb rows
|
||
Y = 0 # Nb columns
|
||
D = 0 # Nb drones
|
||
T = 0 # Deadline
|
||
M = 0 # Maximum load
|
||
P = 0 # Nb products
|
||
W = 0 # Nb warehouses
|
||
C = 0 # Nb customers
|
||
|
||
t = -1 # Turn
|
||
|
||
Dp = [] # Positions of drones
|
||
# (x, y)
|
||
Di = [] # Items of drones
|
||
# {product number: qt}
|
||
Dd = [] # Turn avaibility of drone
|
||
# int
|
||
Da = [] # Avaibles drones
|
||
# int
|
||
|
||
Pw = [] # Weight of products
|
||
# int
|
||
|
||
Wp = [] # Positions of warehouses
|
||
# (x, y)
|
||
Wi = [] # Items of warehouses
|
||
# {product number: qt}
|
||
|
||
Cp = [] # Positions of customers
|
||
# (x, y)
|
||
Ci = [] # Needs of customers
|
||
# {product number: qt}
|
||
|
||
# Reading raw data
|
||
f = open(sys.argv[1], 'r')
|
||
|
||
line = f.readline()
|
||
info = line.split(' ')
|
||
|
||
GRILLE = (int(info[0]), int(info[1]))
|
||
X, Y = GRILLE
|
||
D = int(info[2])
|
||
T = int(info[3])
|
||
M = int(info[4])
|
||
|
||
line = f.readline()
|
||
P = int(line)
|
||
|
||
line = f.readline()
|
||
info = line.split(' ')
|
||
for i in range(0, P):
|
||
Pw.append(int(info[i]))
|
||
|
||
line = f.readline()
|
||
W = int(line)
|
||
|
||
for i in range(0, W):
|
||
line = f.readline()
|
||
info = line.split(' ')
|
||
Wp.append((int(info[0]), int(info[1])))
|
||
line = f.readline()
|
||
info = line.split(' ')
|
||
productQ = {}
|
||
for j in range(0, len(info)):
|
||
productQ[j] = int(info[j])
|
||
Wi.append(productQ)
|
||
|
||
line = f.readline()
|
||
C = int(line)
|
||
|
||
for i in range(0, C):
|
||
line = f.readline()
|
||
info = line.split(' ')
|
||
Cp.append((int(info[0]), int(info[1])))
|
||
line = f.readline()
|
||
nbP = int(line)
|
||
line = f.readline()
|
||
info = line.split(' ')
|
||
orderQ = {}
|
||
for k in range(0, P):
|
||
orderQ[k] = 0
|
||
for j in range(0, nbP):
|
||
orderQ[int(info[j])] += 1
|
||
Ci.append(orderQ)
|
||
|
||
f.close()
|
||
|
||
# Constituting data
|
||
for d in range(D):
|
||
Dp.append(Wp[0])
|
||
Di.append(dict((p, 0) for p in range(P)))
|
||
Dd.append(0)
|
||
|
||
Out = [] # Drones commands
|
||
|
||
# Debug
|
||
|
||
assert(len(Dp) == len(Di) == len(Dd) == D)
|
||
assert(len(Pw) == P)
|
||
assert(len(Wp) == len(Wi) == W)
|
||
assert(len(Cp) == len(Ci) == C)
|
||
|
||
#def droneInfos(d):
|
||
# print("- Drone", d, "carries", ", ".join([p + ": " + q + "×" + Dw[p] for
|
||
|
||
def showGrid():
|
||
for y in range(Y):
|
||
for x in range(X):
|
||
if (x, y) in Wp:
|
||
print("W", end="")
|
||
if (x, y) in Cp:
|
||
print("C", end="")
|
||
else:
|
||
print("·", end="")
|
||
print()
|
||
|
||
# Utilities
|
||
def distance(A, B):
|
||
return math.ceil(sqrt(pow((B[0] - A[0], 2) + pow(B[1] - A[1], 2))))
|
||
|
||
def weight(d):
|
||
return sum([Pw[i] for i in Di[d]])
|
||
|
||
# Actions
|
||
def load(d, w, p, q):
|
||
# drone number, warehouse, product, qt
|
||
assert(d in Da)
|
||
if (Dp[d] != Wp[w]):
|
||
Dd[d] += distance(Dp[d], Wp[w])
|
||
Wi[w][p] += -q
|
||
Di[d][p] += +q
|
||
assert(Wp[w][p] >= 0)
|
||
assert(weight(d) <= M)
|
||
assert(Dd[d] <= T)
|
||
print("Drone", d, "loads", q, "of", p, "from warehouse", w, "→", Dd[d])
|
||
Out.append(str(d) + ' L ' + str(w) + ' ' + str(p) + ' ' + str(q))
|
||
|
||
def unload(d, w, p, q):
|
||
# drone number, warehouse, product, qt
|
||
assert(d in Da)
|
||
if (Dp[d] != Wp[w]):
|
||
Dd[d] += distance(Dp[d], Wp[w])
|
||
Wi[w][p] += +q
|
||
Di[d][p] += -q
|
||
assert(Dd[d] <= T)
|
||
print("Drone", d, "unloads", q, "of", p, "from warehouse", w, "→", Dd[d])
|
||
Out.append(str(d) + ' U ' + str(w) + ' ' + str(p) + ' ' + str(q))
|
||
|
||
def deliver(d, c, p, q):
|
||
# drone number, customer, product, qt
|
||
assert(d in Da)
|
||
if (Dp[d] != Cp[c]):
|
||
Dd[d] += distance(Dp[d], Cp[c])
|
||
Ci[w][p] += +q
|
||
Di[d][p] += -q
|
||
Dd[d] += 1
|
||
assert(Dd[d] <= T)
|
||
print("Drone", d, "delivers", q, "of", p, "to client", w, "→", Dd[d])
|
||
Out.append(str(d) + ' D ' + str(c) + ' ' + str(p) + ' ' + str(q))
|
||
|
||
def wait(d, w=1):
|
||
assert(d in Da)
|
||
Dd[d] += w
|
||
Da.remove(d)
|
||
print("Drone", d, "waits", w, "turn" + ('s' if w >= 2 else ''), "→", Dd[d])
|
||
Out.append(str(d) + ' W ' + str(w))
|
||
|
||
|
||
# Control
|
||
def newTurn():
|
||
global t
|
||
t += 1
|
||
print("--- Turn", t)
|
||
for d in Da:
|
||
wait(d)
|
||
assert(len(Da) == 0)
|
||
for d in range(D):
|
||
if Dd[d] <= t:
|
||
Da.append(d)
|
||
print("Drones", ", ".join([str(d) for d in Da]), "(", len(Da), ")", "are avaible")
|
||
|
||
def end():
|
||
print("--- End!")
|
||
|
||
# IA
|
||
newTurn()
|
||
newTurn()
|
||
|
||
# Output
|
||
f = open(sys.argv[1] + 'o', 'w')
|
||
f.write(str(len(Out)) + '\n' + '\n'.join(Out))
|
||
f.close()
|
||
|
||
def SortCustomer():
|
||
CpSort = []
|
||
for i in range(0, len(Cp)):
|
||
CpSort.append((i, Cp[i], distance(Wp[0], Cp[i])))
|
||
CpSort.sort(key=lambda tup: tup[2])
|
||
|
||
def WarehouseHasItems(w, items):
|
||
for i in range(0, len(items):
|
||
if Wi[w][items[i][0]] < items[i][1]:
|
||
return False
|
||
return True |