This is an ILP model version 2.
printSolution <- function(solx){
if (sum(solx$value) == 0){
cat("No solution found.\n")
return
}
cat("Zone | alpha | h | m | Stations \n")
for (i in c(1:NZones)){
cat(" ",i," | ", alpha[i], " | ", h[i], " | ", m[i], " | ")
for (j in c(1:NLocations))
if (N[j,i] && solx$value[j])
cat(j, " ")
cat("\n")
}
cat("\nZone |sum(x_j * c_j)| alpha * h \n")
objective = 0
for (i in c(1:NZones)){
objective <- objective + (solx$value * N[ , i]) %*% C
cat(" ",i," | ", (solx$value * N[ , i]) %*% C , " | ", alpha[i] * h[i], "\n")
}
cat("\nTotal sum(x_j * c_j): ", objective)
cat("\nTotal number of stations: ", sum(solx$value))
}
library("igraph")
##
## Dołączanie pakietu: 'igraph'
## Następujące obiekty zostały zakryte z 'package:stats':
##
## decompose, spectrum
## Następujący obiekt został zakryty z 'package:base':
##
## union
library(ROI)
## ROI: R Optimization Infrastructure
## Registered solver plugins: nlminb, glpk.
## Default solver: auto.
library(Rglpk)
## Ładowanie wymaganego pakietu: slam
## Using the GLPK callable library version 5.0
library(ompr)
library(ompr.roi)
library(ROI.plugin.glpk)
NZones = 5
NLocations = 25
# N_i - The set of candidate parking locations that are in zone i
N1 <- c(rep(1, 5), rep(0, 20))
N2 <- c(rep(0, 5), rep(1, 5), rep(0, 15))
N3 <- c(rep(0, 10), rep(1, 5), rep(0, 10))
N4 <- c(rep(0, 15), rep(1, 5), rep(0, 5))
N5 <- c(rep(0, 20), rep(1, 5))
N <- array(c(N1, N2, N3, N4, N5), dim=c(NLocations, NZones))
# cj = Capacity of candidate bicycle parking location j
C <- c(rep(25, NLocations))
#hi = Demand (number of bicycle users) in zone i
h <- round(runif(n=NZones, min=20, max=100))
# alphai : The minimum percentage of demand that needs to be satisfied for zone i
#alpha <- round(runif(n=NZones, min=1, max=100))
alpha <- c(rep(0.7,NZones))
# mi: Minimum number of covering stations required for zone i (redundancy)
m <- round(runif(n=NZones, min=1, max=5))
# B = Maximum number of bicycle parking stations that can be built (due to budget constraints)
B <- sum(m)
model <- MIPModel() %>%
add_variable(x[j], j = 1:NLocations, type = "integer", lb = 0, ub = 1) %>%
set_objective(sum_expr(x[j] * C[j], j = 1:NLocations), "max") %>%
add_constraint(sum_expr(x[j] * C[j] * N[j ,i] , j = 1:NLocations) >= alpha[i] * h[i], i = 1:NZones) %>%
add_constraint(sum_expr(x[j] * N[j ,i], j = 1:NLocations) >= m[i], i = 1:NZones) %>%
add_constraint(sum_expr(x[j], j = 1:NLocations) <= B)
result <- solve_model(model, with_ROI(solver = "glpk", verbose = TRUE))
## <SOLVER MSG> ----
## GLPK Simplex Optimizer 5.0
## 11 rows, 25 columns, 75 non-zeros
## 0: obj = -0.000000000e+00 inf = 2.132e+02 (10)
## 21: obj = 3.250000000e+02 inf = 1.520e+00 (1)
## LP HAS NO PRIMAL FEASIBLE SOLUTION
## GLPK Integer Optimizer 5.0
## 11 rows, 25 columns, 75 non-zeros
## 25 integer variables, all of which are binary
## glp_intopt: optimal basis to initial LP relaxation not provided
## <!SOLVER MSG> ----
solx <- get_solution(result, x[j])
#sol <- list("x" = solx)
printSolution(solx)
## No solution found.
## Zone | alpha | h | m | Stations
## 1 | 0.7 | 60 | 2 |
## 2 | 0.7 | 39 | 5 |
## 3 | 0.7 | 90 | 1 |
## 4 | 0.7 | 54 | 2 |
## 5 | 0.7 | 43 | 3 |
##
## Zone |sum(x_j * c_j)| alpha * h
## 1 | 0 | 42
## 2 | 0 | 27.3
## 3 | 0 | 63
## 4 | 0 | 37.8
## 5 | 0 | 30.1
##
## Total sum(x_j * c_j): 0
## Total number of stations: 0