def reverse_sequence_Mutation(mutationRate: float, tour: Tour) -> Tour: # Mutated Child mutated = Tour() # Getting Sub-tour startpos = randrange(0, tour.getSize()) endpos = randrange(0, tour.getSize()) if(startpos > endpos): startpos, endpos = endpos, startpos mutated.tour = tour.tour[0:startpos] + \ list(reversed(tour.tour[startpos:endpos])) + tour.tour[endpos:] return mutated
def mainloop(self): self.sortPopulation() for i in range(self.iteration_limit): blackHole = self.tours[0] # Moving Stars toward the Black Hole for j in range(1, self.population_size): newStar = self.moveCloser(blackHole, self.tours[j]) self.setTour(j, newStar) # Checking for Event Horizon blackHole_value = 1 / self.tours[0].getDistance() sumValues = [ 1 / self.tours[j].getDistance() for j in range(1, self.population_size) ] # Deaths of stars radius = blackHole_value / sum(sumValues) for j in range(1, self.population_size): if (abs(self.tours[j].getDistance() - blackHole_value) < radius): self.setTour(j, Tour()) self.sortPopulation() if i % 10 == 0: print( f"Generation: {i} Best Fitness: {self.tours[0].getDistance()}" ) return self.tours, i
def __init__(self, population_size, iteration_limit): # Store the stars self.population_size = population_size self.tours = [Tour() for _ in range(population_size)] # Max Iterations self.iteration_limit = iteration_limit
def __init__(self, size: int, initialize: bool) -> None: # Holds Population of Tours self.tours: List[Tour] = [] # Initializing Population if initialize: self.tours = [Tour() for _ in range(size)] else: self.tours = [None for _ in range(size)]
def swap_Mutation(mutationRate: float, tour: Tour) -> Tour: for i, city in enumerate(tour.tour): if random() <= mutationRate: # Getting second random city randomId = randrange(0, tour.getSize()) other_city = tour.getCity(randomId) # Swapping the Route tour.setCity(i, other_city) tour.setCity(randomId, city) return tour
def moveCloser(self, blackHole, star): min_possible = star.getDistance() min_tour = star for i in range(7): # New Child newStar = Tour() # Getting Sub-tour startpos = randrange(0, blackHole.getSize()) endpos = randrange(0, blackHole.getSize()) if (startpos > endpos): startpos, endpos = endpos, startpos for i in range(newStar.getSize()): if i > startpos and i < endpos: newStar.setCity(i, blackHole.getCity(i)) else: newStar.setCity(i, None) for i in range(newStar.getSize()): if not newStar.containsCity(star.getCity(i)): for j in range(0, newStar.getSize()): if newStar.getCity(j) == None: newStar.setCity(j, star.getCity(i)) break if (newStar.getDistance(True) < min_possible): return newStar return min_tour
def ordered_crossover(parent_a: Tour, parent_b: Tour) -> Tour: # New Child offspring = Tour() # Getting Sub-tour startpos = randrange(0, parent_a.getSize()) endpos = randrange(0, parent_a.getSize()) if(startpos > endpos): startpos, endpos = endpos, startpos for i in range(offspring.getSize()): if i > startpos and i < endpos: offspring.setCity(i, parent_a.getCity(i)) else: offspring.setCity(i, None) for i in range(offspring.getSize()): if not offspring.containsCity(parent_b.getCity(i)): for j in range(0, offspring.getSize()): if offspring.getCity(j) == None: offspring.setCity(j, parent_b.getCity(i)) break return offspring