From 1c5f23d624b1c924c6951f63b230f76f4af374cc Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Sat, 13 Feb 2016 14:07:13 +0100 Subject: [PATCH] Fill drones --- reborn.py | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/reborn.py b/reborn.py index 535c9cf..192f9c7 100755 --- a/reborn.py +++ b/reborn.py @@ -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()