from entity import * from dispatch import dispatch import numpy as np import time as tt import copy env = [[[] for _ in range(30)] for _ in range(30)] # def didis_way(): # test_drivers = load("get_test_data/test_drivers.pkl") # test_orders = load("get_test_data/test_orders.pkl") # distance_sum = 0. # money_sum = 0. # count = 0 # for t in range(144): # print("当前时间段:",t) # order_list = test_orders[t] # new_env = env.copy() # for order in order_list: # new_env[order.x][order.y].append(order) # t = t + 1 # matchs_list = [] # sign = tt.time() # n = 0 # for driver in test_drivers: # if driver.time == t: # a_drivers_order = [] # for orders in new_env[driver.x-6:driver.x+6+1]: # for _orders in orders[driver.y-6:driver.y+6+1]: # for order in _orders: # if abs(order.x - driver.x) + abs(order.y - driver.y) <= 6: # a_drivers_order.append(Match(order,driver,get_value=True)) # # matchs_list.append(Match(order,driver,get_value=True)) # a_drivers_order = value_max_k(a_drivers_order,k=10) # n+=len(a_drivers_order) # print('\r'+str(n),end='') # matchs_list.extend(a_drivers_order) # print('\ndispatch start--') # _dispatch = dispatch(matchs_list) # for match in _dispatch: # d_id = match.driver.id # update_driver = Driver(d_id,match.order.to_x,match.order.to_y,match.arrive_time) # test_drivers[d_id] = update_driver # # 数据统计 # distance_sum += match.distance # money_sum += match.money # count += 1 # print(tt.time()-sign) # print("平均接客距离:",distance_sum/count) # print("总收入:",money_sum) # print("总单数:",count) # print("平均每单收入:",money_sum/count) def didis_way(): # test_orders = load("get_test_data/test_orders.pkl") test_orders = load("get_test_data/test_orders05.pkl") test_drivers = load("get_test_data/test_drivers.pkl") distance_sum = 0. money_sum = 0. count_orders = 0 count_orders_money = 0 count = 0 for t in range(1,145): print("当前时间段:", t) for order_list in test_orders[t-1]: # order_list = test_orders[t - 1] # 总单数 count_orders += len(order_list) count_orders_money += sum([o.order_money for o in order_list]) rest_orders = order_list[:] is_empty = False while not is_empty: have_done_orders = [] new_env = copy.deepcopy(env) for order in rest_orders: new_env[order.x][order.y].append(order) # matchs_list = [] sign = tt.time() n = 0 graph = [[test_drivers[_],set()] for _ in range(6000)] for driver in test_drivers: if driver.atime == t: a_drivers_order = [] for orders in new_env[max(driver.x - 5,0):driver.x + 5 + 1]: for _orders in orders[max(driver.y - 5,0):driver.y + 5 + 1]: for order in _orders: if abs(order.x - driver.x) + abs(order.y - driver.y) <= 5: _match = Match(order, driver, get_value=True) a_drivers_order.append(_match) a_drivers_order = value_max_k(a_drivers_order, k=10) update_graph(graph,a_drivers_order) n += len(a_drivers_order) print('\r' + str(n), end='') # matchs_list.extend(a_drivers_order) print('\ndispatch start--') if sum([len(_[1]) for _ in graph]): _dispatch = get_dispatch(graph) else: is_empty = True _dispatch = [] for match in _dispatch: # 已完成订单 have_done_orders.append(match.order) d_id = match.driver.id update_driver = Driver(d_id, match.order.to_x, match.order.to_y, time=match.arrive_time) test_drivers[d_id] = update_driver # 数据统计 distance_sum += match.distance money_sum += match.money count += 1 rest_orders = list(set(rest_orders) - set(have_done_orders)) if len(rest_orders)==0: is_empty = True print("耗时", tt.time() - sign) if t: for i in range(len(test_drivers)): if test_drivers[i].atime == t: dr = test_drivers[i] test_drivers[dr.id] = Driver(dr.id, dr.x, dr.y, time=t+1) sss = [a.atime for a in test_drivers] print(np.mean(np.array(sss))) print("s_time"+str(t)+":",sss) print("didis_way:") print("接单总数:", count) weijiedan = count_orders - count print("未接单总数:", weijiedan) weijiedan_money = count_orders_money - money_sum print("未接单总金额:", weijiedan_money) print("未接单平均金额:", weijiedan_money / weijiedan) print("平均接客距离:", distance_sum / count) print("总收入:", money_sum) print("平均每单收入:", money_sum / count) def min_distance(): # test_orders = load("get_test_data/test_orders.pkl") test_orders = load("get_test_data/test_orders05.pkl") test_drivers = load("get_test_data/test_drivers.pkl") distance_sum = 0. money_sum = 0. count_orders = 0 count_orders_money = 0 count = 0 new_data = [[] for _ in range(144)] for t in range(1,145): print("当前时间段:", t) for order_list in test_orders[t-1]: # order_list = test_orders[t - 1] new_env = copy.deepcopy(env) for order in order_list: new_env[order.x][order.y].append(order) #总单数count count_orders += len(order_list) count_orders_money += sum([o.order_money for o in order_list]) matchs_list = [] sign = tt.time() n = 0 graph = [[test_drivers[_],set()] for _ in range(8000)] for driver in test_drivers: if driver.atime == t: a_drivers_order = [] for orders in new_env[max(driver.x - 5,0):driver.x + 5 + 1]: for _orders in orders[max(driver.y - 5,0):driver.y + 5 + 1]: for order in _orders: if abs(order.x - driver.x) + abs(order.y - driver.y) <= 5: _match = Match(order, driver, get_value=True) _match.value = _match.distance a_drivers_order.append(_match) a_drivers_order = value_min_k(a_drivers_order, k=10) update_graph(graph,a_drivers_order) n += len(a_drivers_order) print('\r' + str(n), end='') matchs_list.extend(a_drivers_order) print('\ndispatch start--') if sum([len(_[1]) for _ in graph]): _dispatch = get_dispatch(graph) else: _dispatch = [] for match in _dispatch: #收集事务 new_data[t-1].append(match) d_id = match.driver.id update_driver = Driver(d_id, match.order.to_x, match.order.to_y, time=match.arrive_time) test_drivers[d_id] = update_driver # 数据统计 distance_sum += match.distance money_sum += match.money count += 1 print("耗时", tt.time() - sign) if t: for i in range(len(test_drivers)): if test_drivers[i].atime == t: dr = test_drivers[i] test_drivers[dr.id] = Driver(dr.id, dr.x, dr.y, time=t+1) sss = [a.atime for a in test_drivers] print(np.mean(np.array(sss))) print("s_time"+str(t)+":",sss) save(new_data,"train_data111.pkl") print("min_distance:") print("接单总数:", count) weijiedan = count_orders - count print("未接单总数:",weijiedan) weijiedan_money = count_orders_money - money_sum print("未接单总金额:", weijiedan_money) print("未接单平均金额:", weijiedan_money/weijiedan) print("平均接客距离:", distance_sum / count) print("总收入:", money_sum) print("平均每单收入:", money_sum / count) def min_distance222(): # test_orders = load("get_test_data/test_orders.pkl") test_orders = load("get_test_data/test_orders05.pkl") test_drivers = load("get_test_data/test_drivers.pkl") distance_sum = 0. money_sum = 0. count_orders = 0 count_orders_money = 0 count = 0 new_data = [[] for _ in range(144)] for t in range(1,145): print("当前时间段:", t) for order_list in test_orders[t-1]: # order_list = test_orders[t - 1] # 总单数count count_orders += len(order_list) count_orders_money += sum([o.order_money for o in order_list]) rest_orders = order_list[:] is_empty = False while not is_empty: have_done_orders = [] new_env = copy.deepcopy(env) for order in rest_orders: new_env[order.x][order.y].append(order) # matchs_list = [] sign = tt.time() n = 0 graph = [[test_drivers[_],set()] for _ in range(6000)] for driver in test_drivers: if driver.atime == t: a_drivers_order = [] for orders in new_env[max(driver.x - 5,0):driver.x + 5 + 1]: for _orders in orders[max(driver.y - 5,0):driver.y + 5 + 1]: for order in _orders: if abs(order.x - driver.x) + abs(order.y - driver.y) <= 5: _match = Match(order, driver, get_value=True) _match.value = _match.distance a_drivers_order.append(_match) a_drivers_order = value_min_k(a_drivers_order, k=10) update_graph(graph,a_drivers_order) n += len(a_drivers_order) print('\r' + str(n), end='') # matchs_list.extend(a_drivers_order) print('\ndispatch start--') if sum([len(_[1]) for _ in graph]): _dispatch = get_dispatch(graph) else: is_empty = True _dispatch = [] for match in _dispatch: #收集事务 new_data[t-1].append(match) have_done_orders.append(match.order) d_id = match.driver.id update_driver = Driver(d_id, match.order.to_x, match.order.to_y, time=match.arrive_time) test_drivers[d_id] = update_driver # 数据统计 distance_sum += match.distance money_sum += match.money count += 1 rest_orders = list(set(rest_orders) - set(have_done_orders)) if len(rest_orders)==0: is_empty = True print("耗时", tt.time() - sign) if t: for i in range(len(test_drivers)): if test_drivers[i].atime == t: dr = test_drivers[i] new_data[t-1].append(Match(Order(-1,dr.x,dr.y,dr.x,dr.y,t),dr,get_value=True,is_cancel=True)) test_drivers[dr.id] = Driver(dr.id, dr.x, dr.y, time=t+1) sss = [a.atime for a in test_drivers] print(np.mean(np.array(sss))) print("s_time"+str(t)+":",sss) save(new_data,"train_data111.pkl") print("min_distance:") print("接单总数:", count) weijiedan = count_orders - count print("未接单总数:",weijiedan) weijiedan_money = count_orders_money - money_sum print("未接单总金额:", weijiedan_money) print("未接单平均金额:", weijiedan_money/weijiedan) print("平均接客距离:", distance_sum / count) print("总收入:", money_sum) print("平均每单收入:", money_sum / count) def min_distance333(): # test_orders = load("get_test_data/test_orders.pkl") test_orders = load("get_test_data/test_orders05.pkl") test_drivers = load("get_test_data/test_drivers.pkl") distance_sum = 0. money_sum = 0. count_orders = 0 count_orders_money = 0 count = 0 new_data = [[] for _ in range(144)] for t in range(1,145): print("当前时间段:", t) for order_list in test_orders[t-1]: # order_list = test_orders[t - 1] # 总单数count count_orders += len(order_list) count_orders_money += sum([o.order_money for o in order_list]) rest_orders = order_list[:] is_empty = False for matching_distance in [4,6]: no_match = False print("matching_distance:::",matching_distance) while not no_match: have_done_orders = [] new_env = copy.deepcopy(env) for order in rest_orders: new_env[order.x][order.y].append(order) # matchs_list = [] sign = tt.time() n = 0 graph = [[test_drivers[_],set()] for _ in range(6000)] for driver in test_drivers: if driver.atime == t: a_drivers_order = [] for orders in new_env[max(driver.x - matching_distance,0):driver.x + matching_distance + 1]: for _orders in orders[max(driver.y - matching_distance,0):driver.y + matching_distance + 1]: for order in _orders: if abs(order.x - driver.x) + abs(order.y - driver.y) <= matching_distance: _match = Match(order, driver, get_value=True) _match.value = _match.distance a_drivers_order.append(_match) a_drivers_order = value_min_k(a_drivers_order, k=10) update_graph(graph,a_drivers_order) n += len(a_drivers_order) print('\r' + str(n), end='') # matchs_list.extend(a_drivers_order) print('\ndispatch start--') if sum([len(_[1]) for _ in graph]): _dispatch = get_dispatch(graph) else: no_match = True _dispatch = [] for match in _dispatch: #收集事务 new_data[t-1].append(match) have_done_orders.append(match.order) d_id = match.driver.id update_driver = Driver(d_id, match.order.to_x, match.order.to_y, time=match.arrive_time) test_drivers[d_id] = update_driver # 数据统计 distance_sum += match.distance money_sum += match.money count += 1 rest_orders = list(set(rest_orders) - set(have_done_orders)) if len(rest_orders)==0: is_empty = True print("耗时", tt.time() - sign) if is_empty: break for i in range(len(test_drivers)): if test_drivers[i].atime == t: dr = test_drivers[i] new_data[t-1].append(Match(Order(-1,dr.x,dr.y,dr.x,dr.y,t),dr,get_value=True,is_cancel=True)) test_drivers[dr.id] = Driver(dr.id, dr.x, dr.y, time=t+1) sss = [a.atime for a in test_drivers] print(np.mean(np.array(sss))) print("s_time"+str(t)+":",sss) # save(new_data,"train_data111.pkl") print("min_distance:") print("接单总数:", count) weijiedan = count_orders - count print("未接单总数:",weijiedan) weijiedan_money = count_orders_money - money_sum print("未接单总金额:", weijiedan_money) print("未接单平均金额:", weijiedan_money/weijiedan) print("平均接客距离:", distance_sum / count) print("总收入:", money_sum) print("平均每单收入:", money_sum / count) def max_money(): # test_orders = load("get_test_data/test_orders.pkl") test_orders = load("get_test_data/test_orders05.pkl") test_drivers = load("get_test_data/test_drivers.pkl") distance_sum = 0. money_sum = 0. count_orders = 0 count_orders_money = 0 count = 0 for t in range(1,145): print("当前时间段:", t) for order_list in test_orders[t - 1]: # order_list = test_orders[t - 1] # 总单数count count_orders += len(order_list) count_orders_money += sum([o.order_money for o in order_list]) rest_orders = order_list[:] is_empty = False while not is_empty: have_done_orders = [] new_env = copy.deepcopy(env) for order in rest_orders: new_env[order.x][order.y].append(order) # matchs_list = [] sign = tt.time() n = 0 graph = [[test_drivers[_], set()] for _ in range(6000)] for driver in test_drivers: if driver.atime == t: a_drivers_order = [] for orders in new_env[max(driver.x - 5, 0):driver.x + 5 + 1]: for _orders in orders[max(driver.y - 5, 0):driver.y + 5 + 1]: for order in _orders: if abs(order.x - driver.x) + abs(order.y - driver.y) <= 5: _match = Match(order, driver, get_value=True) _match.value = _match.money a_drivers_order.append(_match) a_drivers_order = value_max_k(a_drivers_order, k=10) update_graph(graph, a_drivers_order) n += len(a_drivers_order) print('\r' + str(n), end='') # matchs_list.extend(a_drivers_order) print('\ndispatch start--') if sum([len(_[1]) for _ in graph]): _dispatch = get_dispatch(graph) else: is_empty = True _dispatch = [] for match in _dispatch: # 收集事务 have_done_orders.append(match.order) d_id = match.driver.id update_driver = Driver(d_id, match.order.to_x, match.order.to_y, time=match.arrive_time) test_drivers[d_id] = update_driver # 数据统计 distance_sum += match.distance money_sum += match.money count += 1 rest_orders = list(set(rest_orders) - set(have_done_orders)) if len(rest_orders) == 0: is_empty = True print("耗时", tt.time() - sign) if t: for i in range(len(test_drivers)): if test_drivers[i].atime == t: dr = test_drivers[i] test_drivers[dr.id] = Driver(dr.id, dr.x, dr.y, time=t+1) sss = [a.atime for a in test_drivers] print(np.mean(np.array(sss))) print("s_time"+str(t)+":",sss) print("max_money:") print("接单总数:", count) weijiedan = count_orders - count print("未接单总数:",weijiedan) weijiedan_money = count_orders_money - money_sum print("未接单总金额:", weijiedan_money) print("未接单平均金额:", weijiedan_money/weijiedan) print("平均接客距离:", distance_sum / count) print("总收入:", money_sum) print("平均每单收入:", money_sum / count) def value_max_k(match_list,k=10): match_list = sorted(match_list,key=lambda x:x.value,reverse=True) match_list = match_list[:k] return match_list def value_min_k(match_list,k=10): match_list = sorted(match_list,key=lambda x:x.value,reverse=False) match_list = match_list[:k] return match_list def update_graph(graph,match_list): for match in match_list: graph[match.driver.id][1].add(match.order) def matching_strategy(orders,drivers): rest_orders = orders[:] is_empty = False for order in rest_orders: while not is_empty: have_done_orders = [] new_env = copy.deepcopy(env) for order in rest_orders: new_env[order.x][order.y].append(order) sign = tt.time() n = 0 graph = [[drivers[_], set()] for _ in range(6000)] for driver in drivers: if driver.atime == order.order_time: a_drivers_order = [] for orders in new_env[max(driver.x - 5, 0):driver.x + 5 + 1]: for _orders in orders[max(driver.y - 5, 0):driver.y + 5 + 1]: for order in _orders: if abs(order.x - driver.x) + abs(order.y - driver.y) <= 5: _match = Match(order, driver, get_value=True) a_drivers_order.append(_match) a_drivers_order = value_min_k(a_drivers_order, k=10) update_graph(graph, a_drivers_order) n += len(a_drivers_order) print('\r' + str(n), end='') print('\ndispatch start--') if sum([len(_[1]) for _ in graph]): _dispatch = get_dispatch(graph) else: is_empty = True _dispatch = [] for match in _dispatch: # 收集事务 have_done_orders.append(match.order) d_id = match.driver.id update_driver = Driver(d_id, match.order.to_x, match.order.to_y, time=match.arrive_time) drivers[d_id] = update_driver rest_orders = list(set(rest_orders) - set(have_done_orders)) if len(rest_orders) == 0: is_empty = True print("耗时", tt.time() - sign) from rtree import index def test_001(): idx = index.Index() left, bottom, right, top = (8.0, 8.0, 12.0, 12.0) idx.insert(0, (left, bottom, right, top)) driver_x = 9.5 driver_y = 10.6 driver_id = 10 idx.insert(driver_id, (driver_x, driver_y, driver_x, driver_y)) print(list(idx.intersection((left, bottom, right, top)))) idx.nearest() def get_dispatch(graph): dispatch_list = [] while graph: ord_set = graph[0] graph.remove(ord_set) if ord_set[1]: new_list = [] new_list.append(ord_set) res_list = [] res_list.append(ord_set) while new_list: temp_list = [] for o_set in new_list: for s in graph: if o_set[1] & s[1]: graph.remove(s) temp_list.append(s) res_list.append(s) new_list = temp_list _d = [] # print(len(res_list)) for item1 in res_list: for item2 in list(item1[1]): _match = Match(item2,item1[0], get_value=True) _match.value = _match.distance # _match.value = _match.money _d.append(_match) # print('d1') # print(_d) res = dispatch(_d) # print(res) dispatch_list.extend(res) return dispatch_list if __name__ == '__main__': # didis_way() # min_distance() # min_distance222() min_distance333() # test_001() # max_money() pass