GOPS to Simulink
Put the trained policy back into Simulink for closed-loop verification and deployment.
Prerequisites
MATLAB with Simulink.
We recommend the up-to-date MATLAB version. Since the bridge uses
pyrun, the minimum MATLAB version is R2021b.Python installation compatible with MATLAB.
Check MATLAB documentation Configure Your System to Use Python, especially “Versions of Python Compatible with MATLAB Products by Release” in “Related Topics” section.
Usage
Users can refer to the example of document
py2slx_example.pyto use the policy conversion tool.You need to set four parameters(
log_policy_dir_list、trained_policy_iteration_list、export_controller_name、save_path) according to the requirements of the reference example.Run the example file you configured above.
Py2slx tool will check the compatibility of the model to confirm whether it can be converted and whether user’s matlab version meets the requirements.
GOPS builtin models should work well without compatibility issue. If meeting something difficult, you could refer to PyTorch JIT documentation for requirements about
trace-able nn.Module.If matlab is not installed on your computer or the version is incorrect, the corresponding prompt will appear.
If everything is OK, a saved model will exist at
save_pathand the latest version of matlab on your computer will be opened.
Launch MATLAB
o ensure a successful execution, please make sure you have launched MATLAB within a Python environment that has PyTorch installed. If you encounter any issues, verify that
GOPSis also installed in the samePyTorchenvironment.If you prefer using your system-wide Python installation, you may launch MATLAB either from the shortcut or command line.
For the conda-based environment, the most efficient method would be to initiate MATLAB via the command line interface.
conda activate <YOUR_ENV_WITH_PYTORCH> matlab
When utilizing alternative types of environments, the activation method may differ. However, it is crucial to ensure that environment variables are accurate when initiating MATLAB.
To verify the environment is correct, you could type
pyenvin MATLAB Command Window. TheVersion,Executable,LibraryandHomefield of the result should match your target Python environment.Copy
gops_validation_bridge.mto your Simulink model directory. Create aLevel-2 MATLAB S-Functionblock, set:S-Function Namefield togops_validation_bridgeParametersfield to'save_path'(e.g.'model.pt'). The path is relative to your MATLAB working directory and an absolute path will also work.
The observation will be input into the block inlet, and the block outlet will output policy action from the trained policy.
Utilize the
Level-2 MATLAB S-Functionblock as the closed-loop controller in your Simulink model, then initiate the simulation and validation process.