A pesquisa operacional fornece uma variedade de algoritmos para determinar soluções ótimas exatas para problemas discretos de localização de armazéns. No entanto, esses algoritmos requerem uma matriz de custos, especificando o custo de abastecimento do cliente i do armazém j, para cada cliente e cada armazém.
Abaixo, proponho um algoritmo para especificar essa matriz de custos de forma que se possa aplicar esses algoritmos. O cálculo é baseado em distâncias euclidianas como uma medida de custo, ou seja, assume custos variáveis escalados linearmente com base em distâncias euclidianas.
# a function for calculating euclidean distances, based on two numeric vectors as input
euclidean_distance <- function(df1,df2){
sqrt((df1[,1]-df2[,1])**2+(df1[,2]-df2[,2])**2)
}
# parameters: df with warehouse coordinates, df with customers coordinates
cost_matrix <- function(warehouses,customers){
matrix <- as.data.frame(matrix(nrow=nrow(customers),ncol=nrow(warehouses)))
# applying euclidean distances
for(i in 1:nrow(customers)){
df <- as.data.frame(matrix(nrow=nrow(warehouses),ncol=2))
df[,1] <- as.numeric(rep(customers[i,1],times=nrow(df)))
df[,2] <- as.numeric(rep(customers[i,2],times=nrow(df)))
matrix[i,] <- euclidean_distance(df,warehouses)
}
# return cost matrix
matrix
}
I perform a simple test in the code snippet below, and print the cost matrix at the bottom.
# discrete candidate warehouse locations, by latitude and longitude
warehouse_df <- as.data.frame(matrix(nrow=3,ncol=2))
colnames(warehouse_df)<-c("lat","long")
warehouse_df$lat <- runif(n=3,min=-90,max=90)
warehouse_df$long <- runif(n=3,min=-180,max=180)
head(warehouse_df)
## lat long
## 1 -44.17554 102.08728
## 2 -58.51864 -160.25027
## 3 -56.54944 -11.30273
# customer locations
customers_df <- as.data.frame(matrix(nrow=4,ncol=2))
colnames(customers_df)<-c("lat","long")
customers_df$lat <- runif(n=4,min=-90,max=90)
customers_df$long <- runif(n=4,min=-180,max=180)
head(customers_df)
## lat long
## 1 -15.70044 171.1968
## 2 -87.68773 -156.7455
## 3 -62.35740 117.7821
## 4 -85.57747 -102.7820
# test cost matrix function
cost_matrix(warehouse_df,customers_df)
## V1 V2 V3
## 1 74.74596 334.20139 187.0153
## 2 262.46471 29.37890 148.7387
## 3 24.01888 278.05884 129.2154
## 4 209.01088 63.51993 95.9744
I some of my other posts you can find examples on how to locate warehouses at center of mass (center of mass calculation in R) and how to assign customers to the nearest warehouse (customer assignment problem). I also provided an example on how to cluster customers into groups, based on their spatial proximity (spatial proximity customer grouping in R).
Lastly, I also have written a post on how to locate multiple warehouses at each their center of mass (locating multiple warehouses at center of mass).
Cientista de dados com foco em simulação, otimização e modelagem em R, SQL, VBA e Python
Leave a Reply