Using CPLEX in R: Installing cplexAPI in Windows 10

[This article was first published on R – Jason unedited, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I have a very large mixed integer problem to solve.   COIN‘s solver accessed through Rsymphony did a decent job, but had trouble finding feasible solutions.  I had been told that the IBM cplex  solver was the best on the market for mixed integer programming.

Installing cplex is a relatively easy task.  The difficulties began when I tried to install a link from R.  Either cplexAPI or Rcplex.  You can search and see that there is very little helpful information in the usual places.  I reached out to the developers and Jonathan of cplexAPI responded with very helpful troubleshooting and instructions.

We (honestly, all Jonathan) determined that the following sequence should correctly install cplexAPI.  First, read the directions.

After downloading, unpack the .tar.gz file someplace.  I am going to unpack mine on the desktop.

Open the newly created folder cplexAPI and you will find a directory labeled src.  Open the file within that folder.  I have Notepad++ installed, so I right click and open withNotepad++.  The instructions note that you will need to find each of these directories and type them in.  They may (and probably will if you are using any other version).

PKG_CPPFLAGS=-g -D_R_=1 -DUSE_R=1 -I"C:/Program Files/IBM/ILOG/CPLEX_Studio127/cplex/include" PKG_LIBS=-L"C:/Program Files/IBM/ILOG/CPLEX_Studio127/cplex/bin/x64_win64" -lm -lcplex1270

After saving you will need to repackage cplexAPI.  Head over to the command line (remember I unpacked my tar.gz file on my desktop) and use the command:

R CMD build --no-build-vignettes --no-manual  --md5     C:\Users\{your name here}\Desktop\cplexAPI

This will repackage the folder, avoiding the md5 error you can find while searching for solutions.  After a successful repackage you can then install the package at the command line.

R CMD INSTALL --build --no-multiarch .\cplexAPI_1.3.3.tar.gz

At this point, you may have errors.  The key you are looking for is this output:

packaged installation of 'cplexAPI' as DONE (cplexAPI)

Now try it out.  Here is an example from Gabriel Gelius-Dietrich’s cplexAPI – Quick Start.

> library(cplexAPI) env prob chgProbNameCPLEX(env, prob, "sample") [1] 0 nc nr obj rhs sense lb ub cn rn beg cnt ind val copyLpwNamesCPLEX(env, prob, nc, nr, CPX_MAX, obj, rhs, sense, + beg, cnt, ind, val, lb, ub, NULL, cn, rn) [1] 0 lpoptCPLEX(env, prob) [1] 0 solutionCPLEX(env, prob) $lpstat [1] 1 $objval [1] 13 $x [1] 2 0 1 $pi [1] 1 0 1 $slack [1] 0 1 0 $dj [1] 0 -3 0

I am looking forward to using cplexAPI for a larger problem!

For more R posts visit:

To leave a comment for the author, please follow the link and comment on their blog: R – Jason unedited. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)