def local_search_one(crowd_route_list, route_list, sol, model, node): b = 0 while b < 15: route_list_l1 = copy.deepcopy(route_list) owncar_route = random.choice(route_list_l1) route_list_l1.remove(owncar_route) owncar_nodeIndex1 = random.choice(range(1, len(owncar_route) - 1)) owncar_nodeIndex2 = random.choice(range(1, len(owncar_route) - 1)) if owncar_nodeIndex1 != owncar_nodeIndex2: owncar_node1 = owncar_route[owncar_nodeIndex1] owncar_route[owncar_nodeIndex1] = owncar_route[owncar_nodeIndex2] owncar_route[owncar_nodeIndex2] = owncar_node1 owncar_route = list(set(owncar_route)) a1 = checkPriority(owncar_route) # a2 = checkTimeWindow(owncar_route, model, node) a3 = checkCapacity(owncar_route, model) if a1+a3 == 0: route_list_l1.append(owncar_route) model.tempt_cost = calobj(crowd_route_list, route_list_l1, sol, model) else: continue else: continue b = b + 1 return model.tempt_cost, route_list_l1 这一段代码有什么问题吗
时间: 2024-02-29 10:56:21 浏览: 125
这段代码的问题可能是出现在`checkPriority()`、`checkCapacity()`和`calobj()`这三个函数中的实现。在没有看到这些函数的实现细节之前,无法确定代码是否存在问题。此外,还需要了解`sol`和`model`这两个参数的具体含义和实现。另外,如果在函数内部对`route_list`进行了修改,需要注意是否会影响函数外部的变量。
相关问题
def calCrowdcarCost(crowd_route_list, model): cost_of_distance = 0 crowd_wait_time = 0 model.crowdcarcost = 0 for route in crowd_route_list: timetable = [] for i in range(len(route)): if i == 0: depot_id = route[i] next_node_id = route[i+1] cost_of_distance = model.distance_matrix[depot_id, next_node_id] travel_time = int(model.distance_matrix[depot_id, next_node_id]) departure = max(0, model.demand_dict[next_node_id].start_time - travel_time) if departure == 0: crowd_wait_time = travel_time - model.demand_dict[next_node_id].start_time timetable.append((departure, 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]) cost_of_distance += model.distance_matrix[last_node_id, current_node_id] arrival = max(timetable[-1][1]+travel_time, current_node.start_time) if arrival == timetable[-1][1]+travel_time: crowd_wait_time += timetable[-1][1]+travel_time - current_node.start_time else: break model.crowdcarcost += model.fixcost + model.percost * cost_of_distance return model.crowdcarcost, crowd_wait_time 在这一段代码中我想知道每个点的等待时间总和,等待时间等于到达这个的时间减去它的时间窗上限
在这段代码中,你已经计算了每个点的等待时间,它们被累加到了`crowd_wait_time`变量中。如果你想得到每个点的等待时间总和,可以在循环中添加一个变量来记录。以下是修改后的代码:
```python
def calCrowdcarCost(crowd_route_list, model):
cost_of_distance = 0
crowd_wait_time = 0
point_wait_time = {} # 记录每个点的等待时间
model.crowdcarcost = 0
for route in crowd_route_list:
timetable = []
for i in range(len(route)):
if i == 0:
depot_id = route[i]
next_node_id = route[i+1]
cost_of_distance = model.distance_matrix[depot_id, next_node_id]
travel_time = int(model.distance_matrix[depot_id, next_node_id])
departure = max(0, model.demand_dict[next_node_id].start_time - travel_time)
if departure == 0:
crowd_wait_time = travel_time - model.demand_dict[next_node_id].start_time
point_wait_time[next_node_id] = travel_time - model.demand_dict[next_node_id].start_time
timetable.append((departure, 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])
cost_of_distance += model.distance_matrix[last_node_id, current_node_id]
arrival = max(timetable[-1][1]+travel_time, current_node.start_time)
if arrival == timetable[-1][1]+travel_time:
wait_time = timetable[-1][1]+travel_time - current_node.start_time
crowd_wait_time += wait_time
point_wait_time[current_node_id] = wait_time
else:
break
model.crowdcarcost += model.fixcost + model.percost * cost_of_distance
return model.crowdcarcost, crowd_wait_time, point_wait_time
```
这样,`point_wait_time`字典中的每个键值对代表一个点的等待时间,其中键为点的ID,值为该点的等待时间。
Traceback (most recent call last): File "C:/Users/lmx/PycharmProjects/pythonProject/1.py", line 523, in <module> main(demand_file=demand_file, depot_file=depot_file, epochs=250, v_cap=3) File "C:/Users/lmx/PycharmProjects/pythonProject/1.py", line 503, in main sol3, route_list3 = local_search_two(crowd_route_list1, route_list2, sol, model, node) TypeError: cannot unpack non-iterable NoneType object
这个错误通常出现在你试图对一个 NoneType 对象进行解包操作的时候。在这个具体的错误中,是因为 `local_search_two` 函数返回了 None,导致解包时出现了这个错误。请检查 `local_search_two` 函数的实现,确保其返回一个可迭代的对象。同时,你也可以添加条件判断来避免这个错误的出现,例如:
```python
res = local_search_two(crowd_route_list1, route_list2, sol, model, node)
if res is not None:
sol3, route_list3 = res
else:
# 处理 res 为 None 的情况
```
阅读全文
相关推荐
















