Interface to connect Reinforcement Learning libraries to Functional Mock-up Units for control under uncertainties
Project description
FMUGym: An Interface for Reinforcement Learning-based Control of Functional Mock-up Units under Uncertainties
This project provides an interface for reinforcement learning(RL)-based control of functional mock-up units (FMUs) under uncertainties. The interface is designed to be compatible with the Functional Mock-up Interface (FMI) standard. It is implemented in Python and connects FMUs as control plants to pre-existing RL libraries such as Stable Baselines3 or SKRL for training.
For more information refer to the corresponding paper FMUGym: An Interface for Reinforcement Learning-based Control of Functional Mock-up Units under Uncertainties.
Installation
Installation via pip
You can easily install FMUGym via pip by running the following command:
pip install fmugym
Installation from source
To install the project from source, first clone this repo and then run the following command from its root directory:
pip install -e .
Installation of additional Python packages
Further, we recommend using jupyter notebooks for testing. To install jupyter notebook, run the following command:
pip install notebook
As RL libraries, we tested Stable Baselines3 and SKRL. To install Stable Baselines3, run the following command:
pip install stable-baselines3
To install SKRL, run the following command:
pip install skrl
Content
This project contains the following content:
- fmugym: The library containing the abstract FMUGym interface and classes for the environment configuration.
- examples: Jupyter notebooks demonstrating the usage of FMUGym
- dummy default: A linear MIMO system connected to an SB3 SAC agent with continuous action and observation space. All possible kinds of uncertainties are included as well as comprehensive comments.
- dummy skrl: A linear MIMO system connected to an SKRL SAC agent with continuous action and observation space.
- dummy discrete: A linear MIMO system connected to an SB3 A2C agent with discrete action and observation space.
- nonlinear: A nonlinear MIMO system connected to an SB3 SAC agent with continuous action and observation space.
- geom tolerance: Monte Carlo simulation for geometric tolerance analysis, related to Paper(tba) at the 16th International Modelica & FMI Conference.
- DC Motor Workshop: A workshop at the 16th International Modelica & FMI Conference on controlling a DC motor optimizing classical PID control and residual RL under uncertainties.
- FMUs: A collection of Modelica models and their corresponding FMUs used in the examples. We just provide FMUs exported with Open Modelica for Linux x86. If you are running on another OS / architecture, please export the FMUs accordingly.
Further, we chose the CVODE solver used by Open Modelica, which is causing issues in long simulation runs. So we recommend exporting Modelica models with e.g. Dymola instead on your own. - trained_agents: Models of RL agents after completing training.
- tests: Unit tests for compliance of FMUGym library with gymnasium API.
Usage
The following code snippet demonstrates the usage of FMUGym for the most simple linear MIMO system connected to an SB3 agent with continuous action and observation space. For specific implementations please see the dummy example notebook notebook.
1 FMU export
The first step involves exporting the desired Modelica model as a co-simulation FMU compliant with FMI 1.0/2.0/3.0. The inputs and outputs of the model have to be clearly defined using the appropriate Modelica standard library blocks (Modelica.Blocks.Interfaces.RealInput for inputs and Modelica.Blocks.Interfaces.RealOutput for outputs). See below the model view of our dummy example in Open Modelica.
2 Implement abstract FMUGym class
class FMUEnv(FMUGym):
...
The super class FMUGym has to be inherited for model-specific utilization by implementing the following abstract methods:
_get_info(): Used bystep()andreset(), returns any relevant debugging information._get_obs(): Retrieves FMU output values by possibly callingself.fmu._get_fmu_outputfor handling different FMU versions and stores them in theself.observationdictionary. It can also add output noise (usingself._get_output_noise()) and update the set point (usingself._setpoint_trajectory()) to return a goal-oriented observation dictionary._get_input_noise(): Returns input noise for each input component, potentially by sampling from the self.input_noise dictionary._get_output_noise(): Similar toself._get_input_noise, generates output noise for each output component, potentially by sampling from theself.output_noise dictionary._get_terminated(): Returns two booleans indicating first the termination and second truncation status._create_action_space(inputs): Constructs the action space from aVarSpaceobject representing the inputs. It can usegymnasium.spaces.Boxfor continuous action spaces._create_observation_space(outputs): Constructs the observation space returning it as agymnasium.spaces.Dict. The observation space typically includes observation,achieved_goal, anddesired_goal, each created from aVarSpaceobject._noisy_init(): Random variations to initial system states and dynamic parameters by sampling fromself.random_vars_refsand propagates to corresponding initial output values. It also allows for direct manipulation and randomization of set point goals using theself.y_stopclass variable._process_action(action): Called byself.step()to add noise to action from RL library. May be used to execute low-level controller and adapt action space._setpoint_trajectory(): Determines the set point values at the current time step within the trajectory, called byself._get_obs()._process_reward(self, obs, acts, info): Interface between step method of fmugym and compute_reward. adjusts the necessary paramters for reward method of RL library (SKRL, SB3).compute_reward(): Computes and returns a scalar reward value in case of SB3 fromachieved_goal,desired_goal, and possibly further parameters.
3 Create FMUGymConfig object
config = FMUGymConfig(...)
By passing an FMUGymConfig object containing the necessary parameters to the FMUGym implementation an environment instance can be created. Several custom data types simplify the environment configuration:
- VarSpace: Holds a dictionary of class variables. It provides methods like add_var_box(name: str, min: float, max: float) to add a gymnasium.spaces.Box entry or add_var_discrete(name: str, n: int, start: int) to add a gymnasium.spaces.Discrete.
- State2Out: Holds a dictionary of class variables. It offers an add_map(state: str, out: str) method to map a system state variable to an output variable.
- TargetValue: Holds a dictionary of class variables. The method add_target(name: str, value: float) incorporates a target value, such as a nominal initial output value.
4 Create FMUEnv object
dummyEnv = FMUEnv(config)
Create instance of previously implemented FMUEnv by passing the config object to it.
5 Creation of RL agent from FMUEnv and start training
model = ...
Create the RL agent leveraging a pre-existing library like Stable Baselines3 or SKRL or implement a custom RL method compatible to the gymnasium standard. Proceed with training the agent and save the resulting policy.
6 Evaluate performance of policy with inference
Running one inference episode with the trained agent while capturing trajectories. To statistically assess the trained agent's performance, the optimized policy can be deployed for multiple randomized episodes to evaluate the results.
License
This project is licensed under MIT license, as found in the LICENSE file.
Citing
If you use this project in your research, please cite:
@inproceedings{Wrede2024FMUGym,
author = {Wrede, Konstantin and Huang, Chenzi and Wohlfahrt, Tommy and Hartmann, Nick},
title = {{FMUGym}: An Interface for Reinforcement Learning-based Control of Functional Mock-up Units under Uncertainties},
booktitle = {31st International Workshop on Intelligent Computing in Engineering, EG-ICE 2024},
year = {2024},
pages = {647--656},
doi = {https://doi.org/10.35869/Proceedings_EGICE2024},
url = {https://publica.fraunhofer.de/entities/publication/d31204fd-3480-469f-9c45-5c376d81a721/fullmeta},
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fmugym-0.2.0.tar.gz.
File metadata
- Download URL: fmugym-0.2.0.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.10.18 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a0811e44d69c2444ebd530abf7d485e83b7a28b42179dba61301b3e3ea3878b
|
|
| MD5 |
c925772ffc91f24a8993241d76fd4852
|
|
| BLAKE2b-256 |
46d3d1c65c3123f58466bc997eba2b91ad37a66280b27cfa78dc94381e11080d
|
File details
Details for the file fmugym-0.2.0-py3-none-any.whl.
File metadata
- Download URL: fmugym-0.2.0-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.10.18 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ffb332f2f0c347cea7e7039f1e938b2aeec4a18f76e1a6879ade97749d7d98e
|
|
| MD5 |
bea68f561597b57fdb7388cab2e170a4
|
|
| BLAKE2b-256 |
19e8c2e3028dfc677221070c86589c112b71efad7252a1c440152859b8813c7f
|