Bayesian Optimization in real-time with Simulink and Code Generation
29 views (last 30 days)
Show older comments
For a particular application, I need to implement Bayesian Optimization so to continuously run in a Simulink model. The simulink model is a control algorithm, which has to be translated into C code, and run on an ECU. The Bayesian Optimization should update the controller parameters (which are the parameters to be optimized) based on a certain cost function, which is directly measured from some sensors available to the ECU. So in practice my cost function should "wait" for a flag "experiment completed" coming from the simulink model; after the flag is generated, the cost function is evaluated based on the data.
More in details, the algorithm idea is something like this:
- Perform the experiment by using a certain set of parameters.
- After the experiment is done, the cost function for that set of parameters can be evaluated.
- Run an iteration of bayesopt() and update the GP model, etc. A new set of parameters is selected through acquisition function.
- The experiment is then performed again, and the new cost function is evaluated, and so on...
Now, I know that I can't directly use bayesopt() in code generation. Is there a way to do this without having to completely rewrite the code for the Bayesian Optimization?
1 Comment
Yanick Büchel
on 8 Aug 2024 at 9:14
Edited: Yanick Büchel
on 8 Aug 2024 at 9:14
Hi Federico,
I have a very similar problem. Have you solved it? Would be interested to see your approach.
Answers (1)
Kanishk
on 9 Aug 2024 at 9:52
I understand the algorithm you are trying to implement and as ‘bayesopt’ cannot be used in codegen, here are two alternatives which you can use:
1. Separate code for control Algorithm in Simulink and Bayes Optimization in MATLAB
You can deploy the control algorithm using Simulink and set up a communication interface between MATLAB and the ECU.
The MATLAB script will handle the Bayesian Optimization process.
- Establish communication between MATLAB and the ECU using either Serial (UART) or TCP/IP protocol.
- The MATLAB script will read sensor data and an "experiment completed" flag from the ECU.
- Based on the data, the MATLAB script will set new parameters for the control algorithm.
You can setup communication using Serial port(UART) or TCP/IP protocol. Here are the MATLAB documentations for the same.
a. Serial Communication (UART)
- https://www.mathworks.com/help/matlab/serial-port-devices.html
- https://www.mathworks.com/help/matlab/ref/serial.html
b. TCP/IP Communication
- https://www.mathworks.com/help/matlab/tcpip-communication.html
- https://www.mathworks.com/help/matlab/ref/tcpclient.html
2. Implement a Simplified Version of ‘bayesopt’ for Code Generation
If communication overhead and dependency on a connected system are concerns, you can implement a simplified version of the ‘bayesopt’ function tailored to your needs. This can be integrated directly into Simulink using a MATLAB Function block, making it suitable for code generation.
Hope this helps!!
0 Comments
See Also
Categories
Find more on Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!