Traveling Salesman Problem (Dynamic Solution)

67 Views Asked by At

I have managed to implement a dynamic TSP algorithm and the code works and runs but it produces the wrong output for larger data sets

This is my code

    //TSP Dynamic Application
    public static int tspDynamic(int[][] costMatrix) 
    {
    int numCities = costMatrix.length;
    int numSets = 1 << numCities;
    int[][] dp = new int[numSets][numCities];

    // Initialize dp array with max values
    for (int[] row : dp) {
        Arrays.fill(row, Integer.MAX_VALUE / 2); // Divide by 2 to avoid integer overflow
    }
    
    dp[1][0] = 0; // Starting from city 0
    
    for (int set = 1; set < numSets; set += 2) {
        for (int u = 1; u < numCities; u++) {
            if ((set & (1 << u)) != 0) {
                for (int v = 0; v < numCities; v++) {
                    if ((set & (1 << v)) != 0 && u != v) {
                        dp[set][u] = Math.min(dp[set][u], dp[set ^ (1 << u)][v] + costMatrix[v][u]);
                    }
                }
            }
        }
    }
    
    int finalSet = (1 << numCities) - 1;
    int minCost = Integer.MAX_VALUE;
    for (int u = 1; u < numCities; u++) {
        minCost = Math.min(minCost, dp[finalSet][u] + costMatrix[u][0]);
    }
    
    return minCost;
}

A tried the problem on a 17x17 matrix and it gave the right output but when i tried it on a 33x33 matrix or anything higher. It always gives the wrong output. You can find the atsp files here : https://drive.google.com/drive/folders/1IsmLCUlo9-LHKkXFICYDVyhdNHWjYxd_?usp=sharing The 17x17 matrix is testFile.atsp

The answers for the output is available here http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/ATSP.html

This is my readfile method

     public static int [][] ReadTSPFile(String file, String delimiter)
     {
     //declaration
     int [][] costMatrix = null;
     int numOfCities;
     int numCol = -1;

     try
     {
         BufferedReader br = new BufferedReader(new FileReader(file));
         
         //reads the first line to get #no of cities
         String line = br.readLine();
         numOfCities = Integer.parseInt(line);
         int no = 0;
         
         //skips the empty line
         br.readLine();
         
         //2D Array that stores cost matrix
         costMatrix = new int[numOfCities][numOfCities];
         
         //counter for rows
         int rows = 0;
         int columns  = 0;
         
         //reads remaining lines and populates the cost matrix
          while ((line = br.readLine()) != null && rows < numOfCities) 
          {
            no++;
            //splits by commas
            String[] values = line.trim().split(delimiter);
            
            if(numCol == -1)
            {
                numCol = values.length;
            }
            
                for (String value : values) 
                {
                    costMatrix[rows][columns] = Integer.parseInt(value);
                    columns++;

                if (columns >= numOfCities) 
                {
                    columns = 0;
                    rows++;

                    if (rows >= numOfCities) 
                    {
                        break; // Stop reading if you've filled the entire matrix
                    }
                }
            }

          }
         //closes the file reader
         br.close();
     }
     catch(IOException e )
     {
         e.printStackTrace();
     }

     return costMatrix;
0

There are 0 best solutions below