使用一些约束进行聚类

时间:2019-05-15 10:09:19

标签: r

我正在尝试将一些项目(例如200个目的地)聚集/分配给一些销售经理(例如50个销售经理),以使经理所走的总距离最小。我正在使用k-均值聚类。我得到的答案是距离最小,但是分配的目的地数量不是很均匀。一个销售经理将获得10个目的地,而另一个销售经理仅获得1个目的地,而其他之间。

因此,我想添加一个限制,即允许的最大目的地应为6,这样,销售经理就不会获得超过6个目的地。有关如何添加此限制的任何帮助?

D1=read.xlsx("brisbane.xlsx")
km <- kmeans(cbind(D1$lat, D1$lng), centers = 50,iter.max = 100000)
    scluster<-km$cluster
    centers<-km$centers
out1 <- cbind(D1, clusterNum = scluster)
write.csv(out1,"brisbaneout.csv")
write.csv(centers,"brisbanecenters.csv")

1 个答案:

答案 0 :(得分:0)

好的,这是解决类似问题的方法。在线性编程中,这种问题称为“运输”问题。 NP困难意味着解决该问题的唯一真正方法是遍历所有可能的组合,但是已经有了启发式方法来帮助解决这些问题。

这是R中的解决方案

library(lpSolveAPI)

# This is a random matrix of 50 salespeople over 200 locations
# This would really be your distance matrix for each person and town

locations <- matrix(runif(200*50, 1, 500), nrow = 50)

# For the row constraint you want each sales person to travel less than 600 miles
row.signs <- rep ("<", 50)
row.rhs <- rep(600, 50)

# You additionally want each site to be visited once
col.signs <- rep (">", 200)
col.rhs <- rep(1, 200)

# Now you can solve the problem and verify that it converged
# Here we are trying to minimize the distance traveled hence "min"
(result <- lp.transport (locations, "min", row.signs, 
                         row.rhs, col.signs, col.rhs))

# And the output is a matrix with a "1" for each salesperson to visit a specific city
solution <- result$solution

这就是我要解决的方法。