def recommendation1(driver_list): for driver in driver_list: if driver.num_of_occupied_position == 0 and not driver.route: # part_id_of_driver = nodes_belong_to_which_partition[driver.cur_location] # partition_in_range = [] # for i in Gd[part_id_of_driver-1]: # if D[part_id_of_driver+245-1][int(i)+245-1] <= driver.area_finding: # partition_in_range.append(i) # # 找到要求范围内,热门指数最大的区域 # max_hot_index_partition = partitions[int(partition_in_range[0])-1] # for i in range(1,len(partition_in_range)): # if partitions[int(partition_in_range[i])-1].hot_index > max_hot_index_partition.hot_index: # max_hot_index_partition = partitions[int(partition_in_range[i])-1] next_partition = partitions[random.randint(0, 174)] driver.route.extend( (str(driver.cur_location) + obtainPath(driver.cur_location - 1, next_partition.partition_id + 245 - 1) + str(next_partition.partition_id + 245)).split()) del driver.route[0]
def recommendation(driver_list): total_hot = 484 total_empty_driver = 0 for partition in partitions: partition.num_of_empty_drivers = 0 for driver in driver_list: if driver.num_of_occupied_position == 0: partitions[nodes_belong_to_which_partition[driver.cur_location] - 1].num_of_empty_drivers += 1 total_empty_driver += 1 for driver in driver_list: if driver.num_of_occupied_position == 0 and not driver.route: part_id_of_driver = nodes_belong_to_which_partition[ driver.cur_location] partition_in_range = [] for i in Gd[part_id_of_driver - 1]: if D[part_id_of_driver + 245 - 1][int(i) + 245 - 1] <= driver.area_finding: partition_in_range.append(i) # 找到要求范围内,热门指数最大的区域 max_hot_index_partition = partitions[int(partition_in_range[0]) - 1] for i in range(1, len(partition_in_range)): if partitions[int( partition_in_range[i] ) - 1].hot_index / total_hot - partitions[int( partition_in_range[i] ) - 1].num_of_empty_drivers / total_empty_driver > max_hot_index_partition.hot_index / total_hot - max_hot_index_partition.num_of_empty_drivers / total_empty_driver: max_hot_index_partition = partitions[ int(partition_in_range[i]) - 1] if not driver.cur_location == max_hot_index_partition.partition_id + 245: driver.route.extend( (str(driver.cur_location) + obtainPath( driver.cur_location - 1, max_hot_index_partition.partition_id + 245 - 1) + str(max_hot_index_partition.partition_id + 245)).split()) del driver.route[0]
def insertion_feasibility_check(query, driver, m, n, t_cur): # Calculate the time from driver.cur_location to query.pickup_location t_i = 0 t_i = t_i + T[driver.cur_location - 1][driver.cur_schedule[0][0] - 1] for i in range(m - 2): t_i = t_i + T[driver.cur_schedule[i][0] - 1][driver.cur_schedule[i + 1][0] - 1] t_i = t_i + T[driver.cur_schedule[m - 2][0] - 1][query.pickup_location - 1] if t_cur + datetime.timedelta(seconds=t_i) > query.latest_pickup_time: return False # Check the time delay incurred by the insertion of query.pickup_location causes the slack time of any point after position i in schedule V.s smaller than 0 t_i = t_i + T[query.pickup_location - 1][driver.cur_schedule[m - 1][0] - 1] if t_cur + datetime.timedelta(seconds=t_i) > driver.cur_schedule[m - 1][1]: return False for i in range(m, len(driver.cur_schedule) - 1): t_i = t_i + T[driver.cur_schedule[i - 1][0] - 1][driver.cur_schedule[i][0] - 1] if t_cur + datetime.timedelta(seconds=t_i) > driver.cur_schedule[i][1]: return False driver.cur_schedule.insert(m - 1, [ query.pickup_location, query.latest_pickup_time, 0 ]) # Insert query.pickup_location into driver.cur_schedule at position m # Calculate the time from driver.cur_location to query.destination t_j = 0 t_j = t_j + T[driver.cur_location - 1][driver.cur_schedule[0][0] - 1] for j in range(n + 1 - 2): t_j = t_j + T[driver.cur_schedule[j][0] - 1][driver.cur_schedule[j + 1][0] - 1] t_j = t_j + T[driver.cur_schedule[n + 1 - 2][0] - 1][query.delivery_location - 1] if t_cur + datetime.timedelta(seconds=t_j) > query.latest_delivery_time: del driver.cur_schedule[m - 1] return False elif n == len(driver.cur_schedule): driver.cur_schedule.insert( n + 1 - 1, [query.delivery_location, query.latest_delivery_time, 1] ) # Insert query.delivery_location into driver.cur_schedule as position n driver.add_passenger(1) # Recalculate route after schedule change driver.route = [] driver.route.extend((str(driver.cur_location) + obtainPath( driver.cur_location - 1, driver.cur_schedule[0][0] - 1) + str(driver.cur_schedule[0][0])).split()) for i in range(len(driver.cur_schedule) - 1): driver.route.extend( (str(driver.cur_schedule[i][0]) + obtainPath(driver.cur_schedule[i][0] - 1, driver.cur_schedule[i + 1][0] - 1) + str(driver.cur_schedule[i + 1][0])).split()) if m == 1 and driver.cur_schedule[0][0] == driver.cur_schedule[1][0]: driver.assist_t = 0 return True #Check the time delay incurred by the insertion of query.source causes the slack time of any point after position i in schedule V.s smaller than 0 t_j = t_j + T[query.delivery_location - 1][driver.cur_schedule[n + 1 - 1][0] - 1] if t_cur + datetime.timedelta(seconds=t_j) > driver.cur_schedule[n + 1 - 1][1]: del driver.cur_schedule[m - 1] return False for j in range(n + 1, len(driver.cur_schedule) - 1): t_j = t_j + T[driver.cur_schedule[j - 1][0] - 1][driver.cur_schedule[j][0] - 1] if t_cur + datetime.timedelta(seconds=t_j) > driver.cur_schedule[j][1]: del driver.cur_schedule[m - 1] return False driver.cur_schedule.insert(n + 1 - 1, [ query.delivery_location, query.latest_delivery_time, 1 ]) # Insert query.delivery_location into driver.cur_schedule as position n driver.add_passenger(1) # Recalculate route after schedule change driver.route = [] driver.route.extend( (str(driver.cur_location) + obtainPath(driver.cur_location - 1, driver.cur_schedule[0][0] - 1) + str(driver.cur_schedule[0][0])).split()) for i in range(len(driver.cur_schedule) - 1): driver.route.extend((str(driver.cur_schedule[i][0]) + obtainPath( driver.cur_schedule[i][0] - 1, driver.cur_schedule[i + 1][0] - 1) + str(driver.cur_schedule[i + 1][0])).split()) if m == 1 and driver.cur_schedule[0][0] == driver.cur_schedule[1][0]: driver.assist_t = 0 return True
if not satisfied_driver_list[j].cur_schedule: satisfied_driver_list[j].number_of_order += 1 number_of_order_served += 1 total_time += (starttime - query.generation_time).seconds total_distance += D[query.pickup_location - 1][query.delivery_location - 1] # 如果一开始route非空 if satisfied_driver_list[j].route: satisfied_driver_list[j].cur_schedule.append( [query.pickup_location, query.latest_pickup_time, 0]) first_location_origin_route = satisfied_driver_list[ j].route[0] satisfied_driver_list[j].route = [] satisfied_driver_list[j].route.extend( (str(satisfied_driver_list[j].cur_location) + obtainPath(satisfied_driver_list[j].cur_location - 1, query.pickup_location - 1) + str(query.pickup_location)).split()) satisfied_driver_list[j].cur_schedule.append([ query.delivery_location, query.latest_delivery_time, 1 ]) satisfied_driver_list[j].route.extend( (str(query.pickup_location) + obtainPath(query.pickup_location - 1, query.delivery_location - 1) + str(query.delivery_location)).split()) del satisfied_driver_list[j].route[0] if satisfied_driver_list[j].route[ 0] == first_location_origin_route: satisfied_driver_list[ j].assist_t = satisfied_driver_list[j].assist_t else:
# print("0") if empty_driver_list: sign0 = False for driver in empty_driver_list: if starttime + datetime.timedelta(seconds=T[ driver.cur_location - 1][query.pickup_location]) < query.latest_pickup_time: driver.cur_schedule.append( [query.pickup_location, query.latest_pickup_time, 0]) #如果一开始route非空 if driver.route: first_location_origin_route = driver.route[0] driver.route = [] driver.route.extend( (str(driver.cur_location) + obtainPath(driver.cur_location - 1, query.pickup_location - 1) + str(query.pickup_location)).split()) driver.cur_schedule.append([ query.delivery_location, query.latest_delivery_time, 1 ]) driver.route.extend( (obtainPath(query.pickup_location - 1, query.delivery_location - 1) + str(query.delivery_location)).split()) del driver.route[0] if driver.route[0] == first_location_origin_route: driver.assist_t = driver.assist_t else: driver.assist_t = -abs(driver.assist_t) #一开始为空
l = [1, 2, 3, 4] del l[0] print(l) print(-1 / 2) d = datetime.datetime.now() print(d) d = d + datetime.timedelta(seconds=random.randint(1, 3)) print(d) # for i in range(0,V): # for j in range(0,V): # route_temp = str(i+1) + obtainPath(i,j) + str(j+1) # route[i][j] = route_temp.split() route_temp = str(4) + obtainPath(3, 7) + str(8) print(route_temp.split()) l1 = [1, 2, 3] l2 = [4, 5, 6] print(l1) l1.extend(l2) print(l1) del l1[1] print(l1) a = l1[3] l1.remove(a) print(l1) l1.append(3) print(l1)
def insertion_feasibility_check(query, driver, m, n, t_cur): # No remaining seats if driver.num_of_occupied_position == Driver.capacity: return False # Calculate the time from driver.cur_location to query.source t_i = 0 t_i = t_i + T[get_which_grid_the_location_belongs_to(driver.cur_location)-1][get_which_grid_the_location_belongs_to(driver.cur_schedule[0][0])-1] for i in range(m-2): t_i = t_i + T[get_which_grid_the_location_belongs_to(driver.cur_schedule[i][0])-1][get_which_grid_the_location_belongs_to(driver.cur_schedule[i+1][0])-1] t_i = t_i + T[get_which_grid_the_location_belongs_to(driver.cur_schedule[m-2][0])-1][get_which_grid_the_location_belongs_to(query.source)-1] if t_cur + datetime.timedelta(seconds=t_i) > query.pickup_window[1]: return False # Check the time delay incurred by the insertion of query.source causes the slack time of any point after position i in schedule V.s smaller than 0 t_i = t_i + T[get_which_grid_the_location_belongs_to(query.source)-1][get_which_grid_the_location_belongs_to(driver.cur_schedule[m-1][0])-1] if t_cur + datetime.timedelta(seconds=t_i) > driver.cur_schedule[m-1][1]: return False for i in range(m,len(driver.cur_schedule)-1): t_i = t_i + T[get_which_grid_the_location_belongs_to(driver.cur_schedule[i-1][0])-1][get_which_grid_the_location_belongs_to(driver.cur_schedule[i][0])-1] if t_cur + datetime.timedelta(seconds=t_i) > driver.cur_schedule[i][1]: return False driver.cur_schedule.insert(m-1, [query.source,query.pickup_window[1], 0]) # Insert query.source into driver.cur_schedule at position m # Calculate the time from driver.cur_location to query.destination t_j = 0 t_j = t_j + T[get_which_grid_the_location_belongs_to(driver.cur_location)-1][get_which_grid_the_location_belongs_to(driver.cur_schedule[0][0])-1] for j in range(n + 1 - 2): t_j = t_j + T[get_which_grid_the_location_belongs_to(driver.cur_schedule[j][0])-1][ get_which_grid_the_location_belongs_to(driver.cur_schedule[j + 1][0])-1] t_j = t_j + T[get_which_grid_the_location_belongs_to(driver.cur_schedule[n + 1 - 2][0])-1][ get_which_grid_the_location_belongs_to(query.destination)-1] if t_cur + datetime.timedelta(seconds=t_j) > query.delivery_window[1]: del driver.cur_schedule[m-1] return False # Check the time delay incurred by the insertion of query.source causes the slack time of any point after position i in schedule V.s smaller than 0 t_j = t_j + T[get_which_grid_the_location_belongs_to(query.destination)-1][get_which_grid_the_location_belongs_to(driver.cur_schedule[n+1-1][0])-1] if t_cur + datetime.timedelta(seconds=t_j) > driver.cur_schedule[n+1-1][1]: del driver.cur_schedule[m - 1] return False for j in range(n+1,len(driver.cur_schedule)-1): t_j = t_j + T[get_which_grid_the_location_belongs_to(driver.cur_schedule[j-1][0])-1][get_which_grid_the_location_belongs_to(driver.cur_schedule[j][0])-1] if t_cur + datetime.timedelta(seconds=t_j) > driver.cur_schedule[j][1]: del driver.cur_schedule[m - 1] return False driver.cur_schedule.insert(n + 1 - 1, [query.destination, query.delivery_window[1], 1]) # Insert query.destination into driver.cur_schedule at position n driver.add_passenger(1) # Recalculate route after schedule change driver.route.extend((str(get_which_grid_the_location_belongs_to(driver.cur_schedule[-3][0])) + obtainPath( get_which_grid_the_location_belongs_to(driver.cur_schedule[-3][0]) - 1, get_which_grid_the_location_belongs_to(driver.cur_schedule[-2][0]) - 1) + str( get_which_grid_the_location_belongs_to(driver.cur_schedule[-2][0]))).split()) driver.route.extend((str(get_which_grid_the_location_belongs_to(driver.cur_schedule[-2][0])) + obtainPath( get_which_grid_the_location_belongs_to(driver.cur_schedule[-2][0]) - 1, get_which_grid_the_location_belongs_to(driver.cur_schedule[-1][0]) - 1) + str( get_which_grid_the_location_belongs_to(driver.cur_schedule[-1][0]))).split()) # print('world') return True
1][q.delivery_location - 1] query_list.append(q) query_id += 1 break # Search for each order for query in query_list: # Search empty driver empty_driver_list = empty_driver_search(driver_list, query, starttime) if empty_driver_list: empty_driver_list[0].cur_schedule.append( [query.pickup_location, query.latest_pickup_time, 0]) empty_driver_list[0].route = [] empty_driver_list[0].route.extend( (str(empty_driver_list[0].cur_location) + obtainPath(empty_driver_list[0].cur_location - 1, query.pickup_location - 1) + str(query.pickup_location)).split()) empty_driver_list[0].cur_schedule.append( [query.delivery_location, query.latest_delivery_time, 1]) empty_driver_list[0].route.extend( (str(query.pickup_location) + obtainPath( query.pickup_location - 1, query.delivery_location - 1) + str(query.delivery_location)).split()) empty_driver_list[0].add_passenger(1) empty_driver_list[0].number_of_order += 1 query_list.remove(query) number_of_order_served += 1 total_time += (starttime - query.generation_time).seconds total_distance += D[query.pickup_location - 1][query.delivery_location - 1] continue