Fill drones

This commit is contained in:
Geoffrey Frogeye 2016-02-13 14:07:13 +01:00
parent 3da2b36465
commit 1c5f23d624
1 changed files with 27 additions and 8 deletions

View File

@ -46,6 +46,7 @@ class Warehouse:
self.plannedItems = self.items
# Set functions
def near(pos):
couples = []
for el in __class__.ALL:
@ -74,6 +75,22 @@ class Client:
def satisfied(self):
return len(self.needs) == 0
def pack(self, payload=-1):
if payload == -1:
payload = Drone.PAYLOAD
p = []
load = 0
# Sort needs by weight
couples = [(i, Product.get(i).weight) for i in self.plannedNeeds]
couples.sort(key=lambda c: c[1])
for couple in couples:
need, weight = couple
if load + weight <= payload:
p.append(need)
load += weight
return p
# Set functions
def near(pos):
couples = []
for el in __class__.ALL:
@ -170,9 +187,10 @@ class Drone:
def wait(self, turns=1):
assert(self.available())
self.busyFor(1)
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)
# Set functions
def near(pos):
couples = []
for el in __class__.ALL:
@ -246,17 +264,18 @@ def newTurn():
turn += 1
log("--- Turn", turn)
availableDrones = [str(drone.id) for drone in Drone.ALL if drone.available()]
log("Drones", ", ".join(availableDrones), "("+str(len(availableDrones))+")", "are available")
#log("Drones", ", ".join(availableDrones), "("+str(len(availableDrones))+")", "are available")
SIMULATION = 2
SIMULATION = 100
try:
d = 0
for c in Client.near(Warehouse.get(0).pos):
N = c.needs.copy()
for n in N:
if d < Drone.len():
Drone.get(d).addTask('load', Warehouse.get(0), Product.get(n), 1)
Drone.get(d).addTask('deliver', c, Product.get(n), 1)
if d < Drone.len():
pack = c.pack()
for i in pack:
Drone.get(d).addTask('load', Warehouse.get(0), Product.get(i), 1)
for i in pack:
Drone.get(d).addTask('deliver', c, Product.get(i), 1)
d += 1
while turn < SIMULATION:
newTurn()