⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myclusterbyclique.java

📁 Clique聚类的java简单实现
💻 JAVA
字号:
package cluster;

import java.util.*;
import java.util.regex.Pattern;
import java.io.*;

public class MyClusterByClique {
	private static int cellRow;
	private static int cellColumn;
	private static int numberClusters;
	private static Cell[][] cells;
	private static List clusters;

	public static void main(String[] args) {
		try {
			initialize();
			read();
			cluster();
			printResult();
			System.out.println("Cluster by Clique is over.:)");

		} catch (Exception e) {
			System.err.println("Err in main() ::" + e);
		}
	}

	private static void initialize() {
		try {
			cellRow = ClusterConfig.cellRow;
			cellColumn = ClusterConfig.cellColumn;
			numberClusters = 0;
			cells = new Cell[cellRow + 1][cellColumn + 1];
			clusters = new ArrayList();
			clusters.add(new Cluster(numberClusters));
			int i, j;
			for (i = 1; i <= ClusterConfig.cellRow; i++) {
				for (j = 1; j <= ClusterConfig.cellColumn; j++) {
					cells[i][j] = new Cell();
				}
			}

		} catch (Exception e) {
			System.err.println("Err in  ::" + e);
		}

	}// initialize() end;

	private static void read() {
		try {
			BufferedReader inByLine = new BufferedReader(new FileReader(
					ClusterConfig.filePath));
			System.out.println("打开文件成功: " + ClusterConfig.filePath + " opened");
			System.out.println("开始读取: Read() begin");
			String piontStr = null;
			int i = 0;
			while ((piontStr = inByLine.readLine()) != null) {
				double x;
				double y;
				int cellx;
				int celly;
				String[] pointsStr = Pattern.compile("\\s").split(piontStr);
				i++;
				x = Double.valueOf(pointsStr[0]);
				y = Double.valueOf(pointsStr[1]);
				cellx = (int) Math.ceil((x - ClusterConfig.xmin)
						/ ClusterConfig.intervalx);
				celly = (int) Math.ceil((y - ClusterConfig.ymin)
						/ ClusterConfig.intervaly);
				cells[celly][cellx].addnumberPoints();
				if (cells[celly][cellx].getNumberPoints() >= ClusterConfig.densityThreshold)
					cells[celly][cellx].setQualified(1);
				System.out.println("读入点 " + i + "# : " + x + " , " + y);
			}	
			inByLine.close();
			System.out.println("读取结束:Read() finished ");
		} catch (Exception e) {
			System.err.println("Err in read() ::" + e);
		}
	}// read() end

	private static void cluster() {
		try {
			int i, j;
			for (i = 1; i <= ClusterConfig.cellRow; i++) {
				for (j = 1; j <= ClusterConfig.cellColumn; j++) {
					if (cells[i][j].getClusterNo() == 0 && cells[i][j].getChecked() != 1) {
						if (cells[i][j].getQualified() > 0) {
							numberClusters += 1;
							clusters.add(new Cluster(numberClusters));
							retrieve(numberClusters,i,j);
							System.out.println("Cluster():i = " + i + " , j = "
									+ j+"ClusterNo = "+ ((Cluster)clusters.get(numberClusters)).getClusterNo() );
							
						} else {
							cells[i][j].setChecked(1);
							cells[i][j].setClusterNo(0);
						}// end if
					}// end if
				}// end for
			}// end for
			
		} catch (Exception e) {
			System.err.println("Err in cluster() ::" + e);
		}
	}// cluster() end;

	private static void retrieve(int k,int i ,int j) {
		try {
			int l, m;
			if (cells[i][j].getChecked() != 1 &&  cells[i][j].getClusterNo() == 0) {
				System.out.println("infor in retireve( i ="+ i + ", j ="+ j+  " ) :: nubmer of points = "+cells[i][j].getNumberPoints()+"  , number of threshold = "+(int) ((Cluster)clusters.get(k)).getDensity()* ClusterConfig.densityRatioThreshold);
				if (cells[i][j].getQualified() > 0 &&  cells[i][j].getNumberPoints() >= (int) ((Cluster) clusters.get(k)).getDensity()* ClusterConfig.densityRatioThreshold) {
					cells[i][j].setChecked(1);
					cells[i][j].setClusterNo(k);
					((Cluster) clusters.get(k)).addCell(cells[i][j]
							.getNumberPoints());
					// cluster the neighbors of cells[i][j]

					if (i != 1) {
						l = i - 1;
						retrieve(k, l, j);
					}
					if (i != cellColumn) {
						l = i + 1;
						retrieve(k,l,j);
					}
					if (j != 1) {
						m = j - 1;
						retrieve(k,i, m);
					}
					if (j != cellRow) {
						m = j + 1;
						retrieve(k, i, m);
					}
				} else {
					cells[i][j].setChecked(1);
					cells[i][j].setClusterNo(0);
				}
			}

		} catch (Exception e) {
			System.err.println("Err in retrieve() ::" + e);
		}
	}// retrieve() end;

	private static void printResult() {
		try {
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new FileWriter(ClusterConfig.outPath)));
			out.println("*******************aurthor Email.angus006@gmail.com××××××××××××××××××××××××");
			for (int j = 0; j <= cellColumn; j++)
				out.printf("%7d", j);
			out.println("\n");

			for (int i = 1; i <= cellRow; i++) {
				out.printf("%7d", i);
				for (int j = 1; j <= cellColumn; j++){
					//out.printf("%2d", gv.cells[i][j].getClusterNo());
					//out.printf(":");
					out.printf("%7d",cells[i][j].getNumberPoints());
				}
				out.println("\n");
			}
			
			System.out.println("输出单元点数信息完毕,结果写入文件" + ClusterConfig.outPath);
			out.println("\n");
			out.println("\n");
			out.println("\n");
			out.println("\n");
			out.println("\n");
			out.println("\n");
			
			for (int j = 0; j <= cellColumn; j++)
				out.printf("%7d", j);
			out.println("\n");
			for (int i = 1; i <= cellRow; i++) {
				out.printf("%7d", i);
				for (int j = 1; j <= cellColumn; j++){
					out.printf("%2d", cells[i][j].getClusterNo());
					
					out.printf("%7d",cells[i][j].getNumberPoints());
					out.printf(":");
				}
				out.println("\n");
			}
			out.println("下面是各点详细信息,其中类别 0 表示离散点");
			BufferedReader inByLine = new BufferedReader(new FileReader(
					ClusterConfig.filePath));
			System.out.println("打开文件成功: " + ClusterConfig.filePath + " opened");
			System.out.println("开始读取: Read() begin");
			String piontStr = null;
			int i = 0;
			
			while ((piontStr = inByLine.readLine()) != null) {
				double x;
				double y;
				int cellx;
				int celly;
				String[] pointsStr = Pattern.compile("\\s").split(piontStr);
				i++;
				x = Double.valueOf(pointsStr[0]);
				y = Double.valueOf(pointsStr[1]);
				cellx = (int) Math.ceil((x - ClusterConfig.xmin)
						/ ClusterConfig.intervalx);
				celly = (int) Math.ceil((y - ClusterConfig.ymin)
						/ ClusterConfig.intervaly);
				out.println("点 ( "+x+" , "+y + " )  ,  类别:" +cells.clone()[celly][cellx].getClusterNo()+"; 该点位于 行号为 "+celly+ " ,列号为 "+cellx+"的单元格内.");
			}
			
			System.out.println("输出单元聚类信息完毕,结果写入文件" + ClusterConfig.outPath);
			inByLine.close();
			out.close();

		} catch (Exception e) {
			System.err.println("Err in printResult() ::" + e);
		}
	}// printResult() end;

}

⌨️ 快捷键说明

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