R 中的循环,TSP 的 VNS

问题描述 投票:0回答:0

我正在 R 中为 TSP(旅行商问题)做一个 VNS 算法,但是我在选择多个邻居时遇到了麻烦。当我解决城市和一个邻居的第一个组合时,解决方案并没有得到改善,我不知道我应该写什么来让循环再次开始选择多个邻居。有什么想法吗?

x<-c(1:7) #cambiar x por la solución, x es el vector de sitios a los que ir 
list<-list(ciudades) #cambiar x por la solución
max.inter=15
k<-400
interacciones<-0

#CREAMOS LAS POSIBILIDADES DE VECINOS K=1
for(i in 1:(length(ciudades)-1))  {list[[i+1]]<-append(ciudades[-c(i,i+1)],ciudades[c(i+1,i)],i-1)}#Cambiar x por la solución
comb<-t(as.data.frame(list))
rownames(comb)<-c(paste("combinación",1:length(ciudades)))
comb

#CALCULAMOS EL COSTE DE CADA UNA DE LAS POSIBILIDADES
coste<-c()
solu<-c()
for (i in 1:nrow(comb)){
  solu<-c()
  for (n in 1:nrow(comb)-1){
    solu<-append(solu,matriz[comb[i,n],comb[i,n+1]]) #Cambiar matriz por viaje
    #if (n==nrow(comb)-1){ solu<-append(solu,matriz[comb[i,n+1],comb[i,1]])}#Cambiar matriz por viaje
    if (n==nrow(comb)-1){ solu<-sum(solu)}
  }
  coste[i]<-solu  }
coste
repeat{
#Seleccionamos el mínimo de los costes y #lo ponemos como nueva solución
mejorsol<-coste[1]
if(mejorsol>coste[which.min(coste)] ){
  #coste<-coste[-1]
  mejorsol<-coste[which.min(coste)] 
  x<-comb[which.min(coste),]
  comb<-comb[which.min(coste),]
  
  #CREAMOS LAS POSIBILIDADES DE VECINOS K=1
  for(i in 1:(length(ciudades)-1))  {list[[i+1]]<-append(ciudades[-c(i,i+1)],ciudades[c(i+1,i)],i-1)}#Cambiar x por la solución
  comb<-t(as.data.frame(list))
  rownames(comb)<-c(paste("combinación",1:length(ciudades)))
  comb
  
  #CALCULAMOS EL COSTE DE CADA UNA DE LAS POSIBILIDADES
  coste<-c()
  solu<-c()
  for (i in 1:nrow(comb)){
    solu<-c()
    for (n in 1:nrow(comb)-1){
      solu<-append(solu,matriz[comb[i,n],comb[i,n+1]]) #Cambiar matriz por viaje
      #if (n==nrow(comb)-1){ solu<-append(solu,matriz[comb[i,n+1],comb[i,1]])}#Cambiar matriz por viaje
      if (n==nrow(comb)-1){ solu<-sum(solu)}
    }
    coste[i]<-solu  }
  coste

  }else{#Cambiar x por la solución
    comb<-comb[-which(coste==min(coste)),]
    #CREAMOS LAS POSIBILIDADES DE VECINOS K=2
    
    for (h in 1:k){
      
      list<-list()
      filas<-nrow(comb)*nrow(comb)
      for (h in 1:nrow(comb)){
        for (i in 1:(ncol(comb)-1)){
          list[[(nrow(comb)*h+i)-nrow(comb)]]<-append(comb[h,-c(k-1,k)],comb[h,c(k,k-1)],k-2)}}
      list<-list[!sapply(list,is.null)]
      comb<-t(as.data.frame(list))
      rownames(comb)<-c(paste("combinación",1:nrow(comb)))
      comb
      coste<-c()
      solu<-c()
      for (i in 1:nrow(comb)){
        solu<-c()
        for (n in 1:ncol(comb)-1){
          solu<-append(solu,matriz[comb[i,n],comb[i,n+1]]) #Cambiar matriz por viaje
          #if (n==ncol(comb)-1){ solu<-append(solu,matriz[comb[i,n+1],comb[i,1]])}#Cambiar matriz por viaje
          if (n==ncol(comb)-1){ solu<-sum(solu)}
        }
        coste[i]<-solu  }
      coste
      if(mejorsol<min(coste)) {next}
      
    }}#cierre del else 
interacciones<-interacciones+1
if(interacciones==max.inter){break}

}


coste
which.min(coste)
comb[which.min(coste),]
min(coste)

else{}
开始,我不知道如何做到这一点,如果 k=1 的解决方案没有改进我当前的解决方案,循环会尝试 k>1。我知道当我从每个城市组合中更改城市时,我不会选择超过 2 个城市,但是我也不知道如何更改超过 2 个。

r loops for-loop while-loop traveling-salesman
© www.soinside.com 2019 - 2024. All rights reserved.