Step-by-step guide for installing Cplex and use it in Flexsim

Step-by-step guide for installing Cplex and use it in Flexsim

jacopo_r
Not applicable
208 Views
20 Replies
Message 1 of 21

Step-by-step guide for installing Cplex and use it in Flexsim

jacopo_r
Not applicable

[ FlexSim 23.0.1 ]

Hi everyone,

I am currently struggling with using Cplex (in a python code) through FlexSim.

I managed to get the connection between FlexSim and Python working and I tested it out with a simple code returning the sum of two numbers I give as parameters, and that works.

I installed Cplex and if I run a python code with a cplex model in Jupyter Notebook it works, but when I try to call that function from FlexSim it does not. I started to think that there could be something wrong "behind the scene". I read something about setting the environment variables properly and stuff like that. I have to admit that I am not an expert in this kind of things, so I decided to look for someone very patient and willing to explain step by step how I should install and make sure that cplex is properly set for FlexSim.

Thank you all in advance 🙂

0 Likes
Accepted solutions (1)
209 Views
20 Replies
Replies (20)
Message 2 of 21

kavika_faleumu
Autodesk
Autodesk
Accepted solution

Hey @jacopo_r, I have a few questions that may help me identify your issue.

1. What version(s) of Python is installed on your machine? You can identify this by opening up Windows PowerShell and typing

py -0

This will print out the list of python versions you have installed on your Windows machine.

2. Is Python in your System/User Variables? You can find this by going to System Properties (which you can find by simply searching for "Environment Variables").1684343999641.png

After clicking the option above to edit system environment variables, you'll get the window below.

1684344378935.png

Click the Environment Variables button and another window will pop up with those variables. In your User variables and System variables, you'll see variables called "Path". Double click on each of them and look for paths related to Python. For example:

%APPDATA%\..\Local\Programs\Python\<PYTHON_VERSION>\Scripts

Or something along those lines. You can also go to that location by putting the address in your File Explorer to see if there's actually anything there.

3. Does your FlexSim Global Preferences use the same Python version as the one you have installed? You can check this by opening FlexSim, going to Global Preferences, selecting the "Code" tab, and checking your Python Version.

1684344631789.png

Message 3 of 21

jacopo_r
Not applicable

Hi @Kavika F ,

my python version is 3.10 and it is in line with the python version I selected in the Global Preferences of Flexsim. However, I don't think I have any Environment Variable related to Python either in the User or System variables. I can see I have variables related to Cplex in the system variables, and the only one containing the name python is the one below, which is the only one called " Path".
1684398489231.png(I had to take a screenshot cause I couldn't copy it, but if you open the image file it should be readable)

(I can see the name Gurobi in the path. I installed Gurobi but I am not actually using it cause I switch to Cplex)

So, I guess this could be at least one issue.

Looking forward to hear back from you!

Thank you very much!

0 Likes
Message 4 of 21

jacopo_r
Not applicable

I forgot to mention that even though there are no Python Path Variables related, I managed to connect FlexSim with Python and run a simple sum or feeding a global table to python and return the sum of rows (as you suggested in here https://answers.flexsim.com/questions/141940/running-a-python-code-with-cplex-through-flexsim.html). But when I try to run a python script including a cplex optimization model, then it does not work.

🙂

0 Likes
Message 5 of 21

kavika_faleumu
Autodesk
Autodesk

Paths are separated by semicolons; I can see that Python actually is in your path. That makes sense since you can use it for FlexSim already.

The only thing I can think of is that maybe your python version doesn't have the docplex or cplex libraries installed. Have you tried opening a PowerShell and running

pip install docplex

and

pip install cplex

? What does it say after you run those? If they are installed, then it should say something along the lines of "you already have this installed" or "this library is up to date". If not, then it will download them.

0 Likes
Message 6 of 21

jacopo_r
Not applicable

Everything seems fine. See picture below:

1684435421111.png

I tried also to see if Chat GTP has something to say that makes sense.

I got this:

FlexSim has the ability to execute Python code, but it cannot directly interact with Python libraries such as CPLEX. Instead, it runs Python code through the Python interpreter installed on your system.

Here are a few potential reasons why your CPLEX model may not be executing properly:

You might be using a different Python environment in FlexSim and outside of it. If you installed CPLEX in a specific Python environment, but FlexSim is using a different one, then FlexSim wouldn't be able to access CPLEX. To solve this, ensure that FlexSim is using the correct Python interpreter. You can specify the path to the Python interpreter in the FlexSim settings.

There might be a problem with your CPLEX installation. Try running your CPLEX Python code outside of FlexSim to verify that it works correctly.

There might be a problem with your Python code. Check the FlexSim Console or Output Console for any error messages that can help you troubleshoot the issue.


About this I can say that Cplex works when I run the Pyhton script with Jupyter Notebook. However, it could make sense that Flexsim is not able to access the same Python environment, but on the other hand I think a normal a+b function run in Python from Flexsim wouldn't work either.

Does this ring a bell maybe?

0 Likes
Message 7 of 21

kavika_faleumu
Autodesk
Autodesk
What values, exceptions, or errors are you getting when you run your FlexSim Model? If you put
test_cplex()

in a script window, what does it return? Maybe the output (or lack thereof) can give me a hint as to what's going on.

0 Likes
Message 8 of 21

jacopo_r
Not applicable

I just notice that it give the error "name 'np' is not defined".

Maybe it is related to the numpy package that I have in this example but I don't think I really needed it for the model I am working on. I am currently trying to run a pyhton script with cplex, but without numpy and feeding a table myself from Flexsim.

Could it be it?

0 Likes
Message 9 of 21

kavika_faleumu
Autodesk
Autodesk
If your python script imports numpy, then you'll need to make sure the numpy package is installed with your Python 3.10 version. Could you try pip installing numpy in a PowerShell to see if it is there?
0 Likes
Message 10 of 21

jacopo_r
Not applicable

1684442344343.png

it seems like it's there

0 Likes
Message 11 of 21

kavika_faleumu
Autodesk
Autodesk

Is FlexSim giving you the error "name 'np' is not defined"?

0 Likes
Message 12 of 21

jacopo_r
Not applicable
yes it is
0 Likes
Message 13 of 21

kavika_faleumu
Autodesk
Autodesk

What does your python script look like? Is it different from the one you originally posted? Because the error makes it sound like the

import numpy as np

line is commented out or removed. The next line in the script should be

cost = np.random.randint(1,10, (4,4))

which uses the np variable set.

0 Likes
Message 14 of 21

jacopo_r
Not applicable
@Kavika F you are actually right. The line importing numpy was commented. But I think the first one I tried and uploaded was not commented. Now I am not sure about what I did or did not for making it work, but for sure all your support helped a lot!

Thank you very much for this!!!

And since you are so nice and helpful, I would like to ask you another thing, since we're here.

In the python script, the cplex model gives a solution. I then need that solution, namely the variable x[i,j], to be returned to flexsim as an array, I guess, so then I can transport it into a global table. Could you help me with this please? I guess it's more python/cplex related than flexsim.

Again, thanks!

0 Likes
Message 15 of 21

kavika_faleumu
Autodesk
Autodesk
@jacopo_r Yeah of course. If your python function wants to return an Array, your User Command in FlexSim should have a "var" as a return type. Check out this documentation for more info on that.

Then you can do something similar to a post I linked you to in an earlier question you had which goes over returning an Array from python and using it to populate a Global Table. Essentially you'll just take the Array and loop through it, setting values on a Global Table as you go.

0 Likes
Message 16 of 21

jacopo_r
Not applicable

I see, thanks. I tried the suggestions, also from the previous post, and they work by themselves.

Now I am having some difficulties with returning the x from the Cplex model. I have "return x" as the end of the python code, and then I read as Array and loop through it, FlexSim says "exception: FlexScript exception: Array index out of bounds at <no path> c: <no path> i: <no path>". I think it can be related to the fact that the model variable x is defined as "assignment_model.binary_var_matrix(...)", thus it can be read as an array when return to FlexSim. Also, I am not sure the variable is returned properly.

0 Likes
Message 17 of 21

kavika_faleumu
Autodesk
Autodesk

@jacopo_r, reading the documentation for the

binary_var_matrix()

function tells me that it returns a dictionary, not an array. If you type

test_cplex()

into a new Script window, what does it return? That may give us an idea of what type of data structure FlexSim interprets your return value as.

0 Likes
Message 18 of 21

jacopo_r
Not applicable

It returns this:

1684449053088.png

The pairs of numbers seem to be the index for rows and columns, knowing that in python a list starts from index 0 for both rows and columns. Nonetheless the value seem to be NULL for all of them.
My goal is to be able to return the x in order to create a table with 0 and 1, where 1 means that a specific task has been assigned to a specific worker.

Hope this can give you a better understanding.

0 Likes
Message 19 of 21

jacopo_r
Not applicable

I just read about Map type from here https://docs.flexsim.com/en/23.0/Reference/CodingInFlexSim/FlexScriptAPIReference/Data/Map.html#Prop... and tried to access some of the map type values, but all of them seem indeed to be NULL. Can it be that the Cplex model is actually not optimizing? As I already said it works when I run it outside of Flexsim. So, since it is the same script, it should also work when called by Flexsim...

0 Likes
Message 20 of 21

jacopo_r
Not applicable

@Kavika F, I am trying to mess around and I also see some issues in feeding a table to the cplex.

In the two files uploaded, the return 1 function until I keep everything below the line where x is defined comment, this line included. After I un-comment it, the code does not return 1 anymore. Any thoughts on this?
Thanks

test-cplex-numpy.fsm

test-cplex-numpy.py

0 Likes