Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
cvxr_docs/content/cvxr_examples/using-other-solvers.Rmd
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
151 lines (117 sloc)
5.69 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: Using Other Solvers | |
author: Anqi Fu and Balasubramanian Narasimhan | |
date: '2018-05-15' | |
slug: cvxr_using-other-solvers | |
bibliography: ../bibtex/cvxr_refs.bib | |
link-citations: true | |
categories: [] | |
tags: [] | |
--- | |
```{r prereqs, message = FALSE, echo = FALSE} | |
library(CVXR) | |
library(magrittr) | |
library(kableExtra) | |
``` | |
## Introduction | |
The default installation of `CVXR` comes with several (imported) open source solvers: | |
- [ECOS](https://github.com/embotech/ecos) and its mixed integer | |
cousin `ECOS_BB` | |
- [OSQP](https://www.osqp.org) | |
- [SCS](https://github.com/cvxgrp/scs) | |
`CVXR` can also make use of commercial solvers and other open-source solvers: | |
- The commercial solver [`MOSEK`](https://www.mosek.com/) | |
- The commercial solver [`GUROBI`](https://www.gurobi.com/) | |
- The commercial solver [`CPLEX`](https://www.ibm.com/analytics/cplex-optimizer) | |
- The open source linear and mixed integer programming package | |
[`GLPK`](https://www.gnu.org/software/glpk/) via the | |
[`Rglpk`](https://cran.r-project.org/package=Rglpk) package. | |
- The CBC solver via the R package | |
[`rcbc`](https://github.com/dirkschumacher/rcbc). This is not yet on | |
CRAN but can be installed directly from github. | |
Since these are optional, you have to install these packages | |
yourself. | |
__NOTE__: `MOSEK` ,`GUROBI`, and `CPLEX` require licenses to use them | |
but free academic licenses are available for all three. | |
## Solver capabilities | |
Table 1 below shows the capabilities of various solvers. Once the | |
appropriate packages are installed, a call to | |
`CVXR::installed_solvers()` will indicate what solvers `CVXR` is aware | |
of. | |
```{r, echo = FALSE} | |
check <- "\U2713" | |
d <- tibble::tibble( | |
Solver = c("CBC", "GLPK", "GLPK_MI", "OSQP", "CPLEX", "ECOS", "ECOS_BB", "GUROBI", "MOSEK", "SCS"), | |
`R package` = c("[rcbc](https://github.com/dirkschumacher/rcbc)", | |
"[`Rglpk`](https://cran.r-project.org/package=Rglpk)", | |
"[`Rglpk`](https://cran.r-project.org/package=Rglpk)", | |
"[osqp](https://www.osqp.org)", | |
"[Rcplex](https://cran.r-project.org/package=Rcplex)", | |
"[ECOSolveR](https://cran.r-project.org/package=ECOSolveR)", | |
"[ECOSolveR](https://cran.r-project.org/package=ECOSolveR)", | |
"[gurobi](https://www.gurobi.com/documentation/9.0/refman/ins_the_r_package.html)", | |
"[Rmosek](https://cran.r-project.org/package=Rglpk)", | |
"[scs](https://cran.r-project.org/package=scs)"), | |
LP = c(check, check, check, check, check, check, " " , check, check, check), | |
QP = c(" " , " " , " " , check, check, check, " " , check, check, check), | |
SOCP = c(" " , " " , " " , " " , check, check, " " , check, check, check), | |
SDP = c(" " , " " , " " , " " , " " , " " , " " , " " , check, check), | |
EXP = c(" " , " " , " " , " " , " " , " " , " " , " " , check, check), | |
MIP = c(check, " " , check, " " , " " , " " , check, check, check, check) | |
) | |
knitr::kable(d, format = "html", caption = "Solver Capabilities") %>% | |
kable_styling("striped") %>% | |
column_spec(1:8, background = "#ececec") | |
``` | |
## Commercial Solvers | |
Pre-1.0 versions of `CVXR` used vendor python solver packages via | |
[`reticulate`](https://cran.r-project.org/package=reticulate), _not R | |
packages_. Version 1.x directly uses native R packages that have now | |
become available, resulting in a cleaner, more efficient interface. | |
Brief pointers on installation follow. | |
### Installing `MOSEK` | |
[MOSEK](https://www.mosek.com) provides an academic version that is | |
free of charge: one can obtain the free academic license after | |
registering. Once the license for the product has been activates, the | |
[Rmosek documentation](https://docs.mosek.com/9.1/rmosek/index.html) | |
provides all the details for installation. A quick check to ensure | |
things are working is to run the example: | |
```{r, eval = FALSE} | |
library(Rmosek) | |
example(mosek) | |
``` | |
Once everything is working, you can check that `CVXR` recognizes the | |
solver; `installed_solvers()` should list `MOSEK`. Otherwise, rinse | |
and repeat until success. | |
### Installing `GUROBI` | |
[GUROBI](https://www.gurobi.com) also provides an academic version | |
that is free of charge. After registering, install the _Gurobi | |
Optimizer_ software and activate your license as necessary. The | |
[Gurobi | |
documentation](https://www.gurobi.com/documentation/9.0/quickstart_mac/r_interface.html#section:R) | |
provides details. | |
Once activated, you can check that `CVXR::installed_solvers()` lists | |
`GUROBI`. Otherwise, rinse and repeat until success. | |
### Installing `Rcplex` | |
[CPLEX](https://www.ibm.com/analytics/cplex-optimizer) is available in | |
a community edition. After installation, the | |
[`Rcplex`](https://cran.r-project.org/package=Rcplex) package needs to | |
be told where to find the libraries for linking. A sample session on | |
macOS is shown below for reference, where CPLEX version 22.1 was installed in the | |
standard `Applications` folder. | |
```{bash, eval = FALSE} | |
cplex_location <- "/Applications/CPLEX_Studio_Community221" | |
configure_args <- c(Rcplex = sprintf("--with-cplex-include='%s/cplex/include' --with-cplex-lib='-L%s/cplex/lib/x86-64_osx/static_pic -lilocplex -lcplex'", cplex_location, cplex_location)) | |
install.packages("Rcplex", configure.args = configure_args) | |
``` | |
### Example session | |
```{r} | |
CVXR::installed_solvers() | |
``` | |
## Session Info | |
```{r} | |
sessionInfo() | |
``` | |
## Source | |
[R Markdown](https://github.com/bnaras/cvxr_docs/blob/master/content/cvxr_examples/using-other-solvers.Rmd) | |
## References |