From 552736b566783277a5a2ff28325647e83cb9a5a1 Mon Sep 17 00:00:00 2001 From: Paul Lange Date: Thu, 11 Feb 2016 22:27:14 +0100 Subject: [PATCH] test --- .idea/.name | 1 + .idea/encodings.xml | 6 + .idea/hashcode.iml | 11 + .idea/misc.xml | 14 ++ .idea/modules.xml | 8 + .idea/workspace.xml | 526 ++++++++++++++++++++++++++++++++++++++++++++ logger.py | 12 +- models.py | 6 +- out.txt | 3 + out0.txt | 1 + out100.txt | 5 + out200.txt | 5 + out300.txt | 5 + out400.txt | 5 + out500.txt | 5 + out600.txt | 5 + out700.txt | 5 + parser.py | 54 +++-- 18 files changed, 646 insertions(+), 31 deletions(-) create mode 100644 .idea/.name create mode 100644 .idea/encodings.xml create mode 100644 .idea/hashcode.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/workspace.xml create mode 100644 out.txt create mode 100644 out0.txt create mode 100644 out100.txt create mode 100644 out200.txt create mode 100644 out300.txt create mode 100644 out400.txt create mode 100644 out500.txt create mode 100644 out600.txt create mode 100644 out700.txt diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..535d218 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +hashcode \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/hashcode.iml b/.idea/hashcode.iml new file mode 100644 index 0000000..2ef51e3 --- /dev/null +++ b/.idea/hashcode.iml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..99c2ab0 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..976f160 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..2ef621e --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,526 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1455219424948 + + + 1455219672286 + + + 1455219704568 + + + 1455221407393 + + + 1455222302839 + + + 1455224464485 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$PROJECT_DIR$/logger.py + 5 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/logger.py b/logger.py index a898c89..768529b 100644 --- a/logger.py +++ b/logger.py @@ -3,12 +3,12 @@ def __init__(self): self.commands = {} def append_command(self, t, command): - clist = self.commands[t] + clist = self.commands.get(t, []) clist.append(command) self.commands[t] = clist - def write_to_file(self, t_max): - fname = 'out.txt' + def write_to_file(self, t_max, s): + fname = 'out{}.txt'.format(s) linear = [] @@ -17,16 +17,16 @@ def write_to_file(self, t_max): linear.extend(self.commands[t]) with open(fname, 'w') as f: - f.write('{}'.format(len(self.commands))) + f.write('{}\n'.format(len(linear))) for cmd in linear: cmd_name = cmd['name'] if cmd_name == 'load': - f.write('{} L {} {} {}'.format(cmd['drone_id'], + f.write('{} L {} {} {}\n'.format(cmd['drone_id'], cmd['warehouse_id'], cmd['prod_id'], cmd['prod_count'])) elif cmd_name == 'deliver': - f.write('{} D {} {} {}'.format(cmd['drone_id'], + f.write('{} D {} {} {}\n'.format(cmd['drone_id'], cmd['order_id'], cmd['prod_id'], cmd['prod_count'])) diff --git a/models.py b/models.py index 1da17a4..f00c4e2 100644 --- a/models.py +++ b/models.py @@ -7,7 +7,7 @@ def __init__(self, items, location, warehouseDistances): self.warehouseDistances = warehouseDistances def is_ready_at(self, warehouse): - for idx, req_item in self.items.enumerate(): + for idx, req_item in enumerate(self.items): if warehouse.stock[idx] < req_item: return False return True @@ -31,7 +31,7 @@ def load(self, nItems, itemType, warehouse): """Moves the drone to a target warehouse and takes the required items and increments its finishedAt counter""" turnsMoving = self._move(warehouse.location) - warehouse.items[itemType] -= nItems + warehouse.stock[itemType] -= nItems self.cargo[itemType] += nItems self.finishedAt += turnsMoving + 1 @@ -59,6 +59,6 @@ def _move(self, targetLocation): """Moves the drone to the targetLocation and returns the turns taken""" distance = math.sqrt( (self.location[0] - targetLocation[0])**2 + - self.location[1] - targetLocation[1]) + (self.location[1] - targetLocation[1])**2) self.location = targetLocation return math.ceil(distance) diff --git a/out.txt b/out.txt new file mode 100644 index 0000000..1fa6719 --- /dev/null +++ b/out.txt @@ -0,0 +1,3 @@ +2 +0 L 0 0 3 +0 D 1 0 3 diff --git a/out0.txt b/out0.txt new file mode 100644 index 0000000..573541a --- /dev/null +++ b/out0.txt @@ -0,0 +1 @@ +0 diff --git a/out100.txt b/out100.txt new file mode 100644 index 0000000..61b1bf4 --- /dev/null +++ b/out100.txt @@ -0,0 +1,5 @@ +4 +0 L 0 50 1 +0 L 0 242 1 +0 D 43 50 1 +0 D 43 242 1 diff --git a/out200.txt b/out200.txt new file mode 100644 index 0000000..61b1bf4 --- /dev/null +++ b/out200.txt @@ -0,0 +1,5 @@ +4 +0 L 0 50 1 +0 L 0 242 1 +0 D 43 50 1 +0 D 43 242 1 diff --git a/out300.txt b/out300.txt new file mode 100644 index 0000000..61b1bf4 --- /dev/null +++ b/out300.txt @@ -0,0 +1,5 @@ +4 +0 L 0 50 1 +0 L 0 242 1 +0 D 43 50 1 +0 D 43 242 1 diff --git a/out400.txt b/out400.txt new file mode 100644 index 0000000..61b1bf4 --- /dev/null +++ b/out400.txt @@ -0,0 +1,5 @@ +4 +0 L 0 50 1 +0 L 0 242 1 +0 D 43 50 1 +0 D 43 242 1 diff --git a/out500.txt b/out500.txt new file mode 100644 index 0000000..61b1bf4 --- /dev/null +++ b/out500.txt @@ -0,0 +1,5 @@ +4 +0 L 0 50 1 +0 L 0 242 1 +0 D 43 50 1 +0 D 43 242 1 diff --git a/out600.txt b/out600.txt new file mode 100644 index 0000000..61b1bf4 --- /dev/null +++ b/out600.txt @@ -0,0 +1,5 @@ +4 +0 L 0 50 1 +0 L 0 242 1 +0 D 43 50 1 +0 D 43 242 1 diff --git a/out700.txt b/out700.txt new file mode 100644 index 0000000..61b1bf4 --- /dev/null +++ b/out700.txt @@ -0,0 +1,5 @@ +4 +0 L 0 50 1 +0 L 0 242 1 +0 D 43 50 1 +0 D 43 242 1 diff --git a/parser.py b/parser.py index eb00e63..59e47b1 100644 --- a/parser.py +++ b/parser.py @@ -9,7 +9,9 @@ def getDistance(location1, location2): euclidDist = math.sqrt((location1[0]-location2[0])**2+(location1[1]-location2[1])**2) return math.ceil(euclidDist) -f = open("example.in","r") + + +f = open("busy_day.in","r") commandList = [] #Parameters of simulation [nRows,nCols,nDrones,maxSteps,maxLoad]=[int(i) for i in f.readline().split()] @@ -36,22 +38,46 @@ def getDistance(location1, location2): 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 = warehouseDistances.sort() + 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 = warehouseDistances.sort() + warehouseDistances.sort() #Find order idx for drone, starting by looking at the closest warehouse for i in xrange(nWarehouses): preferredWarehouseIdx = warehouseDistances[i][1] @@ -80,7 +106,7 @@ def getDistance(location1, location2): for itemIdx in xrange(len(currentOrder.items)): itemcount = currentOrder.items[itemIdx] if itemcount > 0: - droneList[iDrone].deliver(itemcount, itemIdx, order[orderIdx]) + droneList[iDrone].deliver(itemcount, itemIdx, orderList[orderIdx]) #TODO create command in logger commandDict = { 'name' : 'deliver', @@ -94,21 +120,5 @@ def getDistance(location1, location2): #update available orders #availableOrders = easyOrders(orderList,warehouseList) -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, order in orders.enumerate(): - if orderWeight(order) < maxLoad: - for widx in order.warehouseDistances: - if order.is_ready_at(wh[widx]): - easy.append((oidx, widx)) - for i in range(len(wh[widx].stock)): - wh[widx].stock[i] -= order.items[i] - break - return easy + +log.write_to_file(maxSteps) \ No newline at end of file