CPLEX made simple with Python
Posted: November 5th, 2009 | Author: ari.bjornsson | Filed under: Daily Life, Tech | Tags: cplex, dtu, imm, management, planning, python, simplex | 4 Comments »Update: As a project in a Python programming, I’ve developed a online interface to Cplex where the user only has to upload his/her lp model and the solution takes care of everything else and in just moments the results will be available online. The project report is in the making but for those who are interested (extremely interested), I’m posting a link to an A0 poster I made to introduce my project.
It’s amazing sometimes when designers decide to make their products difficult to use. I’m mentioning this because I’ve been attending a planning course at DTU which is all about making mathematical models of problems and then to solve them using solvers such as CPLEX. Now when I was working on the first assignment in this course I started by developing the model and then converting it to a simplex model and I was feeling jolly and gay. After creating a first draft of the simplex model I skimmed through the instruction on how to use the CPLEX terminal, which did not strike me at being difficult at all but then I started playing with it and I just froze out of frustration.
This God awful junk requires you to type every command without providing shortcuts as TAB completion or arrow-up to re-enter previous commands. As expected my simplex formulation did not work so I had to change it a couple of times and of course I tried to solve it after each edit to see if it worked. After just sitting a while and imagining how often I would have to enter the commands: “read problem.lp -> opt -> display solution objective -> display solution variables all” I felt a chill go down my spine and decided that this way of working was not for me.
So, what happened next was that I spent an hour or two to write as short python script which can be run from the terminal, with the problem.lp file as input, and it automatically enters these repetitive commands into CPLEX and the results are displayed in the log file. It can’t get more simpler than that! Because this little script has been very useful to me, I want to share it with those that are finding themselves in the same predicament.
Here’s a download link to the script: runCplexScript.py
To use it do the following:
- copy the script to a unix server which has access to CPLEX
- make sure you can execute it by issuing $ chmod +x runCplexScript.py
- edit runCplexScript.py, change: cplex_script # add additional CPLEX commands here, if you need them
- edit runCplexScript.py, change: cplex_path # path to the CPLEX executable on your server
- ./runCplexScript.py problem.lp
- monitor the cplex.log file for the results
What I usually do is that I have three terminal windows open; the first one I use to edit the problem.lp file, the second to run the python script and the third to tail the log file to view the results. To tail or view the results I recommend using
- $ less cplex.log # issue shift-f to start tailing and q to stop tailing
- or $ tail -f cplex.log
Note that you’ll get an error if the cplex.log does not exist, so issue the command after your first iteration.
After using this script for a while I decided that it was better for me to work with the problem.lp file locally instead of having to log in to the server to do all the work. I therefore wrote another python script which takes the problem.lp file as input, copies the file over to the CPLEX server, uses runCplexScript.py to solve the problem and finally it copies the cplex.log to my computer. This it does all in one go! In order to get the script to work you’ll have to configure your computer and the server so that you don’t have to enter a password to your account on the server to copy or retrieve files. It actually pretty simple, just do exactly like this:
$ mkdir ~/.ssh/
$ cd ~/.ssh/
$ ssh-keygen -t dsa
$ cat ~/.ssh/id_dsa.pub | ssh student_id@host “cat – >> ~/.ssh/authorized_keys”
And now you can log in without having to enter a password, but you should be aware that your account will be less secure since anyone accessing your local computer can access your server as well without a password.
Here’s a download link to the script: runCplexRemotely.py
There are additional steps involved in getting it to work:
- make sure you can execute it $ chmod +x runCplexRemotely.py
- edit runCplexRemptely.py and edit the following parameters
- student_id # DTU username or just your username for the CPLEX server
- host # CPLEX server host name
- remotedir # the directory you keep runCplexScript.py on the remote server
- logfile # default is cplex.log but that can be configured
Finally just issue $ ./runCplexRemotely.py problem.lp and rejoice because your life has become simpler.



Hi,
I’m really interested on your your script because I’m currently looking for a script which exactly does that! I’m using Cplex for my work (I’m a phD student) and I realize how boring is the interactive optimizer when one uses it frequently.
Unfortunately, I’m not able to download your script … link broken ? Is there another way to download it ? Thank you in advance !
Hi Bedwyr,
dropbox may have been unavailable when you tried to download the files, but I tried just now and it worked for me.
Drop me an email if this issue persists.
Thanks for posting this. It’s great to be able to call cplex without having to manually enter commands in the “interactive optimizer”. I am using your script (within a script of my own) to automatically solve directories full of models.
I found the following modification to be useful: replace “proc.communicate()” in runCplexScript.py with “print proc.communicate()[0]” to display the output of the cplex process.
Thanks Stefan, I’ll update the script in accordance to your comment as soon as I have time to do so. I’ll also include some additional changes which I’ve found useful.