2017-03-02 6 views
2
def calcDistance(x1, y1, x2, y2): 
    distance = sqrt((x1-x2)**2 + (y1-y2)**2) 

    return distance 

def make_dict(): 
    return defaultdict(make_dict) 

# Capture 1 input from the command line. 
# NOTE: sys.argv[0] is the name of the python file 
# Try "print sys.argv" (without the quotes) to see the sys.argv list 
# 1 input --> the sys.argv list should have 2 elements. 
if (len(sys.argv) == 2): 
    print "\tOK. 1 command line argument was passed." 

    # Now, we'll store the command line inputs to variables 
    myFile = str(sys.argv[1]) 
else: 
    print 'ERROR: You passed', len(sys.argv)-1, 'input parameters.' 
    quit() 


# Create an empty list: 
cities = [] 

# Create an empty dictionary to hold our (x,y) coordinate info: 
myCoordinates = {} 

# Open our file: 
myFile = '%s.csv' % (myFile) 
with open(myFile, 'rb') as csvfile: 
    spamreader = csv.reader(csvfile, delimiter=',', quotechar='|') 
    for row in spamreader: 
     # Only read rows that do NOT start with the "%" character. 
     if (row[0][0] != '%'): 
      # print row 
      id = int(row[0]) 
      isHome = int(row[1]) 
      x = float(row[2]) 
      y = float(row[3]) 

      myCoordinates[id] = {'x': x, 'y': y} 
      # print myCoordinates[id]['x'] 
      # print myCoordinates[id]['y'] 


      if (isHome == 1): 
       # Store this id as the home city 
       homeCity = id 

      cities.append(id)   

print homeCity 
print cities 

# Create a TSP tour. 
# VERSION 1 -- Using range() and for() loops: 

myTour = [] 
for i in range(homeCity, len(cities)+1): 
    myTour.append(i) 
for i in range(1, homeCity+1): 
    myTour.append(i)  
print myTour 

# VERSION 2 -- Using only range() 
''' 
firstPart = range(homeCity, len(cities)+1) 
secondPart = range(1, homeCity+1) 
myTour = firstPart + secondPart 
print myTour 
''' 

tau = defaultdict(make_dict) 

for i in cities: 
    # print "distance[%d][%d] = 0" % (i, i) 
    tau[i][i] = 0 
    for j in range(i+1, len(cities)+1):   
     # print "distance[%d][%d] > 0" % (i, j) 
     tau[i][j] = calcDistance(myCoordinates[i]['x'], myCoordinates[i]['y'], myCoordinates[j]['x'], myCoordinates[j]['y']) 
     # print "distance[%d][%d] = distance[%d][%d]" % (j, i, i, j) 
     tau[j][i] = tau[i][j] 


# FIXME -- Edit the code below... 
# Calculate the total distance of our TSP solution: 
i = myTour[i] 
for myIndex in range(1, len(myTour)+1): 
    j = myTour[myIndex] 
    print j 

Funktion zur Berechnung der Kosten basierend auf der Entfernung. Muss geändert werden.Ich erhalte den Index aus der Liste. Fehler

def cost(rate,j): 
    cost = rate * j 
cost = cost(1000,j) 
print cost 

Auch ich muss Kosten basierend auf der zurückgelegten Strecke berechnen. mit myIndex erhalte ich einen Fehler von list index out of range. Ich weiß nicht, was genau dort hingeht. Die ist wie Gesamtentfernung berechnet.

+0

Beim nächsten Mal versuchen Sie bitte, ein [minimales, vollständiges und verifizierbares Beispiel] (http://stackoverflow.com/help/mcve) zu erstellen. –

Antwort

2

Liste in Python haben 0 basierten Index. Wenn Sie einer Liste n Elemente hinzufügen, lauten die Indizes 0 bis n-1. Aber Sie laufen die Schleife von 1 bis n. Also, es wird list index out of range Fehler.

sollten Sie tun this-

for myIndex in range(0, len(myTour)): 
    j = myTour[myIndex] 
    print(j) 

Wenn Sie immer list index out of range Fehler dann die Schleife ändern, in dem Sie die Fehlermeldung erhalten und eine list unter Verwendung von 1-basierte Indizierung, range(1,len(some_list)+1)-range(0,len(some_list)) erreichbar. Oder Sie können einfach range(len(some_list)) schreiben. Wenn in der Bereichsfunktion kein Startwert übergeben wird, wird standardmäßig von 0 gestartet.

cost Zur Berechnung versuchen, diese -

for myIndex in range(0, len(myTour)): 
    j = myTour[myIndex] 
    cost = rate * j 
    print(cost) 

den Wert rate ein, bevor die Schleife beginnt.

+0

Jetzt mit der Entfernung J können Sie bitte helfen, wie ich die Kosten basierend auf der Entfernung berechnen, die Entfernung * Rate = Kosten. – user3647522

+0

Auch MyTour erzeugt eine Liste von [3,4,5,6,1,2,3] – user3647522

+0

Noch bekomme ich Liste aus Index Fehler – user3647522

Verwandte Themen