行人与非机动车仿真

行人仿真

行人仿真在交通仿真中是一个重要的组成部分,特别是在城市交通仿真中。Aimsun 提供了强大的行人仿真功能,可以模拟行人流量、行人行为和行人与车辆的交互。本节将详细介绍如何在 Aimsun 中进行行人仿真,包括行人网络的创建、行人流量的设置、行人行为的定义以及行人仿真的运行。

1. 创建行人网络

在 Aimsun 中,行人网络是独立于车辆网络的。创建行人网络需要以下几个步骤:

  1. 定义行人节点

    • 行人节点通常表示行人可以进出的地点,如人行横道、公交站、地铁站等。

    • 在 Aimsun 中,可以通过 GNEPedestrianNode 类来定义行人节点。

  2. 定义行人路段

    • 行人路段表示行人可以行走的路径,如人行道、楼梯等。

    • 可以通过 GNEPedestrianEdge 类来定义行人路段。

  3. 定义行人区域

    • 行人区域表示行人可以停留的区域,如公交站候车区、地铁站候车区等。

    • 可以通过 GNEPedestrianArea 类来定义行人区域。

示例代码

# 导入 Aimsun API

import aimsun_api as api



# 创建 Aimsun 项目实例

project = api.GNProject()



# 创建行人节点

def create_pedestrian_node(node_id, x, y, node_type):

    """

    创建行人节点



    :param node_id: 节点 ID

    :param x: 节点的 X 坐标

    :param y: 节点的 Y 坐标

    :param node_type: 节点类型(如人行横道、公交站等)

    :return: 行人节点对象

    """

    node = api.GNEPedestrianNode(node_id, x, y, node_type)

    project.add_pedestrian_node(node)

    return node



# 创建行人路段

def create_pedestrian_edge(edge_id, from_node, to_node, length, lanes):

    """

    创建行人路段



    :param edge_id: 路段 ID

    :param from_node: 起始节点

    :param to_node: 结束节点

    :param length: 路段长度

    :param lanes: 路段车道数

    :return: 行人路段对象

    """

    edge = api.GNEPedestrianEdge(edge_id, from_node, to_node, length, lanes)

    project.add_pedestrian_edge(edge)

    return edge



# 创建行人区域

def create_pedestrian_area(area_id, node, area_type):

    """

    创建行人区域



    :param area_id: 区域 ID

    :param node: 区域所属的节点

    :param area_type: 区域类型(如候车区、休息区等)

    :return: 行人区域对象

    """

    area = api.GNEPedestrianArea(area_id, node, area_type)

    project.add_pedestrian_area(area)

    return area



# 示例数据

node1 = create_pedestrian_node("node1", 100, 200, "crosswalk")

node2 = create_pedestrian_node("node2", 300, 200, "bus_stop")

edge1 = create_pedestrian_edge("edge1", node1, node2, 200, 1)

area1 = create_pedestrian_area("area1", node2, "waiting_area")

2. 设置行人流量

行人流量的设置是行人仿真的基础。Aimsun 提供了多种方式来设置行人流量,包括静态流量和动态流量。

  1. 静态流量

    • 静态流量是指在仿真开始前定义好的固定流量。

    • 可以通过 GNEPedestrianFlow 类来设置静态流量。

  2. 动态流量

    • 动态流量是指在仿真过程中根据某些条件变化的流量。

    • 可以通过 GNEPedestrianFlow 类结合 GNEPedestrianDetector 类来动态调整流量。

示例代码

# 设置静态行人流量

def set_static_pedestrian_flow(flow_id, from_node, to_node, start_time, end_time, rate):

    """

    设置静态行人流量



    :param flow_id: 流量 ID

    :param from_node: 起始节点

    :param to_node: 结束节点

    :param start_time: 流量开始时间

    :param end_time: 流量结束时间

    :param rate: 流量率(每小时行人数量)

    :return: 行人流量对象

    """

    flow = api.GNEPedestrianFlow(flow_id, from_node, to_node, start_time, end_time, rate)

    project.add_pedestrian_flow(flow)

    return flow



# 设置动态行人流量

def set_dynamic_pedestrian_flow(flow_id, from_node, to_node, start_time, end_time, rate, detector_id):

    """

    设置动态行人流量



    :param flow_id: 流量 ID

    :param from_node: 起始节点

    :param to_node: 结束节点

    :param start_time: 流量开始时间

    :param end_time: 流量结束时间

    :param rate: 初始流量率(每小时行人数量)

    :param detector_id: 检测器 ID

    :return: 行人流量对象

    """

    detector = api.GNEPedestrianDetector(detector_id, from_node, to_node)

    project.add_pedestrian_detector(detector)

    flow = api.GNEPedestrianFlow(flow_id, from_node, to_node, start_time, end_time, rate, detector)

    project.add_pedestrian_flow(flow)

    return flow



# 示例数据

static_flow = set_static_pedestrian_flow("flow1", node1, node2, 0, 3600, 100)

dynamic_flow = set_dynamic_pedestrian_flow("flow2", node1, node2, 0, 7200, 50, "detector1")

3. 定义行人行为

定义行人行为是行人仿真的核心部分。Aimsun 提供了多种行人行为模型,包括基本行为模型、随机行为模型和自定义行为模型。

  1. 基本行为模型

    • 基本行为模型是指行人按照预定的路径和速度行走。

    • 可以通过 GNEPedestrianBehavior 类来定义基本行为模型。

  2. 随机行为模型

    • 随机行为模型是指行人在行走过程中会随机改变路径和速度。

    • 可以通过 GNEPedestrianBehavior 类结合随机数生成器来定义随机行为模型。

  3. 自定义行为模型

    • 自定义行为模型是指用户可以根据具体需求定义行人的行为。

    • 可以通过编写脚本来实现自定义行为模型。

示例代码

# 定义基本行人行为

def define_basic_pedestrian_behavior(behavior_id, speed, path):

    """

    定义基本行人行为



    :param behavior_id: 行为 ID

    :param speed: 行人速度(m/s)

    :param path: 行人路径(节点列表)

    :return: 行为对象

    """

    behavior = api.GNEPedestrianBehavior(behavior_id, speed, path)

    project.add_pedestrian_behavior(behavior)

    return behavior



# 定义随机行人行为

def define_random_pedestrian_behavior(behavior_id, min_speed, max_speed, path):

    """

    定义随机行人行为



    :param behavior_id: 行为 ID

    :param min_speed: 最小速度(m/s)

    :param max_speed: 最大速度(m/s)

    :param path: 行人路径(节点列表)

    :return: 行为对象

    """

    behavior = api.GNEPedestrianBehavior(behavior_id, min_speed, max_speed, path, is_random=True)

    project.add_pedestrian_behavior(behavior)

    return behavior



# 定义自定义行人行为

def define_custom_pedestrian_behavior(behavior_id, custom_logic, path):

    """

    定义自定义行人行为



    :param behavior_id: 行为 ID

    :param custom_logic: 自定义逻辑(函数)

    :param path: 行人路径(节点列表)

    :return: 行为对象

    """

    behavior = api.GNEPedestrianBehavior(behavior_id, custom_logic, path, is_custom=True)

    project.add_pedestrian_behavior(behavior)

    return behavior



# 示例数据

path1 = [node1, node2]

basic_behavior = define_basic_pedestrian_behavior("basic_behavior", 1.5, path1)

random_behavior = define_random_pedestrian_behavior("random_behavior", 1.0, 2.0, path1)



# 自定义行为逻辑

def custom_behavior_logic(pedestrian, time_step):

    """

    自定义行人行为逻辑



    :param pedestrian: 行人对象

    :param time_step: 当前时间步

    :return: 新的速度

    """

    if time_step % 100 == 0:

        return 1.2  # 每100秒改变速度

    else:

        return 1.5



custom_behavior = define_custom_pedestrian_behavior("custom_behavior", custom_behavior_logic, path1)

4. 运行行人仿真

运行行人仿真需要设置仿真参数和运行仿真。Aimsun 提供了丰富的仿真参数设置选项,以确保仿真结果的准确性和可靠性。

  1. 设置仿真参数

    • 仿真参数包括仿真时间、仿真步长、仿真模式等。

    • 可以通过 GNSimulationParameters 类来设置仿真参数。

  2. 运行仿真

    • 运行仿真可以通过 GNSimulation 类来实现。

    • 可以设置仿真开始和结束时间,以及仿真步长。

示例代码

# 设置仿真参数

def set_simulation_parameters(simulation_id, start_time, end_time, step_length, mode):

    """

    设置仿真参数



    :param simulation_id: 仿真 ID

    :param start_time: 仿真开始时间

    :param end_time: 仿真结束时间

    :param step_length: 仿真步长(秒)

    :param mode: 仿真模式(如行人、车辆等)

    :return: 仿真参数对象

    """

    parameters = api.GNSimulationParameters(simulation_id, start_time, end_time, step_length, mode)

    project.add_simulation_parameters(parameters)

    return parameters



# 运行仿真

def run_simulation(simulation_id):

    """

    运行仿真



    :param simulation_id: 仿真 ID

    """

    simulation = api.GNSimulation(simulation_id)

    simulation.run()



# 示例数据

simulation_params = set_simulation_parameters("simulation1", 0, 7200, 1, "pedestrian")

run_simulation("simulation1")

非机动车仿真

非机动车仿真在交通仿真中同样重要,特别是在城市交通中。Aimsun 提供了多种非机动车仿真功能,包括非机动车网络的创建、非机动车流量的设置、非机动车行为的定义以及非机动车仿真的运行。

1. 创建非机动车网络

在 Aimsun 中,非机动车网络是独立于车辆网络和行人网络的。创建非机动车网络需要以下几个步骤:

  1. 定义非机动车节点

    • 非机动车节点通常表示非机动车可以进出的地点,如自行车道、停车场等。

    • 可以通过 GNEBicycleNode 类来定义非机动车节点。

  2. 定义非机动车路段

    • 非机动车路段表示非机动车可以行驶的路径,如自行车道、人行道等。

    • 可以通过 GNEBicycleEdge 类来定义非机动车路段。

  3. 定义非机动车区域

    • 非机动车区域表示非机动车可以停留的区域,如停车场、维修站等。

    • 可以通过 GNEBicycleArea 类来定义非机动车区域。

示例代码

# 创建非机动车节点

def create_bicycle_node(node_id, x, y, node_type):

    """

    创建非机动车节点



    :param node_id: 节点 ID

    :param x: 节点的 X 坐标

    :param y: 节点的 Y 坐标

    :param node_type: 节点类型(如自行车道、停车场等)

    :return: 非机动车节点对象

    """

    node = api.GNEBicycleNode(node_id, x, y, node_type)

    project.add_bicycle_node(node)

    return node



# 创建非机动车路段

def create_bicycle_edge(edge_id, from_node, to_node, length, lanes):

    """

    创建非机动车路段



    :param edge_id: 路段 ID

    :param from_node: 起始节点

    :param to_node: 结束节点

    :param length: 路段长度

    :param lanes: 路段车道数

    :return: 非机动车路段对象

    """

    edge = api.GNEBicycleEdge(edge_id, from_node, to_node, length, lanes)

    project.add_bicycle_edge(edge)

    return edge



# 创建非机动车区域

def create_bicycle_area(area_id, node, area_type):

    """

    创建非机动车区域



    :param area_id: 区域 ID

    :param node: 区域所属的节点

    :param area_type: 区域类型(如停车场、维修站等)

    :return: 非机动车区域对象

    """

    area = api.GNEBicycleArea(area_id, node, area_type)

    project.add_bicycle_area(area)

    return area



# 示例数据

b_node1 = create_bicycle_node("b_node1", 100, 200, "bicycle_path")

b_node2 = create_bicycle_node("b_node2", 300, 200, "parking_lot")

b_edge1 = create_bicycle_edge("b_edge1", b_node1, b_node2, 200, 1)

b_area1 = create_bicycle_area("b_area1", b_node2, "parking_area")

2. 设置非机动车流量

非机动车流量的设置与行人流量类似,可以是静态的或动态的。

  1. 静态流量

    • 静态流量是指在仿真开始前定义好的固定流量。

    • 可以通过 GNEBicycleFlow 类来设置静态流量。

  2. 动态流量

    • 动态流量是指在仿真过程中根据某些条件变化的流量。

    • 可以通过 GNEBicycleFlow 类结合 GNEBicycleDetector 类来动态调整流量。

示例代码

# 设置静态非机动车流量

def set_static_bicycle_flow(flow_id, from_node, to_node, start_time, end_time, rate):

    """

    设置静态非机动车流量



    :param flow_id: 流量 ID

    :param from_node: 起始节点

    :param to_node: 结束节点

    :param start_time: 流量开始时间

    :param end_time: 流量结束时间

    :param rate: 流量率(每小时非机动车数量)

    :return: 非机动车流量对象

    """

    flow = api.GNEBicycleFlow(flow_id, from_node, to_node, start_time, end_time, rate)

    project.add_bicycle_flow(flow)

    return flow



# 设置动态非机动车流量

def set_dynamic_bicycle_flow(flow_id, from_node, to_node, start_time, end_time, rate, detector_id):

    """

    设置动态非机动车流量



    :param flow_id: 流量 ID

    :param from_node: 起始节点

    :param to_node: 结束节点

    :param start_time: 流量开始时间

    :param end_time: 流量结束时间

    :param rate: 初始流量率(每小时非机动车数量)

    :param detector_id: 检测器 ID

    :return: 非机动车流量对象

    """

    detector = api.GNEBicycleDetector(detector_id, from_node, to_node)

    project.add_bicycle_detector(detector)

    flow = api.GNEBicycleFlow(flow_id, from_node, to_node, start_time, end_time, rate, detector)

    project.add_bicycle_flow(flow)

    return flow



# 示例数据

static_b_flow = set_static_bicycle_flow("b_flow1", b_node1, b_node2, 0, 3600, 50)

dynamic_b_flow = set_dynamic_bicycle_flow("b_flow2", b_node1, b_node2, 0, 7200, 30, "b_detector1")

3. 定义非机动车行为

定义非机动车行为是确保仿真准确性的关键。Aimsun 提供了多种非机动车行为模型,包括基本行为模型、随机行为模型和自定义行为模型。

  1. 基本行为模型

    • 基本行为模型是指非机动车按照预定的路径和速度行驶。

    • 可以通过 GNEBicycleBehavior 类来定义基本行为模型。

  2. 随机行为模型

    • 随机行为模型是指非机动车在行驶过程中会随机改变路径和速度。

    • 可以通过 GNEBicycleBehavior 类结合随机数生成器来定义随机行为模型。

  3. 自定义行为模型

    • 自定义行为模型是指用户可以根据具体需求定义非机动车的行为。

    • 可以通过编写脚本来实现自定义行为模型。

示例代码

# 定义基本非机动车行为

def define_basic_bicycle_behavior(behavior_id, speed, path):

    """

    定义基本非机动车行为



    :param behavior_id: 行为 ID

    :param speed: 非机动车速度(m/s)

    :param path: 非机动车路径(节点列表)

    :return: 行为对象

    """

    behavior = api.GNEBicycleBehavior(behavior_id, speed, path)

    project.add_bicycle_behavior(behavior)

    return behavior



# 定义随机非机动车行为

def define_random_bicycle_behavior(behavior_id, min_speed, max_speed, path):

    """

    定义随机非机动车行为



    :param behavior_id: 行为 ID

    :param min_speed: 最小速度(m/s)

    :param max_speed: 最大速度(m/s)

    :param path: 非机动车路径(节点列表)

    :return: 行为对象

    """

    behavior = api.GNEBicycleBehavior(behavior_id, min_speed, max_speed, path, is_random=True)

    project.add_bicycle_behavior(behavior)

    return behavior



# 定义自定义非机动车行为

def define_custom_bicycle_behavior(behavior_id, custom_logic, path):

    """

    定义自定义非机动车行为



    :param behavior_id: 行为 ID

    :param custom_logic: 自定义逻辑(函数)

    :param path: 非机动车路径(节点列表)

    :return: 行为对象

    """

    behavior = api.GNEBicycleBehavior(behavior_id, custom_logic, path, is_custom=True)

    project.add_bicycle_behavior(behavior)

    return behavior



# 示例数据

path1 = [b_node1, b_node2]

basic_b_behavior = define_basic_bicycle_behavior("basic_b_behavior", 5.0, path1)

random_b_behavior = define_random_bicycle_behavior("random_b_behavior", 4.0, 6.0, path1)



# 自定义行为逻辑

def custom_bicycle_behavior_logic(bicycle, time_step):

    """

    自定义非机动车行为逻辑



    :param bicycle: 非机动车对象

    :param time_step: 当前时间步

    :return: 新的速度

    """

    if time_step % 100 == 0:

        return 4.5  # 每100秒改变速度

    else:

        return 5.0



custom_b_behavior = define_custom_bicycle_behavior("custom_b_behavior", custom_bicycle_behavior_logic, path1)

4. 运行非机动车仿真

运行非机动车仿真需要设置仿真参数和运行仿真。Aimsun 提供了丰富的仿真参数设置选项,以确保仿真结果的准确性和可靠性。

  1. 设置仿真参数

    • 仿真参数包括仿真时间、仿真步长、仿真模式等。

    • 可以通过 GNSimulationParameters 类来设置仿真参数。

  2. 运行仿真

    • 运行仿真可以通过 GNSimulation 类来实现。

    • 可以设置仿真开始和结束时间,以及仿真步长。

示例代码

# 设置仿真参数

def set_simulation_parameters(simulation_id, start_time, end_time, step_length, mode):

    """

    设置仿真参数



    :param simulation_id: 仿真 ID

    :param start_time: 仿真开始时间

    :param end_time: 仿真结束时间

    :param step_length: 仿真步长(秒)

    :param mode: 仿真模式(如行人、非机动车等)

    :return: 仿真参数对象

    """

    parameters = api.GNSimulationParameters(simulation_id, start_time, end_time, step_length, mode)

    project.add_simulation_parameters(parameters)

    return parameters



# 运行仿真

def run_simulation(simulation_id):

    """

    运行仿真



    :param simulation_id: 仿真 ID

    """

    simulation = api.GNSimulation(simulation_id)

    simulation.run()



# 示例数据

b_simulation_params = set_simulation_parameters("b_simulation1", 0, 7200, 1, "bicycle")

run_simulation("b_simulation1")

总结

通过以上步骤,我们可以在 Aimsun 中创建和运行行人和非机动车仿真。行人仿真包括行人网络的创建、行人流量的设置、行人行为的定义以及仿真运行。非机动车仿真也包括类似的步骤,但针对非机动车的特点进行了调整。这些功能使得 Aimsun 成为一个强大的交通仿真工具,能够满足各种复杂的交通仿真需求。在这里插入图片描述

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐