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,这种情况怎么解决
时间: 2024-02-28 12:53:58 浏览: 142
这种情况下,程序会陷入无限循环,因为当所有车辆都被分配任务后,仍有剩余的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 generateInitialSol(model): demand_id_list=copy.deepcopy(model.demand_id_list) for i in range(model.popsize): seed=int(random.randint(0,10)) random.seed(seed) random.shuffle(demand_id_list) sol=Sol() sol.node_id_list=copy.deepcopy(demand_id_list) model.sol_list.append(sol)
生成初始解的伪代码如下:
```
function generateInitialSol(model)
demand_id_list = deepcopy(model.demand_id_list)
for i = 1 to model.popsize do
seed = random.randint(0, 10)
random.seed(seed)
random.shuffle(demand_id_list)
sol = Sol()
sol.node_id_list = deepcopy(demand_id_list)
model.sol_list.append(sol)
end for
end function
```
其中,`model`是模型对象,`demand_id_list`表示需求点的列表,`popsize`表示种群大小,`Sol()`是一个解的类,`node_id_list`表示解中经过的节点列表。函数通过循环生成`popsize`个初始解,每个解的节点顺序都是随机的。
优化代码:def generateInitialSol(model): demand_id_list=copy.deepcopy(model.demand_id_list) for i in range(model.popsize): seed=int(random.randint(0,10)) random.seed(seed)#设置随机数种子 random.shuffle(demand_id_list)#将需求id列表打乱 sol=Sol()#设置空集 sol.node_id_list=copy.deepcopy(demand_id_list) model.sol_list.append(sol)
可以尝试优化的地方如下:
1. 避免不必要的深拷贝操作,可以使用浅拷贝。这样可以减少内存的使用和提高代码的执行效率。
2. 在使用 random.seed(seed) 时,不需要每次都设置种子,可以在函数外部设置一次即可。
3. 在使用 random.shuffle(demand_id_list) 时,可以直接在原列表上进行操作,不需要创建新的列表。
优化后的代码如下:
```
def generateInitialSol(model):
demand_id_list = model.demand_id_list[:]
random.seed(10)
for i in range(model.popsize):
random.shuffle(demand_id_list)
sol = Sol()
sol.node_id_list = demand_id_list[:]
model.sol_list.append(sol)
```
值得注意的是,使用浅拷贝可能会导致意外修改原始列表,需要根据实际情况进行判断。
阅读全文