2
$\begingroup$

I have just installed Anaconda and am intending to use Spyder as it is able to display variable, parameter values for debugging. After which, I am planning to use Pyomo to code my MIP model and invoke CPLEX from it.

I am able to install Pyomo from the instructions from the Pyomo Documentation site, but am stuck at the CPLEX portion as it is not mentioned within the site.

Having trawled the net, to the best of my knowledge, I am unable to locate any comprehensive instructions on it. I would specifically like to enquire about the following (Kindly correct me if I am wrong):

  1. I understand the Academic Version of CPLEX is downloadable, and that one needs to just download and click for installation.

  2. The Cplex executable is different from the API as the API is just a wrapper that translated Python instructions into C instructions during execution, and the Cplex executable is invoked specifically by Pyomo.

  3. One can either set the path within windows so that Pyomo would automatically locate the Cplex executable upon being invoked by the SolverFactory, or one can code the path within code itself.

  4. I would like to enquire if the instructions found from the IBM support site is sufficient for setting up the Cplex executable.

  5. With regards to 3 and 4, how does one invoke Cplex from Pyomo via coding the path with the code itself? On the other hand, even without explicitly coding the path, is invoking the SolverFactory sufficient? Or is it necessary to set the path via the Anaconda Prompt?

Greatly appreciate your kind advice on the above!

Thank you!

$\endgroup$
3
  • $\begingroup$ can you import cplex in python/anaconda? $\endgroup$
    – Betty
    Jun 10, 2020 at 20:06
  • $\begingroup$ I am afraid not. I adhered to the IBM site's instructions for downloading and installing CPLEX. And used standard commands to invoke CPLEX such as import pyomo.environ as pyo from pyomo.opt import SolverFactory ..... ..... model = pyo.ConcreteModel() opt = SolverFactory('cplex') opt.solve(model) This link (groups.google.com/forum/#!searchin/pyomo-forum/cplex|sort:date/…) seems promising, but I am having trouble with the formatting such as r' within the sample code (solverpath_folder = \ r'your_cplex_directory\bin\x86_win32'). Thanks! $\endgroup$
    – Mike
    Jun 10, 2020 at 20:33
  • $\begingroup$ Hello @Mike, Please see my answer below. BTW, I think "r" means replace... you need to type your CPLEX location in there. $\endgroup$ Jun 11, 2020 at 0:39

2 Answers 2

3
$\begingroup$

At the end of the academic version installation, IBM ILOG asks you whether you want to add CPLEX to the system PATH. The only thing that you need to do is to confirm this question. Once it has been added to the system Path it will be visible for Pyomo after following steps:

  • open a command prompt
  • go to the location that IBM/ILOG installed
  • Locate CPLEX_Studio1210 folder
  • inside that folder, you will find another folder named python
  • once you are there, in command prompt type the following line and hit enter:

    C:\Program Files\IBM\ILOG\CPLEX_Studio1210\python>python setup.py install

If you have your model $m$:

opt = SolverFactory('cplex')
solution = opt.solve(m)
$\endgroup$
4
  • $\begingroup$ Dear Oguz, thank you for replying. Another dumb question: May I confirm with you if the entering of the line C:\Program Files\IBM\ILOG\CPLEX_Studio1210\python>python setup.py install is a once and for all thing, as in after doing so whenever Pyomo needs to invoke CPLEX via instructions such as opt = SolverFactory('cplex') solution = opt.solve(m) there is no need to enter the line again? Thank you $\endgroup$
    – Mike
    Jun 11, 2020 at 12:01
  • $\begingroup$ Yes that’s correct. $\endgroup$ Jun 11, 2020 at 12:35
  • $\begingroup$ You can also use the below code. ``` opt = SolverFactory('cplex_direct') opt.solve(model) model.pprint() ``` $\endgroup$ Nov 12, 2021 at 3:39
  • $\begingroup$ @AnanyaPandey what is the difference between cplex_direct and cplex? $\endgroup$ Dec 2, 2022 at 11:44
4
$\begingroup$

First of all, you need to be able to use CPLEX from Python scripts.

After installation of CPLEX in your machine, you have two ways to install CPLEX-Python modules:

1. Use setup.py script

Open a cmd and navigate to yourCplexhome/python/pythonVERSION/PLATFORM (it should be like C:\Program Files\IBM\ILOG\CPLEX_Studio129\cplex\python\3.7\x64_win64, there should be the script setup.py; simply run this:

python setup.py install

OR

2. Set environement variable PYTHONPATH

  • In the windows Start Search, type "env" and select "Edit the system environment variables" then click "Environment Variables"
  • If PYTHONPATHis among the variable, edit it, otherwise create a new varaible and name it "PYTHONPATH"
  • In the variable name, add yourCplexhome/python/pythonVERSION/PLATFORM. If the variable already contains a path, add a semicolumn ; before adding the cplex path, otherwise, just copy-paste the cplex location to the newly created varaible value.

To check if CPLEX is now available in Python, open a command line and type python, then import cplex, you should not have any error message saying that "there is no module named cplex" if CPLEX is recognized in Python.

In Pyomo, don't forget to import cplex, then call cplex as a solver:

opt = SolverFactory('cplex') 
results = opt.solve(concreteModel)

You may refer to Setting up the Python API in CPLEX for more details.

$\endgroup$
6
  • $\begingroup$ Dear Betty, thank you for replying. Another dumb question: For the first method, may I confirm with you if the entering of the line python setup.py install is a once and for all thing, as in after doing so the path has been set permanently, thus whenever Pyomo needs to invoke CPLEX via instructions such as opt = SolverFactory('cplex') results = opt.solve(concreteModel) there is no need to enter the line again? $\endgroup$
    – Mike
    Jun 11, 2020 at 12:17
  • $\begingroup$ For the second method, the third point mention that the variable might have already contained a path, is it the original path configured by the setup. Thus, via adding another path by adding a semicolon, it is asking the system to look for cplex in the other location too just in case it does not find it in the original path? $\endgroup$
    – Mike
    Jun 11, 2020 at 12:18
  • $\begingroup$ Lastly, I suppose none of this steps is actually meant for setting up the Python API, is it? Cos you mentioned at the last part about referring to Setting up the Python API in CPLEX for more details. As at this current stage, I am only thinking of invoking CPLEX from Pyomo. Thank you! $\endgroup$
    – Mike
    Jun 11, 2020 at 12:20
  • 1
    $\begingroup$ Q1: Yes, it will be done permanently. Q2: Generally, you will not have that variable in your system, unless you have performed a previous cplex installation, which I doubt from your question. Actually, you can method 1 OR method 2, no need to use both. These methods are to setup the CPLEX-Python API, the link is provided for refrence and more reading. Pyomo is based on Python so you cannot use CPLEX in Pyomo without being able to use it in Python. $\endgroup$
    – Betty
    Jun 11, 2020 at 12:51
  • $\begingroup$ Dear Betty, thank you for replying. I assume that the two steps mentioned in prior reply are meant for python. Previously, I mentioned this IBM link (ibm.com/support/knowledgecenter/SSSA5P_12.9.0/…) with regards to setting CPLEX for windows. May I ask if it is necessary? If so, is it the correct sequence to first set up for Windows, followed by that for Python? Thank you! $\endgroup$
    – Mike
    Jun 11, 2020 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.