tl;dr: although I use R every day and love it, doing mathematical programming using Julia is much simpler and more flexible than anything I know that is currently available in R.<br><br>Recently I have learned that Iain Dunning and Joey Huchette and Miles Lubin have received 2016 INFORMS Computing Society prize for the development of JuMP, a Julia-based domain-specific modeling language. Congratulations!<br><br>Together with Wit Jakuczun we have decided to test drive Julia's JuMP against R's package glpkAPI.<br><br>The problem we decided to solve is a standard MIP model for finding clusters in data using k-medoids method (we have used <a target="_blank" href="https://cseweb.ucsd.edu/~dasgupta/291-unsup/lec3.pdf">this</a> specification of the model without relaxation). The work breakdown was that Wit writes a solution in R and I developed Julia code.<br><br><h3>Data preparation</h3>The data set used for the analysis can be generated using this Julia code:<br><br><span>function gendata(N, K, scaling)</span><br><span>    x = randn(N, 2)</span><br><span>    for i in 1:N</span><br><span>        α = 2π * i / K</span><br><span>        x[i, :] += scaling * [cos(α); sin(α)] / (2 * sin(π/K))</span><br><span>    end</span><br><span>    return x</span><br><span>end</span><br><span><br></span><span>srand(1)</span><br><span>d = gendata(50, 4, 5)</span><br><span>writecsv(&quot;test.txt&quot;, d)</span><br><br>or similar R code respectively:<br><br><span>gen_data &lt;- function(N, K, scaling) {</span><br><span>    alpha &lt;- 2 * pi * (1:N) / K</span><br><span>    sin_pi_K &lt;- 2 * sin(pi / K)</span><br><span><br></span><span>    X &lt;- as.data.frame(matrix(data=rnorm(n=2*N), nrow=N, ncol=2) +</span><br><span>                       scaling*matrix(data = c(cos(alpha), sin(alpha)),</span><br><span>                                      nrow = N, ncol = 2) / sin_pi_K)</span><br><span>    return(data.frame(x = X$V1, y = X$V2))</span><br><span>}</span><br><span><br></span><span>set.seed(1)</span><br><span>write.table(x = gen_data(200, 4, 5), file = &quot;test.txt&quot;,</span><br><span>            col.names = TRUE, row.names = FALSE,</span><br><span>            sep = &quot;,&quot;, dec = &quot;.&quot;)</span><br><br>(Julia codes below assume that data set was generated using Julia and R assumes data was generated in R: the difference is that file generated in R has a header)<br><br>Comparing the above codes (although they are not the main objective of the exercise) highlights two things about Julia: 1) you can use unicode literals in your code and 2) in Julia using explicit loop is OK (fast). Those two points are in this case of course only an issue of taste but I actually find that both of them make the code a bit more readable.<br><br>The end result is 200 points placed in 2D plane in four clusters.<br><br><h3>Now we move to the actual challenge: <i>write a code that executes k-medoids algorithm for number of clusters from 2 to 6 and compare the results</i>.</h3>Here is the solution in Julia:<br><br><span>using JuMP</span><br><span>using GLPKMathProgInterface</span><br><span>using Distances</span><br><span><br></span><span>function find_clusters(K, ds)</span><br><span>    N = size(ds, 1)</span><br><span>    rho = pairwise(Euclidean(), ds')</span><br><span><br></span><span>    m = Model(solver=GLPKSolverMIP())</span><br><span>    @variable(m, y[1:N], Bin)</span><br><span>    @variable(m, x[1:N,1:N], Bin)</span><br><span><br></span><span>    @objective(m, Min, sum{x[i,j]*rho[i,j], i=1:N, j=1:N})</span><br><span><br></span><span>    @constraint(m, sum(y) == K)</span><br><span>    for i in 1:N</span><br><span>        @constraint(m, sum(x[i,:]) == 1)</span><br><span>    end</span><br><span>    for i in 1:N, j in 1:N</span><br><span>        @constraint(m, x[i,j] &lt;= y[j])</span><br><span>    end</span><br><span><br></span><span>    solve(m)</span><br><span>    return getvalue(x), getvalue(y), getobjectivevalue(m)</span><br><span>end</span><br><span><br></span><span>function exec()</span><br><span>    ds = readcsv(&quot;test.txt&quot;)</span><br><span>    N = size(ds, 1)</span><br><span>    for K in 6:-1:2</span><br><span>        println(&quot;--- K=$K ---&quot;)</span><br><span>        @time x, y, obj = find_clusters(K, ds)</span><br><span>        println(&quot;Objective: $obj&quot;)</span><br><span>        println(&quot;Centers:&quot;)</span><br><span>        for i in 1:N</span><br><span>            y[i] > 0.0 &amp;&amp; println(&quot;\t$i:\t&quot;, ds[i,:])</span><br><span>        end</span><br><span>    end</span><br><span>end</span><br><span><br></span><span>exec()</span><br><br>and here is the R code:<br><br><span>library(glpkAPI)</span><br><span><br></span><span>find_clusters &lt;- function(K, ds) {</span><br><span>    N &lt;- nrow(ds)</span><br><span>    </span><br><span>    rho &lt;- cbind(expand.grid(i = 1:N, j = 1:N),</span><br><span>        dist = as.numeric(as.matrix(dist(ds, upper=TRUE, diag=TRUE))))</span><br><span>    write.table(x = rho, </span><span>file = &quot;dist.csv&quot;,</span><br><span>                sep = &quot;,&quot;, </span><span>dec = &quot;.&quot;,</span><br><span>                col.names = TRUE, </span><span>row.names = FALSE)</span><br><span><br></span><span>    #dat file</span><br><span>    cat(&quot;data;\n&quot;, file = &quot;kmedoids.dat&quot;, append = FALSE)</span><br><span>    cat(sprintf(&quot;param pN := %s;\n&quot;, N),file=&quot;kmedoids.dat&quot;, append=T)</span><br><span>    cat(sprintf(&quot;param pK := %s;\n&quot;, K),file=&quot;kmedoids.dat&quot;, append=T)</span><br><span>    cat(&quot;end;&quot;, file = &quot;kmedoids.dat&quot;, append = TRUE)</span><br><span><br></span><span>    wk &lt;- mplAllocWkspGLPK()</span><br><span>    termOutGLPK(GLP_OFF)</span><br><span>    model &lt;-  mplReadModelGLPK(wk, &quot;kmedoids.mod&quot;, TRUE)</span><br><span>    data &lt;- mplReadDataGLPK(wk, &quot;kmedoids.dat&quot;)</span><br><span>    mplGenerateGLPK(wk)</span><br><span>    lp &lt;- initProbGLPK()</span><br><span>    mplBuildProbGLPK(wk, lp)</span><br><span>    scaleProbGLPK(lp, GLP_SF_AUTO)</span><br><span>    setDefaultMIPParmGLPK()</span><br><span>    setSimplexParmGLPK(parm = c(MSG_LEV), val = c(GLP_MSG_OFF))</span><br><span>    setMIPParmGLPK(parm = c(MSG_LEV), val = c(GLP_MSG_OFF))</span><br><span>    solveSimplexGLPK(lp)</span><br><span>    solveMIPGLPK(lp)    </span><br><span>    </span><br><span>    obj  &lt;- mipObjValGLPK(lp)</span><br><span>    all_values &lt;- mipColsValGLPK(lp)</span><br><span>    all_names &lt;- rep(NA_character_, length(all_values))</span><br><span>    for (i in 1:length(all_values)) {</span><br><span>        all_names[i] &lt;- getColNameGLPK(lp, i)</span><br><span>    }</span><br><span>    sol &lt;- data.frame(var = all_names, val = all_values)</span><br><span>    </span><br><span>    return(list(</span><br><span>      obj = obj,</span><br><span>      y = sol[grep(pattern = &quot;vSegmentCenter&quot;, x = all_names), ],</span><br><span>      x = sol[grep(pattern = &quot;vPointToSegment&quot;, x = all_names), ]))</span><br><span>}</span><br><span><br></span><span><br></span><span>ds &lt;- read.table(file = &quot;test.txt&quot;, </span><span>sep = &quot;,&quot;,</span><br><span>                 dec = &quot;.&quot;, </span><span>header = TRUE)</span><br><span>N &lt;- nrow(ds)</span><br><span><br></span><span>for (K in seq(6,2)) {</span><br><span>  print(sprintf(&quot;--- K = %s ---&quot;, K))</span><br><span>  start_time &lt;- Sys.time()</span><br><span>  sol &lt;- find_clusters(K, ds)</span><br><span>  calc_duration &lt;- Sys.time() - start_time</span><br><span>  print(sprintf(&quot;    %s sec&quot;, calc_duration))</span><br><span>  print(sprintf(&quot;Objective: %s&quot;, sol$obj))</span><br><span>  print(&quot;Centers:&quot;)</span><br><span>  print(ds[sol$y$val > 0, ])</span><br><span>}</span><br><span><br></span>which requires auxiliary kmedoids.mod file:<br><br><span>param pN;</span><br><span>param pK;</span><br><span>set pPoints := 1..pN;</span><br><span>set pSegments := 1..pK;</span><br><span><br></span><span>param pDist{i in pPoints, j in pPoints} >= 0;</span><br><span><br></span><span>table T_dist IN &quot;CSV&quot; &quot;dist.csv&quot;:</span><br><span><span> </span>[i,j], pDist ~ dist;</span><br><span><br></span><span>var vPointToSegment{i in pPoints, j in pPoints}, binary;</span><br><span>var vSegmentCenter{i in pPoints}, binary;</span><br><span><br></span><span>minimize total_cost: sum{i in pPoints, j in pPoints} (vPointToSegment[i,j] * pDist[i,j]);</span><br><span><br></span><span>subject to</span><br><span><br></span><span>cPointToOnlyOneSegment{i in pPoints}:</span><br><span><span> </span>sum{j in pPoints} vPointToSegment[i,j] = 1;</span><br><span><br></span><span>cSegmentsCnt:</span><br><span><span> </span>sum{i in pPoints} vSegmentCenter[i] = pK;</span><br><span><br></span><span>cPoinOnlyToActiveSegment_1{i in pPoints, j in pPoints}:</span><br><span><span> </span>vPointToSegment[i, j] &lt;= vSegmentCenter[j];</span><br><span><br></span><span>cPoinOnlyToActiveSegment_2{j in pPoints}:</span><br><span><span> </span>sum{i in pPoints} vPointToSegment[i, j] &lt;= card(pPoints)*vSegmentCenter[j];</span><br><span><br></span><span>end;</span><br><br>I do not want to go through the code in detail - please judge it yourself. However, the two things are immediately apparent in my opinion:<br><br><ol><li>the ability to be able to specify the model within Julia beats hands down glpkAPI, where you have to use external file for specification of the model and external files to communicate data to the model;</li><li>in Julia if we want to switch the solver from GLPK to any other (eg. CBC) the only line we would have to change is <span>m = Model(solver=GLPKSolverMIP())</span>. In R you have bindings to other solvers but they are much more low-level and most importantly you would have to rewrite most of the code.</li></ol>