title: Calculating the geographic and euclidean distances for TSP in Java
body: <p>Basically I'm using data from TSPLIB and I have this  spec .</p>
  <p>This is how I calculated the Euclidean distance (according to the above spec):</p>
  <pre><code>public static double calculateDistance(double x1, double y1, double x2, double y2){
      double xDistance = Math.abs(x1 - x2);
      double yDistance = Math.abs(y1 - y2);
      double distance = Math.sqrt( (xDistance*xDistance) + (yDistance*yDistance) );
  
      return distance;
  }
  </code></pre>
  <p>This is how I calculated the geographic distance (according to the above spec):</p>
  <pre><code>public static double calculateGeoDistance(double lat1, double lon1, double lat2, double lon2) {
      double lat1Rad = coordinates2Radians(lat1);
      double lat2Rad = coordinates2Radians(lat2);
      double lon1Rad = coordinates2Radians(lon1);
      double lon2Rad = coordinates2Radians(lon2);
  
      double R = 6378.388;
  
      double q1 = Math.cos(lon1Rad - lon2Rad);
      double q2 = Math.cos(lat1Rad - lat2Rad);
      double q3 = Math.cos(lat1Rad + lat2Rad);
  
      double distance = (R * Math.acos(0.5 * ((1.0 + q1) * q2 - (1.0 - q1) * q3)) + 1.0);
      return distance;
  }
  
  private static double coordinates2Radians(double coordinate) {
      double deg = Math.round(coordinate);
      double min = coordinate - deg;
      double rad = Math.PI * (deg + 5.0 * min / 3.0) / 180.0; 
      // NOTE: Bug in TSPLIB95 Docu. Divide by 300 instead of 3.0.
      // 5.0 * 60 / 300 = 1 (60 minutes are 1 degree)
      // or use Math.PI * (deg in decimal) / 180.0; 
      return rad;
  }
  </code></pre>
  <p>But this problem is that I'm getting results that are less than the TSPLIB optimal (that cant be possible!). Is there something wrong with my calculation? I have tried calculating the optimal using predefined data and distances and I do get the optimal, I'm not sure why this isnt working..</p>
  <p>Many thanks.</p>