Advertisement
daskalot

TSP

May 15th, 2019
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.27 KB | None | 0 0
  1. package aps2.tsp;
  2. class Node{
  3.     int id;
  4.     int x;
  5.     int y; 
  6.     Node(int x,int y,int id){
  7.         this.id = id;
  8.         this.x = x;
  9.         this.y = y;
  10.     }
  11. }
  12. public class TravellingSalesman {
  13.     int N;
  14.     int[] optimal;
  15.     int added;
  16.     double resDist;
  17.     Node nodes[];
  18.     public TravellingSalesman() {
  19.         N=0;
  20.         nodes = new Node[100];     
  21.     }
  22.     /**
  23.      * To solve the travelling salesman problem (TSP) you need to find a shortest
  24.      * tour over all nodes in the graph where each node must be visited exactly
  25.      * once. You need to finish at the origin node.
  26.      *
  27.      * In this task we will consider complete undirected graphs only with
  28.      * Euclidean distances between the nodes.
  29.      */
  30.    
  31.     /**
  32.      * Adds a node to a graph with coordinates x and y. Assume the nodes are
  33.      * named by the order of insertion.
  34.      *
  35.      * @param x X-coordinate
  36.      * @param y Y-coordinate
  37.      */
  38.     public void addNode(int x, int y){
  39.         Node n = new Node(x,y,N);
  40.         nodes[N] = n;
  41.         N++;
  42.     }
  43.    
  44.     /**
  45.      * Returns the distance between nodes v1 and v2.
  46.      *
  47.      * @param v1 Identifier of the first node
  48.      * @param v2 Identifier of the second node
  49.      * @return Euclidean distance between the nodes
  50.      */
  51.     public double getDistance(int v1, int v2) {
  52.         return Math.sqrt(Math.pow(nodes[v1].x - nodes[v2].x, 2)+Math.pow(nodes[v1].y - nodes[v2].y, 2));
  53.     }
  54.    
  55.     /**
  56.      * Finds and returns an optimal shortest tour from the origin node and
  57.      * returns the order of nodes to visit.
  58.      *
  59.      * Implementation note: Avoid exploring paths which are obviously longer
  60.      * than the existing shortest tour.
  61.      *
  62.      * @param start Index of the origin node
  63.      * @return List of nodes to visit in specific order
  64.      */
  65.     void utilCalc(int pos,int count,boolean visited) {
  66.         if(count == N) {
  67.            
  68.             return;
  69.         }
  70.     }
  71.     public int[] calculateExactShortestTour(int start){
  72.         boolean visited[] = new boolean[N];
  73.         visited[start] = true;
  74.         utilCalc(start);
  75.         return optimal;
  76.     }
  77.     /**
  78.      * Returns an optimal shortest tour and returns its distance given the
  79.      * origin node.
  80.      *
  81.      * @param start Index of the origin node
  82.      * @return Distance of the optimal shortest TSP tour
  83.      */
  84.     public double calculateExactShortestTourDistance(int start){
  85.         //calculateExactShortestTour(start);
  86.         return resDist;
  87.     }  
  88.    
  89.     /**
  90.      * Returns an approximate shortest tour and returns the order of nodes to
  91.      * visit given the origin node.
  92.      *
  93.      * Implementation note: Use a greedy nearest neighbor apporach to construct
  94.      * an initial tour. Then use iterative 2-opt method to improve the
  95.      * solution.
  96.      *
  97.      * @param start Index of the origin node
  98.      * @return List of nodes to visit in specific order
  99.      */
  100.     public int[] calculateApproximateShortestTour(int start){
  101.         throw new UnsupportedOperationException("You need to implement this function!");
  102.     }
  103.    
  104.     /**
  105.      * Returns an approximate shortest tour and returns its distance given the
  106.      * origin node.
  107.      *
  108.      * @param start Index of the origin node
  109.      * @return Distance of the approximate shortest TSP tour
  110.      */
  111.     public double calculateApproximateShortestTourDistance(int start){
  112.         throw new UnsupportedOperationException("You need to implement this function!");
  113.     }
  114.    
  115.     /**
  116.      * Constructs a Hamiltonian cycle by starting at the origin node and each
  117.      * time adding the closest neighbor to the path.
  118.      *
  119.      * Implementation note: If multiple neighbors share the same distance,
  120.      * select the one with the smallest id.
  121.      *
  122.      * @param start Origin node
  123.      * @return List of nodes to visit in specific order
  124.      */
  125.     public int[] nearestNeighborGreedy(int start){
  126.         int[] res = new int[N+1];
  127.         boolean visited[] = new boolean[N];
  128.         res[0] = start; visited[start] = true;
  129.         int prev = start;
  130.         for(int i=1;i<N;i++) {
  131.             int next = -1;
  132.             double min = Integer.MAX_VALUE;
  133.             for(int j=0;j<N;j++) {
  134.                 double tempDist = getDistance(prev,j);
  135.                 if(!visited[j] && tempDist <= min) {
  136.                     min=tempDist;
  137.                     next = j;
  138.                 }
  139.             }
  140.             visited[next] = true;
  141.             res[i] = next;
  142.             prev = next;
  143.         }
  144.         res[N] = start;
  145.         return res;
  146.     }
  147.    
  148.     /**
  149.      * Improves the given route using 2-opt exchange.
  150.      *
  151.      * Implementation note: Repeat the procedure until the route is not
  152.      * improved in the next iteration anymore.
  153.      *
  154.      * @param route Original route
  155.      * @return Improved route using 2-opt exchange
  156.      */
  157.     private int[] twoOptExchange(int[] route) {
  158.         throw new UnsupportedOperationException("You need to implement this function!");
  159.     }
  160.    
  161.     /**
  162.      * Swaps the nodes i and k of the tour and adjusts the tour accordingly.
  163.      *
  164.      * Implementation note: Consider the new order of some nodes due to the
  165.      * swap!
  166.      *
  167.      * @param route Original tour
  168.      * @param i Name of the first node
  169.      * @param k Name of the second node
  170.      * @return The newly adjusted tour
  171.      */
  172.     public int[] twoOptSwap(int[] route, int i, int k) {
  173.         throw new UnsupportedOperationException("You need to implement this function!");
  174.     }
  175.  
  176.     /**
  177.      * Returns the total distance of the given tour i.e. the sum of distances
  178.      * of the given path add the distance between the final and initial node.
  179.      *
  180.      * @param tour List of nodes in the given order
  181.      * @return Travelled distance of the tour
  182.      */
  183.     public double calculateDistanceTravelled(int[] tour){
  184.         double distanceTurn = 0;
  185.         for(int i=0;i<N;i++) distanceTurn += getDistance(tour[i],tour[i+1]);       
  186.         return distanceTurn;
  187.     }
  188.    
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement