Now functionnal
This commit is contained in:
parent
c56e442d29
commit
f0de43a61d
35
main.py
35
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
|
||||||
|
@ -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)
|
||||||
|
@ -131,7 +132,7 @@ def weight(d):
|
||||||
# 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])
|
||||||
Wi[w][p] += -q
|
Wi[w][p] += -q
|
||||||
|
@ -143,7 +144,7 @@ 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])
|
||||||
Wi[w][p] += +q
|
Wi[w][p] += +q
|
||||||
|
@ -153,7 +154,7 @@ 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
|
Ci[w][p] += +q
|
||||||
|
@ -163,9 +164,11 @@ def deliver(d, c, p, q):
|
||||||
print("Drone", d, "delivers", q, "of", p, "to client", w, "→", Dd[d])
|
print("Drone", d, "delivers", q, "of", p, "to client", w, "→", Dd[d])
|
||||||
|
|
||||||
def wait(d, w=1):
|
def wait(d, w=1):
|
||||||
assert(d in Da)
|
assert(Da[d])
|
||||||
|
global Dd, Da
|
||||||
|
print(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))
|
||||||
|
|
||||||
|
@ -173,24 +176,26 @@ 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()
|
newTurn()
|
||||||
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()
|
||||||
|
|
Reference in a new issue