0

I am doing a VNS algorithm in R for a TSP (Travelling Salesman Problem), however I'm having trouble with selecting more than one neighbours. When I solve the first combination of cities and one neighbour does not improve the solution I don't know what I should write to make the loop start again picking more than one neighbour. Any idea?

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)

From the else{} on, I don't know how to make it so that if the solution with k=1 did not improve my current solution, the loop tries with k>1 instead. I know that when I change cities from each combination of cities I don't select more than 2 cities, however I don't know how change more than 2 either.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Han
  • 1

0 Answers0