it.unibz.algorithms
Class KMeans

java.lang.Object
  extended by javax.swing.SwingWorker<java.lang.Void,java.lang.Void>
      extended by it.unibz.algorithms.KMeans
All Implemented Interfaces:
java.lang.Runnable, java.util.concurrent.Future<java.lang.Void>, java.util.concurrent.RunnableFuture<java.lang.Void>

public class KMeans
extends javax.swing.SwingWorker<java.lang.Void,java.lang.Void>

This class represents the KMeans algorithm in all its iterative steps Extends SwingWorker in order to be executed as a background Thread and to keep control of progress.


Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.SwingWorker
javax.swing.SwingWorker.StateValue
 
Field Summary
private  boolean clearnoise
           
private static java.util.List<Cluster> clusters
           
private  java.util.Vector<Instance> instances
           
private  int numberiterations
           
 
Constructor Summary
KMeans(int k, int iter, java.util.Vector<Instance> instances, boolean clearnoise)
          Base constructor initializes the needed Clusters
 
Method Summary
protected  java.lang.Void doInBackground()
          Executes the main Kmeans algorithm composed by various steps Note: The various isCancelled() are used to proper stop this thread if the user closes the operation.
private  Centroid generateCentroid(int currentpos)
          Method generates the first centroids used to perform initial steps
 java.util.List<Cluster> getClusters()
          Method returns all Clusters to be printed out
private  void initializeCentroids()
          Method used to complete step 1 of the Kmeans algorithm.
private  void randomAssign()
          Method is used for step 2.
private  void refreshCentroids()
          Method refreshes all clusters centroid
 
Methods inherited from class javax.swing.SwingWorker
addPropertyChangeListener, cancel, done, execute, firePropertyChange, get, get, getProgress, getPropertyChangeSupport, getState, isCancelled, isDone, process, publish, removePropertyChangeListener, run, setProgress
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

clusters

private static java.util.List<Cluster> clusters

numberiterations

private int numberiterations

clearnoise

private boolean clearnoise

instances

private java.util.Vector<Instance> instances
Constructor Detail

KMeans

public KMeans(int k,
              int iter,
              java.util.Vector<Instance> instances,
              boolean clearnoise)
Base constructor initializes the needed Clusters

Method Detail

initializeCentroids

private void initializeCentroids()
Method used to complete step 1 of the Kmeans algorithm. It initalizes the various centroids


generateCentroid

private Centroid generateCentroid(int currentpos)
Method generates the first centroids used to perform initial steps

Parameters:
currentpos - Is the current position of the loop
Returns:
A new Cluster for given position

doInBackground

protected java.lang.Void doInBackground()
                                 throws java.lang.Exception
Executes the main Kmeans algorithm composed by various steps Note: The various isCancelled() are used to proper stop this thread if the user closes the operation. Note: The setProgress() method calls are used to keep the ProgressBar up to date 1. Initialize centroid 2. assign Instances randomly to clusters 3. Perform the initial evaluation of distances --> Done directly when adding a Instance to a cluster 4. Refresh Centroids at least once 5. Start the main computation and stop if we no more moves can be done or the selected number of iterations has been reached

Specified by:
doInBackground in class javax.swing.SwingWorker<java.lang.Void,java.lang.Void>
Throws:
java.lang.Exception

refreshCentroids

private void refreshCentroids()
Method refreshes all clusters centroid


randomAssign

private void randomAssign()
Method is used for step 2. It randomly assigns various Instances to Cluster


getClusters

public java.util.List<Cluster> getClusters()
Method returns all Clusters to be printed out