package multinomad.tools.kmeans;

import java.util.ArrayList;
import java.util.Iterator;

import multinomad.individuals.DoubleChromosome;
import multinomad.individuals.DoubleIndividual;

public class Centroid extends DoubleIndividual implements Iterable<DoubleIndividual>{
	
	ArrayList<DoubleIndividual> cluster;
	
	//A way to initialize the centroid
	public Centroid(DoubleIndividual ind) {
		super(ind.getChr().asdouble());
		cluster = new ArrayList<DoubleIndividual>();
	}
	
	public void updateMean(double[] newmean) {
		this.setChr(new DoubleChromosome(newmean));
	}
	
	public boolean isInCluster(DoubleIndividual ind) {
		double[] x2 =ind.getChr().asdouble();
		for(DoubleIndividual itemInCluster : cluster) {
			double[] x1 = itemInCluster.getChr().asdouble();
			if (areEqual(x1, x2)) {
				break;
			}
		}
		//return isInThere;
		return cluster.contains(ind);
	}
	
	private boolean areEqual(double[] x1, double[] x2) {
		for (int i=0;i<x1.length;i++) {
			if(x1[i] != x2[i])
				return false;
		}
		return true;
	}
	
	public boolean addToCluster(DoubleIndividual ind) {
		return cluster.add(ind);
	}
	
	public boolean removeFromCluster (DoubleIndividual ind) {
		return cluster.remove(ind);
	}
	
	public int clusterSize() {
		return cluster.size();
	}
	
	public Iterator<DoubleIndividual> iterator() {
		return cluster.iterator();
	}

}
