自动化模拟运行的方法
Method to automate simulation runs
我在 Python 中创建了一个离散事件模拟,它接受一定数量的输入,目前保存在数据库 table 中,并产生也存储在数据库 (SQLite) 中的输出.我已经实现了使用给定输入集 运行 多次复制的能力(因为模拟中存在一些随机性)。我现在希望能够 运行 使用多组输入进行模拟,而不必在每个 运行 之后手动更改输入。但是我不确定实现此目标的最佳方法。
我目前的想法是有一个脚本可以创建一个 JSON 文件,其中包含每个 运行 的输入。例如:
输入数据table(为了解释极其简化):
id name input 1 input 2
1 Entity 1 67 red
2 Entity 2 54 blue
JSON 示例(再次简化以匹配上面的输入):
{"simulation inputs":[
{"run 1":
"replications": 100,
"entity 1": "name of entity"
{
"input 1": 67,
"input 2": "red"
}
"entity 2": "a different entity of same class with different values"
{
"input 1": 54,
"input 2": "blue"
}
},
{"run 2":
"replications": 100,
"entity 1": "name of entity"
{
"input 1": 69,
"input 2": "red"
}
"entity 2": "a different entity of same class with different values"
{
"input 1": 54,
"input 2": "blue"
}
},
{"run 3":
"replications": 100,
"entity 1": "name of entity"
{
"input 1": 71,
"input 2": "red"
}
"entity 2": "a different entity of same class with different values"
{
"input 1": 54,
"input 2": "blue"
}
},
]}
根据上面的示例,其中包含三个 运行 的信息,其中每个 运行 调整一个输入 - 实体 1 的输入 1。在实际情况下,可能会有很大输入的变体数量,每个变体在配置文件中都有 1 运行 个条目。 Python 程序将 运行 遍历 JSON 文件中的列表,使用定义的值作为特定 运行 的输入。输出存储在数据库中。每个实体都是相同 class 的实例,具有不同的输入值。
以上是明智的做法吗?如果没有,还有什么其他方法?如果是,是否有此类功能或类似功能的实现,我可以在任何地方用作起点?
我进行了很好的搜索,但是,也许是因为我不熟悉配置文件术语,未能找到 suitable 答案。
注意:经过编辑以更好地表示将要使用的输入数据的类型。
非常感谢。
如果您有一个输入 table,其中每一行都引用单个 运行 的一组输入,您可以执行如下操作。
import sqlite3
db_path = "C:/.../mydatabase.db"
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
#get parameters from table
for row in cursor.execute('SELECT * FROM input_table'):
param1 = row[0]
param2 = row[1]
...
go_for_simulation(param1, param2,...)
循环中的每个 row
变量都是一个包含所有参数的元组。
希望这个方法可以帮到你。
我实现了 JSON 文件技术并且效果很好所以我想我会分享答案。
创建了一个 JSON 文件。在可能的情况下,顶层是一个标识 "run" 的键——本质上是输入的唯一组合。该值是另一个字典,其中包含用于设置模拟的所有值,例如重复次数——因为 DES 需要多次重复。它只是使用 Python.
中的内置 JSON 功能加载
with open(file_name) as data_file:
input_data = json.load(data_file) # dict of the whole file
所以在这个阶段我们有 input_data 这只是整个文件的字典。通过提取顶级密钥并遍历每个密钥,您可以按顺序将每个 运行s 输入传递到模拟中。对于每个 运行 ,您只传递需要传递的一组输入,而不是整个字典。
list_of_runs = sorted(list(input_data.keys()), key=int)
for run in list_of_runs:
replications = input_data[run]['replications']
reps = 0
下一个循环 运行 每次使用同一组输入进行定义次数的模拟。
for reps in range(replications):
current_run = initialize.Run(input_data[run])
'''some other code here'''
env.run(until=end)
reps += 1
在我的实际代码中 "Run" 是一个 class 并且在那个 class 中定义了其他 classes,每个都被传递了原始 JSON 他们需要的文件。这意味着当 class 需要访问一个值时,如果只需要在已传递的字典中查找,并且所有这些字典都会自动更新为代码 运行 通过每个 运行s.
我在 Python 中创建了一个离散事件模拟,它接受一定数量的输入,目前保存在数据库 table 中,并产生也存储在数据库 (SQLite) 中的输出.我已经实现了使用给定输入集 运行 多次复制的能力(因为模拟中存在一些随机性)。我现在希望能够 运行 使用多组输入进行模拟,而不必在每个 运行 之后手动更改输入。但是我不确定实现此目标的最佳方法。
我目前的想法是有一个脚本可以创建一个 JSON 文件,其中包含每个 运行 的输入。例如:
输入数据table(为了解释极其简化):
id name input 1 input 2
1 Entity 1 67 red
2 Entity 2 54 blue
JSON 示例(再次简化以匹配上面的输入):
{"simulation inputs":[
{"run 1":
"replications": 100,
"entity 1": "name of entity"
{
"input 1": 67,
"input 2": "red"
}
"entity 2": "a different entity of same class with different values"
{
"input 1": 54,
"input 2": "blue"
}
},
{"run 2":
"replications": 100,
"entity 1": "name of entity"
{
"input 1": 69,
"input 2": "red"
}
"entity 2": "a different entity of same class with different values"
{
"input 1": 54,
"input 2": "blue"
}
},
{"run 3":
"replications": 100,
"entity 1": "name of entity"
{
"input 1": 71,
"input 2": "red"
}
"entity 2": "a different entity of same class with different values"
{
"input 1": 54,
"input 2": "blue"
}
},
]}
根据上面的示例,其中包含三个 运行 的信息,其中每个 运行 调整一个输入 - 实体 1 的输入 1。在实际情况下,可能会有很大输入的变体数量,每个变体在配置文件中都有 1 运行 个条目。 Python 程序将 运行 遍历 JSON 文件中的列表,使用定义的值作为特定 运行 的输入。输出存储在数据库中。每个实体都是相同 class 的实例,具有不同的输入值。
以上是明智的做法吗?如果没有,还有什么其他方法?如果是,是否有此类功能或类似功能的实现,我可以在任何地方用作起点?
我进行了很好的搜索,但是,也许是因为我不熟悉配置文件术语,未能找到 suitable 答案。
注意:经过编辑以更好地表示将要使用的输入数据的类型。
非常感谢。
如果您有一个输入 table,其中每一行都引用单个 运行 的一组输入,您可以执行如下操作。
import sqlite3
db_path = "C:/.../mydatabase.db"
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
#get parameters from table
for row in cursor.execute('SELECT * FROM input_table'):
param1 = row[0]
param2 = row[1]
...
go_for_simulation(param1, param2,...)
循环中的每个 row
变量都是一个包含所有参数的元组。
希望这个方法可以帮到你。
我实现了 JSON 文件技术并且效果很好所以我想我会分享答案。
创建了一个 JSON 文件。在可能的情况下,顶层是一个标识 "run" 的键——本质上是输入的唯一组合。该值是另一个字典,其中包含用于设置模拟的所有值,例如重复次数——因为 DES 需要多次重复。它只是使用 Python.
中的内置 JSON 功能加载with open(file_name) as data_file:
input_data = json.load(data_file) # dict of the whole file
所以在这个阶段我们有 input_data 这只是整个文件的字典。通过提取顶级密钥并遍历每个密钥,您可以按顺序将每个 运行s 输入传递到模拟中。对于每个 运行 ,您只传递需要传递的一组输入,而不是整个字典。
list_of_runs = sorted(list(input_data.keys()), key=int)
for run in list_of_runs:
replications = input_data[run]['replications']
reps = 0
下一个循环 运行 每次使用同一组输入进行定义次数的模拟。
for reps in range(replications):
current_run = initialize.Run(input_data[run])
'''some other code here'''
env.run(until=end)
reps += 1
在我的实际代码中 "Run" 是一个 class 并且在那个 class 中定义了其他 classes,每个都被传递了原始 JSON 他们需要的文件。这意味着当 class 需要访问一个值时,如果只需要在已传递的字典中查找,并且所有这些字典都会自动更新为代码 运行 通过每个 运行s.