Results 1 to 2 of 2
  1. #1
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Creating a hierarchical divisive clustering algorithm

    It works( or should work) like this. The main "Divisive" class reads a file using the Read class and sets it as a linked list Table (Reading works, no problem there). Then one cluster is created, passing the whole table and adding the newly created cluster to list for management. The cluster creates it's distance matrix from the passed table. The cluster has to be divided, so the Split() method is called, finds the MAX distance between to elements in all of the clusters, afterwards goes through all the elements and adds it to the closest cluster. Everything seems fine one paper, but the algorithm works only if i call Split() once. Could it be a problem with how java passes variables? After the second call to Split() I'm getting empty tables and matrix's.. Thank you.

    Main "Divisive" class:

    Java Code:
    package clusters;
    
    import java.util.LinkedList;
    
    public class Divisive 
    {
    	LinkedList<Record> table;
    	LinkedList<Cluster> clusterList;
    	LinkedList<Cluster> temp;
    	
    	public void Split()
    	{
    		MatrixElement max=new MatrixElement(0,0,0);
    		int pos=0;
    		for(int k=0;k<clusterList.size();k++)
    		{
    			MatrixElement temp=clusterList.get(k).MaxDistance();
    			if(temp.value>max.value)
    			{
    				max=temp;
    			}
    		}
    		
    		System.out.println(pos+". cluster: "+max.rowId+" "+max.columnId+" "+max.value);
    		
    		if(max.value>1.9)
    		{
    			LinkedList<Record> tempOne=new LinkedList<Record>();
    			LinkedList<Record> tempTwo=new LinkedList<Record>();
    			LinkedList<Integer> iD=new LinkedList<Integer>();
    			
    			for(int i=0;i<table.size();i++)
    			{
    				if(!iD.contains(table.get(i).id))
    				{
    					iD.add(table.get(i).id);
    				}
    			}
    			
    			for(int i=0;i<iD.size();i++) //goes through the clusters record id table
    			{
    				for(int j=0;j<clusterList.get(pos).table.size();j++) //goes through the clusters records
    				{
    					if(table.get(j).id==iD.get(i)) //finds the location of the first id
    					{
    						for(int k=0;k<clusterList.get(pos).matrix.size();k++) //goes through the matrix
    						{
    							if(clusterList.get(pos).matrix.get(k).id==iD.get(i)) //finds the id in the matrix 
    							{
    								if(clusterList.get(pos).matrix.get(k).rowValues.get(max.columnId).value > clusterList.get(pos).matrix.get(k).rowValues.get(max.rowId).value)
    								{
    									tempOne.add(table.get(j));
    									table.remove(j);
    									System.out.println("i got here!");
    								}
    								else 
    								{
    									tempTwo.add(table.get(j));
    									table.remove(j);
    									System.out.println("i got here!");
    								}
    							}
    						}
    					}
    				}
    			}
    			
    			clusterList.add(new Cluster(tempTwo,this));
    			clusterList.add(new Cluster(tempOne,this));
    			clusterList.remove(pos);
    		}
    		else
    		{
    			
    		}
    	}
    	
    	Divisive()
    	{
    		clusterList=new LinkedList<Cluster>();
    		temp=new LinkedList<Cluster>();
    		Read read=new Read(false,"Example.xlsx");
    		this.table=read.table;
    		clusterList.add(new Cluster(table,this));
    		Split();
    		Split();
    		for(int i=0;i<clusterList.size();i++)
    		{
    			clusterList.get(i).WriteFile(i);
    			System.out.println("Matrix size: "+clusterList.get(i).matrix.size());
    			System.out.println("Table size: "+clusterList.get(i).table.size());
    		}
    	}
    	
    	public static void main(String[] args) 
    	{
    		new Divisive();
    	}
    
    }
    The cluster class:

    Java Code:
    package clusters;
    
    import java.util.LinkedList;
    
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import java.io.*;
    
    public class Cluster 
    {
    	LinkedList<Record> table;
    	LinkedList<MatrixRow> matrix;
    	Divisive parent;
    	
    	public MatrixElement MaxDistance()
    	{
    		MatrixElement max=new MatrixElement(0,0,0);
    		for(int i=0;i<matrix.size();i++)
    		{
    			for(int j=0;j<matrix.size();j++)
    			{
    				if(matrix.get(i).rowValues.get(j).value>max.value && i!=j)
    				{
    					max=matrix.get(i).rowValues.get(j);
    				}
    			}
    		}
    		return max;
    	}
    	
    	class RecordID
    	{
    		int i,j,id;
    		RecordID(int i,int id)
    		{
    			this.i=i;
    			this.id=id;
    		}
    	}
    	
    	public void CreateMatrix()
    	{
    		matrix=new LinkedList<MatrixRow>();
    		
    		LinkedList<RecordID> ids=new LinkedList<RecordID>();
    		for(int i=0;i<table.size();i++)
    		{
    			
    				ids.add(new RecordID(i,table.get(i).id));
    			
    		}
    	
    		for(int i=0;i<table.size();i++)
    		{
    			MatrixRow matrixRow=new MatrixRow(ids.get(i).id);
    			
    			for(int j=0;j<table.size();j++)
    			{
    				matrixRow.rowValues.add(new MatrixElement(i,j,Euclede(table.get(i), table.get(j))));
    			}
    			matrix.add(matrixRow);
    		}
    		
    	}
    	
    	Cluster(LinkedList<Record> table, Divisive parent)
    	{
    		this.parent=parent;
    		this.table=table;
    		CreateMatrix();
    	}
    	
    	public double Euclede(Record one, Record two)
    	{
    		double sum=0;
    		for(int i=0;i<one.values.size();i++)
    		{
    			sum+=Math.pow(one.values.get(i)-two.values.get(i), 2);
    		}
    		return (double) Math.sqrt(sum);
    	}
    	
    	public void WriteFile(int sheet)
    	{
    		Workbook workbook=new XSSFWorkbook();
    		Sheet targetSheet=workbook.createSheet();
    		
    		for(int i=0;i<matrix.size();i++)
    		{
    			Row target=targetSheet.createRow(i);
    			for(int j=0;j<matrix.size();j++)
    			{
    				Cell targetCell=target.createCell(j);
    				targetCell.setCellValue(matrix.get(i).rowValues.get(j).value);
    			}
    		}
    		try{
    			FileOutputStream output=new FileOutputStream("test"+sheet+".xlsx");
    			workbook.write(output);
    			output.close();
    		}catch(Exception e){
    			
    		}	
    	}
    }
    Matrix Row class:

    Java Code:
    package clusters;
    
    import java.util.LinkedList;
    
    public class MatrixRow 
    {
    	int id;
    	LinkedList<MatrixElement> rowValues;
    	
    	MatrixRow(int id)
    	{
    		this.id=id;
    		rowValues=new LinkedList<MatrixElement>();
    	}
    }
    File reading:

    Java Code:
    package clusters;
    
    import org.apache.poi.openxml4j.opc.OPCPackage;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import java.io.*;
    import java.util.LinkedList;
    
    public class Read implements Runnable{
    	
    	FileLoader parent;
    	LinkedList<Record> table;
    	int skip=0;
    	String filename;
    	
    	public void run()
    	{
    		try{
    			OPCPackage excel = OPCPackage.open(new File(filename));
    			XSSFWorkbook workBook = new XSSFWorkbook(excel);
    			Sheet sheet=workBook.getSheetAt(0);
    			 
    			table=new LinkedList<Record>();
    			 for(int i=skip;i<sheet.getLastRowNum()+1;i++) //atsija pirmo rindu
    			 { 
    				 Row temp=sheet.getRow(i);
    				 Record rec=new Record();
    				 table.add(rec);
    				 
    				 
    				 
    				 for(int j=0;j<temp.getLastCellNum();j++)
    				 {
    					 Cell cell=temp.getCell(j);
    					 if(j==temp.getLastCellNum()-1)
    					 {
    						 rec.realType=cell.getStringCellValue();
    					}
    					else
    					{
    						switch(cell.getCellType())
    						{
    						case Cell.CELL_TYPE_NUMERIC:
    							rec.values.add(cell.getNumericCellValue());
    							break;
    						case Cell.CELL_TYPE_STRING:
    							rec.values.add((double)0);
    							break;
    						}
    						
    					}
    				 }
    			 }
    			Average();
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		
    		//parent.setTable(table);
    	}
    	
    	public void Average()
    	{
    		for(int i=0;i<table.get(0).values.size();i++)
    		{
    				double avg=0;
    				for(int k=0;k<table.size();k++)
    				{
    					avg+=table.get(k).values.get(i);
    				}
    				avg=avg/(table.size()); 
    				for(int k=0;k<table.size();k++)
    				{
    					if(table.get(k).values.get(i).doubleValue()==0)
    					{
    						table.get(k).values.set(i, avg);
    					}
    				}
    		}
    	}
    	
    	Read(boolean skip, FileLoader parent,String filename)
    	{
    		this.filename=filename;
    		this.parent=parent;
    		if(skip==true)
    		{
    			this.skip=1;
    		}
    		run();
    	}
    	
    	Read(boolean skip,String filename)
    	{
    		this.filename=filename;
    		if(skip==true)
    		{
    			this.skip=1;
    		}
    		run();
    	}
    }
    Tables line:

    Java Code:
    package clusters;
    
    import java.util.LinkedList;
    
    public class Record {
    	int id;
    	LinkedList<Double> values;
    
    	Record()
    	{
    		values=new LinkedList<Double>();
    	}
    }
    MatrixElement:

    Java Code:
    package clusters;
    
    public class MatrixElement 
    {
    	int rowId,columnId;
    	double value;
    	
    	MatrixElement(int rowId, int columnId, double value)
    	{
    		this.rowId=rowId;
    		this.columnId=columnId;
    		this.value=value;
    	}
    }

  2. #2
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Creating a hierarchical divisive clustering algorithm

    Quote Originally Posted by MustSeeMelons View Post
    how java passes variables? After the second call to Split() I'm getting empty tables and matrix's..
    I think you will create and call Split method with parameters that like..

    you create method like
    Java Code:
    public void Split(LinkedList<Cluster> clusterList){
    
    //code here
    }
    call like
    Java Code:
    Split(clusterList);
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

Similar Threads

  1. jitterbit clustering
    By shivshambhu in forum Forum Lobby
    Replies: 0
    Last Post: 05-11-2012, 10:42 AM
  2. Java and clustering
    By jeroen Vranckx in forum Advanced Java
    Replies: 4
    Last Post: 04-19-2012, 10:44 AM
  3. Replies: 16
    Last Post: 12-24-2011, 04:06 PM
  4. k-means algorithm for document clustering
    By Ramya Selvaraj in forum Forum Guides
    Replies: 0
    Last Post: 02-06-2009, 06:11 AM
  5. Concurrent Hierarchical State Machine 4.3
    By levent in forum Java Software
    Replies: 0
    Last Post: 08-03-2007, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •