Running a python code with Cplex through FlexSim

Running a python code with Cplex through FlexSim

jacopo_r
Not applicable
2,632 Views
9 Replies
Message 1 of 10

Running a python code with Cplex through FlexSim

jacopo_r
Not applicable

[ FlexSim 23.0.1 ]

Hi all,

I am trying to run Python through FlexSim. I managed to get the connection between python and flexsim (following this https://docs.flexsim.com/en/22.2/Reference/DeveloperAdvancedUser/ConnectingToExternalCode/Connecting...) and now I want to run a python code that has a cplex optimization algorithm in it. For the moment I am not feeding any tables or parameters, I want to execute the code and return "1" to get a proof that the code went through. It is not working.

I know the code works because I ran it with Jupyter Notebook.
Can anyone help me figuring out what am I missing?

Also, what would be the next step in order to be able to feed a table to the python code and return to flexsim the x[i,j] variable that the cplex calculated?

(Flexsim model and python code attached)cplex_test.fsmCplex_test_1.py

Thanks


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

kavika_faleumu
Autodesk
Autodesk
Accepted solution

Hey @jacopo_r, I found that the issue was that your file name was using underscores in the code but it was named with dashes in the file explorer.

1684184234258.png

1684184243727.png

If you edit it to match, then it will work.

Alternate ways to connect:

If you scroll up a little on that same doc page, it will point you to the "External" button which will format the code for Python for you.

1684183933295.png

1684183940132.png

If you'd like a more detailed guide, you could check out this post. Ensure that in your Global Preferences you have the correct Python version selected as well.

1684183960402.png

Hope this helps!

Message 3 of 10

jacopo_r
Not applicable
Hi @Kavika F , thanks for your answer.

I did what you suggested but the value I got from the running of the python script is "NULL", while I put a "return 1" at the end of the cplex model just to see if I can actually return something. Does it return "1" to you when you run it?
Can it be that I miss some settings on FlexSim about Cplex?

0 Likes
Message 4 of 10

kavika_faleumu
Autodesk
Autodesk

I got NULL once or twice while testing things out. I think it was because I didn't have the Cplex module installed for my desired version of Python. I had to run

py -3.10 -m pip install cplex

in my windows PowerShell to make sure that it was installed for the correct version of Python because I have 3.10 and 3.11.

0 Likes
Message 5 of 10

jacopo_r
Not applicable
So, as far as you know, there is nothing specific to do in order to make sure that FlexSim can properly access Cplex?

Thanks again

0 Likes
Message 6 of 10

kavika_faleumu
Autodesk
Autodesk
Not that I know of. After installing all the modules for Python 3.10, making sure that my Global Settings > Code tab was set to use Python 3.10, and fixing the names to match, I was getting a 1 as a label for my token. I also tried returning the command in the script console and I got a 1 as well.
0 Likes
Message 7 of 10

jacopo_r
Not applicable
ok, thanks.

Could you upload the two files you have tested, please? I know they are exactly the same I uploaded, but I want to try also this option and see if something went wrong in mine at some point.
Thanks

0 Likes
Message 8 of 10

jacopo_r
Not applicable
@Kavika F I also have another request if possible.

I basically need to feed the python code with a table from Flexsim, run the cplex, and return (possibly) another table with the assignment of different tasks. I am not quite sure how to do that. I read you post about using python for creating a table and return it to Flexsim and populate it. But what about how python reads a flexsim table?

Thank you in advance

0 Likes
Message 9 of 10

kavika_faleumu
Autodesk
Autodesk

Here are the two files I used to get the return value of 1 out of your original question.

cplex-test_1.fsm

cplex-test-1.py


You can also upload Global Tables to a Python script. The clone() function of a Table returns an Array, which can be a parameter in a User Command. After hooking up a python function to the User Command, you can pass in a Global Table's clone and get a result. Here's an example of it working (I'll post the files below as well):

1684342844821.png

1684342879411.png

1684342980474.png

1684342836340.png

cplex-test_2.fsm

global-table-1.py

0 Likes
Message 10 of 10

jacopo_r
Not applicable
ok, thank you for the answer and the explanation. I'll try to implement this logic :)
0 Likes