package machinelearning.kmeans;

import java.util.List;

public class ClustersScoring<T>{

    List<Cluster<T>> clusters = null;
    GenericTool<T> genericTool = null;

    public ClustersScoring(List<Cluster<T>> clusters, GenericTool<T> genericTool){
        this.clusters = clusters;
        this.genericTool = genericTool;
    }

    public double getSilhouetteScore(){
        for(Cluster<T> cluster : this.clusters){
            
        }
        return 0.0;
    }
    
    private double computeSilhouetteForPoint(T point, Cluster<T> associatedCluster, Cluster<T> nearestCluster){
        double a = computeSilhouettePartClusterForPoint(point, associatedCluster, true);
        double b = computeSilhouettePartClusterForPoint(point, nearestCluster, false);
        double silhouette = (b - a)/(Math.max(a,b));
    }
    
    private double computeSilhouettePartClusterForPoint(T point, Cluster<T> cluster,boolean sameCluster){
        double value = 0.0;
        for(T otherPoint : cluster.getPoints()){
            if(!sameCluster || !otherPoint.equals(point))
                value += genericTool.distanceBetween(point, otherPoint);
        }
        return value /= ((double)cluster.getSize());
    }

    public double getBasicScore(){
        double score = 0.0;
        int clustersSize = clusters.size();
        for(int i=0; i<clustersSize; i++){
            Cluster<T> cluster = clusters.get(i);
            for(int j=i+1; j<clustersSize; j++){
                score += this.genericTool.distanceBetween(cluster.getCentroid(),clusters.get(j).getCentroid());
            }
            double distanceInCluster = 0.0;
            for(T point : cluster.getPoints()){
                distanceInCluster += genericTool.distanceBetween(point,cluster.getCentroid());
            }
            score += 1.0/distanceInCluster;
        }
        return score;
    }
}

@@ -378,90 +378,1165 @@
     
-for(Cluster%3CT%3E cluster : this.clusters)%7B%0A            %0A        %7D%0A        return 0.0
+double score = 0.0;%0A        double numberOfPoints = 0.0;%0A        for(Cluster%3CT%3E cluster : this.clusters)%7B%0A            for(T point : cluster.getPoints())%7B%0A                score += computeSilhouetteForPoint(point, cluster, getNearestCluster(cluster));%0A                numberOfPoints++;%0A            %7D%0A        %7D%0A        return score / numberOfPoints;%0A    %7D%0A    %0A    private Cluster%3CT%3E getNearestCluster(Cluster%3CT%3E cluster) throws AlgorithmException%7B%0A        if(this.clusters.size()==1)%7B%0A            throw new AlgorithmException(%22Try to get silhouette but only one cluster%22);%0A            return null;%0A        %7D%0A        Cluster%3CT%3E nearest = null;%0A        T centroid = cluster.getCentroid();%0A        double minDist = Double.MAX_VALUE;%0A        for(Cluster%3CT%3E tmpCluster : this.clusters)%7B%0A            if(!cluster.equals(tmpCluster))%7B%0A                T tmpCentroid = tmpCluster.getCentroid();%0A                double tmpDist = this.genericTool.distanceBetween(centroid, tmpCentroid);%0A                if(tmpDist %3C minDist)%7B%0A                    nearest = tmpCluster;%0A                    minDist = tmpDist;%0A                %7D%0A            %7D%0A        %7D%0A        return nearest
 ;%0A  

Update ClustersScoring.java