Ejemplo n.º 1
0
 def _generate_random_chromosome_(self):
     route = self.graph.nodes()
     shuffle(route)
     solution = TSPSolver.nodes_to_solution(self, route)
     TSPSolver.set_solution(self, solution[0], solution[1])
     distance = self.graph.route_distance(solution[1])
     return solution[0][:-1], distance
Ejemplo n.º 2
0
 def _complete_generation_(self, population, new_population):
     elite = sorted(population, key=lambda tup: tup[1])[0]
     solution = TSPSolver.nodes_to_solution(self, elite[0])
     TSPSolver.set_solution(self, solution[0], solution[1])
     s_population = sorted(new_population, key=lambda tup: tup[1])[::-1]
     new_population[new_population.index(s_population[0])] = elite
     return new_population
Ejemplo n.º 3
0
 def _initial_population_from_acs_(self):
     self._acs_.solve()
     population = []
     for ant in self._acs_.ants.values():
         route = ant.route[:-1]
         solution = TSPSolver.nodes_to_solution(self, route)
         TSPSolver.set_solution(self, solution[0], solution[1])
         distance = self.graph.route_distance(solution[1])
         population.append((solution[0][:-1], distance))
     while len(population) < self._chromosomes_:
         population.append(self._generate_random_chromosome_())
     return population
Ejemplo n.º 4
0
 def solve(self, show_progress=False):
     self.show_progress = show_progress
     TSPSolver.start_solving(self)
     last_solution = self._new_solution_()
     while not self._conditions_met_():
         TSPSolver.update_progress(
             self, "> Temp. = {0}, last solution - {1}".format(
                 round(self.t, 3), last_solution))
         new_solution = self._new_solution_(last_solution)
         TSPSolver.set_solution(self, new_solution[0], new_solution[1])
         if self._accept_(last_solution, new_solution) is True:
             last_solution = new_solution
         self.t = self.t * self.decrement
     TSPSolver.end_solving(self)
Ejemplo n.º 5
0
 def _initial_population_using_ants(self, num_ants, beta):
     population = []
     ants = {}
     for a in xrange(0, num_ants):
         ants[a] = Ant(self.graph, self.graph.nodes())
     for _ in xrange(num_ants, self._chromosomes):
         population.append(self._generate_random_chromosome())
     for ant in ants.itervalues():
         ant.start()
         while len(ant.remaining_cities()) > 0:
             self._initial_population_try_to_visit_city(ant)
         route = ant.route[:]
         solution = TSPSolver.nodes_to_solution(self, route)
         TSPSolver.set_solution(self, solution[0], solution[1])
         distance = self.graph.route_distance(solution[1])
         population.append((solution[0][:-1], distance))
     return population
Ejemplo n.º 6
0
 def solve(self, show_progress=False):
     self.show_progress = show_progress
     TSPSolver.start_solving(self)
     all_nodes = range(0, len(self.graph.nodes()))
     combinations = permutations(all_nodes)
     for combination in combinations:
         TSPSolver.update_progress(self,
                                   "Combination - %s" % (combination, ))
         if TSPSolver.conditions_met(self):
             self.solution_canceled = True
             break
         last_node = combination[0]
         full_route = [last_node]
         for i in xrange(1, len(combination)):
             next_node = combination[i]
             full_route += shortest_path(self.graph, last_node,
                                         next_node)[1:]
             last_node = next_node
         route = list(combination)
         route.append(combination[0])
         full_route += shortest_path(self.graph, last_node,
                                     combination[0])[1:]
         TSPSolver.set_solution(self, route, full_route)
     TSPSolver.end_solving(self)
Ejemplo n.º 7
0
 def _route_to_chromosome_(self, route):
     solution = TSPSolver.nodes_to_solution(self, route)
     TSPSolver.set_solution(self, solution[0], solution[1])
     return solution[0][:-1], self.graph.route_distance(solution[1])