动态系统的数学模型

Python 中的离散事件模拟

Diogo Costa (PhD, MSc)

Adjunct Professor, University of Saskatchewan, Canada & CEO of ImpactBLUE-Scientific

什么是数学模型?

使用数学概念与语言描述自然或人为系统。

模型可分为:

  • 动态 vs. 非动态
  • 离散 vs. 连续
  • 确定性 vs. 概率(随机)
  • 线性 vs. 非线性
  • 其他

数学模型可简可繁。

  • 对现实的数字化近似
  • 不可能涵盖所有过程

George Box:"所有模型都是错的,但有些是有用的。"

一条河流经岩石地貌的照片。

Python 中的离散事件模拟

示例:自然过程预测模型

河流流量模型

中国谷芽水文站的观测与模拟流量随时间变化图。模型能捕捉观测记录。两者流量随时间大幅波动。

  • 该模型预测中国谷芽水文站的流量。

  • 预测基于降雨信息,作为模型输入。

其他示例

  • 天气预报
  • 海浪预报
  • 湖泊水质
  • 城市内涝
  • 以及更多应用
1 Chen, C., He, W., Zhou, H. et al. A comparative study among machine learning and numerical models for simulating groundwater dynamics in the Heihe River Basin, northwestern China. Sci Rep 10, 3904 (2020). https://doi.org/10.1038/s41598-020-60698-9
Python 中的离散事件模拟

示例:人为活动预测模型

通胀预测

英国通胀记录(2002–2021)及法国巴黎银行的 2022 年预测图。预测显示 2022 年通胀为 7%。

  • 通胀预测对避免经济危机至关重要

其他示例

  • 供应链
  • 制造业
  • 物流
  • 经济预测
  • 交通
  • 其他
1 https://www.ft.com/content/218d35ab-b044-467a-bb62-0d17547a4350
Python 中的离散事件模拟

通用代码组件与结构

  • 3 个主要组成部分
  • 输入数据与模型参数
# Define model parameters
processes = {"process_1": 5,
             "process_2": 2,
             "process_3": 3}
  • 运行配置
# Simulation period
simulation_time = 365

# Run model
discrete_model(processes, simulation_time)
  • 模型引擎
def discrete_model(processes, simulation_time):

  # 1) Run end-condition
  while (time < simulation_time):
      process_names = list(processes.keys())

# 2) Loop over all processes for p in range(len(process_names)): process_name_p = process_names[p]
# 3) Account for effect of each process time += processes[process_name_p]
Python 中的离散事件模拟

模型输出

制造活动的离散事件模型输出示例

=> START OF SIMULATION  (Time = 0 days) 
Time =    6.00 days  |  Process Complete: Transport of raw material
Time =    9.00 days  |  Process Complete: Building components
Time =   11.00 days  |  Process Complete: Assembling parts
Time =   14.00 days  |  Process Complete: Selling product
=> COMPLETED: Supply-Chain cycle #1 | Time = 15.5 days
Time =   21.50 days  |  Process Complete: Transport raw material
Time =   24.50 days  | Process Complete: Building components
Time =   26.50 days  |  Process Complete: Assembling parts
Time =   29.50 days  |  Process Complete: Selling product
=> COMPLETED: Supply-Chain cycle #2 | Time = 31.0 days
Python 中的离散事件模拟

模型结果可视化

  • 结果可视化:有助于发现模式与临界点

  • 可视化应贴合仿真目标

  • 常用可视化库:matplotlibseabornplotly

Matplotlib 库标志。 Seaborn 库标志。 Plotly 库标志。

示例

  • 将建模数据 (y) 与时间值 (x) 做 2D 折线/散点图
plt.plot(x, y, color='green', marker='o', 
         markersize=12, linestyle='dashed', 
         linewidth=2)
  • 直方图:将 x 分箱并统计每箱数量
plt.hist(x, 50, density=True, 
         facecolor='g', alpha=0.75)
Python 中的离散事件模拟

Vamos praticar!

Python 中的离散事件模拟

Preparing Video For Download...