Fill drones

This commit is contained in:
Geoffrey Frogeye 2016-02-13 14:07:13 +01:00
parent 3da2b36465
commit 1c5f23d624

View file

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