Skip to content

Instantly share code, notes, and snippets.

@felixlindemann
Created December 4, 2014 13:49
Show Gist options
  • Save felixlindemann/dad3d0e1af2855487c12 to your computer and use it in GitHub Desktop.
Save felixlindemann/dad3d0e1af2855487c12 to your computer and use it in GitHub Desktop.
Repair short cycles in a genetic algorithm
repair2 <- function(tour){
#repair short cycles (DFJc)
# iteriere über jeden Stop im Tourenplan
for(i in 1:length(tour)){
# wenn der Nachfolger nicht das Depot ist (hier index = 1)
# dann kann es Kurzzyklen geben
if(tour[i]>1){ # not returning to depot
# finde sukzezzive jeden nachfolger in der Tour --> Rekursiv
r<-searchFor(tour, tour[i], i+1) #recursive call
# Ein kurzzyklus liegt vor, wenn der Startknoten wieder erreicht wird.
# in diesem Fall ist r = FALSE
if(r == FALSE){ #if shortcycle found, return current node to depot
tour[i] <- 1
}
}
}
#elimiate cycles
return (tour)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment