question

jacopo-r avatar image
0 Likes"
jacopo-r asked jacopo-r commented

Running a python code with Cplex through FlexSim

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/ConnectingToExternalCode.html#python) 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


FlexSim 23.0.1
interaction between flexsim and pythoncplex
cplex-test.fsm (28.4 KiB)
cplex-test-1.py (906 B)
5 |100000

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

1 Answer

Kavika F avatar image
2 Likes"
Kavika F answered jacopo-r commented

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!


· 8
5 |100000

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

jacopo-r avatar image jacopo-r commented ·
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 0 ·
Kavika F avatar image Kavika F ♦ jacopo-r commented ·

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 0 ·
jacopo-r avatar image jacopo-r Kavika F ♦ commented ·
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 0 ·
Show more comments