gaclust.java

来自「clustering data for the different techni」· Java 代码 · 共 1,955 行 · 第 1/5 页

JAVA
1,955
字号
	refPart = db[nCols-1];	// do not use this attribute in clustering	nColsClust--;	nColsClust_copy--;      }    GALogTextArea.append("\n***\nClustering database:" + Strings.endl + DBName 		      + "(" + nRows + " rows " 		      + nColsClust + " attributes)" + Strings.endl);        if (useWeights == 1)      {	GALogTextArea.append("Computing weights" + Strings.endl);	    	wTargetCondOnDB = new double[nCols - 1];	wDBCondOnTarget = new double[nCols - 1];	weight = new double[nCols - 1];	    	targetEntr = Partition.estimateWeights(db, nCols, sampleDBPct,					       entropyMeasure, 					       wTargetCondOnDB, 					       wDBCondOnTarget, 					       weight, rand);	    	if (excludeAttr == 1)	  {	    // keep at least one attribute	    if (noRemAttr < 1)	      noRemAttr++;			    int[] remAttr = new int[noRemAttr];	    double[] sortingValues = new double[nColsClust];	    for (int i = 0; i < nColsClust; i++)	      sortingValues[i] = Math.abs(targetEntr 					  - wTargetCondOnDB[i]);			    // sort the indices of the attributes in descending order	    SelectionMethods.selectBest(sortingValues, nColsClust, 0, 					remAttr, noRemAttr);			    GALogTextArea.append("attributes that are considered: ");	    for (int i = 0; i < noRemAttr; i++)	      GALogTextArea.append(remAttr[i] + " ");	    GALogTextArea.append(Strings.endl);			    // change the order of the references in the database such	    // that the first noRemAttr be the remaining attributes	    // followed by the target attribute	    for (int i = 0; i < noRemAttr; i++)	      {		Partition temp = db[i];		db[i] = db[remAttr[i]];		db[remAttr[i]] = temp;		    		// change also the corresponding weights		double tempWeight = wTargetCondOnDB[i];		wTargetCondOnDB[i] = wTargetCondOnDB[remAttr[i]];		wTargetCondOnDB[remAttr[i]] = tempWeight;		    		tempWeight = wDBCondOnTarget[i];		wDBCondOnTarget[i] = wDBCondOnTarget[remAttr[i]];		weight[remAttr[i]] = tempWeight;		    		tempWeight = weight[i];		weight[i] = weight[remAttr[i]];		weight[remAttr[i]] = tempWeight;	      }			    // one last swap to put target attribute on position	    // noRemAttr	    Partition temp = db[noRemAttr];	    db[noRemAttr] = db[nCols-1];	    db[nCols-1] = temp;	    nColsClust = noRemAttr;			    GALogTextArea.append("Working with " + nColsClust 			      + " attributes" + Strings.endl);	  } // end of excludeAttr test	    	if (fitnessMeasure == Global.FM_NORM_W)	  {	    // normilize the weights  	    double allWeights = 0.0;	    for (int i = 0; i < nColsClust; i++)	      allWeights += weight[i];			    for (int i = 0; i < nColsClust ; i++)	      weight[i] /= allWeights;			    allWeights = 0.0;	    for (int i = 0; i < nColsClust; i++)	      allWeights += wDBCondOnTarget[i];			    for (int i = 0; i < nColsClust ; i++)	      wDBCondOnTarget[i] /= allWeights;			    allWeights = 0.0;	    for (int i = 0; i < nColsClust; i++)	      allWeights += wTargetCondOnDB[i];			    for (int i = 0; i < nColsClust ; i++)	      wTargetCondOnDB[i] /= allWeights;			    GALogTextArea.append("normalized total weights" + Strings.endl);	    GALogTextArea.append("(attrNo)  wTCondOnDB  wDBCondOnT  sum" + Strings.endl);	    for (int i = 0; i < nColsClust; i++)	      GALogTextArea.append("(" + i + ")  "				   + nf.format(wTargetCondOnDB[i])				   + "  " + nf.format(wDBCondOnTarget[i])				   + "  " + nf.format(weight[i]) 				   + Strings.endl);	  } // end fitness Global.FM_NORM_W loop      } // end useWeights test    else      {	// no attribute weights	wTargetCondOnDB = null;	wDBCondOnTarget = null;	weight = null;      }	    // for this fitness measure print statistics about    // entropy of intersection and average entropy    if (fitnessMeasure == Global.FM_ALTERNATE_HAVG)      {	double entropyIntersect;	int noClassesIntersect;	Vector info = new Vector(2);	// compute informations about the attribute partitions	Partition.computeInfoIntersections(db, nColsClust, 					   entropyMeasure, info);	entropyIntersect = ((Double)info.elementAt(0)).doubleValue(); 	noClassesIntersect = ((Integer)info.elementAt(1)).intValue();	    	GALogTextArea.append("Intersection of attribute partitions"			     + " has entropy " 			     + nf.format(entropyIntersect) 			     + " and " + noClassesIntersect 			     + " blocks" + Strings.endl); 	    	// compute average entropy of attribute partitions	double avgEntr = 0.0;	for (int i = 0; i < nColsClust; i++)	  {	    double attrPartEntropy = db[i].entropy(entropyMeasure);			    if (Global.VERBOSE == 1)	      {		GALogTextArea.append(attrPartEntropy + Strings.endl);		output = new StringBuffer();		db[i].printClassCardinalities(output);		GALogTextArea.append(output.toString());	      }			    avgEntr += attrPartEntropy;	  }	    	GALogTextArea.append("Average entropy of attribute partitions "			  + nf.format(avgEntr/(double)nColsClust) + Strings.endl);      }	    GALogTextArea.append("Initializing the GA" + Strings.endl);    ga = new GA(popSize, nRows, K, crossoverRate, mutationRate, 		classFitnessType, crossoverType, mutationType, 		distributionType, entropyMeasure, fitnessMeasure, 		minimization, fitnessThreshold, consecItrs, rand, 		db, nColsClust, wTargetCondOnDB, wDBCondOnTarget, 		weight, this);	    status = GA_RUNNING;    lastFitnessMeasureUsed = fitnessMeasure;    GALogTextArea.append("Starting the evolution" + Strings.endl);        //delete the old table with results if there is one    if (resModel != null)      resModel.reset();    // while the ga is running desable the Tab for database    // selection/generation    MainTabbedPane.setEnabledAt(0, false);    MainTabbedPane.setEnabledAt(1, false);    timer.reset();    timer.start();        try      {	ga.start();      }    catch(Throwable e)      {	GALogTextArea.append(Strings.ERROR + e + Strings.endl);      }  }  /** this function is called at the thread termination */  public void threadTermination(Thread t)  {    if (t == genSynDB)      {	isGenerating = false;	GenProgressBar.setValue(0);	if (nRows != genSynDB.getRealRowNo())	  GenLogTextArea.append("Generation of database was aborted!" + Strings.endl);	// get real row number	nRows = genSynDB.getRealRowNo();	// set size of the partition to be this number	for (int i = 0; i < nCols; i++)	  db[i].setSize(nRows);	GenLogTextArea.append("Generated database: " + DBName);	GenLogTextArea.append("(" + nRows + " rows " + nCols + " attributes " 			      + nDBClasses + " classes) " + Strings.endl);		GenDBTable.setModel(new DBTableModel());	MainTabbedPane.setEnabledAt(2, true);	flipTarget = true; // we get a new database we can flip the			   // target if required	status = IDLE;	GenAbortButton.setEnabled(false);	// set the copy of the database	db_copy = new Partition[nCols];	for (int i = 0; i < nCols; i++)	  db_copy[i] = db[i];      }    if (t == ga)      {	timer.stop();	ClusterAbortButton.setEnabled(false);	Vector v = null;	try	  {	    v = ga.getResults();	  }	catch(IllegalStateException e)	  {	    GALogTextArea.append(e.getMessage() + Strings.endl);	    return;	  }	finalChrom = (Chromosome)v.elementAt(0);	noIterations = (Integer)v.elementAt(1);      	if (ga.areResultsFinal())	  GALogTextArea.append("Results are final" + Strings.endl);	else // if (ga.areResultsPartial())	  GALogTextArea.append("Results are partial" + Strings.endl);	if (useTarget == 1)	  GAResultsTable.setModel(resModel = new GAResultsReferenceModel());	else	  GAResultsTable.setModel(resModel = new GAResultsModel());	ClusterButton.setEnabled(true);	status = IDLE;	// enable the Tab for database selection/generation	MainTabbedPane.setEnabledAt(0, true);	MainTabbedPane.setEnabledAt(1, true);	if (lastFitnessMeasureUsed == Global.FM_WE)	  {	    // restore the database	    for (int i = 0; i < nCols; i++)	      db[i] = db_copy[i];	    nColsClust = nColsClust_copy;	  }      }  }  /** Exit the Application */  private void exitForm(java.awt.event.WindowEvent evt)   {    System.exit(0);  }    /**   * @param args the command line arguments   */  public static void main(String args[])   {    new GAClust().show();  }  // GAResultsTable model  private abstract class ResultsTableModel extends AbstractTableModel  {    protected ArrayList cells;    protected ArrayList column_names;    public int getRowCount()    {      return cells.size();    }      public int getColumnCount()    {      return column_names.size();    }        public Object getValueAt(int row, int column)    {      ArrayList cellRow = (ArrayList)cells.get(row);      Object cell = cellRow.get(column);      return cell;    }        public String getColumnName(int i)    {      if (i < column_names.size())	return column_names.get(i).toString();      return "";    }    public void reset()    {      cells.clear();      column_names.clear();      fireTableDataChanged();      fireTableStructureChanged();    }  }  // DBTable model  private class DBTableModel extends ResultsTableModel  {    public DBTableModel()    {      cells = new ArrayList(nCols);      column_names = new ArrayList();      column_names.add("Attribute name");      column_names.add("Class (cardinality)");      for (int i = 0; i < nCols; i++)	{	  ArrayList innerCell = new ArrayList();	  innerCell.add("Attribute " + i);	  innerCell.add(db[i].getClassesAndCardinalities());	  cells.add(innerCell);	}      fireTableDataChanged();    }    public void reset()    {      for (int i = 0; i < cells.size(); i++)	((ArrayList)cells.get(i)).clear();      cells.clear();      column_names.clear();      fireTableDataChanged();      fireTableStructureChanged();    }  }  // GAResultsReferenceModel model  private class GAResultsReferenceModel extends ResultsTableModel  {    public GAResultsReferenceModel()    {      // get a local copy of the reference Partition      Partition refPartLocal = new Partition(refPart.getSize());      refPartLocal.set(refPart);      cells = new ArrayList();      column_names = new ArrayList();      column_names.add("Reference");      column_names.add("Clustering");      ArrayList innerCell = new ArrayList();      innerCell.add("H(reference) = " 		    + nf.format(refPartLocal.entropy(entropyMeasure)));      innerCell.add("H(clustering) = " 		    + nf.format(finalChrom.entropy(entropyMeasure)));      cells.add(innerCell);      innerCell = new ArrayList();      refPartLocal.computeFitness(db, nColsClust, 				  entropyMeasure, fitnessMeasure, 				  wTargetCondOnDB, wDBCondOnTarget, weight);      innerCell.add("Fitness = " + nf.format(refPartLocal.getFitness()));      finalChrom.computeFitness(db, nColsClust, 				entropyMeasure, fitnessMeasure, 				wTargetCondOnDB, wDBCondOnTarget, weight);      innerCell.add("Fitness = " + nf.format

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?