declare_custom_block

(function from pyomo.core.base.block)

pyomo.core.base.block.declare_custom_block(name, new_ctype=None, rule=None)[source]

Decorator to declare components for a custom block data class

This decorator simplifies the definition of custom derived Block classes. With this decorator, developers must only implement the derived “Data” class. The decorator automatically creates the derived containers using the provided name, and adds them to the current module:

>>> @declare_custom_block(name="FooBlock")
... class FooBlockData(BlockData):
...    pass
>>> s = FooBlock()
>>> type(s)
<class 'ScalarFooBlock'>
>>> s = FooBlock([1,2])
>>> type(s)
<class 'IndexedFooBlock'>

It is frequently desirable for the custom class to have a default rule for constructing and populating new instances. The default rule can be provided either as an explicit function or a string. If a string, the rule is obtained by attribute lookup on the derived Data class:

>>> @declare_custom_block(name="BarBlock", rule="build")
... class BarBlockData(BlockData):
...    def build(self, *args):
...        self.x = Var(initialize=5)
>>> m = pyo.ConcreteModel()
>>> m.b = BarBlock([1,2])
>>> print(m.b[1].x.value)
5
>>> print(m.b[2].x.value)
5