Deliver everyone

This commit is contained in:
Geoffrey Frogeye 2016-02-13 15:36:44 +01:00
parent f31e6e74c8
commit 726474abb9
1 changed files with 57 additions and 16 deletions

View File

@ -3,11 +3,14 @@
import sys
import math
import copy
import progressbar
DEBUG = False
outLines = []
def log(*data):
if True:
if DEBUG:
print(*data)
def output(*values):
@ -44,7 +47,11 @@ class Warehouse:
self.pos = pos
self.items = items
self.plannedItems = self.items
self.plannedItems = self.items.copy()
def plan(self, items):
for i in items:
self.plannedItems.remove(i)
# Set functions
def near(pos):
@ -70,12 +77,17 @@ class Client:
self.pos = pos
self.needs = needs
self.plannedNeeds = self.needs
self.plannedNeeds = self.needs.copy()
def plan(self, needs):
for n in needs:
self.plannedNeeds.remove(n)
def satisfied(self):
return len(self.needs) == 0
def pack(self, payload=-1):
# TODO Optimise for same product wanted more than once
if payload == -1:
payload = Drone.PAYLOAD
p = []
@ -187,7 +199,7 @@ 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
@ -210,6 +222,7 @@ T = 0 # Deadline
turn = 0 # Turn
score = 0 # Score
done = False
def readFile(filename):
global X, Y, T
@ -266,20 +279,47 @@ def newTurn():
availableDrones = [str(drone.id) for drone in Drone.ALL if drone.available()]
#log("Drones", ", ".join(availableDrones), "("+str(len(availableDrones))+")", "are available")
SIMULATION = 100
# Algorithm that only works for 1 warehouse
def think():
# For each drone that has nothing to do
for drone in [d for d in Drone.ALL if d.available() and not d.tasks]:
# Find the nearest warehouse
warehouse = Warehouse.near(drone.pos)[0]
# Find the nearest client that still has things to be delivered
remainingClients = [c for c in Client.near(warehouse.pos) if c.plannedNeeds]
if remainingClients:
client = remainingClients[0]
# Create a pack to deliver
pack = client.pack()
# Plan the delivery
# TODO Optimise for same product wanted more than once
for i in pack:
drone.addTask('load', warehouse, Product.get(i), 1)
warehouse.plan(pack)
for i in pack:
drone.addTask('deliver', client, Product.get(i), 1)
client.plan(pack)
else:
global done
done = True
break
if DEBUG:
SIMULATION = 1000
else:
SIMULATION = 8*T/10
try:
d = 0
for c in Client.near(Warehouse.get(0).pos):
if d < Drone.len():
pack = c.pack()
if len(pack) == len(c.needs):
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:
if not DEBUG:
bar = progressbar.ProgressBar(max_value=SIMULATION)
while turn < SIMULATION and not done:
think()
newTurn()
if not DEBUG:
bar.update(turn)
if not DEBUG:
bar.finish()
except KeyboardInterrupt:
@ -287,5 +327,6 @@ except KeyboardInterrupt:
with open(sys.argv[1] + 'o', 'w') as f:
f.write(str(len(outLines)) + '\n' + '\n'.join(outLines) + '\n')
print("Turn:", turn)
print("Score:", score)