城市轨道行车组织运行时刻表自动化生成---使用python代码实现

一、设计目标

(1)理解《城市轨道交通系统运营管理》课程所学理论知识,进一步巩固列车运行图编制理论;

(2)掌握城市轨道交通列车运行图的基本要素,运行图编制的步骤和技术关键,并进行绘图等基本技能的训练;

(3)培养团队合作和沟通能力;

二、按照《城市轨道交通系统运营管理》所学的列车运行图编制步骤要求,进行python代码编写,生成列车运行时刻表,可配合qetrc软件进行列车运行图生成。代码中的数据皆可以自由修改(注意代码中设立前一个时间段和最后一个时间段皆为半个小时),生成的时刻表在满载率方面能在要求范围内随机生成,确保每个运行图不尽相同

import numpy as np
import pandas as pd
from datetime import datetime, timedelta
import random

# 定义早高峰小时客流量
peak_hour_passenger_flow = 43000

# 表1中的客流量比例
time_period_proportions = [
    0.08, 0.43, 1, 0.76, 0.51, 0.41, 0.47, 0.59, 0.64, 0.64, 
    0.69, 0.73, 0.88, 0.62, 0.39, 0.31, 0.26, 0.20, 0.08,
]

# 定义时间段(起始时间)
time_periods = [
    "05:00", "05:30", "06:30", "07:30", "08:30", "09:30", "10:30", "11:30", "12:30",
    "13:30", "14:30", "15:30", "16:30", "17:30", "18:30", "19:30", "20:30", "21:30", "22:30"
]

# 计算每个时间段的客流量
passenger_flow = [round(peak_hour_passenger_flow * proportion) for proportion in time_period_proportions]

# 列车编组和车辆定员
train_capacity = 6 * 310  # 6辆车,每辆车310人

# 定义区间运行时间和停站时间
section_times_up = [222, 250, 232, 175, 180, 210, 265]  # 上行方向停站时间(单位:秒)
section_times_down = [215, 245, 240, 182, 212, 300, 250]  # 下行方向停站时间(单位:秒)
stop_times_up = [40, 30, 50, 30, 30, 50, 30, 40]  # 各站停站时间(上行方向,单位:秒)
stop_times_down = [40, 30, 50, 30, 30, 50, 30, 40]  # 各站停站时间(下行方向,单位:秒)
turnaround_time = 200  # 折返时间(单位:秒)
entry_time = 260  # 进场时间(单位:秒)
exit_time = 270  # 出场时间(单位:秒)

stations = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']

# 满载率计算
def get_full_load_rate(period):
    if time_periods[period] in ["06:30", "16:30"]:
        return np.random.uniform(1.1, 1.2)
    else:
        return np.random.uniform(0.7, 0.9)

full_load_rates = [get_full_load_rate(i) for i in range(len(time_periods))]

# 计算各时间段的列车数量
train_numbers = [
    np.ceil((passenger_flow[i] * np.random.uniform(0.9, 1.1)) / (train_capacity * full_load_rates[i])).astype(int) 
    for i in range(len(passenger_flow))
]

# 计算列车运行间隔时间(单位:秒)
time_intervals = [1800 if i == 0 or i == len(time_periods) - 1 else 3600 for i in range(len(time_periods))]  # 修改后的每个时间段总时间
departure_intervals = [int(np.ceil(time_intervals[i] / train_numbers[i])) for i in range(len(train_numbers))]

# 计算一列列车的循环时间(包括上行、下行、折返和停站时间)
def calculate_cycle_time():
    up_time = sum(section_times_up) + sum(stop_times_up)
    down_time = sum(section_times_down) + sum(stop_times_down)
    return up_time + down_time + 2 * turnaround_time + entry_time + exit_time

cycle_time = calculate_cycle_time()

# 生成列车运行图
timetable = []
train_id = 1

# 起始时间
start_time = datetime.strptime("05:00", "%H:%M")

# 列车池,记录每列车的下一次可用时间
train_pool = []

for period in range(len(time_periods)):
    departure_time = start_time + timedelta(seconds=sum(time_intervals[:period]))
    current_train_numbers = train_numbers[period]
    active_trains = []

    for _ in range(current_train_numbers):
        # 检查列车池中是否有可用列车
        if train_pool and train_pool[0][1] <= departure_time:
            # 复用已有列车
            train_no, available_time = train_pool.pop(0)
        else:
            # 创建新列车
            train_no = f"01{train_id:03d}"
            train_id += 1
        
        active_trains.append(train_no)

    for train_no in active_trains:
        # 初始化时间
        current_time = departure_time
        
        # 上行方向运行
        for i, station in enumerate(stations):
            if i == 0:
                # 进场时间
                current_time += timedelta(seconds=entry_time)
            else:
                # 区间运行时间
                current_time += timedelta(seconds=section_times_up[i - 1])
            
            # 记录到达时间
            arrive_time = current_time
            
            # 停站时间
            current_time += timedelta(seconds=stop_times_up[i])
            
            # 记录出发时间
            depart_time = current_time
            
            # 添加到时刻表
            timetable.append([train_no, station, arrive_time.strftime("%H:%M:%S"), depart_time.strftime("%H:%M:%S"), round(full_load_rates[period], 2)])
        
        # 折返时间
        current_time += timedelta(seconds=turnaround_time)
        
        # 下行方向运行
        for i, station in enumerate(reversed(stations)):
            if i == 0:
                # 出发时间
                current_time += timedelta(seconds=0)
            else:
                # 区间运行时间
                current_time += timedelta(seconds=section_times_down[i - 1])
            
            # 记录到达时间
            arrive_time = current_time
            
            # 停站时间
            current_time += timedelta(seconds=stop_times_down[i])
            
            # 记录出发时间
            depart_time = current_time
            
            # 添加到时刻表
            timetable.append([train_no, station, arrive_time.strftime("%H:%M:%S"), depart_time.strftime("%H:%M:%S"), round(full_load_rates[period], 2)])
        
        # 出场时间
        current_time += timedelta(seconds=exit_time)
        
        # 更新列车池
        next_available_time = current_time + timedelta(seconds=turnaround_time)
        train_pool.append((train_no, next_available_time))
        train_pool.sort(key=lambda x: x[1])
        
        # 调整下一列车的发车时间,避免重叠
        departure_time += timedelta(seconds=departure_intervals[period])

# 创建DataFrame并保存到Excel文件
df = pd.DataFrame(timetable, columns=["列车号", "站点名称", "到达时间", "出发时间", "满载率"])

# 保存为Excel文件到D盘
file_path = "D:/train_timetable.xlsx"
df.to_excel(file_path, index=False)

print(f"列车运行图已生成并保存为 {file_path} 文件。")
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值