Page tree
Skip to end of metadata
Go to start of metadata

警告:Jupyter启动时会重置教程的笔记本。如果你想修改并保存它们,请复制一份。

Scale泛化

Scale泛化组件支持三个功能:

  1. 读取设计空间文件。
  2. 根据设计空间文件来创建样本。样本是二维数组。每一行对应一次仿真。每一列对应一个参数名。每一个单元格对应一个参数值。
  3. 给定一个样本空间文件和样本内的一行数据,便可以应用一个样本实例。这样会创建具体仿真,可以将其直接提供给某个仿真器(例如:VTD)。

注意:目前,API不支持设计空间文件的创建。设计空间文件必须手工创建。

Scale泛化组件是孤立的,可以独立于Scale自动化使用。API可以在用户的本地机器上使用,从任何集群断开连接。

为了查看API参考手册,你可以从管理仪表板导航至文档,然后再导航至“Scale Python API→Variation”。

Scale泛化的核心是设计空间文件

下面的文件做了两件事情:

  1. 包含另一个设计空间文件
  2. 只激活场景相关的参数(例如:SUT相关的参数是未激活的)

代码:

%%bash
mkdir -p /tmp/variation
rm -fr /tmp/variation/*
cp -fr /hdfs/vtd/demodata/ScaleDriver-LaneChange /tmp/variation

cat /tmp/variation/ScaleDriver-LaneChange/design_space_scenario.xml

输出:

<VariationSpace entry_point=".">
    <Includes>
        <Include path="design_space.xml" activateOnlyGroups="scenario" />
    </Includes>
</VariationSpace>

设计空间文件包含:

  1. 参数(Parameters):在仿真中可能发生变化的变量
  2. 填充器(Applicators):定义如何应用参数的特定取值,以及如何更新文件
  3. 约束(Constraints):用于限制一个或多个参数的有效值
  4. 插件(Plugins):用于高级用例,它们能够支持创建自定义参数和填充器逻辑
  5. 包含(Includes):可以支持重用,多个设计文件可能包含同一个设计文件

代码:

%%bash
cat /tmp/variation/ScaleDriver-LaneChange/design_space.xml

输出:

<VariationSpace entry_point=".">
    <Plugins>
        <Plugin module="scale.variation.plugins" class_name="DefaultPlugin"/>
    </Plugins>
    <Parameters>
        <UniformRange group="scenario" name="TgtTTC_StartLaneChange" type="real" default="2.0" min="1.0" max="10.0" />
        <UniformRange group="scenario" name="TgtRate" type="real" default="1.5" min="0.5" max="3.0" />
        <UniformRange group="scenario" name="TgtSpeed" type="real" default="5.56" min="0.0" max="30.0" />

        <UniformRange group="sut" name="SensorNear" type="real" default="0.0" min="0.0" max="5.0" />
        <UniformRange group="sut" name="SensorFar" type="real" default="150.0" min="0.0" max="250.0" />
        <UniformRange group="sut" name="SensorLeft" type="real" default="10.0" min="0.0" max="90.0" />
        <UniformRange group="sut" name="SensorRight" type="real" default="10.0" min="0.0" max="90.0" />
        <UniformRange group="sut" name="SensorBottom" type="real" default="5.0" min="0.0" max="90.0" />
        <UniformRange group="sut" name="SensorTop" type="real" default="5.0" min="0.0" max="90.0" />
        <UniformRange group="sut" name="SensorLatency" type="real" default="2.0" min="0.0" max="100.0" />

        <UniformRange group="sut" name="EgoSpeed" type="real" default="40.0" min="0.0" max="60.0" />
        <UniformRange group="sut" name="EgoTimeGap" type="real" default="1.0" min="0.0" max="10.0" />
        <UniformRange group="sut" name="EgoMinDist" type="real" default="10.0" min="0.0" max="20.0" />
        <UniformRange group="sut" name="EgoBrakeAgility" type="real" default="0.7" min="0.0" max="10.0" />
        <UniformRange group="sut" name="EgoMaxAccelCmd" type="real" default="6.0" min="0.0" max="10.0" />
        <UniformRange group="sut" name="EgoMaxDecelCmd" type="real" default="-9.81" min="-10.0" max="0.0" />
    </Parameters>
    <Applicators>
        <Mustache group="scenario" path="scenario.template" new_extension="xml" />
        <Mustache group="sut" path="moduleManager.template" new_extension="xml" />
    </Applicators>
    <Constraints />
</VariationSpace>

创建样本 — 蒙特卡洛

MonteCarlo将根据参数的分布随机选择值。

代码:

from scale.variation import load_xml, MonteCarlo, GridSearch, LocalResample

design_space = load_xml('/tmp/variation/ScaleDriver-LaneChange/design_space_scenario.xml')

monte_carlo_sample = MonteCarlo(design_space, count=5).sample

monte_carlo_sample

输出:


TgtTTC_StartLaneChangeTgtRateTgtSpeedSensorNearSensorFarSensorLeftSensorRightSensorBottomSensorTopSensorLatencyEgoSpeedEgoTimeGapEgoMinDistEgoBrakeAgilityEgoMaxAccelCmdEgoMaxDecelCmd
04.0535631.93332821.1311970.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
11.8780081.05196729.7838190.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
24.7690712.43574716.9662680.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
35.2708172.62716115.6455420.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
44.2725591.3571131.6565900.0150.010.010.05.05.02.040.01.010.00.76.0-9.81

创建样本 — 网格搜索

GridSearch将遍历所有可能的取值组合。

代码:

#  continuous_count变量定义了将连续参数分割成多少个级别
grid_search_sample = GridSearch(design_space, continuous_count=2).sample

# 因为有3个活跃参数,并且连续计数是2
# 样本行数 = 2^3 = 8

grid_search_sample

输出:


TgtTTC_StartLaneChangeTgtRateTgtSpeedSensorNearSensorFarSensorLeftSensorRightSensorBottomSensorTopSensorLatencyEgoSpeedEgoTimeGapEgoMinDistEgoBrakeAgilityEgoMaxAccelCmdEgoMaxDecelCmd
01.00.50.00.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
11.00.530.00.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
21.03.00.00.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
31.03.030.00.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
410.00.50.00.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
510.00.530.00.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
610.03.00.00.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
710.03.030.00.0150.010.010.05.05.02.040.01.010.00.76.0-9.81

创建样本 — 局部重采样

LocalResample将使用以种子样本中提供的数值为中心的正态分布。

代码:

# 重采样使用一个输入样本,然后创建另一个样本

# ICD支持在一个输出中打印多个数据帧
from IPython.core import display as ICD

# 从蒙特卡洛样本中取一个任意的切片(2行)
seed_sample = monte_carlo_sample[3:5]
ICD.display(seed_sample)

local_sample = LocalResample(design_space, seed_sample, count=10, normalized_stddev=0.01).sample

# 这些值将“接近”seed_sample中的值
local_sample

输出:


TgtTTC_StartLaneChangeTgtRateTgtSpeedSensorNearSensorFarSensorLeftSensorRightSensorBottomSensorTopSensorLatencyEgoSpeedEgoTimeGapEgoMinDistEgoBrakeAgilityEgoMaxAccelCmdEgoMaxDecelCmd
35.2708172.62716115.6455420.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
44.2725591.3571131.6565900.0150.010.010.05.05.02.040.01.010.00.76.0-9.81



TgtTTC_StartLaneChangeTgtRateTgtSpeedSensorNearSensorFarSensorLeftSensorRightSensorBottomSensorTopSensorLatencyEgoSpeedEgoTimeGapEgoMinDistEgoBrakeAgilityEgoMaxAccelCmdEgoMaxDecelCmd
05.2160332.60984615.2606460.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
14.4436191.3640071.9031950.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
25.1242992.64949215.6589800.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
34.3517451.3388581.4428600.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
45.3111532.67176615.8268540.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
54.2599701.3733031.5821340.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
65.2080082.64788716.1432650.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
74.4584611.3443982.2286130.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
85.3463452.63494215.7460020.0150.010.010.05.05.02.040.01.010.00.76.0-9.81
94.2824651.3288241.7081860.0150.010.010.05.05.02.040.01.010.00.76.0-9.81

Mustache填充器

Mustache填充器是最易于使用的填充器。Mustache是一个流行的模板包。

<Mustache group="scenario" path="scenario.template" new_extension="xml" />

scenario.template(简化版):

<SpeedChange Rate="{{TgtRate}}" Target="{{TgtSpeed}}" Force="true" ExecutionTimes="1" ActiveOnEnter="true" DelayTime="0.0000000000000000e+00"/>

scenario.template文件是OpenSCENARIO文件的模板

在将样本中的某一行数据应用至上述模板时,将会创建一个新文件(scenario.xml)。

scenario.xml(简化版示例):

<SpeedChange Rate="1.692823" Target="22.427826" Force="true" ExecutionTimes="1" ActiveOnEnter="true" DelayTime="0.0000000000000000e+00"/>

代码:

# 基于monte_carlo_sample中的第一个样本实例,这将创建一组“具体”的文件。
design_space.apply(monte_carlo_sample.iloc[0], to_path='/tmp/variation/applied')

!find /tmp/variation/applied

输出:

/tmp/variation/applied
/tmp/variation/applied/config.xml
/tmp/variation/applied/design_space_defaults_only.xml
/tmp/variation/applied/scenario.xml
/tmp/variation/applied/processor
/tmp/variation/applied/processor/vtd
/tmp/variation/applied/processor/vtd/result_processor.py
/tmp/variation/applied/processor/vtd/result.py
/tmp/variation/applied/processor/vtd/__init__.py
/tmp/variation/applied/processor/vtd/result_comfort_factor.py
/tmp/variation/applied/processor/vtd/settings.json
/tmp/variation/applied/processor/vtd/settings.py
/tmp/variation/applied/processor/worker
/tmp/variation/applied/processor/worker/vtdprocessorfunction_helper.py
/tmp/variation/applied/Germany_2018.xodr
/tmp/variation/applied/moduleManager.xml
/tmp/variation/applied/design_space_sut.xml


Write a comment...