CoefficientRegistry

class flexops.core.registration.CoefficientRegistry

A dict-like name -> Var container for a surrogate block’s coefficients.

Each registered name is one surrogate parameter, the unit a spec’s parameters addresses. A parameter is either a scalar (a scalar Var or one entry of an indexed Var) or an array (a whole indexed Var, one name for many values, e.g. an ML weight matrix). An array’s values map onto the Var’s index set in its iteration order, read row-major for nested lists; one fixed flag covers the whole array.

To register every entry of an indexed Var as its own scalar parameter, named by its index, pass it to register_coefficients().

The registry dynamically expands as coefficients are registered. It is attached to every surrogate block as block.coefficients before build() returns, so a developer can call register_coefficient multiple times in a single build() method — useful when coefficients come from several independent Var groups (e.g. separate low-flow and high-flow regimes).

fix(name: str | None = None) → None

Fix one parameter, or every one when name is None, at its value.

is_fixed(name: str) → bool

Return whether every entry of a parameter is fixed.

is_partially_fixed(name: str) → bool

Return whether an array parameter has some entries fixed and some free.

items()

Return the registered (name, Var) pairs; an array’s Var is indexed.

names() → list[str]

Return the registered parameter names, in registration order.

register_coefficient(name: str, var: Any, *, shape: tuple[int, ...] | None = None) → None

Add one named parameter: a scalar Var entry, or a whole indexed Var.

Parameters:
  • name – The parameter name used by specs, update_parameters and register_surrogate_coefficients.

  • var – A scalar Pyomo Var or one entry of an indexed Var (a scalar parameter), or a whole indexed Var (an array parameter).

  • shape – An array parameter’s shape, e.g. (3, 2). Defaults to (len(var),) for an indexed Var; must be () or omitted for a scalar.

Raises:

FlexConfigError – If name is already registered, var is not a Pyomo Var, or shape does not match var’s size.

register_coefficients(mapping) → None

Bulk-add scalar parameters from a name->Var mapping or an indexed Var.

Parameters:

mapping – Either a dict of parameter name to Pyomo Var, or a single indexed pyo.Var whose entries each become a scalar parameter named by its index.

Raises:

FlexConfigError – If any name is already registered or any value is not a Pyomo Var or Var entry.

set_value(name: str, value: Any) → None

Set a parameter’s value without changing whether it is fixed.

Parameters:
  • name – A registered parameter.

  • value – A number for a scalar; for an array, nested lists matching its shape.

Raises:

FlexConfigError – If value’s shape does not match the parameter’s.

shape(name: str) → tuple[int, ...]

Return a parameter’s shape: () for a scalar.

unfix(name: str | None = None) → None

Unfix one parameter, or every registered parameter when name is None.

value(name: str) → Any

Return a parameter’s current value: a float, or nested lists of floats.

An entry whose Var has no value yet reads as None.