参数扫描器

(类来自 pyomo.util.subsystems)

class pyomo.util.subsystems.ParamSweeper(n_scenario, input_values, output_values=None, to_fix=None, to_deactivate=None, to_reset=None)[source]

基础:TemporarySubsystemManager

此类允许根据提供的序列设置变量/参数的值。遍历此对象会将值设置为序列中的下一个值,此时可以执行计算并比较输出值。退出时,原始值将被恢复。

这对于测试一个旨在执行某些计算的求解器非常有用,适用于计算有效的值范围。例如:

model = pyo.ConcreteModel()
model.v1 = pyo.Var()
model.v2 = pyo.Var()
model.c = pyo.Constraint(expr=model.v2 - model.v1 >= 0.1)
model.o = pyo.Objective(expr=model.v1 + model.v2)
solver = pyo.SolverFactory('glpk')
input_vars = [model.v1]
n_scen = 2
input_values = pyo.ComponentMap([(model.v1, [1.1, 2.1])])
output_values = pyo.ComponentMap([(model.v2, [1.2, 2.2])])
with ParamSweeper(
        n_scen,
        input_values,
        output_values,
        to_fix=input_vars,
        ) as param_sweeper:
    for inputs, outputs in param_sweeper:
        solver.solve(model)
        # inputs and outputs contain the correct values for this
        # instance of the model
        for var, val in outputs.items():
            # Test that model.v2 was calculated properly.
            # First that it equals 1.2, then that it equals 2.2
            assert var.value == val, f"{var.value} != {val}"
__init__(n_scenario, input_values, output_values=None, to_fix=None, to_deactivate=None, to_reset=None)[source]
Parameters:
  • n_scenario (Integer) – 我们期望每个输入变量具有的不同值的数量

  • input_values (ComponentMap) – 将每个输入变量映射到长度为n_scenario的值列表

  • output_values (ComponentMap) – 将每个输出变量映射到长度为n_scenario的值列表

  • to_fix (List) – to_fix 参数用于基类

  • to_deactivate (List) – to_deactivate 参数用于基类

  • to_reset (List) – 基类的to_reset参数。此列表会扩展输入变量。

方法

__init__(n_scenario, input_values[, ...])

成员文档