How to connect simulink real-time(or step-by-step) control with python for RL

9 views (last 30 days)
I'm trying to connect simulink model with python to control the model with Reinforcement learning
For this, I need to simulate a few timesteps (for example, 0.01hr)
then python reads current states and sets the control value by using policy / also learning and optimizing policy
I am currently using the pause method with matlab engine API:
By using clock and assertion block, if simulation time reaches that specific interval(0.01hr), simulation pauses
python checks the simulation status constantly, and if simulation is paused, it reads state values, set control, learns
and then resume simulation.
This works quite well, but repeatedly pausing simulation spends too much time.
Doing 5hr simulation 6000 times takes 3 whole days.
Is there any way better/faster?
I tried TCP-IP connecting but I don't have lincense for corresponding toolbox and I think control action cannot be applied for every fixed timestep for its real-timedness.
Thanks in advance.
  2 Comments
Sijin
Sijin on 30 Nov 2022
Hi,
Did you find a solution for this? I am having a similar usecase, and currently using the pause to update the control action from Python. As you mentioned, it takes a lot of time for a meaningful number of training steps.
Please share if you have found some better solution.
Best Regards,
Sijin
Enzo Yacometti
Enzo Yacometti on 13 Dec 2022
Hello, if anyone has a solution to this I would be very interested. I'm currently also pausing and resuming constantly the simulation. Thank you.

Sign in to comment.

Accepted Answer

Pratik
Pratik on 3 Jan 2024
Hi 원혁 최,
As per my understanding, you want to connect a Simulink model with python to control the model with Reinforcement learning. Using the MATLAB engine API and pausing the Simulink model makes the whole process slow.
To run Python code in a Simulink model, a MATLAB function block or a MATLAB System block can be used. To call Python modules in MATLAB, there should be a supported version of the reference implementation (CPython) installed on the system.
Please refer to the following documentation for an example usage of a MATLAB function block and a MATLAB System block:
Please refer to this question on MATLAB answers for more information:
I Hope this helps.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!