降低这段代码的重复率: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-10-22 13:09:33 浏览: 70
Timetable_of_course.rar_MATLAB排课遗传_matlab排课_matlab排课代码_排课 MATLAB
5星 · 资源好评率100%
Here is a possible way to reduce the repetition in the code:
```python
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]]
v_type = route[0]
free_speed = vehicle.free_speed
fixed_cost = vehicle.fixed_cost
variable_cost = vehicle.variable_cost
for i, node_id in enumerate(route):
if i == 0:
next_node_id = route[i + 1]
travel_distance, travel_time, departure = _compute_departure_time(model, v_type, next_node_id, free_speed, 0)
elif i < len(route) - 1:
last_node_id = route[i - 1]
current_node = model.demand_dict[node_id]
travel_distance, travel_time, arrival, departure = _compute_arrival_and_departure_time(model, last_node_id, current_node, free_speed, timetable[-1][1])
timetable.append((int(arrival), int(departure)))
else:
last_node_id = route[i - 1]
travel_distance, travel_time, departure = _compute_departure_time(model, last_node_id, v_type, free_speed, timetable[-1][1])
timetable.append((int(departure), int(departure)))
distance_of_routes += travel_distance
time_of_routes += travel_time
if model.opt_type == 0:
obj += fixed_cost + distance_of_routes * variable_cost
else:
obj += fixed_cost + time_of_routes * variable_cost
timetable_list.append(timetable)
return timetable_list, time_of_routes, distance_of_routes, obj
def _compute_departure_time(model, from_node_id, to_node_id, free_speed, arrival_time):
travel_distance = model.distance_matrix[from_node_id, to_node_id]
travel_time = travel_distance / free_speed
departure_time = max(arrival_time, model.demand_dict[to_node_id].start_time - travel_time)
return travel_distance, travel_time, departure_time
def _compute_arrival_and_departure_time(model, from_node_id, to_node, free_speed, arrival_time):
travel_distance = model.distance_matrix[from_node_id, to.id]
travel_time = travel_distance / free_speed
arrival_time = max(arrival_time + travel_time, to.start_time)
departure_time = arrival_time + to.service_time
return travel_distance, travel_time, arrival_time, departure_time
```
In this refactored code, I extracted two helper functions `_compute_departure_time` and `_compute_arrival_and_departure_time` to avoid duplication of code. I also simplified the loop that iterates over the nodes in each route by using the `enumerate` function to get both the index and the value of each node. Finally, I moved the computation of `distance_of_routes`, `time_of_routes`, and `obj` inside the loop to avoid redundancy.
阅读全文