Choose the best option based on efficiency

Regression...
This commit is contained in:
Geoffrey Frogeye 2016-02-13 18:22:16 +01:00
parent 13b0a4d054
commit 3509d21856

View file

@ -292,33 +292,45 @@ 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 and c not in roadmap['clients']]
#for client in remainingClients:
options = []
for client in remainingClients[:CLIENT_TRIES]:
# Create a pack to deliver
pack = client.pack(Drone.PAYLOAD - Product.totalWeight(roadmap['loads']))
if not pack:
continue
# Efficiency of a travel = number of items carried / time of travel
# Calcultes the efficiency of adding a stop
routeTime = distance(roadmap['pos'], client.pos) + len(list(set(pack)))
routeEfficiency = len(pack) / routeTime
if roadmap['stops']:
# Efficiency of a travel = number of items carried / time of travel
# Calcultes the efficiency of adding a stop
routeEfficiency = Product.totalWeight(pack) / routeTime
# Calculates the efficiency of coming back to warehouse and to the client again
backTime = distance(roadmap['pos'], roadmap['warehouse'].pos) + distance(roadmap['warehouse'].pos, client.pos)
backEfficiency = Product.totalWeight(client.pack()) / backTime
backPack = client.pack()
backTime = len(list(set(backPack))) + distance(roadmap['pos'], roadmap['warehouse'].pos) + distance(roadmap['warehouse'].pos, client.pos)
backEfficiency = len(backPack) / backTime
if backEfficiency > routeEfficiency:
continue
options.append({
'client': client,
'efficiency': routeEfficiency,
'routeTime': routeTime,
'pack': pack
})
if options:
# Choose the best option (i.e. the max efficiency)
option = sorted(options, key=lambda c: c['efficiency'])[0]
# Plan the delivery
roadmap['warehouse'].plan(pack)
client.plan(pack)
roadmap['deliverTime'] += routeTime
roadmap['pos'] = client.pos
roadmap['loads'] += pack
roadmap['clients'].append(client)
roadmap['stops'].append((client, pack))
roadmap['warehouse'].plan(option['pack'])
option['client'].plan(option['pack'])
roadmap['deliverTime'] += option['routeTime']
roadmap['pos'] = option['client'].pos
roadmap['loads'] += option['pack']
roadmap['clients'].append(option['client'])
roadmap['stops'].append((option['client'], option['pack']))
return route(roadmap)
return roadmap
else:
return roadmap
def think():