2017-03-02 3 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 

距離に基づいてコストを計算する機能。変更する必要があります。インデックスがリストから外れるエラー

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

また、移動距離に基づいて費用を計算する必要があります。 myIndexlist index out of rangeというエラーが表示されています。私は正確に何が起こっているのか分からない。 jは、計算された総距離と似ています。

+0

次回は、[最小で完全で検証可能な例]を作成してください(http://stackoverflow.com/help/mcve)。 –

答えて

2

リストには、0から始まるインデックスがあります。 n個の要素をリストに追加すると、インデックスは0からn-1になります。しかし、あなたは1からnにループを実行しています。だから、list index out of rangeエラーが発生します。あなたは、あなたがエラーを取得し、range(1,len(some_list)+1)からrange(0,len(some_list))に、1ベースのインデックスを使用してlistにアクセスしているループを変更list index out of rangeエラーを取得している場合は

あなたはthis-

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

を行う必要があります。または、単にrange(len(some_list))と書くこともできます。範囲関数に渡された開始値がない場合、デフォルトで0から開始します。

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

ループを開始する前にrateの値を設定 - costを計算するのに

が、これは試してみてください。

+0

距離Jを使用すると、距離*料金=費用と仮定して、距離に基づいて費用を計算する方法を教えてください。 – user3647522

+0

また、mytourは[3,4,5,6,1,2,3]のリストを生成します – user3647522

+0

まだインデックスエラーのリストを取得しています – user3647522

関連する問題