Preparation for multiple stop trips
Nothing should have changed in the results, yet it won three points. Oh well...
This commit is contained in:
parent
f25aca1c31
commit
dd206307b9
70
reborn.py
70
reborn.py
|
@ -30,6 +30,12 @@ class Product:
|
||||||
|
|
||||||
self.weight = weight
|
self.weight = weight
|
||||||
|
|
||||||
|
def totalWeight(items):
|
||||||
|
s = 0
|
||||||
|
for i in items:
|
||||||
|
s += Product.get(i).weight
|
||||||
|
return s
|
||||||
|
|
||||||
def get(pid):
|
def get(pid):
|
||||||
return __class__.ALL[pid]
|
return __class__.ALL[pid]
|
||||||
|
|
||||||
|
@ -87,11 +93,15 @@ class Client:
|
||||||
return len(self.needs) == 0
|
return len(self.needs) == 0
|
||||||
|
|
||||||
def pack(self, payload=-1):
|
def pack(self, payload=-1):
|
||||||
# TODO Optimise for same product wanted more than once
|
|
||||||
if payload == -1:
|
if payload == -1:
|
||||||
payload = Drone.PAYLOAD
|
payload = Drone.PAYLOAD
|
||||||
p = []
|
p = []
|
||||||
load = 0
|
load = 0
|
||||||
|
# # Sort occurences
|
||||||
|
# occ = [(i, self.plannedNeeds.count(i)) for i in self.plannedNeeds]
|
||||||
|
# occ.sort(key=lambda c: c[1])
|
||||||
|
# # TODO Optimise for same product wanted more than once
|
||||||
|
# Looks like it's not necessary for set 2, we'll see that later
|
||||||
# Sort needs by weight
|
# Sort needs by weight
|
||||||
couples = [(i, Product.get(i).weight) for i in self.plannedNeeds]
|
couples = [(i, Product.get(i).weight) for i in self.plannedNeeds]
|
||||||
couples.sort(key=lambda c: c[1])
|
couples.sort(key=lambda c: c[1])
|
||||||
|
@ -143,10 +153,7 @@ class Drone:
|
||||||
self.wait()
|
self.wait()
|
||||||
|
|
||||||
def weight(self):
|
def weight(self):
|
||||||
s = 0
|
return Product.totalWeight(self.items)
|
||||||
for i in self.items:
|
|
||||||
s += Product.get(i).weight
|
|
||||||
return s
|
|
||||||
|
|
||||||
def busyFor(self, time):
|
def busyFor(self, time):
|
||||||
self.avail += time
|
self.avail += time
|
||||||
|
@ -280,31 +287,52 @@ def newTurn():
|
||||||
#log("Drones", ", ".join(availableDrones), "("+str(len(availableDrones))+")", "are available")
|
#log("Drones", ", ".join(availableDrones), "("+str(len(availableDrones))+")", "are available")
|
||||||
|
|
||||||
# Algorithm that only works for 1 warehouse
|
# Algorithm that only works for 1 warehouse
|
||||||
|
def route(roadmap):
|
||||||
|
# Find the nearest client that still has things to be delivered
|
||||||
|
remainingClients = [c for c in Client.near(roadmap['pos']) if c.plannedNeeds]
|
||||||
|
if remainingClients:
|
||||||
|
client = remainingClients[0]
|
||||||
|
# Create a pack to deliver
|
||||||
|
pack = client.pack()
|
||||||
|
# TODO if pack ... else ...
|
||||||
|
# Plan the delivery
|
||||||
|
roadmap['warehouse'].plan(pack)
|
||||||
|
client.plan(pack)
|
||||||
|
roadmap['deliverTime'] += distance(roadmap['pos'], client.pos) + len(list(set(pack)))
|
||||||
|
roadmap['pos'] = client.pos
|
||||||
|
roadmap['loads'] += pack
|
||||||
|
roadmap['stops'].append((client, pack))
|
||||||
|
return roadmap
|
||||||
|
|
||||||
|
|
||||||
def think():
|
def think():
|
||||||
# For each drone that has nothing to do
|
# For each drone that has nothing to do
|
||||||
for drone in [d for d in Drone.ALL if d.available() and not d.tasks]:
|
for drone in [d for d in Drone.ALL if d.available() and not d.tasks]:
|
||||||
# Find the nearest warehouse
|
# Find the nearest warehouse
|
||||||
warehouse = Warehouse.near(drone.pos)[0]
|
warehouse = Warehouse.near(drone.pos)[0]
|
||||||
# Find the nearest client that still has things to be delivered
|
roadmap = route({
|
||||||
remainingClients = [c for c in Client.near(warehouse.pos) if c.plannedNeeds]
|
'pos': warehouse.pos,
|
||||||
if remainingClients:
|
'warehouse': warehouse,
|
||||||
client = remainingClients[0]
|
'deliverTime': 0,
|
||||||
# Create a pack to deliver
|
'loads': [],
|
||||||
pack = client.pack()
|
'stops': []
|
||||||
# Plan the delivery
|
})
|
||||||
packOccurences = dict((i, pack.count(i)) for i in pack)
|
|
||||||
# From http://stackoverflow.com/a/7843090/2766106
|
if not roadmap['stops']:
|
||||||
for i in packOccurences:
|
|
||||||
drone.addTask('load', warehouse, Product.get(i), packOccurences[i])
|
|
||||||
warehouse.plan(pack)
|
|
||||||
for i in packOccurences:
|
|
||||||
drone.addTask('deliver', client, Product.get(i), packOccurences[i])
|
|
||||||
client.plan(pack)
|
|
||||||
else:
|
|
||||||
global done
|
global done
|
||||||
done = True
|
done = True
|
||||||
break
|
break
|
||||||
|
|
||||||
|
loadOcc = dict((i, roadmap['loads'].count(i)) for i in roadmap['loads'])
|
||||||
|
for i in loadOcc:
|
||||||
|
drone.addTask('load', warehouse, Product.get(i), loadOcc[i])
|
||||||
|
|
||||||
|
for client, items in roadmap['stops']:
|
||||||
|
itemsOcc = dict((j, items.count(j)) for j in items)
|
||||||
|
for i in itemsOcc:
|
||||||
|
drone.addTask('deliver', client, Product.get(i), itemsOcc[i])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
SIMULATION = 1000
|
SIMULATION = 1000
|
||||||
|
|
Reference in a new issue