Use CVXOPT from R

library(PythonInR)
## 
## Initialize Python Version 2.7.9 (default, Mar  1 2015, 13:01:26) 
## [GCC 4.9.2]

CVXOPT (Andersen et al., 2016) is a popular Python package for optimization, which is distributed under GPL-3. The following example shows how to solve an linear programming problem based on the example provided in the Rglpk (Theussl and Hornik, 2015) package.

## Simple linear program.
## maximize:   2 x_1 + 4 x_2 + 3 x_3
## subject to: 3 x_1 + 4 x_2 + 2 x_3 <= 60
##             2 x_1 +   x_2 + 2 x_3 <= 40
##               x_1 + 3 x_2 + 2 x_3 <= 80
##               x_1, x_2, x_3 are non-negative real numbers
obj <- matrix(-c(2, 4, 3))
mat <- rbind(matrix(c(3, 2, 1, 4, 1, 3, 2, 2, 2), nrow = 3), -diag(3))
rhs <- matrix(c(60, 40, 80, 0, 0, 0))

There are various options how to solve this problem with PythonInR, the following will show two of them.

1. Import all the solvers from cvxopt

pyImport(import="solvers", from="cvxopt")
sol <- solvers$lp(th.cvxopt(obj), th.cvxopt(mat), th.cvxopt(rhs))
##      pcost       dcost       gap    pres   dres   k/t
##  0: -9.0519e+01 -3.2896e+02  1e+02  2e-01  2e+00  1e+00
##  1: -7.3854e+01 -1.3575e+02  3e+01  6e-02  7e-01  2e+00
##  2: -7.6566e+01 -7.8776e+01  8e-01  2e-03  2e-02  2e-01
##  3: -7.6666e+01 -7.6688e+01  8e-03  2e-05  3e-04  2e-03
##  4: -7.6667e+01 -7.6667e+01  8e-05  2e-07  3e-06  2e-05
##  5: -7.6667e+01 -7.6667e+01  8e-07  2e-09  3e-08  2e-07
## Optimal solution found.
sol$x
##              [,1]
## [1,] 3.984107e-08
## [2,] 6.666667e+00
## [3,] 1.666667e+01
class(sol$x) ## an R matrix!
## [1] "matrix"

2. Use pyCall directly

pyExec("from cvxopt import solvers")
sol <- pyCall("solvers.lp", list(th.cvxopt(obj), th.cvxopt(mat), th.cvxopt(rhs)))
##      pcost       dcost       gap    pres   dres   k/t
##  0: -9.0519e+01 -3.2896e+02  1e+02  2e-01  2e+00  1e+00
##  1: -7.3854e+01 -1.3575e+02  3e+01  6e-02  7e-01  2e+00
##  2: -7.6566e+01 -7.8776e+01  8e-01  2e-03  2e-02  2e-01
##  3: -7.6666e+01 -7.6688e+01  8e-03  2e-05  3e-04  2e-03
##  4: -7.6667e+01 -7.6667e+01  8e-05  2e-07  3e-06  2e-05
##  5: -7.6667e+01 -7.6667e+01  8e-07  2e-09  3e-08  2e-07
## Optimal solution found.
sol$x
##              [,1]
## [1,] 3.984107e-08
## [2,] 6.666667e+00
## [3,] 1.666667e+01

References

Martin S Andersen, Joachim Dahl, and Lieven Vandenberghe. Cvxopt: A python package for convex optimization, version 1.1.8. Available at cvxopt. org, 2016. URL http://cvxopt.org/.

Stefan Theussl and Kurt Hornik. Rglpk: R/GNU Linear Programming Kit Interface, 2015. URL https://CRAN.R-project.org/package=Rglpk. R package version 0.6-1.