library(ROI)
## ROI: R Optimization Infrastructure
## Registered solver plugins: nlminb, glpk, optimx, symphony.
## Default solver: auto.
library(ROI.plugin.glpk)
library(ompr)
library(ompr.roi)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
modelo.minima.distancia=MIPModel()%>%
  add_variable(x[i,j],i=1:5,j=1:5,type = "binary")%>%
  set_objective(100*x[1,2]+20*x[1,3]+15*x[2,3]+30*x[3,4]+40*x[3,5]+
                  15*x[4,2]+15*x[4,5],sense="min")%>%
  add_constraint(x[1,2]+x[1,3]==1)%>%
  add_constraint(x[1,2]+x[4,2]-x[2,3]==1)%>%
  add_constraint(x[1,3]+x[2,3]-x[3,4]-x[3,5]==0)%>%
  add_constraint(x[3,4]-x[4,2]-x[4,5]==0)%>%
  add_constraint(x[3,5]+x[4,5]==0)%>%
  add_constraint(x[i,j]==0,i=1:5,j=1:5,i=j)
 
solucion.minima.distancia=solve_model(modelo.minima.distancia,
                                      with_ROI(solver = "glpk",verbose=TRUE))
## <SOLVER MSG>  ----
## GLPK Simplex Optimizer, v4.65
## 30 rows, 25 columns, 39 non-zeros
##       0: obj =   0.000000000e+00 inf =   2.000e+00 (2)
##       1: obj =   1.000000000e+02 inf =   0.000e+00 (0)
## *     6: obj =   6.500000000e+01 inf =   0.000e+00 (0)
## OPTIMAL LP SOLUTION FOUND
## GLPK Integer Optimizer, v4.65
## 30 rows, 25 columns, 39 non-zeros
## 25 integer variables, all of which are binary
## Integer optimization begins...
## Long-step dual simplex will be used
## +     6: mip =     not found yet >=              -inf        (1; 0)
## +     6: >>>>>   6.500000000e+01 >=   6.500000000e+01   0.0% (1; 0)
## +     6: mip =   6.500000000e+01 >=     tree is empty   0.0% (0; 1)
## INTEGER OPTIMAL SOLUTION FOUND
## <!SOLVER MSG> ----
objective_value(solucion.minima.distancia)
## [1] 65
get_solution(solucion.minima.distancia,x[i,j])%>%filter(value>0)
##   variable i j value
## 1        x 4 2     1
## 2        x 1 3     1
## 3        x 3 4     1