question

Jonas Fl avatar image
6 Likes"
Jonas Fl asked Phil BoBo commented

Future Python and FlexSim integration

I have seen many posts in the forum regarding Python and Flexim integration.


After reading most of them, my understanding is that:

- There's no "easy" way of connecting Python with Flexim (with easy I mean without having to master client-server connections, dlls, etc).

- Your FlexSim Development Team is working towards easing this integration, but specifically for Reinforcement Learning (RL) applications.


My question is:

Are you planning, maybe for the near future, to provide a "standard" way (for non-programmers, not limited to RL) to connect Python and FlexSim?


In my opinion, having this possibility would facilitate very much the work of simulation engineers and data scientist both in industrial and in academic environments, where Python is becoming ubiquitous, and where there is not much time to be invested in the connection side of the problem (but rather on the python code or on the simulation).


Specifically, what I have in mind regarding the connection between FlexSim-Python, is the academic concept of "simheuristics": https://www.sciencedirect.com/science/article/pii/S221471601500007X. In summary, "simheuristics" require the simulation (FlexSim) and some algorithm/metaheuristic (written in Python) to work closely together, i.e passing information back and forth seamlessly (simulation outputs, re-starting the simulation at certain stages, updating objects...).


I am not assuming that any of this is easy to implement, but this "question" tries only to encourage the development of the software in this direction should you also consider it of interest.


Thank you very much in advance and congratulations for the good work.

FlexSim 21.2.4
python
· 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.

Phil BoBo avatar image Phil BoBo ♦♦ commented ·

Your question is if FlexSim is planning to provide a way for non-programmers to connect Python (a programming language) to FlexSim (an application)?

Python is a programming language. What do you envision is a "standard" way to connect from a programming language to an application other than programming code?

It takes 2 lines of code (not limited to RL) to launch FlexSim from Python:

args = [flexsimPath, modelPath]
flexsimProcess = subprocess.Popen(args)
1 Like 1 ·
Jonas Fl avatar image Jonas Fl Phil BoBo ♦♦ commented ·

Hello Phil.

Thank you for your prompt answer. I must apologize since I think my question wasn't formulated in the best way possible. Definitively both are to be connected through programming code.

When I said a "non-programmer" I mean somebody that doesn't hold a Computer Science degree (so he/she would be expected to know about networks, sockets, os, spawning, app architecture, a couple or three programming languages, and what not). I meant people coming from other backgrounds (which could be engineering, maths, physics...) and intend to use a simulation tool, in combination with Python (some level of programing is obviously required). I think this is not a strange situation nowadays in industry or in academia.

That being said, what I meant by a "standard way of connecting", is probably a documented API that allows access and control of FlexSim directly from Python code. Does FlexSim provides this?

I think in your answer to the question below you gave many options on how to connect Python-FlexSim:

https://answers.flexsim.com/questions/92218/using-flexsim-with-pythonc-api.html

Also, you provided a python socket example to communicate with FlexSim through FlexScript here:

https://answers.flexsim.com/questions/109364/c-or-c-or-python.html

I think these options would work, but I was overwhelmed by the complexity of the procedure (blame is obviously on me).

Again, sorry if the question wasn't clear. I am sure that there are many way to connect Python and FlexSim, just wanted to know if the "entry cost" was as high as I thought.

Thanks and apologies once again.

2 Likes 2 ·
Phil BoBo avatar image Phil BoBo ♦♦ Jonas Fl commented ·

That makes more sense to me. Thanks.

In writing the code in https://answers.flexsim.com/questions/109364/c-or-c-or-python.html, I first implemented a direct connection to the flexsim.dll with an API for communicating directly with it from Python. This works well if you want to communicate with FlexSim immediately in-line within your Python code. Doing so makes it extremely difficult and complicated to see the updates in FlexSim though, as FlexSim then runs as a sub-process of the Python script rather than an application with its own event loop for processing GUIs messages, interacting with the mouse and keyboard, repainting views, etc.

The socket communication code was actually simpler for non-programmers than trying to deal with the application directly. The socket code is honestly not very complicated. The main complication is that I only posted the Python side of that code. An example with both sides of the communication (FlexScript and Python) boiled down to the basics instead of showing an RL example would show how simple this type of communication is. Directly accessing FlexSim is quite a bit more complicated than inter-process communication with sockets.

So it isn't out the question that we could create a C API that can be directly accessed from Python to execute functions in FlexSim, and we could also make it easy to call Python functions from FlexSim (similar to calling C++ DLL functions right now). Neither of those features seem to address your original question though (and were explicitly excluded by your original question).

Communication between Python and FlexSim is going to require some amount of programming though, as Python is a programming language.

2 Likes 2 ·
anthony.johnson avatar image
6 Likes"
anthony.johnson answered Jon Abbott commented

I agree that this is a good application of simulation, and many industrial engineers are gravitating toward using python to solve these questions. Over the past week or so I have been thinking myself about the problem of AGV dispatching/scheduling, and have thrown around the idea of creating an example in FlexSim of a tabu search method for optimizing the scheduling of AGVs within some time horizon. As I thought about implementing this, the question came, should I implement the algorithm in FlexScript, or would it be more beneficial to implement the tabu search in a commonly used language like python? The advantage of implementing it in python is that, once the algorithm is written and optimized (and if I have implemented it properly) it would be relatively easy to then plug that python tabu search algorithm into a live system. But, yes, as Phil mentioned, I don't think you're going to get this without some programming, as, obviously, python is a programming language.

The key, though, is implementing the python algorithm such that it is host-agnostic. In other words, the python algorithm needs to not assume that it is communicating with FlexSim, but that there is just some host feeding it information, and it processes that information, and gives back decisions. Thus, it should not use FlexSim-specific types. I think this would be relatively easy to do, because most of FlexSim's Variant types have direct corollaries in python. Flexsim's Map is essentially a python dict. FlexSim's Array is essentially a python list. FlexSim's string is essentially a python str, and so on. The one type that might not translate well would be node references. So the simulation story would be, FlexSim runs my AGV model. At some point in the model, FlexSim needs to schedule AGV job assignment out for the next 20-30 minuts. FlexSim calls the python algorithm, and it passes one or more Maps, Arrays, strings, or numbers into the function, that give enough information to the python algorithm regarding what needs to be scheduled. The FlexSim engine automatically translates the parameters passing into the function into native python types, then calls the function. The algorithm then executes, and gives back one of the same various types. Then FlexSim's engine then translates back into FlexSim-native types, and the function returns.

Anyway, this is something we definitely want to look into for future FlexSim development. It fits well as a digital twin strategy, where FlexSim becomes a testing application for real algorithms that will be deployed in live systems.

· 2
5 |100000

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

anthony.johnson avatar image anthony.johnson ♦♦ commented ·

Jonas,

I recently got a proof of concept working, wherein a python function is called from within FlexSim, to make a decision as to which of multiple items to pull from an upstream queue. Hopefully in the next release cycle (22.1) I can get this to a releasable state so you can use it as well.

With that said, obviously there are still questions to answer as to the "orientation" of python scripting using FlexSim. Phil's implementation of RL takes the approach that python should completely control FlexSim, i.e. it is responsible for spawning instances of FlexSim, running them, obtaining result data, etc. This approach uses inter-process communication to control FlexSim. On the other hand, my implementation takes the approach that FlexSim "controls" python, i.e. certain python functions and algorithms are called from within FlexSim, and make decisions that direct the simulation. In my approach, FlexSim is technically the "controlling" application, and python services requests from the controlling application. In Phil's approach, python is the "controlling" application, and FlexSim services requests from the controlling application.

In the end, either one of these approaches can be useful, depending on the modeling situation. Our task as developers is to make both approaches as intuitive and seamless as possible, and perhaps to even let users use both in conjunction.

pythonworks.gif

3 Likes 3 ·
pythonworks.gif (374.2 KiB)
Jon Abbott avatar image Jon Abbott anthony.johnson ♦♦ commented ·
@anthony.johnson - great work. I second the creation of the "FlexSim controlling Python" functionality - this would be very beneficial.
0 Likes 0 ·
alan.zhang avatar image
3 Likes"
alan.zhang answered Phil BoBo commented

This a really nice feature. Just tested in the newest FlexSim 22 Update 1 with a very simple model. It works pretty well. Model and Python file are attached. Put them under the same directory to test it. Great work from FlexSim!

python-test.fsm

PyMod.py


python-test.fsm (27.6 KiB)
pymod.py (34 B)
· 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.

pc neo avatar image pc neo commented ·
Thank you! It works well ! :)


Just a note: the downloaded file has its file name in lowercase (pymod.py), so the module name in the python_test() code need to be change from PyMode.py to pymode.py.

1 Like 1 ·
Joerg Vogel avatar image Joerg Vogel commented ·
May I suggest, you consider to convert this thread into an article. Many thanks! Joerg
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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