【发布时间】:2023-03-03 11:03:02
【问题描述】:
所以我有一个包含 600 个点、它们的纬度、经度和需求的数据集。 我必须制作集群,以便每个集群的点彼此靠近,并且该集群的总容量不会超过某个限制。
问题的示例数据集:
set.seed(123)
id<- seq(1:600)
lon <- rnorm(600, 88.5, 0.125)
lat <- rnorm(600, 22.4, 0.15)
demand <- round(rnorm(600, 40, 20))
df<- data.frame(id, lon, lat, demand)
我写的代码:
library(tidyverse)
constrained_cluster <- function(df,capacity=170){
lon_max <- max(df$lon)
lat_max <- max(df$lat)
#Calculating the distance between an extreme point and all other points
df$distance<-6377.83*acos(sin(lat_max*p)*sin(df$lat*p) + cos(lat_max*p)*cos(df$lat*p) * cos((lon_max-df$lon)*p))
df<- df[order(df$distance, decreasing = FALSE),]
d<-0
cluster_number<-1
cluster_list<- c()
i<-1
#Writing a loop to form the cluster which will fill up the cluster_list accordingly
while (i <= length(df$distance)){
d <- d+ df$demand[i]
if(d<=capacity){
cluster_list[i] <- cluster_number
i<- i+1
}
else{
cluster_number <- cluster_number+1
d <- 0
i<-i
}
}
#Return a dataframe with the list of clusters
return(cbind(df,as.data.frame(cluster_list)))
}
df_with_cluster<- constrained_cluster(df, capacity = 1000)
【问题讨论】:
-
您能否将您的问题描述为优化模型?然后,您可以尝试直接求解模型,而不是依赖标准聚类技术。
-
@EnricoSchumann 可以尝试这样做。关于如何解决这个问题的任何想法(或资源)?
-
我已经添加了答案。
标签: r optimization cluster-analysis hierarchical-clustering operations-research