# Create a simulink model fro, python

12 views (last 30 days)
Lucas on 15 May 2024
Answered: Amish on 27 May 2024
Hello,
I would like to create an algorithm using evolutionnary strategies in order to identify physical systems represented by block diagrams. To do so, I will need to design an algorithm where a population of systems is modified (not only the parameters but also the structure) and tested at each iteration. I need to simulate the resulting population and in order to do that I need to be able to change one block of the model in simulink, which would be a sub system with different transfer functions. I don't know simulink very well so is there a way to crate a custom block, to simulate the model from python and to change one part of the model at each run?

Amish on 27 May 2024
Hi Lucas,
I see that you want to design an algorithm using evolutionary strategies to identify physical systems represented by block diagrams, and interfacing this with Simulink using python. In order to do this, I suggest that you familiarize yourself with some basics of Simulink by using the Simulink Onramp course that is accessible at: https://matlabacademy.mathworks.com/details/simulink-onramp/simulink .
You can then create a custom block in Simulink that represents the subsystem you wish to evolve. There are several ways to create custom blocks:
MATLAB Function Block: This is useful for writing MATLAB code for dynamic systems. YOu can additionally, write a python script or module that is called from the MATLAB Function block as follows:
function y = fcn(x)
y = py.module_name.function_name(x); % Replace `module_name` and `function_name` with your Python module and function
end
S-Function Block: This is used for more complex interactions (and written in C/C++ or MATLAB) however, for performance-critical applications, you might write a C/C++ S-Function that calls Python code using the Python/C API.
Simulink Subsystem Block: This is a straight forward approach involving grouping of a set of blocks into a subsystem, which can then be reused as a block. You can then encapsulate different transfer functions within subsystems as per need.
As for the Simulation part of the Evloution algorithm, MATLAB offers an API for Python that allows you to interact with MATLAB from Python scripts. A generic python code implementing the MATLAB API can be found below:
import matlab.engine
# Start MATLAB engine
eng = matlab.engine.start_matlab()
model = 'your_model_name' # Replace with your model's name
# Example: Set a parameter. Replace 'block_name' and 'parameter_name' with actual names.
eng.set_param(f'{model}/block_name', 'parameter_name', 'new_value')
# Run the simulation
eng.sim(model)
# Optionally, get data from MATLAB to Python for analysis
output = eng.workspace['your_output_variable']
# Stop the MATLAB engine
eng.quit()
Lastly, evolving the model structure would involve a complex logic based on interations and evaluation functions that you have. The algorithm will need to define how structures can change.
This can be implemented by representing your Simulink model's structure in a format that's easy to manipulate in Python (e.g., a graph or a tree), then apply evolutionary operations on this representation. After each modification, you will need to translate this structure back into a Simulink model or modify the existing model accordingly.
Note that the above approach may introduce several complexities and careful management of model files will be necessary to ensure stability.
Here are some documentations that might be helpful for you:
Hope this helps!