You need to enable JavaScript to run this app. Python实现单跑道机场航班空中等待时间计算(泊松到达模拟)
优惠活动
大模型
产品
解决方案
定价
更多

Python实现单跑道机场航班空中等待时间计算(泊松到达模拟)

单跑道着陆模拟等待时长计算的问题修正

原代码的核心逻辑漏洞

你现在算不对等待时长,本质是三个逻辑错误:

  • done_with_landing 变量被放在了for循环内部,每架飞机计算时都会被重置为0,完全丢失了上一架飞机的跑道释放时间记录,导致所有等待计算都基于“跑道一开始就是空的”这个错误前提
  • 列表数据存储混乱:
    • plane_arrival_time 一会存随机生成的间隔时间、一会累加绝对到达时间,没有统一存值规则
    • time_between_planes 初始化列表后没有做append操作,直接被单值覆盖,生成的间隔时间完全丢失
    • waiting_time 一开始赋值时错把飞机索引i当成初始等待值塞进去,没有实际存储计算结果

正确的等待时长计算逻辑

单跑道是典型的单服务台排队模型,计算规则完全匹配你举的场景:

  1. 跟踪一个全局的跑道释放时间runway_free_time,初始值为0(模拟开始前跑道完全空闲)
  2. 对每一架按到达顺序排列的航班:
    • 计算该航班的绝对到达时间(累加之前的指数分布间隔时间即可)
    • 等待时长取两个值的差和0的最大值
    • 该航班开始占用跑道的时间 = 航班到达时间 + 等待时长
    • 该航班落地完成、释放跑道的时间 = 开始占用时间 + 该航班的着陆作业耗时
    • 把这个新的释放时间更新回全局的runway_free_time变量,供下一架航班计算使用

这里用*max(runway_free_time - plane_arrival_time, 0)*的写法,完全不需要额外分支判断:当航班到达时跑道已经空闲(即plane_arrival_time >= runway_free_time),等待时长自动取0,天然兼容无排队场景。

修正后的可运行代码

import math
import random
import numpy as np

def simulate_landing_queue(amount_of_planes=200):
    plane_number = []
    time_between_planes = []
    plane_arrival_time = []
    plane_waiting_time = []
    plane_runway_release_time = []
    
    # 预生成每架航班的着陆作业耗时,和原逻辑保持一致
    time_for_landing = np.array(random.choices(
        [15, 45, 75, 105, 135, 165, 195, 225, 255, 285],
        weights=[0, 8, 16.5, 30.5, 20.5, 12.5, 5, 4, 3, 0],
        k=amount_of_planes
    ))
    
    # 跟踪跑道从空闲到完全释放的全局时间戳,初始为模拟开始时刻
    runway_free_time = 0
    current_arrival_time = 0
    # 泊松到达的到达率换算:12小时共200架,平均每秒到达率约0.00463,和原代码参数一致
    arrival_rate = 0.00462962962

    for i in range(amount_of_planes):
        plane_number.append(i)
        # 用逆CDF生成指数分布的到达间隔
        random_uniform = random.random()
        inter_arrival = -math.log(1.0 - random_uniform) / arrival_rate
        time_between_planes.append(inter_arrival)
        # 累加得到当前航班的绝对到达时刻
        current_arrival_time += inter_arrival
        plane_arrival_time.append(current_arrival_time)
        
        # 核心等待时间计算
        wait_for_this_plane = max(runway_free_time - current_arrival_time, 0)
        plane_waiting_time.append(wait_for_this_plane)
        
        # 计算当前航班占用完跑道的时刻,更新全局跑道空闲时间
        this_plane_release = current_arrival_time + wait_for_this_plane + time_for_landing[i]
        plane_runway_release_time.append(this_plane_release)
        runway_free_time = this_plane_release
        
        # 打印单架航班的过程值,和你之前的输出格式对齐
        print(f"航班{i:03d}: 到达时刻{current_arrival_time:.1f}s, 等待{wait_for_this_plane:.1f}s, 跑道释放时刻{this_plane_release:.1f}s, 着陆耗时{time_for_landing[i]}s")

    # 模拟结束后输出统计值,支撑是否新建第二条跑道的决策
    total_wait = sum(plane_waiting_time)
    avg_wait = total_wait / amount_of_planes
    max_wait = max(plane_waiting_time)
    print(f"\n=== 模拟统计结果 ===")
    print(f"总等待时长:{total_wait/3600:.1f}小时")
    print(f"单架航班平均等待时长:{avg_wait:.1f}秒")
    print(f"最长空中等待时长:{max_wait:.1f}秒")
    print(f"平均队列长度估算:{total_wait / current_arrival_time:.2f}")

simulate_landing_queue(200)

场景验证

用你举的测试场景代入逻辑:

  • 第一架航班100秒到达,着陆耗时75秒,跑道释放时间175秒,等待时长为max(0-100,0)=0
  • 第二架航班150秒到达,等待时长为max(175-150,0)=25,和你手动计算的结果完全一致
  • 如果第三架航班在175秒之后才到达,比如180秒到,等待时长自动为max(175-180,0)=0,完全兼容无排队的空跑道场景

内容的提问来源于stack exchange,提问作者Nikolai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.16 10:36:03