Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import sqrt, ceil
- class Area:
- time = 0
- rows = 0
- columns = 0
- drones = []
- deadline = 0
- max_load = 0
- product_weights = []
- warehouses = []
- orders = []
- def __str__(self):
- return "Area:"\
- + "rows = " + str(self.rows) + "\n"\
- + "columns = " + str(self.columns) + "\n"\
- + "drones = " + str(self.drones) + "\n"\
- + "deadline = " + str(self.deadline) + "\n"\
- + "max_load = " + str(self.max_load) + "\n"\
- + "product_weights = " + str(self.product_weights) + "\n"\
- + "warehouses = " + str(self.warehouses) + "\n"\
- + "orders = " + str(self.orders) + "\n"
- def timeAdvance(self):
- min_adv = self.deadline
- for drone in self.drones:
- if drone.timeready < min_adv:
- min_adv = drone.timeready
- self.time = min_adv
- class drone:
- id = 0
- def __init__(self, num):
- self.id = num
- items = [] #item ids, duplicates allowed
- coords = []
- timeready = 0
- def __str__(self):
- return "drone:\n"\
- + "id: " + str(self.id) + "\n"\
- + "coords: " + str(self.coords) + "\n"
- class warehouse:
- id = 0
- def __init__(self, num):
- self.id = num
- coords = []
- products = []
- def __str__(self):
- return "warehouse:\n"\
- + "coords: " + str(self.coords) + "\n"\
- + "products: " + str(self.products) + "\n"
- def __repr__(self):
- return self.__str__()
- class order:
- def __init__(self, num):
- self.id = num
- coords = []
- items = []
- def __str__(self):
- return "order:\n"\
- + "coords: " + str(self.coords) + "\n"\
- + "items: " + str(self.items) + "\n"
- def __repr__(self):
- return self.__str__()
- def read_problem(filename):
- area = Area()
- f = open(filename, "r")
- f_str = f.read()
- lines = f_str.splitlines()
- for i in range(len(lines)):
- lines[i] = lines[i].split()
- area.rows = int(lines[0][0])
- area.columns = int(lines[0][1])
- for i in range(int(lines[0][2])):
- area.drones.append(drone(i))
- area.deadline = int(lines[0][3])
- area.max_load = int(lines[0][4])
- for i in range(int(lines[1][0])):
- area.product_weights.append(int(lines[2][i]))
- for i in range(int(lines[3][0])):
- linenr = 4 + (i * 2)
- w = warehouse(i)
- coords = [0,0]
- coords[0] = int(lines[linenr][0])
- coords[1] = int(lines[linenr][1])
- w.coords = coords
- prods = []
- for i in range(int(lines[1][0])):
- prods.append(int(lines[linenr + 1][i]))
- w.products = prods
- area.warehouses.append(w)
- base = 4 + (2 * int(lines[3][0]))
- for i in range(int(lines[base][0])):
- linenr = base + 1 + (i * 3)
- ord = order(i)
- coords = [int(lines[linenr][0]), int(lines[linenr][1])]
- ord.coords = coords
- items = []
- for i in range(int(lines[1][0])):
- items.append(0)
- for i in range(int(lines[linenr + 1][0])):
- items[int(lines[linenr + 2][i])] += 1
- ord.items = items
- area.orders.append(ord)
- for _drone in area.drones:
- _drone.coords = area.warehouses[0].coords
- #log(area)
- return area
- def distance(coo1, coo2):
- return ceil(sqrt((coo1[0]-coo2[0])**2 + (coo1[1]-coo2[1])**2))
- def calculateMinOrderLength(o, area):
- chosen = []
- total = 0
- for item in range(len(o.items)):
- dists = []
- for wh in area.warehouses:
- if wh.products[item] > 0:
- dists.append((distance(o.coords, wh.coords), wh.products[item], wh.id))
- dists.sort(reverse = True) #smallest last
- needed = o.items[item]
- while needed > 0:
- temp = dists.pop()
- subtr = min(temp[1], needed)
- # append (dist, whsID, itemID, count
- chosen.append((temp[0], temp[2], item, subtr))
- needed -= subtr
- # count dist
- total += temp[0]
- return (total, chosen, o.id)
- def calculateShortestOrder(area):
- orderscpy = []
- for o in area.orders:
- if o:
- orderscpy.append(calculateMinOrderLength(o, area))
- log(orderscpy)
- return min(orderscpy)
- def findClosestDrone(whs, area):
- maxdist = area.rows + area.columns
- bestdrone = False
- for drone in area.drones:
- dist = distance(drone.coords, area.warehouses[whs].coords)
- if dist < maxdist and drone.timeready <= area.time:
- maxdist = dist
- bestdrone = drone
- return bestdrone
- def write_solution(filename, sol):
- f = open(filename, "w")
- f.write(str(len(sol)) + "\n")
- for i in sol:
- f.write(i + "\n")
- f.close()
- def log(s):
- #print(s)
- pass
- if __name__ == "__main__":
- commands = []
- problem_name = "busy_day"
- #problem_name = "redundancy"
- #problem_name = "mother_of_all_warehouses"
- area = read_problem(problem_name + ".in")
- #while len(area.orders) > 0:
- for jef in range(len(area.orders)):
- print(str(area.orders.count(False)) + "/" + str(len(area.orders)))
- # log(area.orders)
- sor = calculateShortestOrder(area)
- log("sor = " + str(sor))
- for (dist, whsid, itemid, count) in sor[1]:
- drone = findClosestDrone(whsid, area)
- if not drone:
- log("time advance")
- area.timeAdvance()
- drone = findClosestDrone(whsid, area)
- log(drone)
- # 0L123 (Command to drone 0, load at warehouse 1 products of product type 2, three of them.
- commands.append(str(drone.id) + " " +\
- "L " +\
- str(whsid) + " " +\
- str(itemid) + " " +\
- str(count)
- )
- # 0D123 (Command to drone 0, deliver for order 1 items of product type 2, three of them.
- commands.append(str(drone.id) + " " +\
- "D " +\
- str(sor[2]) + " " +\
- str(itemid) + " " +\
- str(count)
- )
- area.warehouses[whsid].products[itemid] -= 1
- if area.warehouses[whsid].products[itemid] < 0:
- log("wtf")
- drone.timeready += distance(drone.coords, area.warehouses[whsid].coords)\
- + distance(area.warehouses[whsid].coords, area.orders[sor[2]].coords) + 2
- drone.coords = area.orders[sor[2]].coords
- area.orders[sor[2]] = False
- print(commands)
- write_solution(problem_name + ".out", commands)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement