优化这段代码:def calTravelCost(route_list,model): timetable_list=[] distance_of_routes=0 time_of_routes=0 obj=0 for route in route_list: timetable=[] vehicle=model.vehicle_dict[route[0]] travel_distance=0 travel_time=0 v_type = route[0] free_speed=vehicle.free_speed fixed_cost=vehicle.fixed_cost variable_cost=vehicle.variable_cost for i in range(len(route)): if i == 0: next_node_id=route[i+1] travel_time_between_nodes=model.distance_matrix[v_type,next_node_id]/free_speed departure=max(0,model.demand_dict[next_node_id].start_time-travel_time_between_nodes) 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_between_nodes=model.distance_matrix[last_node_id,current_node_id]/free_speed arrival=max(timetable[-1][1]+travel_time_between_nodes,current_node.start_time) departure=arrival+current_node.service_time timetable.append((int(arrival),int(departure))) travel_distance += model.distance_matrix[last_node_id, current_node_id] travel_time += model.distance_matrix[last_node_id, current_node_id]/free_speed+\ + max(current_node.start_time - arrival, 0) else: last_node_id = route[i - 1] travel_time_between_nodes = model.distance_matrix[last_node_id,v_type]/free_speed departure = timetable[-1][1]+travel_time_between_nodes timetable.append((int(departure),int(departure))) travel_distance += model.distance_matrix[last_node_id,v_type] travel_time += model.distance_matrix[last_node_id,v_type]/free_speed distance_of_routes+=travel_distance time_of_routes+=travel_time if model.opt_type==0: obj+=fixed_cost+travel_distance*variable_cost else: obj += fixed_cost + travel_time *variable_cost timetable_list.append(timetable) return timetable_list,time_of_routes,distance_of_routes,obj
时间: 2023-11-21 16:05:08 浏览: 65
教你如何优化C代码
4星 · 用户满意度95%
可以尝试对代码进行如下优化:
1. 使用enumerate函数获取遍历列表时的下标和值,可以避免使用range(len(route))。
2. 减少重复计算,如model.distance_matrix[last_node_id,current_node_id]/free_speed和model.distance_matrix[last_node_id,v_type]/free_speed可以放在循环外面计算。
3. 使用+=运算符可以避免重复的赋值操作。
4. 合并if语句和elif语句,使用continue语句可以减少缩进层数。
5. 将int函数的调用移到循环外面,只进行一次强制类型转换。
优化后的代码如下:
```
def calTravelCost(route_list, model):
timetable_list = []
distance_of_routes = 0
time_of_routes = 0
obj = 0
for route in route_list:
timetable = []
vehicle = model.vehicle_dict[route[0]]
travel_distance = 0
travel_time = 0
v_type = route[0]
free_speed = vehicle.free_speed
fixed_cost = vehicle.fixed_cost
variable_cost = vehicle.variable_cost
last_node_id = None
for i, current_node_id in enumerate(route):
if i == 0:
next_node_id = route[i+1]
travel_time_between_nodes = model.distance_matrix[v_type, next_node_id] / free_speed
departure = max(0, model.demand_dict[next_node_id].start_time - travel_time_between_nodes)
timetable.append((departure, departure))
else:
current_node = model.demand_dict[current_node_id]
travel_distance += model.distance_matrix[last_node_id, current_node_id]
travel_time_between_nodes = model.distance_matrix[last_node_id, current_node_id] / free_speed
arrival = max(timetable[-1][1] + travel_time_between_nodes, current_node.start_time)
departure = arrival + current_node.service_time
timetable.append((arrival, departure))
travel_time += travel_time_between_nodes + max(current_node.start_time - arrival, 0)
if i == len(route) - 1:
travel_distance += model.distance_matrix[last_node_id, v_type]
travel_time_between_nodes = model.distance_matrix[last_node_id, v_type] / free_speed
departure = timetable[-1][1] + travel_time_between_nodes
timetable.append((departure, departure))
last_node_id = current_node_id
distance_of_routes += travel_distance
time_of_routes += travel_time
if model.opt_type == 0:
obj += fixed_cost + travel_distance * variable_cost
else:
obj += fixed_cost + travel_time * variable_cost
timetable_list.append([(int(arrival), int(departure)) for arrival, departure in timetable])
return timetable_list, time_of_routes, distance_of_routes, obj
```
阅读全文