2013-08-26 70 views
0

我想知道这个功能是什么:Python的TSP柏林52模拟退火

def Recocido(tour1 = []): 
    # tour1 = tour1[:] 
    izquierda = random.randrange(len(tour1)) 
    derecha = 0 
    while(True): 
     derecha = random.randrange(len(tour1)) 
     if (derecha != izquierda): 
      #tour1[izquierda], tour1[derecha] = tour1[derecha], tour1[izquierda] 
      break 
    return tour1 

我在做这个功能,“退火”的tour1,但我不知道如果我这样做好。我特别纠结于评论线(#),有人可以帮助我,请知道我在做什么!?或者更好,如果我做得很好?

编辑:

这是我的SA部分:

tamañoTour = len(matriz) 
inicioTour = [] 
inicioTour = Tour(tamañoTour) 
print(inicioTour) 
costoTourInicio = PesoTour(inicioTour, matriz) 
print(costoTourInicio) 

nuevoTour = [] 
temp = 1000000000 
#i = 0 

while(temp > 0.000000001): 
    for j in range(40): 

     nuevoTour = Recocido(inicioTour) 
     #print(nuevoTour) 
     costoNuevoTour = PesoTour(nuevoTour, matriz) 
     #print(costoNuevoTour) 
     if (costoNuevoTour < costoTourInicio): 
      inicioTour = nuevoTour 
      #temp = temp*0.99 
     else: 
      numero = random.random() 
      deltaZ = costoNuevoTour - costoTourInicio 
      prob = math.exp(-(deltaZ/temp)) 
      if (numero < prob): 
       inicioTour = nuevoTour 
       #temp = temp*0.99 

    #i += 1 
    temp = temp*0.99 

#print(inicioTour) 
print(nuevoTour) 
#print(costoTourInicio) 
print(costoNuevoTour) 
#print(i) 

matriz是52x52阵列,以及是的berlin52 http://www.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/tsp/

部件和其他功能之间的距离:

def Tour(tamaño): 
    tour1 = [] 
    for i in range(tamaño): 
     while(not False): 
      valor = random.randrange(tamaño) 
      if valor not in tour1: 
       tour1.append(valor) 
       break 
    return tour1 

def PesoTour(tour1 = [], matriz = [[]]): 
    valor = 0 
    i = 0 
    while(i < len(tour1)): 
     if (i == len(tour1) - 1): 
      valor = valor + matriz[tour1[i]][tour1[0]] 
     else: 
      valor = valor + matriz[tour1[i]][tour1[i+1]] 
     i += 1 
    return valor 

多数民众赞成它,感谢您的意见。

回答

2

就这样,该函数会生成一些随机数然后停止。如果取消对注释行的注释,它会创建一个包含交换两个随机元素的输入副本(如果输入只有一个元素,则永远循环,如果输入为空,则会引发异常)。下面是一行一行地击穿:

# The default argument here is useless. 
def Recocido(tour1 = []): 

    # Make a copy of the list. 
    tour1 = tour1[:] 

    # Pick a random index. 
    izquierda = random.randrange(len(tour1)) 

    # Unnecessary. 
    derecha = 0 

    while(True): 

     # # Pick another random index 
     derecha = random.randrange(len(tour1)) 

     # If it's not the same index you picked the first time, 
     if (derecha != izquierda): 

      # swap the elements of the copy at the two indices, 
      tour1[izquierda], tour1[derecha] = tour1[derecha], tour1[izquierda] 

      # and stop looping. 
      break 

    return tour1 

(我希望这是你的模拟退火程序的只是其中的一部分,因为这不是模拟退火没有的功能进行优化,没有冷却的时间表,也没有。概率排斥的状态变化)

如果你想编写一个返回与两个随机元素的输入列表交换的拷贝功能,可以按如下方式清理:

# No default argument - you'd never want to use the default. 
def with_random_swap(lst): 
    # Use random.sample to pick two distinct random indices. 
    index1, index2 = random.sample(xrange(len(lst)), 2) 
    copy = lst[:] 
    copy[index1], copy[index2] = copy[index2], copy[index1] 
    return copy 
+0

是,只是概率部分完成的功能,但我没有结果我想,在真正的答案上太高了。看看编辑过的问题。 – Menticolcito

+0

@Menticolcito:我没有看到任何相关的修改。 – user2357112

+0

我只是添加它们。 – Menticolcito