Surrogate¶
- class flexops.surrogates.base.Surrogate(spec: SurrogateSpec | dict)¶
Base class of every predefined surrogate-structure class.
A leaf class in
flexops(imports onlyflexcore, pydantic and Pyomo), soflexops.core.ops_blockcan import the registry that resolves to it with no cross-package import.- surrogate_type¶
The
SurrogateTypethis 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
configformat. A spec at an older version is migrated bymigrate_config()on construction.- Type:
ClassVar[int]
- replaces_relation¶
Truewhenbuild()swaps a relation;Falsefor 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. Itsconfigreflects anyupdate_state()since construction; itsparametersare the values the surrogate was built with. Read the live values withread_spec().
- config¶
spec.configparsed byconfig_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
OpsBlockDatathe 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
speccan be applied to the built block in place.The default allows it when
spechas 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:
Trueto update in place;Falseto 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.0for 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
configupgraded fromversiontoversion + 1.Override when a type’s config format changes: bump
config_versionand handle the old version here.- Parameters:
version – The version
configis 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_modelinstance.- Returns:
{name: shape}in registration order,()for a scalar,(n,)for a vector,(n, m)for a matrix. A spec’sparametersmust name exactly these, and_buildmust 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.
configisspec’s (including anyupdate_state()); each parameter’s value and fixed flag come from the block’s registry. This changes nothing.
- 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’sconfig; 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.
- classmethod validate_spec(spec: SurrogateSpec | dict) SurrogateSpec¶
Return
specmigrated to the currentconfig_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.