Drone task list

This commit is contained in:
Geoffrey Frogeye 2016-02-13 13:44:11 +01:00
parent 8728346ace
commit 6da3a76ca5

View file

@ -44,6 +44,8 @@ class Warehouse:
self.pos = pos self.pos = pos
self.items = items self.items = items
self.plannedItems = self.items
def near(pos): def near(pos):
couples = [] couples = []
for el in __class__.ALL: for el in __class__.ALL:
@ -67,6 +69,8 @@ class Client:
self.pos = pos self.pos = pos
self.needs = needs self.needs = needs
self.plannedNeeds = self.needs
def satisfied(self): def satisfied(self):
return len(self.needs) == 0 return len(self.needs) == 0
@ -94,8 +98,21 @@ class Drone:
self.pos = Warehouse.get(0).pos self.pos = Warehouse.get(0).pos
self.items = [] self.items = []
self.avail = 0 self.avail = 0
self.tasks = [] self.tasks = []
def addTask(self, *task):
self.tasks.append(task)
def executeTask(self):
if self.available():
if len(self.tasks):
task = self.tasks[0]
getattr(self, task[0])(*task[1:])
self.tasks = self.tasks[1:]
else:
self.wait()
def weight(self): def weight(self):
s = 0 s = 0
for i in self.items: for i in self.items:
@ -223,26 +240,26 @@ readFile(sys.argv[1])
def newTurn(): def newTurn():
global turn global turn
# Finishing turn # Finishing turn
for drone in [drone for drone in Drone.ALL if drone.available()]: for drone in Drone.ALL:
drone.wait() drone.executeTask()
# New turn # New turn
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
try: try:
d = 0 d = 0
c = Client.near(Warehouse.get(0).pos)[0] for c in Client.near(Warehouse.get(0).pos):
N = c.needs.copy() N = c.needs.copy()
for n in N: for n in N:
Drone.get(d).load(Warehouse.get(0), Product.get(n), 1) 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)
d += 1 d += 1
while turn < SIMULATION:
newTurn() newTurn()
d = 0
for n in N:
Drone.get(d).deliver(c, Product.get(Drone.get(d).items[0]), 1)
d += 1
except KeyboardInterrupt: except KeyboardInterrupt: