It can usually be shown that the K-means algorithm never repeats the assignment of nodes to clusters from the previous iteration.
Perhaps this is possible in your case due to the additional cost restriction that you introduced, which is traditionally not present when using K-funds, but perhaps this is still not the case, I'm not sure.
I am wondering how you use this assignCluster() method for which you provided the code. Do you have another loop around it that calls the assignCluster() call with finalClusters = list of the last cluster assignments and clusterNodes = list of all nodes and continues the loop until it finishes the assignment equal to the previous one?
If so, are you sure that cluster.addClusterNode() correctly removes the node from its previous cluster (I suppose it should if you implemented it as described above?). Another thing you can pay attention to is the calculation (cluster.getTotalDemand() + node.getCost()) . I suspect that if you look at the cluster that this node is already included, you may not include node.getCost() in this calculation, since it will be considered double if it is also included in cluster.getTotalDemand() .
I had to make some assumptions about what exactly you want to do for the code, or how you implemented other methods for which the code does not appear ... so you need to indicate if there are any errors in my assumption.
source share