com.venky.clustering.Cluster Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common Show documentation
Show all versions of common Show documentation
Commonly used programming tasks in java
package com.venky.clustering;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import com.venky.core.string.StringUtil;
public class Cluster {
private CenterFinder centerFinder;
private Metric metric;
private int id ;
private static AtomicInteger fakeId = new AtomicInteger();
public Cluster(CenterFinderBuilder cf,Metric m){
this.centerFinder = cf.build(this);
this.metric = m;
this.id = fakeId.addAndGet(1);
}
public int hashCode(){
return id;
}
public String toString(){
return StringUtil.valueOf(id);
}
public boolean equals(Cluster cluster){
return id == cluster.id;
}
private T centroid = null;
public T centroid(){
if (centroid == null && centerFinder != null){
centroid = centerFinder.center();
}
return centroid;
}
private List points = new ArrayList();
public void addPoint(T t){
points.add(t);
if (centerFinder != null){
centroid = centerFinder.center(t);
}
}
public double centroidDistance(T point){
T centroid = centroid();
if (centroid != null) {
return metric.distance(centroid, point);
}
return Double.POSITIVE_INFINITY;
}
public double centroidDistance(Cluster cluster){
T centroid = centroid();
if (centroid != null) {
return metric.distance(centroid, cluster.centroid());
}
return Double.POSITIVE_INFINITY;
}
public Distance distance(T point){
Distance distance = new Distance();
distance.distanceFromCentroid = centroidDistance(point);
distance.minDistance = Double.POSITIVE_INFINITY;
distance.maxDistance = Double.NEGATIVE_INFINITY;
for (T p: points){
double d = metric.distance(p, point);
if (d < distance.minDistance){
distance.minDistance = d;
}
if (d > distance.maxDistance){
distance.maxDistance = d;
}
}
return distance;
}
public List getPoints(){
return points;
}
public Distance distance(Cluster cluster){
Distance distance = new Distance();
distance.distanceFromCentroid = centroidDistance(cluster);
distance.minDistance = Double.POSITIVE_INFINITY;
distance.maxDistance = Double.NEGATIVE_INFINITY;
for (Iterator i = points.iterator() ; i.hasNext() && distance.maxDistance < Double.POSITIVE_INFINITY ; ){
T aPointInThisCluster = i.next();
for (Iterator otherClusterPointIterator = cluster.getPoints().iterator(); otherClusterPointIterator.hasNext() && distance.maxDistance < Double.POSITIVE_INFINITY ; ){
T aPointInOtherCluster = otherClusterPointIterator.next();
double d = metric.distance(aPointInThisCluster, aPointInOtherCluster);
if (d < distance.minDistance){
distance.minDistance = d;
}
if (d > distance.maxDistance){
distance.maxDistance = d;
}
}
}
return distance;
}
public static class Distance {
private double distanceFromCentroid;
private double minDistance;
private double maxDistance;
public double getDistanceFromCentroid() {
return this.distanceFromCentroid;
}
public double getMinDistance() {
return this.minDistance;
}
public double getMaxDistance() {
return this.maxDistance;
}
}
}