-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
124 lines (106 loc) · 4.55 KB
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from models import *
from logger import Logger
import math
import copy
log = Logger()
def getDistance(location1, location2):
euclidDist = math.sqrt((location1[0]-location2[0])**2+(location1[1]-location2[1])**2)
return math.ceil(euclidDist)
f = open("busy_day.in","r")
commandList = []
#Parameters of simulation
[nRows,nCols,nDrones,maxSteps,maxLoad]=[int(i) for i in f.readline().split()]
#weights of the products available for orders
nTypes=int(f.readline())
productWeights = [int(i) for i in f.readline().split()]
#warehouses and availability of individual product types
nWarehouses=int(f.readline())
warehouseList = []
for iWarehouse in xrange(nWarehouses):
location = tuple([int(i) for i in f.readline().split()])
items = [int(i) for i in f.readline().split()]
warehouse = Warehouse(items,location)
warehouseList.append(warehouse)
#customer orders
nOrders=int(f.readline())
orderList = []
for iOrder in xrange(nOrders):
location = tuple([int(i) for i in f.readline().split()])
nOrderedProducts = int(f.readline())
tmpItems = [int(i) for i in f.readline().split()]
items = [tmpItems.count(i) for i in range(nTypes)]
warehouseDistances = [(getDistance(location, warehouseList[i].location),i) for i in range(nWarehouses)]
warehouseDistances.sort()
order = Order(items,location,warehouseDistances)
orderList.append(order)
#Create drones
droneList = [Drone(warehouseList[0].location, maxLoad, nTypes) for i in range(nDrones)]
def orderWeight(order):
sum = 0
for i in range(len(order.items)):
sum += order.items[i] * productWeights[i]
return sum
def easyOrders(orders,warehouses):
easy = []
wh = copy.deepcopy(warehouses)
for oidx, order2 in enumerate(orders):
if orderWeight(order2) < maxLoad:
for _, widx in order2.warehouseDistances:
if order2.is_ready_at(wh[widx]):
easy.append((oidx, widx))
for i in range(len(wh[widx].stock)):
wh[widx].stock[i] -= order2.items[i]
break
return easy
#World
for iStep in xrange(maxSteps):
#do all jobs pending and find free drones
#freeDronesIdx = []
if (iStep % 100 == 0):
log.write_to_file(maxSteps, iStep)
availableOrders = easyOrders(orderList,warehouseList)
for iDrone in xrange(nDrones):
if droneList[iDrone].finishedAt == iStep:
warehouseDistances = [(getDistance(droneList[iDrone].location, warehouseList[i].location),i) for i in range(nWarehouses)]
warehouseDistances.sort()
#Find order idx for drone, starting by looking at the closest warehouse
for i in xrange(nWarehouses):
preferredWarehouseIdx = warehouseDistances[i][1]
orderIdx = None
for availableOrder in availableOrders:
if(availableOrder[1]==preferredWarehouseIdx):
orderIdx = availableOrder[0]
break
if orderIdx is not None:
break
#Handle order for drone
currentOrder = orderList[orderIdx]
for itemIdx in xrange(len(currentOrder.items)):
itemcount = currentOrder.items[itemIdx]
if itemcount > 0:
droneList[iDrone].load(itemcount, itemIdx, warehouseList[preferredWarehouseIdx])
#TODO create command in logger
commandDict = {
'name' : 'load',
'drone_id' : iDrone,
'warehouse_id' : preferredWarehouseIdx,
'prod_id' : itemIdx,
'prod_count' : itemcount
}
log.append_command(droneList[iDrone].finishedAt, commandDict)
for itemIdx in xrange(len(currentOrder.items)):
itemcount = currentOrder.items[itemIdx]
if itemcount > 0:
droneList[iDrone].deliver(itemcount, itemIdx, orderList[orderIdx])
#TODO create command in logger
commandDict = {
'name' : 'deliver',
'drone_id' : iDrone,
'order_id' : orderIdx,
'prod_id' : itemIdx,
'prod_count' : itemcount
}
log.append_command(droneList[iDrone].finishedAt, commandDict)
#update available orders
#availableOrders = easyOrders(orderList,warehouseList)
log.write_to_file(maxSteps)