Bayesian Optimization in real-time with Simulink and Code Generation
조회 수: 29 (최근 30일)
이전 댓글 표시
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
Yanick Büchel
2024년 8월 8일 9:14
편집: Yanick Büchel
2024년 8월 8일 9:14
Hi Federico,
I have a very similar problem. Have you solved it? Would be interested to see your approach.
답변 (1개)
Kanishk
2024년 8월 9일 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
참고 항목
카테고리
Help Center 및 File Exchange에서 Waveform Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!