我正在尝试使用.txt文件实现旅行销售员算法(TSP)。这些文件的读取方式类似于.csv,其中包含","每个号码之间。文件内容的示例可以包括:
"0,8,6,5,2,9
8,0,14,11,10,17
6,14,0,3,5,6
5,11,3,0,4,9
2,10,5,4,0,7
9,17,6,9,7,0"
我希望输出看起来像:
Enter number of nodes:
6
Matrix file "FILENAME.txt" being read in:
"0,8,6,5,2,9
8,0,14,11,10,17
6,14,0,3,5,6
5,11,3,0,4,9
2,10,5,4,0,7
9,17,6,9,7,0"
Nearest neighbor is running:
A,E,D,C,F,B,A Cost = 40
B,A,E,D,C,F,B Cost = 40
C,D,E,A,B,F,C Cost = 40
D,C,E,A,B,F,D Cost = 44
E,A,D,C,F,B,E Cost = 43
F,C,D,E,A,B,F Cost = 40
The best solution for neighbors visited is as follows:
A,B,D,C,F,E,A Cost = 37
到目前为止,我所使用的代码是Java,它实际上具有正确的算法,它会打印文件,并尝试打印最佳解决方案。然而,它不包含成本。它看起来如下:
package TSP;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class TSPNearestNeighbour {
private int numberOfNodes;
private Stack<Integer> stack;
public TSPNearestNeighbour() {
stack = new Stack<Integer>();
}
/* Traveling Saleperson Algorithm */
public void tsp(int adjacencyMatrix[][]) {
numberOfNodes = adjacencyMatrix[1].length - 1;
int[] visited = new int[numberOfNodes + 1];
visited[1] = 1;
stack.push(1);
int element, dst = 0, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(1 + "\t");
while (!stack.isEmpty()) {
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes) {
if (adjacencyMatrix[element][i] > 1 && visited[i] == 0) {
if (min > adjacencyMatrix[element][i]) {
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag) {
visited[dst] = 1;
stack.push(dst);
System.out.print(dst + "\t");
minFlag = false;
continue;
}
stack.pop();
}
}
/* Point to file that is going to be read in */
private static final String FILENAME = "C:\\6.txt";
/* Main */
public static void main(String... arg) throws IOException {
BufferedReader bufferReader = null;
FileReader fileReader = null;
int number_of_nodes;
Scanner scanner = null;
String sCurrentLine;
try {
/* number of nodes */
System.out.println("Enter the number of nodes in the graph");
scanner = new Scanner(System.in);
number_of_nodes = scanner.nextInt();
/* read from designated file */
System.out.println("Matrix file being read in: ");
bufferReader = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = bufferReader.readLine()) != null) {
/* print the file to the console */
System.out.println(sCurrentLine);
}
/* re-scan the file */
int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1];
scanner = new Scanner(new File(FILENAME));
while (scanner.hasNextInt()) {
for (int i = 1; i <= number_of_nodes; i++) {
/* breaks here */
String[] array = sCurrentLine.split(",", number_of_nodes);
for (int j = 1; j <= number_of_nodes; j++) {
adjacency_matrix[i][j] = scanner.nextInt();
}
}
for (int i = 1; i <= number_of_nodes - 1; i++) {
for (int j = 1; j <= number_of_nodes - 1; j++) {
if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0) {
adjacency_matrix[j][i] = 1;
}
}
}
}
System.out.println("The best solution for citys are visited as follows");
TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();
tspNearestNeighbour.tsp(adjacency_matrix);
/* exception for if the file you name does not exist, the program does not crash */
} catch (IOException e) {
} finally {
try {
if (bufferReader != null) {
bufferReader.close();
}
if (fileReader != null) {
fileReader.close();
}
} catch (IOException ex) {
}
}
scanner.close();
}
有人可以帮我合并每个排列的成本,用整数读取文件,就好像它是一个矩阵,然后给出最佳解决方案。 谢谢。