Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
palango committed Feb 11, 2016
1 parent f7d6aff commit 552736b
Show file tree
Hide file tree
Showing 18 changed files with 646 additions and 31 deletions.
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/hashcode.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

526 changes: 526 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -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']))
Expand Down
6 changes: 3 additions & 3 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
3 changes: 3 additions & 0 deletions out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2
0 L 0 0 3
0 D 1 0 3
1 change: 1 addition & 0 deletions out0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
5 changes: 5 additions & 0 deletions out100.txt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions out200.txt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions out300.txt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions out400.txt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions out500.txt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions out600.txt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions out700.txt
Original file line number Diff line number Diff line change
@@ -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
54 changes: 32 additions & 22 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand All @@ -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]
Expand Down Expand Up @@ -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',
Expand All @@ -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)

0 comments on commit 552736b

Please sign in to comment.