while len(own_pickup_node) > 0: route = [depot.depot_id] for i in range(min(len(own_pickup_node), 2)): minIndex = np.argmin([model.distance_matrix[depot.depot_id, own_pickup_node[j]] for j in range(0, len(own_pickup_node))]) minnode = own_pickup_node[minIndex] route.append(minnode) arrival = model.time_matrix[depot.depot_id, minnode] departure = arrival + service_time route.append(own_delivery_node[minIndex]) arrival = departure + model.time_matrix[minnode, own_delivery_node[minIndex]] departure += arrival + service_time last_node = own_delivery_node[minIndex] own_pickup_node.remove(minnode) own_delivery_node.remove(own_delivery_node[minIndex]) for j in own_pickup_node: next_minIndex = np.argmin([model.distance_matrix[last_node, j]]) next_minnode = own_pickup_node[next_minIndex] arrival = departure + model.time_matrix[last_node, next_minnode] if arrival <= model.demand_dict[next_minnode].end_time and arrival <= depot.dend_time: route.append(next_minnode) departure = arrival + service_time route.append(own_delivery_node[next_minIndex]) arrival = departure + model.time_matrix[next_minnode, own_delivery_node[next_minIndex]] departure += arrival + service_time last_node = own_delivery_node[next_minIndex] own_pickup_node.remove(next_minnode) own_delivery_node.remove(own_delivery_node[next_minIndex]) else: continue route.append(depot.depot_id) sol.route_list.append(route) vehicle_number = vehicle_number - 1 if vehicle_number == 0: break
时间: 2024-02-29 14:57:33 浏览: 109
这段代码实现了一个基于贪心算法的送货路径规划。具体来说,它会先将起点设为仓库,然后在所有尚未配对的取货点和配送点中,选取距离仓库最近的两个取货点进行配对,将它们的编号加入路径中。然后,它会根据这两个取货点所对应的配送点的时间窗口,计算出到达和离开的时间,并将这两个配送点的编号加入路径中。接着,它会在剩下的取货点和配送点中,选取距离上一个配送点最近的取货点进行配对,然后重复之前的操作,不断将取货点和配送点加入路径中,直到所有货物都配送完毕。
需要注意的是,这段代码中的距离矩阵、时间矩阵、时间窗口等信息都是由 model 对象提供的,而且这里采用了 NumPy 库中的 argmin 函数来查找距离最近的取货点。另外,这段代码中的 sol 对象则是一个 Solution 类的实例,它用于保存所有的路径方案。
相关问题
def generateOwnCarRoute(service_time, model, sol): pickup_node = copy.deepcopy(model.demand_id_list[0: 16]) own_pickup_node = [] own_delivery_node = [] route = [] sol.route_list = [] depot = model.depot_dict['d1'] vehicle_number = depot.depot_capacity departure = 0 arrival = 0 for i in pickup_node: if i not in model.crowd_pickup_node: own_pickup_node.append(i) own_delivery_node.append(i+16) while vehicle_number > 0 and len(own_pickup_node) > 0: route.append(depot.depot_id) minIndex = np.argmin([model.distance_matrix[depot.depot_id, own_pickup_node[i]] for i in range(0, len(own_pickup_node))]) minnode = own_pickup_node[minIndex] route.append(minnode) arrival = model.time_matrix[depot.depot_id, minnode] departure = arrival + service_time route.append(own_delivery_node[minIndex]) arrival = departure + model.time_matrix[minnode, own_delivery_node[minIndex]] departure += arrival + service_time last_node = own_delivery_node[minIndex] own_pickup_node.remove(minnode) own_delivery_node.remove(own_delivery_node[minIndex]) for j in own_pickup_node: next_minIndex = np.argmin([model.distance_matrix[last_node, j]]) next_minnode = own_pickup_node[next_minIndex] arrival = departure + model.time_matrix[last_node, next_minnode] if arrival <= model.demand_dict[next_minnode].end_time and arrival <= depot.dend_time: route.append(next_minnode) departure = arrival + service_time route.append(own_delivery_node[next_minIndex]) arrival = departure + model.time_matrix[next_minnode, own_delivery_node[next_minIndex]] departure += arrival + service_time last_node = own_delivery_node[next_minIndex] own_pickup_node.remove(next_minnode) own_delivery_node.remove(own_delivery_node[next_minIndex]) else: continue route.append(depot.depot_id) sol.route_list.append(route) vehicle_number = vehicle_number - 1 route = [] print(sol.route_list) return sol.route_list 这段代码的问题是有可能vehicle_number为0了,但是owner_pickup_node的长度还不为0,这种情况怎么解决
这种情况下,程序会陷入无限循环,因为当所有车辆都被分配任务后,仍有剩余的pickup_node,但是vehicle_number为0,所以不会再有车辆来接这些节点。为了解决这个问题,可以在while循环条件中添加一个限制条件,即当所有车辆都被分配任务后,停止while循环。例如可以这样修改while循环的条件:
```
while vehicle_number > 0 and len(own_pickup_node) > 0 and len(sol.route_list) < model.depot_dict['d1'].depot_capacity:
```
这样,当所有车辆都被分配任务后,循环就会停止,避免了程序陷入无限循环的情况。
def checkTimeWindow(route, model, node): timetable = [] a = 0 node = Node() departure = 0 for i in range(len(route)): if i == 0: next_node_id = route[i + 1] travel_time = int(model.distance_matrix[model.depot_dict[route[i]].depot_id, next_node_id]) departure = max(0, model.demand_dict[next_node_id].start_time - travel_time) timetable.append((int(departure), int(departure))) elif 1 <= i <= len(route) - 2: last_node_id = route[i - 1] current_node_id = route[i] current_node = model.demand_dict[current_node_id] travel_time = int(model.distance_matrix[last_node_id, current_node_id]) arrival = max(timetable[-1][1] + travel_time, current_node.start_time) departure = arrival + current_node.service_time timetable.append((int(arrival), int(departure))) if departure > current_node.end_time: departure = float('inf') a = 1 else: last_node_id = route[i - 1] travel_time = int(model.distance_matrix[last_node_id, model.depot_dict[route[i]].depot_id]) departure = timetable[-1][1] + travel_time timetable.append((int(departure), int(departure))) if departure < node.dend_time: a = a + 0 else: a = a + 1 return a
这段代码看起来像是一个函数,函数名为checkTimeWindow,它有三个参数,分别是route,model和node。它的功能可能是检查给定路径中的时间窗口是否满足要求。在函数内部,它使用列表timetable来存储每个节点的到达和离开时间。对于第一个节点,它计算出离开时间并将其添加到timetable中。对于中间节点,它计算到达和离开时间,并将它们添加到timetable中。如果离开时间超出了节点的结束时间,它将a设置为1。对于最后一个节点,它计算离开时间并将其添加到timetable中。最后,如果最后一个节点的离开时间小于节点的结束时间,它将a设置为0。函数返回a的值。
阅读全文