Surrogate

class flexops.surrogates.base.Surrogate(spec: SurrogateSpec | dict)

Base class of every predefined surrogate-structure class.

A leaf class in flexops (imports only flexcore, pydantic and Pyomo), so flexops.core.ops_block can import the registry that resolves to it with no cross-package import.

surrogate_type

The SurrogateType this class implements.

Type:

ClassVar[flexcore.config.schema.SurrogateType]

config_model

The pydantic model that validates config.

Type:

ClassVar[type[pydantic.main.BaseModel]]

config_version

The current version of this type’s config format. A spec at an older version is migrated by migrate_config() on construction.

Type:

ClassVar[int]

replaces_relation

True when build() swaps a relation; False for a surrogate that instead sets a parameter the unit already has (constant_intensity).

Type:

ClassVar[bool]

spec

The validated spec, at the current config_version. Its config reflects any update_state() since construction; its parameters are the values the surrogate was built with. Read the live values with read_spec().

config

spec.config parsed by config_model.

apply_parameters(registry, parameters) → None

Set each parameter’s value and fixed flag on a built registry.

Parameters:
  • registry – The block’s CoefficientRegistry.

  • parameters – {name: ParameterState}; a free parameter without a value keeps its current value.

build(unit, target) → tuple[Any | None, Any]

Return (block, body(t)), with every parameter set from the spec.

Calls _build, checks that the block’s coefficient registry holds exactly the contract’s parameters with their shapes, then sets each parameter’s value and fixes or frees it as the spec says.

Parameters:
  • unit – The OpsBlockData the relationship is built on.

  • target – The Var/Reference the relationship determines.

Returns:

(block, body); see _build.

Raises:

FlexConfigError – If the built registry does not match the contract (a bug in the surrogate class).

can_update_in_place(spec: SurrogateSpec) → bool

Return whether spec can be applied to the built block in place.

The default allows it when spec has this surrogate’s type, config version and config, so only parameter values and fixed flags differ.

Parameters:

spec – A validated spec for the same relation.

Returns:

True to update in place; False to swap a new block in.

classmethod initial_guess(name: str, config: BaseModel) → Any

Return the starting value of a free parameter given without a value.

Override to document a type’s guesses; the default is 0.0 for every entry.

Parameters:
  • name – The parameter.

  • config – The validated config.

Returns:

A number, or nested lists matching the parameter’s shape.

abstract property input_variables: dict[str, str]

Return this relationship’s input variable names and declared units.

classmethod migrate_config(version: int, config: dict) → dict

Return config upgraded from version to version + 1.

Override when a type’s config format changes: bump config_version and handle the old version here.

Parameters:
  • version – The version config is at.

  • config – The config at that version (a copy; may be modified).

Returns:

The config at version + 1.

Raises:

FlexConfigError – If this type has no migration from version.

abstract property output_variables: dict[str, str]

Return this relationship’s output variable names and declared units.

abstractmethod classmethod parameter_shapes(config: BaseModel) → dict[str, tuple[int, ...]]

Return every parameter a config implies and its shape.

Parameters:

config – A validated config_model instance.

Returns:

{name: shape} in registration order, () for a scalar, (n,) for a vector, (n, m) for a matrix. A spec’s parameters must name exactly these, and _build must register exactly these.

Raises:

NotImplementedError – If this class is not yet implemented.

read_spec(block, target) → SurrogateSpec

Return this surrogate’s current spec, read from its built block.

config is spec’s (including any update_state()); each parameter’s value and fixed flag come from the block’s registry. This changes nothing.

Parameters:
  • block – The block build() returned.

  • target – The target passed to build().

Returns:

The spec, which rebuilds this surrogate as it stands.

update_in_place(block, spec: SurrogateSpec) → None

Apply spec’s parameters to the built block.

Called only when can_update_in_place() is true.

Parameters:
  • block – The block build() returned.

  • spec – The validated spec to apply.

update_state(block, target) → None

Record solved state into spec’s config; call after a solve.

The default does nothing: a type without time-dependent state has nothing to record. A type with state (ARIMA’s history) rewrites its config so the spec initializes the next model from the solve.

Parameters:
  • block – The block build() returned, after a solve.

  • target – The target passed to build().

classmethod validate_spec(spec: SurrogateSpec | dict) → SurrogateSpec

Return spec migrated to the current config_version, validated.

No Pyomo objects are built.

Parameters:

spec – The spec to check.

Returns:

The migrated, validated spec.

Raises:

FlexConfigError – If it does not match this class’s contract.

value_of(name: str) → Any

Return the value a parameter is built with: given, or the guess.