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
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
def __init__(self, graph, params=_defaul_params_, conditions={}, absolute_mute=False): TSPSolver.__init__(self, graph, conditions, absolute_mute) self.initial_temp = _defaul_params_['initial_temp'] self.decrement = _defaul_params_['decrement'] self.t = self.initial_temp
def __init__(self, graph, g_args, conditions, absolute_mute=False): TSPSolver.__init__(self, graph, conditions, absolute_mute) self._g_args = g_args self._chromosomes = g_args.get_chr() self._cr = g_args.get_cr() self._mr = g_args.get_mr() self._initial_population_method = _get_method( g_args.get_method_initial_population()) self._selection_method = _get_method(g_args.get_method_select()) self._crossover_method = _get_method(g_args.get_method_cross()) self._mutation_method = _get_method(g_args.get_method_mutate()) self._validate()
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
def get_solution(self): self.display_method = "Simulated annealing" self.display_parameters = { 'initial_temp': self.initial_temp, 'decrement': self.decrement } return TSPSolver.get_solution(self)
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
def __init__(self, graph, params, conditions, absolute_mute=False): TSPSolver.__init__(self, graph, conditions, absolute_mute) if params['chromosomes'] % 2 != 0: raise Exception("chromosomes % 2 != 0") self._chromosomes_ = params['chromosomes'] self._cr_ = params['cr'] self._mr_ = params['mr'] self._selection_method_ = _method_map_[params['selection_method']] self._crossover_method_ = _method_map_[params['crossover_method']] self._mutation_method_ = _method_map_[params['mutation_method']] self._params_ = params self._acs_ = None if 'use_acs' in params: self._acs_ = params['use_acs'] self._dynamic_params_ = None if 'dynamic_params' in params: self._dynamic_params_ = params['dynamic_params']
def get_solution(self): self.display_method = "Genetic algorithm" self.display_parameters = { 'cr': self._cr_, 'mr': self._mr_, 'chromosomes': self._chromosomes_, 'selection_method': self._params_['selection_method'], 'crossover_method': self._params_['crossover_method'], 'mutation_method': self._params_['mutation_method'] } return TSPSolver.get_solution(self)
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)
def solve(self, show_progress=False, absolute_mute=False): self.show_progress = show_progress TSPSolver.start_solving(self) population = None while not self._conditions_met_(): TSPSolver.update_progress(self) if population is None: if self._acs_ is None: population = [] for _ in xrange(0, self._chromosomes_): population.append(self._generate_random_chromosome_()) else: population = self._initial_population_from_acs_() else: new_population = [] while len(new_population) < len(population): chrom_a, chrom_b = self._selection_method_(population) if self._do_crossover_(): chrom_a, chrom_b = self._crossover_(chrom_a, chrom_b) if self._do_mutation_(): chrom_a, chrom_b = self._mutate_(chrom_a, chrom_b) new_population.append(chrom_a) new_population.append(chrom_b) population = self._complete_generation_(population, new_population) TSPSolver.end_solving(self)
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)
def solve(self, show_progress=False, absolute_mute=False): self.show_progress = show_progress TSPSolver.start_solving(self) population = None while not TSPSolver.conditions_met(self): TSPSolver.update_progress(self) if population is None: population = self._generate_initial_population() continue new_population = [] while len(new_population) < len(population): chrom_a, chrom_b = self._select(population) if self._do_crossover(): chrom_a, chrom_b = self._crossover(chrom_a, chrom_b) if self._do_mutation(): chrom_a, chrom_b = self._mutate(chrom_a, chrom_b) new_population.append(chrom_a) new_population.append(chrom_b) population = self._complete_generation(population, new_population) TSPSolver.end_solving(self)
def get_solution(self): self.display_method = "Genetic" self.display_parameters = { 'cr': self._cr, 'mr': self._mr, 'chromosomes': self._chromosomes, 'initial_population_method': self._g_args.get_method_initial_population(), 'selection_method': self._g_args.get_method_select(), 'crossover_method': self._g_args.get_method_cross(), 'mutation_method': self._g_args.get_method_mutate() } if (self._g_args.get_method_initial_population() == INITIAL_POPULATION_SEMIACS): self.display_parameters['initial_ants'] = \ self._g_args.get_initial_ants() return TSPSolver.get_solution(self)
def get_solution(self): self.display_method = "Brute force" self.display_parameters = {'solution_canceled': self.solution_canceled} return TSPSolver.get_solution(self)
def __init__(self, graph, conditions={}): TSPSolver.__init__(self, graph, conditions) self.solution_canceled = False
def _conditions_met_(self): if TSPSolver.conditions_met(self) is True: return True if self.t <= 1: return True return False
def _conditions_met_(self): return TSPSolver.conditions_met(self)
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])