Question

In: Computer Science

Please solve in Java, The traveling salesman, wishing to visit a set of cities in the...

Please solve in Java,

The traveling salesman, wishing to visit a set of cities in the shortest time possible. A tour is a path that starts in one city, visits all the other cities, and then returns to the starting point. The relevant pieces of information, then, are the cities and the distances between them. The given set of cities in this problem are: A, B, C, D, E , and F The cost matrix (distances in miles) between any pair of cities is given below: A B C D E F A - 21 13 17 25 9 B 21 - 14 19 21 27 C 13 14 - 8 18 20 D 17 19 8 - 29 17 E 25 21 18 29 - 15 F 9 27 20 17 16 - Implement the following Hill Climbing local search algorithm to solve the above travelling salesman problem.

Use Java programming language.

Algorithm:

1. Start with an initial current state (generated randomly), such as: C, B, E, F, A, D The cost of this state is: = C to B + B to E + E to F + F to A + A to D + D to C = 14 + 21 + 15 + 9 + 17 + 8 = 84 LOOP: 2. Generate three different possible successor moves from the current state as defined below:

i) Generate three random pairs of numbers: (c1, c2), (c3, c4), (c5, c6) Such that: Ci ( 1 <= i <= 6), has a random value between 1 and 6 (inclusive).

Make sure that the two random values in each pair are different. Also, make sure that no two pairs have the same random values. i.e. all pairs are unique.

ii) Then, the possible three successor moves from the current state are: Move 1: swap the city in position c1 with the city in position c2.

For example: given the current state: C, B, E, F, A, D if c1 = 2 and c2= 5, then swap city B (in position 2) with city A (in position 5) in the current sate to get the following move: C, A, E, F, B, D Move

2: swap the city in position c3 with the city in position c4 Move 3: swap the city in position c5 with the city in position c6

3. Calculate the cost of each of these three successor moves.

4. Choose the move with the best move (i.e. least cost) from these three successor moves.

5. If this best move state has a higher cost than the cost of current state, then return this current state as a solution with its cost and exit the algorithm. Otherwise, replace the current state by this best move state. Output: Initial state Best state found by the algorithm The cost of the best state Number of iterations the algorithm execute to find this best state

Solutions

Expert Solution


class GFG 
{ 

        static int tsp(int[][] graph, boolean[] v, 
                                int currPos, int n, 
                                int count, int cost, int ans) 
        { 

        
                if (count == n && graph[currPos][0] > 0) 
                { 
                        ans = Math.min(ans, cost + graph[currPos][0]); 
                        return ans; 
                } 


                for (int i = 0; i < n; i++) 
                { 
                        if (v[i] == false && graph[currPos][i] > 0) 
                        { 

                                v[i] = true; 
                                ans = tsp(graph, v, i, n, count + 1, 
                                                cost + graph[currPos][i], ans); 

                                v[i] = false; 
                        } 
                } 
                return ans; 
        } 

        // Driver code 
        public static void main(String[] args) 
        { 

                int n = 6; 

                int[][] graph = {{0, 21, 13, 17,25,9}, 
                                                {21,0,14,19,21,27}, 
                                                {13,14,0,8,18,20}, 
                                                {17,19,8,0,29,17},
                        {25,21,18,29,0,15},
                        {9,27,20,17,16,0}
}; 


                boolean[] v = new boolean[n]; 

                v[0] = true; 
                int ans = Integer.MAX_VALUE; 

         
                ans = tsp(graph, v, 0, n, 1, 0, ans); 

        
                System.out.println(ans); 
        } 
} 

Related Solutions

The Traveling Salesman Problem was named after the job of a traveling salesman but finds many...
The Traveling Salesman Problem was named after the job of a traveling salesman but finds many applications elsewhere. Research the traveling salesman and provide two examples of real-world applications. Make sure to explain how these applications are applied.
IN JAVA A salesman wants to go to five different cities and sell some products. The...
IN JAVA A salesman wants to go to five different cities and sell some products. The locations of the cities are listed in the following table. City # X_location Y_location City 1 1 1 City 2 1 3 City 3 4 1 City 4 5 3 City 5 3 5 The distance between two cities is defined as the Euclidean distance. That is: Distance = sqrt( (x1 – x2)^2 + (y1 – y2)^2 ) For example, the distance between cities...
state the input and output of the traveling salesman problem. give the best lower bound on...
state the input and output of the traveling salesman problem. give the best lower bound on length of optimal tour and prove
Q4) The temperature degrees of certain set of cities are normally distributed. The cities are classified...
Q4) The temperature degrees of certain set of cities are normally distributed. The cities are classified into three categories. The lowest 12.92% are in category I, The highest 10.38% are in Category III, and the other cities are in category II, with degrees between α and β. i. Find the z- scores of α and β. ii. Given the mean temperature degrees is 6.84 and the standard deviation is 0.25. Find the values of α and β
solve travelling salesman problem using different simulated annealing cooling schedule
solve travelling salesman problem using different simulated annealing cooling schedule
Lab 1. Java and the Command Prompt a) Visit the Java SE JDK website and see...
Lab 1. Java and the Command Prompt a) Visit the Java SE JDK website and see which version of Java you need to download. b) If granted permissions, try to see how to modify environment variables (PATH and CLASSPATH). c) Open the command prompt/shell. Use cd and dir (ls on UNIX-like systems) commands to navigate your file system and see what files do you have in different locations e.g. on your flash drive. Pay attention to the file extensions. d)...
A) Please set up The Simultaneous Equations for both plans and solve for the breakeven point...
A) Please set up The Simultaneous Equations for both plans and solve for the breakeven point where Plan A and Plan B will have the same cost outlay. ( B)Which plan will be cheaper in the short run and which plan will be cheaper in the long run? Please show Graph.
Please solve this problem with Python Language. P#2. A Pythagorean triplet is a set of three...
Please solve this problem with Python Language. P#2. A Pythagorean triplet is a set of three natural numbers, a < b < c, for which a2 + b2 = c2 . For example, 32 + 42 = 52. And a + b + c = 3 + 4 + 5 = 12. There exists exactly two Pythagorean triplet for which a + b + c = 300. Find a, b, c. Hints: Level-0 Algorithm: 1. Find the possible ranges of...
solve following travelling salesman problem using branch and bound and show matrix and graph at each...
solve following travelling salesman problem using branch and bound and show matrix and graph at each step 5 locations : a, b, c, d, e from a to remaining places = [infinity, 4, 7, 3, 4] from b to remaining places = [4, infinity, 6, 3, 4] from c to remaining = [7, 6, infinity, 7, 5] from d to remaining = [3, 3, 7, infinty, 7] from e to remaining = [4, 4, 5, 7, infinity] PLEASE show ALL...
You are on a plane traveling to visit family (pre-Covid 19) and you scroll through some...
You are on a plane traveling to visit family (pre-Covid 19) and you scroll through some news headlines on your tablet. You end up reading an editorial that discusses the changing nature of the economy. Specifically, the writer speculates that new technology will make the accounting profession outdated, since computers will automatically track and compile all financial data and make it easy for anyone with an online connection to access any data about a company’s operations that they wish. How...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT