package models.clusters;
import java.util.LinkedList;
import java.util.List;
import models.distances.IDistance;
import views.Cluster;
import views.Star;
/**
* Implementation of a hierarcical clustering algorithm
* @author clement
*
*/
public class Hierarchical implements IClusteringAlgorithm {
private IDistance d;
private List<Cluster> clusters;
private Iterable<Star> stars;
private int k;
private final String name = "Hierarchical";
public void setParams(int k, IDistance d, Iterable<Star> stars) {
this.d = d;
this.k = k;
this.stars = stars;
}
@Override
public void run() {
LinkedList<Cluster> clusterList = new LinkedList<>();
int size;
for(Star s : stars){
clusterList.addFirst(new Cluster(s));
}
size = clusterList.size();
Cluster current, toBeMerged = null;
while(size > k){
int index = (int) (Math.random() * size);
current = clusterList.remove(index);
double min = Double.MAX_VALUE;
for(Cluster c : clusterList){
double currentDist = d.get(current.getCentroid(), c.getCentroid());
if(currentDist < min){
min = currentDist;
toBeMerged = c;
}
}
toBeMerged.merge(current);
--size;
}
this.clusters = clusterList;
}
public Iterable<Cluster> getClusters() {
run();
return clusters;
}
@Override
public String getName() {
return this.name;
}
@Override
public Iterable<Cluster> getClusters(String params, IDistance distance, Iterable<Star> stars) {
setParams(Integer.parseInt(params), distance, stars);
return getClusters();
}
@Override
public String getOptionString() {
return "nCluster";
}
@Override
public String getDefaultOptionsValue() {
return "100";
}
}