pyomo
BuildAction
BuildCheck
In many cases, Pyomo models can be constructed without Set and Param data components. Native Python data types class can be simply used to define constant values in Pyomo expressions. Consequently, Python sets, lists and dictionaries can be used to construct Pyomo models, as well as a wide range of other Python classes.
Set
Param
TODO
More examples here: set, list, dict, numpy, pandas.
The Set and Param components used in a Pyomo model can also be initialized with standard Python data types. This enables some modeling efficiencies when manipulating sets (e.g. when re-using sets for indices), and it supports validation of set and parameter data values. The Set and Param components are initialized with Python data using the initialize option.
initialize
In general, Set components can be initialized with iterable data. For example, simple sets can be initialized with:
list, set and tuple data:
model.A = pyo.Set(initialize=[2, 3, 5]) model.B = pyo.Set(initialize=set([2, 3, 5])) model.C = pyo.Set(initialize=(2, 3, 5))
generators:
model.D = pyo.Set(initialize=range(9)) model.E = pyo.Set(initialize=(i for i in model.B if i % 2 == 0))
numpy arrays:
f = numpy.array([2, 3, 5]) model.F = pyo.Set(initialize=f)
Sets can also be indirectly initialized with functions that return native Python data:
def g(model): return [2, 3, 5] model.G = pyo.Set(initialize=g)
Indexed sets can be initialized with dictionary data where the dictionary values are iterable data:
H_init = {} H_init[2] = [1, 3, 5] H_init[3] = [2, 4, 6] H_init[4] = [3, 5, 7] model.H = pyo.Set([2, 3, 4], initialize=H_init)
When a parameter is a single value, then a Param component can be simply initialized with a value:
model.a = pyo.Param(initialize=1.1)
More generally, Param components can be initialized with dictionary data where the dictionary values are single values:
model.b = pyo.Param([1, 2, 3], initialize={1: 1, 2: 2, 3: 3})
Parameters can also be indirectly initialized with functions that return native Python data:
def c(model): return {1: 1, 2: 2, 3: 3} model.c = pyo.Param([1, 2, 3], initialize=c)