private void Seperation(Centroid centroid) { // filter out the cluster which has customers with identical id as that of the centroid which we want to calculate the distance with. List <Centroid> tempCentroidList = (KmeansCentroids.Where(c => c.Id != centroid.Id)).ToList(); for (int i = 0; i < tempCentroidList.Count; i++) { for (int customer = 0; customer < centroid.cluster.Count; customer++) { double sumdistance = 0; // accumulator for (int other_customer = 0; other_customer < tempCentroidList[i].cluster.Count; other_customer++) { if (tempCentroidList[i].cluster[other_customer].CustomerId < centroid.cluster[customer].CustomerId) { // if the value cant't be found in the matrix because of the a> b & b > a implementation, flip the customer and the other customer to find th distance value. sumdistance += Distance_Matrix[tempCentroidList[i].cluster[other_customer].CustomerId] [centroid.cluster[customer].CustomerId]; } else { sumdistance += Distance_Matrix[centroid.cluster[customer].CustomerId] [tempCentroidList[i].cluster[other_customer].CustomerId]; } } // add the accumulated results to the list of neighbours of the customer centroid.cluster[customer].AddNeighbhour(new Tuple <int, double>(tempCentroidList[i].Id, sumdistance)); } } }
//function that returns the average of the sillouette private void SilhouetteCalculation(List <Centroid> kmeansCentroids) { var listCohesianSeperation = new List <Tuple <double, double> >(); //foreach centroid calculate the cohesion and seperation and appendthe results to a list kmeansCentroids.ForEach(cluster => { Cohension(cluster); Seperation(cluster); }); // calculate the sillhouette for each customer kmeansCentroids.ForEach(x => x.cluster.ForEach(cust => cust.calculateSilhouette())); // the sillhouettes of every customer are sbeing summed and divided by the amount of customers to obtain the average Silhouette. var sumSilouettte = KmeansCentroids.Select(cluster => cluster.cluster.Select(cust => cust.customerSillhouette).Sum()).Sum(); AGVSillouette = sumSilouettte / Data.Count(); }