question

Ryosuke S avatar image
0 Likes"
Ryosuke S asked Jing_69 commented

Using FlexSim with Python/C api

Hello! After having discussion with one of your colleague, I have found out that you can use python in FlexSim by Python/C API. However, to confess, I'm not fond in this field. Do you have any kind of manual or note to integrate Python/C to FlexSim? I'd really appreciate if you can also provide some kind of sample project, model,etc. Thank you in advance.

FlexSim 20.2.2
flexsim 20.2.2module sdkpython
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Phil BoBo avatar image
0 Likes"
Phil BoBo answered Ryosuke S commented

Download the Module SDK from the Account section of the website. Follow its embedded documentation on how to use it to create a FlexSim Module and compile a module dll.

Once you can compile a module dll, follow the instructions at https://docs.python.org/3/extending/embedding.html for executing Python from C++ code.

When you install Python, it includes a bunch of header files (including Python.h), lib files (including python3.lib), and dlls (including python3.dll). You will use these in your Module DLL project to interact with Python.

You are unlikely to find someone who has already done this and can share a sample project/model because executing Python from C is rarely helpful. Most 3rd party code libraries are already written in C or C++, so you can connect directly to them. Usually those libraries have Python wrappers so that you can call them from Python. If you are already in C++, then you don't need to use Python wrapper libraries. You can call them directly from C++ instead of going from C++ to Python back to C.

Is there a particular library that you are trying to use, or is this simply an academic exercise to see what is possible?


1603121043564.png (43.3 KiB)
· 6
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Ryosuke S avatar image Ryosuke S commented ·

@Phil BoBo, thank you for your reply. This is going to take sometime for me to create the environment on my PC. In the customer side, they are using their original AI engine, so we don't know how they are created. I just know that they are using Python and want to access to FlexSim with Python. The sample I would like is to test how we can pass parameter value defined in Python to "Processor's process time" for example as a proof.

0 Likes 0 ·
Phil BoBo avatar image Phil BoBo ♦♦ Ryosuke S commented ·

Wanting to access FlexSim from Python is very different than wanting to use/integrate Python in FlexSim. Python is a programming language, and FlexSim is an application.

You can communicate with FlexSim from Python using IPC (inter-process communication) technologies, such as sockets or the FlexSim WebServer.

See https://docs.python.org/3/howto/sockets.html for documentation on using sockets with Python.

See https://archive.flexsim.com/threads/thread3186.html for FlexSim client and server socket examples using FlexScript.

In addition to the FlexScript socket commands, you could also use the DLL Maker or Module SDK to write your own C++ socket code if you want direct access and control over exactly what is happening.

If you just want to call FlexSim from Python and set values, such as a processor's processing time, you can run the FlexSim application from Python and pass it command-line arguments, such as FlexScript code to execute. See https://answers.flexsim.com/answers/21375/view.html and https://docs.python.org/3/library/subprocess.html.

If you want to communicate repeatedly with an already-open FlexSim, then you'll need to use some method of IPC, such as using sockets for a direct connection or using the FlexSim WebServer for communication over HTTP.

See https://docs.python.org/3/library/http.client.html for Python documentation about communicating with a server via HTTP and https://docs.flexsim.com/en/20.2/Reference/DeveloperAdvancedUser/Webserver/#custom for documentation on responding to custom web queries using the FlexSim WebServer.

0 Likes 0 ·
Ryosuke S avatar image Ryosuke S Phil BoBo ♦♦ commented ·

@Phil BoBo, Thank you for your reply. Great! It's nice to know there are several ways. So then, which one do you suggest? The most simplest way to accomplish what I want to do for now. Also, I would like to know what I need to install to my pc, what procedure I need to take to accomplish it. I have gone through the python docs, but are not helpful enough for a beginner. I don't nee any fancy document, but a note describing steps when you tried on your side perhaps? Thank you in advance.

0 Likes 0 ·
Show more comments
Benjamin W2 avatar image Benjamin W2 commented ·

@Ryosuke S,

Please also see the following library and youtube video:

https://pybind11.readthedocs.io/en/stable/

https://www.youtube.com/watch?v=-eIkUnCLMFc

Also consider passing information using a database.

0 Likes 0 ·
Ryosuke S avatar image Ryosuke S commented ·

@Phil BoBo, @Benjamin W2 Thank you for all your help, we were able to integrate flexsim with python.

0 Likes 0 ·
Jing_69 avatar image
0 Likes"
Jing_69 answered Jing_69 commented

@Ryosuke S Hello, we have the same question about how to integrate flexsim with Python, and we know that you've solved it. Could you please tell us the solutions or give some notes. Thank you so much!

· 3
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Ryosuke S avatar image Ryosuke S commented ·

@Jing_69 hello! as in @Jason Lightfoot reply, now that in FlexSim2022, we have ui for reinforcement learning, I think you should use this, because what we have done is almost similar to this.

Thanks to the FlexSim development team, that this feature is available now.

0 Likes 0 ·
Jing_69 avatar image Jing_69 commented ·
@Jason Lightfoot @Ryosuke S Thank you so much for your reply!
0 Likes 0 ·