我在交互式遗传算法中使用来自 Apache Commons Math 的 k-means++ 聚类器,以减少用户评估的个体数量。
Commons Math 使其非常易于使用。用户只需要实现
Clusterable
接口。它有两种方法:
double distanceFrom(T p)
这很清楚 和T centroidOf(Collection<T> p)
,它可以让用户选择一个集群的质心。
如果在欧几里得点上使用,质心很容易计算。但在染色体上却相当困难,因为它们的含义并不总是很清楚。
我的问题:是否有一种有效的通用方法来选择质心,而不取决于问题域?(例如通过使用距离)
编辑
好的,现在这是我的质心计算代码。这个想法:与所有其他点的总距离最短的点离质心最近。
public T centroidOf(Collection<T> c) {
double minDist = Double.MAX_VALUE;
T minP = null;
// iterate through c
final Iterator<T> it = c.iterator();
while (it.hasNext()) {
// test every point p1
final T p1 = it.next();
double totalDist = 0d;
for (final T p2 : c) {
// sum up the distance to all points p2 | p2!=p1
if (p2 != p1) {
totalDist += p1.distanceFrom(p2);
}
}
// if the current distance is lower that the min, take it as new min
if (totalDist < minDist) {
minDist = totalDist;
minP = p1;
}
}
return minP;
}