question

wtj-thu avatar image
0 Likes"
wtj-thu asked Andrew O commented

How to realize real-time interaction between python and Flexsim HC?

Hello everyone,

I want to realize real-time interaction between python and Flexsim HC and realize the control of patient flow. Specifically, Flexsim HC exports the number of people at each location instantly to python, python calculates the flow control strategy at the next moment, and the strategy can change parameters instantly, for example, arrivals or transition probabilities will be changed in order to achieve the optimization of a certain parameter. while now, I can only export the number of people to excel after a simulation process, which is a relatively basic function.

I have searched for some information. Flexsim has complete Reinforcement learning interface with python but the interface is only applied to Reinforcement learning algorithm. There are also some procedures which can realize the control of Flexsim, but most are limited to the reset, run and stop of simulation while I want to change the parameters when the simulation is running.

I want to ask that is there any solution to my question and is there any sample code that can be run. I’m not a professional programmer and new to Flexsim so I’m looking forward to an easy-to-understand solution.

I will be very grateful to your reply. If you want to reply to my question, you can also ask for more detailed information. Thank you.

FlexSim 22.2.3
interaction between python and flexsim hc
· 1
5 |100000

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

Andrew O avatar image Andrew O commented ·

Hi @wtj-thu, was Kavika F's answer helpful? If so, please click the "Accept" button at the bottom of their answer. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always unaccept and comment back to reopen your question.

0 Likes 0 ·

1 Answer

Kavika F avatar image
0 Likes"
Kavika F answered Kavika F commented

Hey @wtj_thu, here's a link to some documentation on connecting to Python. Also, here's an in-depth example I made for someone wanting to write to a Global Table using Python.

Here are some more example files you could check out. Key take aways:

  • Make sure you have Python installed and the correct Python version selected in your Global Settings --> Code tab (and that Python is in your PATH environment variables)
  • Create a User Command that links to your Python function
  • Use that command where you want to run your Python code

In these example files, I'm simply adding two numbers together and saving the sum to a token label.

Python Example.fsm

add.py


python-example.fsm (28.9 KiB)
add.py (36 B)
· 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.

wtj-thu avatar image wtj-thu commented ·

Thank you for your reply. And I still have two questions.

The first question is I can't open your link in-depth example I made .

The second question is that there is error when running Python Example.fsm .

error.png

And I have finished the first step.

  • Make sure you have Python installed and the correct Python version selected in your Global Settings --> Code tab (and that Python is in your PATH environment variables)python.jpg

I don't know whether I have to do something for the other 2 steps.

I want to ask how to make this run successfully. Thank you!

0 Likes 0 ·
error.png (8.6 KiB)
python.jpg (258.1 KiB)
Kavika F avatar image Kavika F ♦ wtj-thu commented ·

My apologies, that question was made private. I just made it public again so you can view my explanation.

Error Code 5 is a Windows error code that appears when the user does not have sufficient permission to access the requested file or location. Have you given that file "trust" after downloading it? Sometimes files will enter a "safe mode" after being downloaded to mitigate potential threats.

If you then get an Error Code 0, it's probably because your script window in Process Flow is set to External when it should be set to FlexScript:

1670861521455.png

Also, make sure that your User Command's custom code is set to External for Python.

0 Likes 0 ·
1670861521455.png (1.4 KiB)