FlexSimPy - run simulation from python

FlexSimPy - run simulation from python

Hanifi_Yilmaz
Not applicable
2,854 Views
9 Replies
Message 1 of 10

FlexSimPy - run simulation from python

Hanifi_Yilmaz
Not applicable

[ FlexSim 22.2.4 ]

I just extended a genetic algorithm with flexsim. Now i have two problems, where i dont know where it comes from.

1.) The model gets "destroyed" every time it finishes an optimization process. Flexsim opens a script console for every run, i dont know why.

- Do I have to open the model in every run or just once at the start?

- When do i have to stop the controller? Only in the end or for every solution in the optimization?


1678795268870.png

2.) Python automatically restarts the script: I dont know if this problem has to do with flexsim or python. After some iterations, i always get the information "Restart python shell". Do you know where it can come from?

1678795588162.png

Thanks.

0 Likes
Accepted solutions (1)
2,855 Views
9 Replies
Replies (9)
Message 2 of 10

kavika_faleumu
Autodesk
Autodesk
Accepted solution
Hey @Hanifi.Yilmaz, from the looks of your screenshot, it is opening a System Console each run, which makes me think that there's an error in your model that is trying to print a statement.

If you have no license, then you can't run an optimization job with the experimenter. You also can't run any scripts within FlexSim.

0 Likes
Message 3 of 10

Hanifi_Yilmaz
Not applicable

Hey @Kavika F


I have a license and i dont use the experimenter at the Moment. The Problem is deterministic.

Even when i set every solution from the algorithm manually in Flexsim, i got the same result as in Python. It solves the Problem, but destroys the Model. I use the Controller.setParameter(name,value)method to set the parameters in flexsim. Maybe the problem is the way of using the methods of flexsimpy. Do i have To open the script only once? And what does the „Controller.stop()“ methods exactly do?


Another question: Is it possible to start the Experimenter with FlexSimPy to make MC Simulations? My next step is to solve a stochastic problem.

Thanks

0 Likes
Message 4 of 10

Hanifi_Yilmaz
Not applicable
Update: I solved the first problem. My mistake was that i opened the script in every iteration instead of opening it once. For the other (2) problem, i dont have a solution.. The script always restarts itself. @Kavika F
0 Likes
Message 5 of 10

kavika_faleumu
Autodesk
Autodesk
Is there a loop in your code calling itself? Could you post it so I could see what could be happening?
0 Likes
Message 6 of 10

Hanifi_Yilmaz
Not applicable

I just debugged the script multiple times and i know where the error comes from now. I have a list in python with values from [1...30]. Also i have 30 paramaters in flexsim and i set a value to every parameter with python. And this is the exact line where the script restarts hisself. Is there another way to give an array directly from python to flexsim? I saw this "send" and "receive" labels in the TestFlexsimPy model which sends and gets values from the model. But i dont understand how you implemented this treenodes and what this exactly does.1678976805055.png

0 Likes
Message 7 of 10

kavika_faleumu
Autodesk
Autodesk
You could have the function return an array; FlexSim can receive the array through a function call. However, with that logic, you could just make a User Command (without a Python connection) to perform your desired calculation. Either way, here's a link to setting up a Python Connected User Command if you need it.
0 Likes
Message 8 of 10

Hanifi_Yilmaz
Not applicable

Hey @Kavika F ,


i just created a dummy python script and a dummy flexsim model. Maybe you can check, if you have this problem too.

TestModelForum.fsmTestScript.py

1679055615949.png

0 Likes
Message 9 of 10

kavika_faleumu
Autodesk
Autodesk

Hey @Hanifi.Yilmaz, I wasn't able to replicate your issue with the test module and model you sent, but I can help you try to understand the PythonModule code a bit better. I actually recently comment on a post referring to the methods the controller and model can call.

65880-1679097754270.pngIf you want to send data from Python to FlexSim, you can use something like

controller.send(myArray)

to send your array data. On the FlexSim side, you can make a User Command that references a Python Function. That function can reference the FlexSimPy file (should be FlexSimPy.pyd) and the function should match either sendToController or receiveFromController. They need to be matched with controller.receive() or controller.send() respectively on the Python side.

1679100031077.png

Above is an example you can follow. I made a User Command called "Receive" that returns a num (assuming I'm only sending numbers from my Python code; you may want to change it to Variant to accept arrays). The Code section says "external python:". That's because at the bottom of the code editor, you can select the "E" (for "external"), then "Python". This lets you set a python file name ("FlexSimPy") and a function from that file ("receiveFromController"). Then somewhere else in my code I simply have to say

Variant var = Receive();
// do something with

Caution: things may hang if you don't have a sender/receiver on each side.

Examine the FlexSimPy\TestFlexSimPy folder, specifically the TestFlexSimPy.py and TestSendReceive.fsm files in the FlexSimPy repository. The code in the Processor labels may help you understand how they all interact.

0 Likes
Message 10 of 10

jason_lightfoot_adsk
Autodesk
Autodesk

Hi @Hanifi.Yilmaz, were Kavika's comments helpful? If so, please let us know and we'll convert them to an accepted 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