figure(); hold on; for n = 1:n_cars t_arrival = arrival_time(n); t_departure = departure_time(n); t_start = ceil(t_arrival / time_step) + 1; t_end = floor(t_departure / time_step) + 1; soc_arrival = energy_at_arrival(n) / bat_cap; soc_departure = soc_min; E_idx = round(soc_arrival * length(E)) + 1; step = t_start; while step <= t_end if path(n, step) == 1 if step == t_start soc = soc_arrival; else soc = soc_departure + (charge_power / bat_cap) * (step - k - 1) * time_step / 3600; end t = (step - 1) * time_step / 3600; plot([t, t+time_step/3600], [soc, soc], 'r', 'LineWidth', 2); hold on; plot([t, t], [soc, soc-departure_time(n)+arrival_time(n)], 'r-', 'LineWidth', 2); k = step; end soc_departure = max(soc_min, soc_arrival - charge_demand(n) / bat_cap); step = step + 1; end end xlabel('Time (h)'); ylabel('State of Charge'); title('Charging Schedule'); grid on; hold on;为什么无法生成曲线
时间: 2024-02-04 13:03:16 浏览: 129
根据代码,生成曲线的过程是在一个 for 循环中进行的,循环次数是 n_cars。因此,无法生成曲线可能是由于以下原因之一:
1. 变量 n_cars 的值为 0,即没有车辆需要生成曲线;
2. 变量 E 的值没有被初始化,导致无法计算 soc_arrival;
3. 变量 path 的值没有被初始化,导致无法判断需要绘制的路径;
4. 变量 charge_power、bat_cap、time_step、charge_demand、arrival_time、departure_time、energy_at_arrival、soc_min 的值没有被正确设置;
5. 曲线被生成,但是没有被正确显示。
需要检查以上原因,找出造成无法生成曲线的具体原因。
相关问题
优化代码“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”
这段代码主要是计算一组路线的时间和距离成本,并返回每个节点的出发和到达时间。其中,_compute_departure_time()函数计算从一个节点到下一个节点的出发时间,_compute_arrival_and_departure_time()函数计算到达一个节点的时间和离开时间,calTravelCost()函数是对这两个函数的封装,用于计算整个路线的时间和距离成本,并返回时间表、时间成本、距离成本和总成本。如果需要优化这段代码,可以考虑使用并行计算来提高计算效率,或者使用更高效的算法来计算时间和距离成本。此外,还可以考虑优化代码结构和变量命名,以提高代码的可读性和可维护性。
优化这段代码: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
可以尝试对代码进行如下优化:
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
```
阅读全文