Merge branch 'master' of github.com:GeoffreyFrogeye/hashcode2016
This commit is contained in:
commit
12b8943514
15
Makefile
15
Makefile
|
@ -1,4 +1,13 @@
|
||||||
../submission.zip:
|
all: 1o 2o 3o ../submission.zip
|
||||||
zip $@ *
|
|
||||||
|
|
||||||
.PHONY: ../submission.zip
|
../submission.zip:
|
||||||
|
zip $@ .git *.py -r
|
||||||
|
|
||||||
|
1o: 1
|
||||||
|
python main.py $<
|
||||||
|
2o: 2
|
||||||
|
python main.py $<
|
||||||
|
3o: 3
|
||||||
|
python main.py $<
|
||||||
|
|
||||||
|
.PHONY: ../submission.zip 1o 2o 3o
|
||||||
|
|
75
main.py
75
main.py
|
@ -14,7 +14,7 @@ P = 0 # Nb products
|
||||||
W = 0 # Nb warehouses
|
W = 0 # Nb warehouses
|
||||||
C = 0 # Nb customers
|
C = 0 # Nb customers
|
||||||
|
|
||||||
t = -1 # Turn
|
t = 0 # Turn
|
||||||
|
|
||||||
Dp = [] # Positions of drones
|
Dp = [] # Positions of drones
|
||||||
# (x, y)
|
# (x, y)
|
||||||
|
@ -23,7 +23,7 @@ Di = [] # Items of drones
|
||||||
Dd = [] # Turn avaibility of drone
|
Dd = [] # Turn avaibility of drone
|
||||||
# int
|
# int
|
||||||
Da = [] # Avaibles drones
|
Da = [] # Avaibles drones
|
||||||
# int
|
# bool
|
||||||
|
|
||||||
Pw = [] # Weight of products
|
Pw = [] # Weight of products
|
||||||
# int
|
# int
|
||||||
|
@ -87,7 +87,7 @@ for i in range(0, C):
|
||||||
for k in range(0, P):
|
for k in range(0, P):
|
||||||
orderQ[k] = 0
|
orderQ[k] = 0
|
||||||
for j in range(0, nbP):
|
for j in range(0, nbP):
|
||||||
orderQ[int(info[j])] += 1
|
orderQ[int(info[j])] += -1
|
||||||
Ci.append(orderQ)
|
Ci.append(orderQ)
|
||||||
|
|
||||||
f.close()
|
f.close()
|
||||||
|
@ -97,12 +97,13 @@ for d in range(D):
|
||||||
Dp.append(Wp[0])
|
Dp.append(Wp[0])
|
||||||
Di.append(dict((p, 0) for p in range(P)))
|
Di.append(dict((p, 0) for p in range(P)))
|
||||||
Dd.append(0)
|
Dd.append(0)
|
||||||
|
Da.append(True)
|
||||||
|
|
||||||
Out = [] # Drones commands
|
Out = [] # Drones commands
|
||||||
|
|
||||||
# Debug
|
# Debug
|
||||||
|
|
||||||
assert(len(Dp) == len(Di) == len(Dd) == D)
|
assert(len(Dp) == len(Di) == len(Dd) == len(Da) == D)
|
||||||
assert(len(Pw) == P)
|
assert(len(Pw) == P)
|
||||||
assert(len(Wp) == len(Wi) == W)
|
assert(len(Wp) == len(Wi) == W)
|
||||||
assert(len(Cp) == len(Ci) == C)
|
assert(len(Cp) == len(Ci) == C)
|
||||||
|
@ -123,20 +124,21 @@ def showGrid():
|
||||||
|
|
||||||
# Utilities
|
# Utilities
|
||||||
def distance(A, B):
|
def distance(A, B):
|
||||||
return math.ceil(sqrt(pow((B[0] - A[0], 2) + pow(B[1] - A[1], 2))))
|
return math.ceil(math.sqrt(pow(B[0] - A[0], 2) + pow(B[1] - A[1], 2)))
|
||||||
|
|
||||||
def weight(d):
|
def weight(d):
|
||||||
return sum([Pw[i] for i in Di[d]])
|
return sum(Di[d][p]*Pw[p]for p in range(P))
|
||||||
|
|
||||||
# Actions
|
# Actions
|
||||||
def load(d, w, p, q):
|
def load(d, w, p, q):
|
||||||
# drone number, warehouse, product, qt
|
# drone number, warehouse, product, qt
|
||||||
assert(d in Da)
|
assert(Da[d])
|
||||||
if (Dp[d] != Wp[w]):
|
if (Dp[d] != Wp[w]):
|
||||||
Dd[d] += distance(Dp[d], Wp[w])
|
Dd[d] += distance(Dp[d], Wp[w])
|
||||||
|
Dd[d] = Cw[c]
|
||||||
Wi[w][p] += -q
|
Wi[w][p] += -q
|
||||||
Di[d][p] += +q
|
Di[d][p] += +q
|
||||||
assert(Wp[w][p] >= 0)
|
assert(Wi[w][p] >= 0)
|
||||||
assert(weight(d) <= M)
|
assert(weight(d) <= M)
|
||||||
assert(Dd[d] <= T)
|
assert(Dd[d] <= T)
|
||||||
print("Drone", d, "loads", q, "of", p, "from warehouse", w, "→", Dd[d])
|
print("Drone", d, "loads", q, "of", p, "from warehouse", w, "→", Dd[d])
|
||||||
|
@ -144,9 +146,10 @@ def load(d, w, p, q):
|
||||||
|
|
||||||
def unload(d, w, p, q):
|
def unload(d, w, p, q):
|
||||||
# drone number, warehouse, product, qt
|
# drone number, warehouse, product, qt
|
||||||
assert(d in Da)
|
assert(Da[d])
|
||||||
if (Dp[d] != Wp[w]):
|
if (Dp[d] != Wp[w]):
|
||||||
Dd[d] += distance(Dp[d], Wp[w])
|
Dd[d] += distance(Dp[d], Wp[w])
|
||||||
|
Dd[d] = Cw[c]
|
||||||
Wi[w][p] += +q
|
Wi[w][p] += +q
|
||||||
Di[d][p] += -q
|
Di[d][p] += -q
|
||||||
assert(Dd[d] <= T)
|
assert(Dd[d] <= T)
|
||||||
|
@ -155,20 +158,22 @@ def unload(d, w, p, q):
|
||||||
|
|
||||||
def deliver(d, c, p, q):
|
def deliver(d, c, p, q):
|
||||||
# drone number, customer, product, qt
|
# drone number, customer, product, qt
|
||||||
assert(d in Da)
|
assert(Da[d])
|
||||||
if (Dp[d] != Cp[c]):
|
if (Dp[d] != Cp[c]):
|
||||||
Dd[d] += distance(Dp[d], Cp[c])
|
Dd[d] += distance(Dp[d], Cp[c])
|
||||||
Ci[w][p] += +q
|
Dd[d] = Cp[c]
|
||||||
|
Ci[c][p] += +q
|
||||||
Di[d][p] += -q
|
Di[d][p] += -q
|
||||||
Dd[d] += 1
|
Dd[d] += 1
|
||||||
assert(Dd[d] <= T)
|
assert(Dd[d] <= T)
|
||||||
print("Drone", d, "delivers", q, "of", p, "to client", w, "→", Dd[d])
|
print("Drone", d, "delivers", q, "of", p, "to client", c, "→", Dd[d])
|
||||||
Out.append(str(d) + ' D ' + str(c) + ' ' + str(p) + ' ' + str(q))
|
Out.append(str(d) + ' D ' + str(c) + ' ' + str(p) + ' ' + str(q))
|
||||||
|
|
||||||
def wait(d, w=1):
|
def wait(d, w=1):
|
||||||
assert(d in Da)
|
assert(Da[d])
|
||||||
|
global Dd, Da
|
||||||
Dd[d] += w
|
Dd[d] += w
|
||||||
Da.remove(d)
|
Da[d] = False
|
||||||
print("Drone", d, "waits", w, "turn" + ('s' if w >= 2 else ''), "→", Dd[d])
|
print("Drone", d, "waits", w, "turn" + ('s' if w >= 2 else ''), "→", Dd[d])
|
||||||
Out.append(str(d) + ' W ' + str(w))
|
Out.append(str(d) + ' W ' + str(w))
|
||||||
|
|
||||||
|
@ -176,26 +181,52 @@ def wait(d, w=1):
|
||||||
# Control
|
# Control
|
||||||
def newTurn():
|
def newTurn():
|
||||||
global t
|
global t
|
||||||
|
# Finishing turn
|
||||||
|
for d in [d for d in range(len(Da)) if Da[d]]:
|
||||||
|
print(d)
|
||||||
|
wait(d)
|
||||||
|
assert(sum(Da) == 0)
|
||||||
|
# New turn
|
||||||
t += 1
|
t += 1
|
||||||
print("--- Turn", t)
|
print("--- Turn", t)
|
||||||
for d in Da:
|
|
||||||
wait(d)
|
|
||||||
assert(len(Da) == 0)
|
|
||||||
for d in range(D):
|
for d in range(D):
|
||||||
if Dd[d] <= t:
|
if Dd[d] <= t:
|
||||||
Da.append(d)
|
Da[d] = True
|
||||||
print("Drones", ", ".join([str(d) for d in Da]), "(", len(Da), ")", "are avaible")
|
print("Drones", ", ".join([str(d) for d in range(len(Da)) if Da[d]]), "(", len(Da), ")", "are avaible")
|
||||||
|
|
||||||
def end():
|
def end():
|
||||||
print("--- End!")
|
print("--- End!")
|
||||||
|
|
||||||
# IA
|
# IA
|
||||||
newTurn()
|
def nearestW(p, pos):
|
||||||
newTurn()
|
minDist = math.inf
|
||||||
|
selW = None
|
||||||
|
for w in range(W):
|
||||||
|
if Wi[w][p] > 0:
|
||||||
|
dist = distance(Wp[w], pos)
|
||||||
|
if dist < minDist:
|
||||||
|
minDist = dist
|
||||||
|
selW = w
|
||||||
|
return selW
|
||||||
|
|
||||||
|
|
||||||
|
def listNeeds():
|
||||||
|
N = []
|
||||||
|
# client, product
|
||||||
|
for c in range(C):
|
||||||
|
for p in range(P):
|
||||||
|
for cp in range(abs(Ci[c][p])):
|
||||||
|
N.append((c, p))
|
||||||
|
return N
|
||||||
|
|
||||||
|
|
||||||
|
while t < 10:
|
||||||
|
N = listNeeds()
|
||||||
|
newTurn()
|
||||||
|
|
||||||
# Output
|
# Output
|
||||||
f = open(sys.argv[1] + 'o', 'w')
|
f = open(sys.argv[1] + 'o', 'w')
|
||||||
f.write(str(len(Out)) + '\n' + '\n'.join(Out))
|
f.write(str(len(Out)) + '\n' + '\n'.join(Out) + '\n')
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def SortCustomer():
|
def SortCustomer():
|
||||||
|
|
Reference in a new issue