ParamSweeper

(class from 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]

Bases: TemporarySubsystemManager

This class enables setting values of variables/parameters according to a provided sequence. Iterating over this object sets values to the next in the sequence, at which point a calculation may be performed and output values compared. On exit, original values are restored.

This is useful for testing a solve that is meant to perform some calculation, over a range of values for which the calculation is valid. For example:

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) – The number of different values we expect for each input variable

  • input_values (ComponentMap) – Maps each input variable to a list of values of length n_scenario

  • output_values (ComponentMap) – Maps each output variable to a list of values of length n_scenario

  • to_fix (List) – to_fix argument for base class

  • to_deactivate (List) – to_deactivate argument for base class

  • to_reset (List) – to_reset argument for base class. This list is extended with input variables.

Methods

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

Member Documentation