公交车最优发车时间

文章探讨了如何通过数学模型和仿真来确定在给定时间内,如何调整两辆公交车的发车时间以最小化乘客的总等待时间,考虑了Poisson分布的乘客到达率。作者通过仿真乘客到达情况并计算平均等待时间,提供了理论解和实际操作的比较。
摘要由CSDN通过智能技术生成

问题描述

假设 [ 0 , 2 ] [0,2] [0,2]时间内一共有两辆公交车发车,发车时间分别为 T 1 , 2 T_1,2 T1,2。而在这段时间内,乘客到达公交车站的人数服从Poisson(10t),若要最小化乘客的总等待时间,求第一班公交车的发车时间 T 1 T_1 T1,即
min ⁡ T = E ( ∑ k = 1 N ( t 1 ) ( t 1 − s k ) + ∑ k = 1 N ( t 2 ) ( t 2 − s k ) ) s . t . t 1 + t 2 = 2 N ( t 1 ) ∼ P o i s s o n ( 10 ⋅ t 1 ) N ( t 2 ) ∼ P o i s s o n ( 10 ⋅ t 2 ) \begin{array}{l} \min T=E\left(\sum_{k=1}^{N(t_1)}(t_1-s_k)+\sum_{k=1}^{N(t_2)}(t_2-s_k)\right)\\ s.t.\quad t_1 + t_2 = 2 \\ \quad\quad N(t_1) \sim Poisson(10\cdot t_1) \\ \quad\quad N(t_2) \sim Poisson(10\cdot t_2) \end{array} minT=E(k=1N(t1)(t1sk)+k=1N(t2)(t2sk))s.t.t1+t2=2N(t1)Poisson(10t1)N(t2)Poisson(10t2)
直接利用过滤泊松模型,得到理论解:
T = 10 ⋅ ( ∫ 0 t 1 ( t 1 − s ) d s + ∫ 0 t 2 ( t 2 − s ) d s ) = 5 ( t 1 2 + t 2 2 ) ≥ 10 T=10\cdot(\int_0^{t_1}(t_1-s)ds+\int_0^{t_2}(t_2-s)ds)=5(t_1^2+t_2^2)\geq10 T=10(0t1(t1s)ds+0t2(t2s)ds)=5(t12+t22)10

仿真验证

乘客到达时间仿真

import numpy as np
import matplotlib.pyplot as plt

def simulate_bus_station(arrival_rate, max_time):
    arrival_times = []
    last_arrival = 0

    # Continuously generate passenger arrival times until the last arrival is at 2 seconds
    while True:
        # Generate the next arrival time
        next_arrival = last_arrival + np.random.exponential(1 / arrival_rate)
        if next_arrival > max_time:
            # If the next arrival time is greater than 2 seconds, stop the simulation
            break
        arrival_times.append(next_arrival)
        last_arrival = next_arrival

    return arrival_times
    
np.random.seed(0)
# Set the arrival rate (lambda) for Poisson distribution
arrival_rate = 10  # For example, 10 passenger per second

# Simulate passenger arrivals
arrival_times = simulate_bus_station(arrival_rate,2)

# Plot the cumulative count of passengers vs. arrival time
plt.step(arrival_times, range(1, len(arrival_times) + 1), where='post')
plt.xlabel('Time (seconds)')
plt.ylabel('Cumulative number of passengers')
plt.title('Passenger Arrivals at a Bus Station')
plt.show()

乘客到达时间仿真
平均等待总时间计算

def calculate_total_waiting_time(T1,T2,arrival_times):
    # 计算每个乘客的等车时间
    wait_times = []

    for arrival in arrival_times:
        if arrival <= T1:
            # 如果乘客在第一班车发车前到达,他们的等车时间是T1减去他们的到达时间
            wait_times.append(T1 - arrival)
        elif T1 < arrival <= T2:
            # 如果乘客在第一班车发车后,且第二班车发车前到达,他们的等车时间是T2减去他们的到达时间
            wait_times.append(T2 - arrival)

    # 计算总的等待时间
    total_wait_time = sum(wait_times)
    return total_wait_time
    
def calculate_average_total_waiting_time(arrival_rate, max_time, T1, sim_num):
    total_wait_times = np.zeros(shape=sim_num)
    for i in range(sim_num):
        arrival_times = simulate_bus_station(arrival_rate, max_time)
        total_wait_time = calculate_total_waiting_time(T1,max_time,arrival_times)
        total_wait_times[i] = total_wait_time
    average_total_wait_time = np.average(total_wait_times)
    return average_total_wait_time

# 设置仿真参数
lam = 10  # 平均每秒到达一个乘客
max_time = 2  # 模拟总时间为2秒
sim_num = 100 # 每种情况模拟100次
T1_values = np.linspace(0.1, 1.9, 50)  # 在区间(0.1, 1.9)内取50个不同的T1值
np.random.seed(0)

# 对不同的T1计算平均等待时间
average_wait_times = []
avg_time_in_theory = []
for T1 in T1_values:
    avg_wait = calculate_average_total_waiting_time(lam, max_time, T1, sim_num)
    average_wait_times.append(avg_wait)
    avg_time_in_theory.append(0.5*lam*(T1**2+(max_time-T1)**2))

# 使用Matplotlib绘制T1与平均等待时间的关系
plt.plot(T1_values, average_wait_times, label='Average Total Wait Time')
plt.plot(T1_values, avg_time_in_theory, label='Theoretically Average Total Wait Time')
plt.xlabel('Bus Departure Time T1 (s)')
plt.ylabel('Wait Time (s)')
plt.title('Average Total Wait Time vs. Bus Departure Time T1')
plt.legend()
plt.grid(True)
plt.show()    

乘客平均总等待时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值