agentlib_mpc.modules.ml_model_training package
Submodules
agentlib_mpc.modules.ml_model_training.data_reduction module
Code stolen from Max Berktold
- class agentlib_mpc.modules.ml_model_training.data_reduction.InducingPoints[source]
Bases:
ABC
The idea is to reduce the effective number of input data points x to the GP from n to m, with m<n, where the set of m points are called inducing points.
Since this makes the effective covariance matrix K smaller, many inducing point approaches reduce the computational complexity from O(n3) to O(nm2). The smaller m is, the bigger the speed up.
Source: https://bwengals.github.io/inducing-point-methods-to-speed-up-gps.html
- class agentlib_mpc.modules.ml_model_training.data_reduction.NystroemReducer(n_components: int, kernel: Kernel = None)[source]
Bases:
InducingPoints
agentlib_mpc.modules.ml_model_training.ml_model_trainer module
- class agentlib_mpc.modules.ml_model_training.ml_model_trainer.ANNTrainer(config: dict, agent: Agent)[source]
Bases:
MLModelTrainer
Module that generates ANNs based on received data.
- build_ml_model() Sequential [source]
Build an ANN with a one layer structure, can only create one ANN
- fit_ml_model(training_data: TrainingData)[source]
Fits the ML Model with the training data.
- model_type
alias of
SerializedANN
- pydantic model agentlib_mpc.modules.ml_model_training.ml_model_trainer.ANNTrainerConfig[source]
Bases:
MLModelTrainerConfig
Pydantic data model for ANNTrainer configuration parser
- Config:
arbitrary_types_allowed: bool = True
validate_assignment: bool = True
extra: str = forbid
frozen: bool = True
- Fields:
- Validators:
- field batch_size: int = 100
- field early_stopping: EarlyStoppingCallback = EarlyStoppingCallback(patience=(1000,), verbose=0, restore_best_weights=True, activate=False)
Specification of the EarlyStopping Callback for training
- field epochs: int = 100
- field layers: list[tuple[int, Literal['relu', 'sigmoid', 'softmax', 'softplus', 'softsign', 'tanh', 'selu', 'elu', 'exponential']]] = [(16, 'sigmoid')]
Hidden layers which should be created for the ANN. An ANN always has a BatchNormalization Layer, and an Output Layer the size of the output dimensions. Additional hidden layers can be specified here as a list of tuples: (#neurons of layer, activation function).
- model_post_init(context: Any, /) None
We need to both initialize private attributes and call the user-defined model_post_init method.
- class agentlib_mpc.modules.ml_model_training.ml_model_trainer.GPRTrainer(config: dict, agent: Agent)[source]
Bases:
MLModelTrainer
Module that generates ANNs based on received data.
- fit_ml_model(training_data: TrainingData)[source]
Fits GPR to training data
- model_type
alias of
SerializedGPR
- pydantic model agentlib_mpc.modules.ml_model_training.ml_model_trainer.GPRTrainerConfig[source]
Bases:
MLModelTrainerConfig
Pydantic data model for GPRTrainer configuration parser
- Config:
arbitrary_types_allowed: bool = True
validate_assignment: bool = True
extra: str = forbid
frozen: bool = True
- Fields:
- Validators:
- field constant_value_bounds: tuple = (0.001, 100000.0)
- field length_scale_bounds: tuple = (0.001, 100000.0)
- field n_restarts_optimizer: int = 0
Defines the number of restarts of the Optimizer for the gpr_parameters of the kernel.
- field noise_level: float = 1.5
- field noise_level_bounds: tuple = (0.001, 100000.0)
- field normalize: bool = False
Defines whether the training data and the inputs are for predictionare normalized before given to GPR.
- field scale: float = 1.0
Defines by which value the output data is divided for training and multiplied after prediction.
- model_post_init(context: Any, /) None
We need to both initialize private attributes and call the user-defined model_post_init method.
- class agentlib_mpc.modules.ml_model_training.ml_model_trainer.LinRegTrainer(config: dict, agent: Agent)[source]
Bases:
MLModelTrainer
Module that generates ANNs based on received data.
- fit_ml_model(training_data: TrainingData)[source]
Fits linear model to training data
- model_type
alias of
SerializedLinReg
- pydantic model agentlib_mpc.modules.ml_model_training.ml_model_trainer.LinRegTrainerConfig[source]
Bases:
MLModelTrainerConfig
Pydantic data model for GPRTrainer configuration parser
- Config:
arbitrary_types_allowed: bool = True
validate_assignment: bool = True
extra: str = forbid
frozen: bool = True
- Fields:
- Validators:
- model_post_init(context: Any, /) None
We need to both initialize private attributes and call the user-defined model_post_init method.
- class agentlib_mpc.modules.ml_model_training.ml_model_trainer.MLModelTrainer(config: dict, agent: Agent)[source]
Bases:
BaseModule
,ABC
Abstract Base Class for all Trainer classes.
- property agent_and_time: str
A string that specifies id and time. Used to create save paths
- create_inputs_and_outputs(full_data_sampled: DataFrame) tuple[pandas.core.frame.DataFrame, pandas.core.frame.DataFrame] [source]
Creates extra columns in the data which contain the shifted time-series data which is lagged accordingly. Returns a tuple (input_data, output_data)
- divide_in_tvt(inputs: DataFrame, outputs: DataFrame)[source]
splits the samples into mpc, validating and testing sets
- abstract fit_ml_model(training_data: TrainingData)[source]
Fits the ML Model with the training data.
- property input_names
- model_type: Type[SerializedMLModel]
- property output_names
- process()[source]
This abstract method must be implemented in order to sync the module with the other processes of the agent and the whole MAS.
- save_all(serialized_ml_model: SerializedMLModel, training_data: TrainingData)[source]
Saves all relevant data and results of the training process if desired.
- save_ml_model(serialized_ml_model: SerializedMLModel, path: Path)[source]
Saves the ML Model in serialized format.
- serialize_ml_model() SerializedMLModel [source]
Serializes the ML Model, sa that it can be saved as json file. :returns: SerializedMLModel version of the passed ML Model.
- property training_info: dict
Returns a dict with relevant config parameters regarding the training.
- pydantic model agentlib_mpc.modules.ml_model_training.ml_model_trainer.MLModelTrainerConfig[source]
Bases:
BaseModuleConfig
,ABC
Abstract Base Class for all Trainer Configs.
- Config:
arbitrary_types_allowed: bool = True
validate_assignment: bool = True
extra: str = forbid
frozen: bool = True
- Fields:
- Validators:
check_valid_fields
»shared_variable_fields
- field MLModel: AgentVariable = AgentVariable(name='MLModel', type=None, timestamp=None, unit='Not defined', description='Not defined', ub=inf, lb=-inf, clip=False, allowed_values=[], value=None, alias='MLModel', source=Source(agent_id=None, module_id=None), shared=None, rdf_class=None)
Serialized ML Model which can be sent to other Agents.
- field data_sources: list[pathlib.Path] = []
List of paths to time series data, which can be loaded on initialization of the agent.
- Validated by:
- field inputs: List[AgentVariable] = []
Variables which are inputs of the ML Model that should be trained.
- field interpolations: dict[str, agentlib_mpc.data_structures.interpolation.InterpolationMethods] = {}
Dictionary specifying the interpolation types of output variables. If not specified, will be set to ‘linear’.
- Validated by:
- field lags: dict[str, int] = {}
Dictionary specifying the lags of each input and output variable. If not specified, will be set to one.
- Validated by:
- field output_types: dict[str, agentlib_mpc.data_structures.ml_model_datatypes.OutputType] = {}
Dictionary specifying the output types of output variables. If not specified, will be set to ‘difference’.
- Validated by:
- field outputs: List[AgentVariable] = []
Variables which are outputs of the ML Model that should be trained.
- field recursive_outputs: dict[str, bool] = {}
Dictionary specifying whether output variables are recursive, i.e. automatically appear as an input as well. If not specified, will be set to ‘recursive’.
- Validated by:
- field retrain_delay: float = 10000000000
Time in seconds, after which retraining is triggered in regular intervals
- field save_data: bool = False
Whether the training data should be saved.
- Validated by:
- field save_directory: Path = None
Path, where created ML Models should be saved.
- field save_ml_model: bool = False
Whether the created ML Models should be saved.
- Validated by:
- field save_plots: bool = False
Whether a plot of the created ML Models performance should be saved.
- Validated by:
check_valid_fields
- field step_size: float [Required]
- Validated by:
- field time_series_length: float = 315360000
Maximum time window of data which is kept for the ML Model training. If saved data is older than current time minus time_series_length, it will be deleted.
- field time_series_memory_size: int = 1000000000
Maximum size of the data which is kept in memory for the ML Model training. If saved data exceeds this value, the oldest data is deleted.
- Validated by:
- field use_values_for_incomplete_data: bool = False
Default False. If True, the values of inputs and outputs which are defined in the config will be used for training, in case historic data has not reached the trainer. If False, an Error will be raised when the data is not sufficient.
- Validated by:
- validator check_data_sources_exist » data_sources[source]
Checks if all given data sources exist
- validator check_if_save_path_is_there » save_data, save_ml_model[source]
Makes sure, the shares amount to one.
- validator fill_interpolations » interpolations[source]
Adds interpolation method to all unspecified methods.
- validator fill_output_types » output_types[source]
Adds output type one to all unspecified output types.
- validator fill_recursive_outputs » recursive_outputs[source]
Adds recursive flag to all unspecified outputs.
- model_post_init(context: Any, /) None
We need to both initialize private attributes and call the user-defined model_post_init method.
agentlib_mpc.modules.ml_model_training.setpoint_generator module
Module which generates random set points within a comfort zone. Code heavily stolen from Max Berktold
- class agentlib_mpc.modules.ml_model_training.setpoint_generator.SetPointGenerator(config: dict, agent: Agent)[source]
Bases:
BaseModule
Module that generates and sends random set points based on daytime and values.
- pydantic model agentlib_mpc.modules.ml_model_training.setpoint_generator.SetPointGeneratorConfig[source]
Bases:
BaseModuleConfig
Pydantic data model for ANNTrainer configuration parser
- Config:
arbitrary_types_allowed: bool = True
validate_assignment: bool = True
extra: str = forbid
frozen: bool = True
- Fields:
- Validators:
check_valid_fields
»shared_variable_fields
- field day_end: int = 16
- field day_lb: float = 292.15
- field day_start: int = 8
- field day_ub: float = 294.15
- field interval: int = 14400
- field night_lb: float = 289.15
- field night_ub: float = 297.15
- Validated by:
check_valid_fields
- field target_variable: AgentVariable = AgentVariable(name='target', type=None, timestamp=None, unit='Not defined', description='Not defined', ub=inf, lb=-inf, clip=False, allowed_values=[], value=None, alias='target', source=Source(agent_id=None, module_id=None), shared=None, rdf_class=None)
- model_post_init(context: Any, /) None
We need to both initialize private attributes and call the user-defined model_post_init method.