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

📄 generalmethod.java

📁 java编写的最短路径算法
💻 JAVA
字号:
package engine;

import integrate.Structure;
public class GeneralMethod {
	public shortestPath sPath;
	public genAlgrthm genRoute;
	public int route[] = new int[41];
	double distance[][]= new double[40][40];
	int cells;
	public double minDisGreedy;
	public int optimalRoute[]=new int[41];
	public double optDis;
	
	public GeneralMethod(Structure input){
		sPath = new shortestPath();
		sPath.cells=input.unitNum;
		
		for (int i=0; i<input.unitNum; i++){
			sPath.xCell[i]=input.unitList[i].layer;
			sPath.yCell[i]=input.unitList[i].rank;
		}
		
	}

	
	public void shortestTest(){
		System.out.print("Step 1: shortest path method"+"\n");
//		shortestPath sPath = new shortestPath();
//		sPath.randomTestData();
//		sPath.preEvaluate();
		
		cells = sPath.cells;
		for (int i = 0; i < cells; i++) {
			int x = sPath.xCell[i];
			int y = sPath.yCell[i];
			System.out.print(x);
			System.out.print(y);
			System.out.print('\n');
		}
		double minDistance = sPath.repeatetiveNearestNeighbour();
		
		
		for (int j = 0; j < cells+1; j++){
			optimalRoute[j]=sPath.route[j];
			route[j]= sPath.route[j];
			System.out.print(sPath.route[j]);
			System.out.print('\n');
		}
		
		for (int i = 0; i < cells; i++){
			for(int j = 0; j < cells; j++){
				distance[i][j]=sPath.d[i][j];
			}
		}
		minDisGreedy=minDistance;
		System.out.print(minDistance);
	}
	
	public void genAlTest(){
		System.out.print("\n"+"Step 2:genetic algorithm"+"\n");

		genRoute = new genAlgrthm(minDisGreedy, optimalRoute);
		genRoute.initialRoute(route);
		genRoute.iteration(cells);
		System.out.print("iterations: "+genRoute.iterations+"\n");

		int it = genRoute.iterations;
//		genRoute.initDistances();
		
		int k=0;
		while (k<=it){
			/*
			for (k = 0; k < cells+1; k++){			
				System.out.print(genRoute.route[k]+"->");
			}
			System.out.print("\n");
			*/
			genRoute.candidateRoute(cells);
			genRoute.newRoute(genRoute.calDistance(distance, cells), cells);
			if (genRoute.isChanged==1) {
				k=0;
				genRoute.isChanged=0;
			}
			else 
				k++;
		}
		
		System.out.print("route is:");
		for (k = 0; k < cells+1; k++){			
			System.out.print(genRoute.optimal[k]+"->");
		}
		System.out.print("\n");
		System.out.print("total distance is :"+genRoute.optimalDistance);
		optDis=genRoute.optimalDistance;
	}
}

⌨️ 快捷键说明

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