1

My task is to calculate the round trip distance from the start location to all of the event locations that I have grabbed from Facebook in miles and back to the start. My code so far:

```
import json
import re
from urllib import urlopen
import math
def getLatRad(latitude):
return float(latitude) * (math.pi/180.0)
def getLongRad(longitude):
return float(longitude) * (math.pi/180.0)
tuplist =[]
finaltuplist = []
#start_latitude = float(input('Pls enter the latitude co-ordinate of your starting location:'))
#start_longitude = float(input('Pls enter the longitude co-ordinate of your starting location:'))
start_latitude = 41.721194054071
start_longitude = -73.934258235003
longRad1= getLongRad(start_longitude)
latRad1 = getLatRad(start_latitude)
def main():
eventids = [
'264100516989470',
'129843580476568',
'158475914271199',
]
for event in eventids:
f = urlopen('http://graph.facebook.com/%s' % event)
d = json.load(f)
name = d['name']
longitude = d["venue"]["longitude"]
latitude = d["venue"]["latitude"]
tuplist.append((name,longitude,latitude))
for coordinates in tuplist:
longRad2= getLongRad(coordinates[1])
latRad2= getLatRad(coordinates[2])
dlon = longRad2 - longRad1
dlat = latRad2 - latRad1
a = math.sin(dlat/2)**2 + math.cos(latRad1) * math.cos(latRad2) * math.sin(dlon/2)**2
c = 2 * math.asin(math.sqrt(a))
m = 3960 * c
sum = m + m
print sum
if __name__ == '__main__':
main()
```

This is as far as I know how to do on my own. Is there any chance someone could point me in the right direction to get the total round trip distance instead of the individual distances from the start location?

Do you intend to calculate the "optimum" round trip distance of all the points, the round trip in a particular order, or "any" round trip that hits all the points - ybakos 2012-04-03 23:44

+1 for ybakos answer. traveling salesman problem, you need to come up with a heuristic, right - tartar 2012-04-03 23:44

I would guess that the events have a calendar ordering (probably that of the ids in the eventids array so there is no concept of "optimum" in the context of this problem - Endophage 2012-04-03 23:48

i need to do it by calendar ordering like endophage sai - matture 2012-04-03 23:54

so, you are sure about how long the event schedules are separated. if two events are 2 hours apart and it takes more than 2 hours to get from event 1 to event 2, then any schedule won't be feasible, right? or are we making this more complicated than it is supposed to be - tartar 2012-04-04 00:03

thats irrelevant, it just has to follow them in correct orde - matture 2012-04-04 00:06

1

So decompose your problem vs your solution. Currently you can get the distance between start and event1, but you cant get the distance between event1 and event2. You can also get the distance between start and event2. What do you need to change in your calculation to get the distance from event1 to event2?

**Edit for Elaboration Request:**

latRad1 = getLatRad(start_latitude) longRad1= getLongRad(start_longitude)

dlon = longRad2 - longRad1 a = math.sin(dlat/2)**2 + math.cos(latRad1) * math.cos(latRad2) * math.sin(dlon/2)**2

The upper 2 are outside of your for loop and are not changed (that I noticed). Therefore when you move to a new location to calculate the distance, your still calculating it against the starting coords.

can you elaborate please - matture 2012-04-05 01:53