Choose the best option based on efficiency
Regression...
This commit is contained in:
parent
13b0a4d054
commit
3509d21856
36
reborn.py
36
reborn.py
|
@ -292,32 +292,44 @@ def route(roadmap):
|
||||||
# Find the nearest client that still has things to be delivered
|
# 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']]
|
remainingClients = [c for c in Client.near(roadmap['pos']) if c.plannedNeeds and c not in roadmap['clients']]
|
||||||
#for client in remainingClients:
|
#for client in remainingClients:
|
||||||
|
options = []
|
||||||
for client in remainingClients[:CLIENT_TRIES]:
|
for client in remainingClients[:CLIENT_TRIES]:
|
||||||
# Create a pack to deliver
|
# Create a pack to deliver
|
||||||
pack = client.pack(Drone.PAYLOAD - Product.totalWeight(roadmap['loads']))
|
pack = client.pack(Drone.PAYLOAD - Product.totalWeight(roadmap['loads']))
|
||||||
if not pack:
|
if not pack:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
routeTime = distance(roadmap['pos'], client.pos) + len(list(set(pack)))
|
|
||||||
if roadmap['stops']:
|
|
||||||
# Efficiency of a travel = number of items carried / time of travel
|
# Efficiency of a travel = number of items carried / time of travel
|
||||||
# Calcultes the efficiency of adding a stop
|
# Calcultes the efficiency of adding a stop
|
||||||
routeEfficiency = Product.totalWeight(pack) / routeTime
|
routeTime = distance(roadmap['pos'], client.pos) + len(list(set(pack)))
|
||||||
|
routeEfficiency = len(pack) / routeTime
|
||||||
|
if roadmap['stops']:
|
||||||
# Calculates the efficiency of coming back to warehouse and to the client again
|
# 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)
|
backPack = client.pack()
|
||||||
backEfficiency = Product.totalWeight(client.pack()) / backTime
|
backTime = len(list(set(backPack))) + distance(roadmap['pos'], roadmap['warehouse'].pos) + distance(roadmap['warehouse'].pos, client.pos)
|
||||||
|
backEfficiency = len(backPack) / backTime
|
||||||
if backEfficiency > routeEfficiency:
|
if backEfficiency > routeEfficiency:
|
||||||
continue
|
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
|
# Plan the delivery
|
||||||
roadmap['warehouse'].plan(pack)
|
roadmap['warehouse'].plan(option['pack'])
|
||||||
client.plan(pack)
|
option['client'].plan(option['pack'])
|
||||||
roadmap['deliverTime'] += routeTime
|
roadmap['deliverTime'] += option['routeTime']
|
||||||
roadmap['pos'] = client.pos
|
roadmap['pos'] = option['client'].pos
|
||||||
roadmap['loads'] += pack
|
roadmap['loads'] += option['pack']
|
||||||
roadmap['clients'].append(client)
|
roadmap['clients'].append(option['client'])
|
||||||
roadmap['stops'].append((client, pack))
|
roadmap['stops'].append((option['client'], option['pack']))
|
||||||
return route(roadmap)
|
return route(roadmap)
|
||||||
|
else:
|
||||||
return roadmap
|
return roadmap
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in a new issue