<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/solver-interfaces-in-cvxr/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2018/07/09/solver-interfaces-in-cvxr/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Introduction</h2>
<p>In our <a rel="nofollow" href="https://rviews.rstudio.com/2017/11/27/introduction-to-cvxr/" target="_blank">previous blog<br>
post</a>, we<br>
introduced <code>CVXR</code>, an R package for disciplined convex<br>
optimization. The package allows one to describe an optimization<br>
problem with <a rel="nofollow" href="http://dcp.stanford.edu/" target="_blank">Disciplined Convex Programming</a><br>
rules using high level mathematical syntax. Passing this problem<br>
definition along (with a list of constraints, if any) to the <code>solve</code><br>
function transforms it into a form that can be handed off to<br>
a solver. The default installation of <code>CVXR</code> comes with two (imported)<br>
open source solvers:</p>
<ul>
<li><a rel="nofollow" href="https://github.com/embotech/ecos" target="_blank">ECOS</a> and its mixed integer<br>
cousin <code>ECOS_BB</code> via the CRAN package<br>
<a rel="nofollow" href="https://cloud.r-project.org/package=ECOSolveR" target="_blank">ECOSolveR</a></li>
<li><a rel="nofollow" href="https://github.com/cvxgrp/scs" target="_blank">SCS</a> via the CRAN package<br>
<a rel="nofollow" href="https://cloud.r-project.org/package=scs" target="_blank">scs</a>.<br>
</li>
</ul>
<p><code>CVXR</code> (version 0.99) can also make use of several other open source<br>
solvers implemented in R packages:</p>
<ul>
<li>The linear and mixed integer programming package<br>
<a rel="nofollow" href="http://lpsolve.sourceforge.net/5.5/" target="_blank"><code>lpSolve</code></a> via the<br>
<a rel="nofollow" href="https://cloud.r-project.org/package=lpSolveAPI" target="_blank"><code>lpSolveAPI</code></a> package</li>
<li>The linear and mixed integer programming package <a rel="nofollow" href="https://www.gnu.org/software/glpk/" target="_blank"><code>GLPK</code></a> via the<br>
<a rel="nofollow" href="https://cloud.r-project.org/package=Rglpk" target="_blank"><code>Rglpk</code></a> package.<br>
</li>
</ul>
<h2>About Solvers</h2>
<p>The real work of finding a solution is done by solvers, and writing<br>
good solvers is hard work. Furthermore, some solvers work particularly<br>
well for certain types of problems (linear programs, quadratic<br>
programs, etc.). Not surprisingly, there are commercial vendors who<br>
have solvers that are designed for performance and scale. Two<br>
well-known solvers are <a rel="nofollow" href="https://www.mosek.com/" target="_blank">MOSEK</a> and<br>
<a rel="nofollow" href="https://www.gurobi.com/" target="_blank">GUROBI</a>. R packages for these solvers are<br>
also provided, but they require the problem data to be constructed in<br>
a specific form. This necessitates a bit of work in the current version of<br>
<code>CVXR</code> and is certainly something we plan to include in future versions.<br>
However, it is also true that these commercial solvers expose a much<br>
richer API to Python programmers than to R programmers. How, then, do we<br>
interface such solvers with R as quickly as possible, at least<br>
in the short term?</p>
<h2>Reticulate to the Rescue</h2>
<p>The current version of <code>CVXR</code> exploits the<br>
<a rel="nofollow" href="https://cran.r-project.org/package=reticulate" target="_blank"><code>reticulate</code></a> package<br>
for commercial solvers such as MOSEK and GUROBI. We took the Python solver interfaces in <a rel="nofollow" href="http://www.cvxpy.org/versions/0.4.11/index.html" target="_blank"><code>CVXPY</code> version<br>
0.4.11</a>, edited them<br>
suitably to make them self-contained, and hooked them up to <code>reticulate</code>.</p>
<p>This means that one needs two prerequisites to use these commercial solvers in the current version of <code>CVXR</code>:</p>
<ul>
<li>A Python installation</li>
<li>The <a rel="nofollow" href="https://cran.r-project.org/package=reticulate" target="_blank"><code>reticulate</code></a> R<br>
package.</li>
</ul>
<h2>Installing MOSEK/GUROBI</h2>
<p>Both <a rel="nofollow" href="https://www.mosek.com/" target="_blank">MOSEK</a> and<br>
<a rel="nofollow" href="https://www.gurobi.com/" target="_blank">GUROBI</a> provide academic versions<br>
(registration required) free of charge. For example,<br>
<a rel="nofollow" href="https://www.anaconda.com/" target="_blank">Anaconda</a> users can install MOSEK with the command:</p>
<pre><code>conda install -c mosek mosek
</code></pre>
<p>Others can use the <code>pip</code> command:</p>
<pre><code>pip install -f https://download.mosek.com/stable/wheel/index.html Mosek
</code></pre>
<p>GUROBI is handled in a similar fashion. The solvers must be activated using a<br>
license provided by the vendor.</p>
<p>Once activated, one can check that <code>CVXR</code> recognizes the solver;<br>
<code>installed_solvers()</code> should list them.</p>
<pre><code>> installed_solvers()
[1] &quot;ECOS&quot;    &quot;ECOS_BB&quot; &quot;SCS&quot;     &quot;MOSEK&quot;   &quot;LPSOLVE&quot; &quot;GLPK&quot;    &quot;GUROBI&quot; 
</code></pre>
<h2>Further information</h2>
<p>More information on these solvers, along with a number of tutorial examples are<br>
available on the <a rel="nofollow" href="https://cvxr.rbind.io/" target="_blank">CVXR</a> site. If you are<br>
attending <a rel="nofollow" href="https://user2018.r-project.org/" target="_blank">useR! 2018</a>, you can catch<br>
Anqi’s <code>CVXR</code> talk on Friday, July 13.</p>
<p> </p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://rviews.rstudio.com/2018/07/09/solver-interfaces-in-cvxr/"> R Views</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/67bwyUui5QI" height="1"></a></div>