1
$\begingroup$

For a linear optimization problem I want to include a dataframe (d_ij) which has binary variables, 1 if customer i is located within the assignable distance of facility j, 0 otherwise. So unless d_ij = 1, the customer cannot be assigned to a facility. To create the model, I have introduced a binary variable X_ij (for customers) and Y_j (for the facilities)

The dataframe is constructed as follows:

#creating distance function
e <- function(i, j) {
  customer <- data[i, ]
  facility <- facility_locations[j, ]
  (sqrt((customer$x - facility$x)^2 + (customer$y - facility$y)^2))
}


#with ifelse function, we model if the distance between consumer and facility is smaller than z =1, 0 otherwise

deltaframe <- data.frame(
  customer=data$ID,
  "j1" =ifelse(e(data$ID, facility_locations$ID==1)<z,1,0),
  "j2" =ifelse(e(data$ID, facility_locations$ID==2)<z,1,0),
  "j3" =ifelse(e(data$ID, facility_locations$ID==3)<z,1,0),
  "j4" =ifelse(e(data$ID, facility_locations$ID==4)<z,1,0),
  "j5" =ifelse(e(data$ID, facility_locations$ID==5)<z,1,0),
  "j6" =ifelse(e(data$ID, facility_locations$ID==6)<z,1,0),
  "j7" =ifelse(e(data$ID, facility_locations$ID==7)<z,1,0),
  "j8" =ifelse(e(data$ID, facility_locations$ID==8)<z,1,0),
  "j9" =ifelse(e(data$ID, facility_locations$ID==9)<z,1,0),
  "j10" =ifelse(e(data$ID, facility_locations$ID==10)<z,1,0),
  "j11" =ifelse(e(data$ID, facility_locations$ID==11)<z,1,0),
  "j12" =ifelse(e(data$ID, facility_locations$ID==12)<z,1,0),
  "j13" =ifelse(e(data$ID, facility_locations$ID==13)<z,1,0),
  "j14" =ifelse(e(data$ID, facility_locations$ID==14)<z,1,0),
  "j15" =ifelse(e(data$ID, facility_locations$ID==15)<z,1,0),
  "j16" =ifelse(e(data$ID, facility_locations$ID==16)<z,1,0),
  "j17" =ifelse(e(data$ID, facility_locations$ID==17)<z,1,0),
  "j18" =ifelse(e(data$ID, facility_locations$ID==18)<z,1,0),
  "j19" =ifelse(e(data$ID, facility_locations$ID==19)<z,1,0),
  "j20" =ifelse(e(data$ID, facility_locations$ID==20)<z,1,0)
)

Here z is a parameter, set to be the maximum distance a customer is willing to travel.

The constraint I try to model is $x_{ij} <= d_{ij} * y_j $ for all i and for all j

However, I do not know how to include this in r

$\endgroup$
2
  • $\begingroup$ What R package are you using to build the model? $\endgroup$
    – prubin
    Jul 11, 2022 at 15:16
  • $\begingroup$ Can you please put a sample of the data frame data? And is z a vector? $\endgroup$ Jul 12, 2022 at 11:39

2 Answers 2

3
$\begingroup$

MILP modelling in R is not as advanced or convenient as it is in Python or Julia. Few exceptions I know about are ROI and ompr packages. ompr seems to be actively maintained thanks to Dirk Schumacher.

edit: Here is a more comprehensive list.

If you are just looking for presenting solutions or solving in a purely functional/algebraic way you might want to check plyr package for apply-style implementations such as mdply, ddply or ldply. purrr package is also a good alternative.

$\endgroup$
1
$\begingroup$

Hope this helps. You'll need to learn the basics of ompr, which is an awesome package. I use it regularly, including with Gurobi for large-scale optimizations.

https://towardsdatascience.com/supply-chain-design-using-r-unconstrained-warehouse-customer-alignment-9ec11c1e4345

$\endgroup$
2
  • $\begingroup$ Thanks for the links! I'm working in ompr indeed and made it work through introducing a function, which i later include in a constraint in the model. delta <- function (i,j){ ifelse(e(i,j)<=z,1,0) } $\endgroup$
    – user9867
    Jul 13, 2022 at 10:42
  • $\begingroup$ OK. You could also just make a static matrix of 1 and 0, 1 representing if the pairing is allowed, 0 if not. then x[i,j] <= my_static_matrix[i,j] in the constraints $\endgroup$ Jul 13, 2022 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.