commonpower.modeling.base.ModelElement

class ModelElement(name: str, element_type: ElementTypes, doc: str, domain: Union[pyo.Integers, pyo.Binary, pyo.Reals] = <pyomo.core.base.set.DeclareGlobalSet.<locals>.GlobalSet object>, bounds: Union[None, tuple[float]] = None, expr: Union[None, callable] = None, initialize: Union[None, any] = None, indexed: Union[None, bool] = None, uncertainty_bounds: Union[None, tuple[float]] = None)[source]

Bases: object

The ModelElement class builds the bridge between the CommonPower object space and the pyomo model representation. Since pyomo does not give us a lot of nuance wrt. the “meaning” of variables/parameters, we capture that via the ElementTypes. We then map the ElementTypes to corresponding pyomo classes and provide an interface to instantiate and add them to the pyomo model.

Parameters:
  • name (str) – Name of the model element.

  • element_type (ElementTypes) – Element type. Will be mapped to the appropriate pyomo class.

  • doc (str) – Additional info/description. Will be passed to the pyomo class as the “doc” argument.

  • domain (Union[pyo.Integers, pyo.Binary, pyo.Reals], optional) – Pyomo domain. Defaults to pyo.Reals.

  • bounds (Union[None, tuple[float]], optional) – Lower and upper bounds. Can be overwritten in add_to_model(). Defaults to None.

  • expr (Union[None, callable], optional) – Expression for constraints. Defaults to None.

  • initialize (Union[None, any], optional) – Values to initialize the element with.

  • indexed (Union[None, bool], optional) – Specifies if the variable should be indexed on model.t. If not provided, all elements except CONSTANT/SET are indexed. Currently the indexing of Constraints and non-indexing of Sets are enforced. TODO: Fully implement indexing flexibility.

  • uncertainty_bounds (Union[None, tuple[float]], optional) – Interval bounds for the uncertainty set. Only relevant for ElementTypes.CONSTANT. Defaults to None.

Raises:

AttributeError – If the given type is unknown or if required arguments are not provided.

Methods

add_to_model

Here we parse the ModelElement to the corresponding pyomo model element and add it to the given model. Some assumptions are made: - All elements mapping to Var/Constraint are automatically indexed with "model.t". - All elements mapping to Var are initialized at the middle between their lower and upper bounds. - All elements mapping to Param are defined as mutable.

add_to_model(model: ConcreteModel, name: str, bounds: None | tuple[float] = None, initialize: None | int | float | ParamInitializer = None) None[source]

Here we parse the ModelElement to the corresponding pyomo model element and add it to the given model. Some assumptions are made:

  • All elements mapping to Var/Constraint are automatically indexed with “model.t”.

  • All elements mapping to Var are initialized at the middle between their lower and upper bounds.

  • All elements mapping to Param are defined as mutable.

Parameters:
  • model (ConcreteModel) – Pyomo model to add the element to.

  • name (str) – Complete name that the element should have in the pyomo model. This is not the same as self.name because it depends on the scope of the given model.

  • bounds (Union[None, tuple[float]], optional) – Lower and upper bounds. Overwrite self.bounds if given. Defaults to None.

  • initialize (Union[None, int, float, ParamInitializer], optional) – Only relevant for ElementTypes that are mapped to Param: Value to initialize the pyomo element with. If self.initialize was defined, we ignore whatever is passed here. If the argument (or self.initialize) is neither int nor float, the Param will be initialized at zero. Defaults to None.

Raises:

NotImplementedError – If no mapping exists for self.pyomo_class.