Find nearest

This commit is contained in:
Geoffrey Frogeye 2016-02-13 13:09:14 +01:00
parent 04dcd605b0
commit 8728346ace

View file

@ -44,6 +44,12 @@ class Warehouse:
self.pos = pos self.pos = pos
self.items = items self.items = items
def near(pos):
couples = []
for el in __class__.ALL:
couples.append([el, distance(el.pos, pos)])
return [couple[0] for couple in sorted(couples, key=lambda c: c[1])]
def get(pid): def get(pid):
return __class__.ALL[pid] return __class__.ALL[pid]
@ -64,6 +70,12 @@ class Client:
def satisfied(self): def satisfied(self):
return len(self.needs) == 0 return len(self.needs) == 0
def near(pos):
couples = []
for el in __class__.ALL:
couples.append([el, distance(el.pos, pos)])
return [couple[0] for couple in sorted(couples, key=lambda c: c[1])]
def get(pid): def get(pid):
return __class__.ALL[pid] return __class__.ALL[pid]
@ -144,6 +156,12 @@ class Drone:
log("Drone", self.id, "waits", turns, "turn" + ('s' if turns >= 2 else ''), "", self.avail) log("Drone", self.id, "waits", turns, "turn" + ('s' if turns >= 2 else ''), "", self.avail)
output(self.id, 'W', turns) output(self.id, 'W', turns)
def near(pos):
couples = []
for el in __class__.ALL:
couples.append([el, distance(el.pos, pos)])
return [couple[0] for couple in sorted(couples, key=lambda c: c[1])]
def get(pid): def get(pid):
return __class__.ALL[pid] return __class__.ALL[pid]
@ -215,14 +233,15 @@ def newTurn():
try: try:
d = 0 d = 0
N = Client.get(0).needs.copy() c = Client.near(Warehouse.get(0).pos)[0]
N = c.needs.copy()
for n in N: for n in N:
Drone.get(d).load(Warehouse.get(0), Product.get(n), 1) Drone.get(d).load(Warehouse.get(0), Product.get(n), 1)
d += 1 d += 1
newTurn() newTurn()
d = 0 d = 0
for n in N: for n in N:
Drone.get(d).deliver(Client.get(0), Product.get(Drone.get(d).items[0]), 1) Drone.get(d).deliver(c, Product.get(Drone.get(d).items[0]), 1)
d += 1 d += 1