Using CPLEX in R: Installing cplexAPI in Windows 10

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)
[1] 1
[1] 13
[1] 2 0 1
[1] 1 0 1
[1] 0 1 0
[1] 0 -3 0

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

For more R posts visit: