flexops.surrogates

Predefined classes for surrogate structures. A SurrogateSpec names one by SurrogateType and carries its config and parameters. Each class owns its contract: it defines what config holds, which parameters (names and shapes) that config implies, and how to build the Pyomo relationship. It validates a spec in __init__ without building any Pyomo objects, builds in build(), and reads its live spec back with read_spec(). swap_relation() is the only caller of build, whether the surrogate came from a spec at build time or was supplied at runtime through apply_to_model().

This is a leaf subpackage of flexops (it imports only flexcore, pydantic and Pyomo), which is what lets flexops.core.ops_block realize a config’s surrogate at construction time with no flexparameterize import anywhere in flexops. flexparameterize builds specs only through each class’s make_spec.

SURROGATES

SurrogateType -> the class that implements it.

flexops.surrogates.surrogates.surrogate_from_spec(spec: SurrogateSpec) → Surrogate

Construct and validate the surrogate spec names.

The surrogate migrates an older config_version of its own config.

Parameters:

spec – The SurrogateSpec to realize.

Returns:

The constructed, validated Surrogate.

Raises:
  • FlexConfigError – If no class is registered for spec.surrogate_type or spec does not match its contract.

  • NotImplementedError – If the named class is not yet implemented.

flexops.surrogates.surrogates.surrogate_class(surrogate_type: SurrogateType | str) → type[Surrogate]

Return the class registered for surrogate_type.

Parameters:

surrogate_type – A SurrogateType or its value.

Returns:

The registered class.

Raises:

FlexConfigError – If no class is registered for it.

flexops.surrogates.surrogates.is_registered(surrogate: Surrogate) → bool

Return whether surrogate’s class is the one registered for its type.

Only a registered class can be rebuilt from a spec, so only such a surrogate is written out by emit_model; a custom subclass is not.

Parameters:

surrogate – A surrogate instance.

Returns:

True when type(surrogate) is SURROGATES[surrogate_type].

Base class

Surrogate

Base class of every predefined surrogate-structure class.

Every subclass declares input_variables/output_variables as {name: units} mappings in its config. These are the units the relationship was fitted or declared in, not necessarily the model’s own. build converts each factor from the unit’s actual variable into the surrogate’s declared units before using it, and swap_relation() converts the whole body from the surrogate’s declared output units into the registered target’s own units. Both conversions double as validation. A declared unit dimensionally incompatible with the model’s variable raises a FlexConfigError rather than silently rescaling.

Developing a surrogate starts with its contract: subclass Surrogate, define config_model, parameter_shapes and a typed make_spec, implement _build registering every parameter, and add the class to SURROGATES. The shared contract tests then cover it. Regression tooling comes after, producing specs only through make_spec. A change to a type’s config format bumps its config_version and adds a migrate_config step.

Multilinear

A constant plus a sum of coefficient * (product of distinct declared inputs). This is the expanded form that covers what a previous milestone called linear (no cross terms) and bilinear (one cross term). config.terms lists the products, each a product of names from input_variables, separated by *, each appearing at most once (no ^ exponent, no repeated factor). The parameters are intercept, the constant term read in the declared output units, plus one per term.

MultilinearSurrogate

A constant plus a sum of coefficient * (product of distinct inputs).

MultilinearConfig

A multilinear surrogate's config: its variables and its product terms.

ARIMA

A non-seasonal ARIMA(p, d, q) with exogenous inputs. config holds the order, the deterministic term (intercept for d=0, drift for d=1, or none), an optional max_ar_coeff and the pre-horizon history. The parameters are the deterministic term, ar.L1 to ar.Lp, ma.L1 to ma.Lq and exog.<input> per input. unit.update_surrogate_state() rewrites history from a solved horizon.

ArimaSurrogate

Non-seasonal ARIMA/ARIMAX relationship with explicit innovations.

ArimaConfig

An ARIMA surrogate's config: order, variables, options and history.

ArimaHistory

Pre-horizon ARIMA state: disturbances and innovations on a dated grid.

arima_parameter_names

Return an ARIMA surrogate's parameter names, in registration order.

Constant intensity

Sets a unit’s energy_intensity (its one parameter) rather than swapping a relation; the unit already has the relationship power_electrical == energy_intensity * flow.

ConstantIntensitySurrogate

Sets a unit's energy_intensity rather than swapping a relation.

ConstantIntensityConfig

A constant-intensity surrogate's config: the variables, for reference.

Not yet implemented

QuadraticSurrogate

A quadratic (up to degree-2, including squared terms) relationship.

ExponentialSurrogate

An exponential relationship in its inputs.

NeuralNetworkSurrogate

A neural-network (e.g. ReLU big-M or ICNN) forward-pass relationship.