def gen_pop(size, dimension, data, method='random'): print "Creating population..." population = set() if method == 'random': # Populate with unique individuals for i in xrange(size): c = Chromosome(dimension, data) # Avoid duplicated while c in all_pop: c = Chromosome(dimension, data) # Calc dist c.dist = data.tour_dist(c.tour) population.add(c) all_pop.add(c) # Generate with 2opt (hard for small tsp) elif method == '2opt': for i in xrange(size): c = Chromosome(dimension, data) c.dist = data.tour_dist(c.tour) c = functions.two_opt(c, data) # Avoid duplicated while c in all_pop: c = Chromosome(dimension, data) c.dist = data.tour_dist(c.tour) c = functions.two_opt(c, data) population.add(c) all_pop.add(c) print "Done" # Return population return population
def random(): c = Chromosome(self._data.dimension) # Avoid duplicates while c in self._population: c = Chromosome(self._data.dimension) c.dist = self._data.tour_dist(c.tour) return c
def gen_pop(self, size, method='random', ratio=1, input=None, output=None): # Regiter local and global start time self._start_time = start_time = time.time() # Need even population assert not (size % 2), "Invalid population size. " \ "Must be even and greater than 0" # Load init pop from file if input: print("Reading inicial population from file") line_counter = 0 for line in open(input, 'r'): if line_counter < size: c = Chromosome(map(int, line.split(','))) c.dist = self._data.tour_dist(c.tour) self._population.append(c) line_counter += 1 else: # Population generation print("Generating initial population...") if method == 'random': self._insert_pop(size, method) else: self._insert_pop(size - ratio * size, 'random') self._insert_pop(ratio * size, method) # Assert population size self._pop_size = len(self._population) assert self._pop_size == size, "gen_pop, pop_size" # Save init pop to file and exit if output: print("Saving inicial population to file") with open(output, 'w') as file: for c in self._population: print >> file, ",".join(map(str, c.tour)) print("Exiting...") exit() # Done print("Done...") # Store execution time self._timers['population'].append(time.time() - start_time)
# F25 # p1 = Chromosome((5, 7, 1, 2, 9, 4, 3, 10, 8, 6)) # p2 = Chromosome((1, 8, 3, 2, 6, 5, 4, 10, 9, 7)) # F25 - Without ghost nodes # p1 = Chromosome((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)) # p2 = Chromosome((1, 16, 4, 5, 10, 11, 6, 7, 15, 14, 2, 3, 8, 9, 12, 13)) # # F27 # p1 = Chromosome((9, 5, 3, 2, 7, 4, 1, 8, 6, 10)) # p2 = Chromosome((4, 5, 1, 7, 9, 8, 6, 3, 2, 10)) p1 = Chromosome((1, 2, 3, 4, 5, 6, 7, 8)) p2 = Chromosome((1, 2, 4, 3, 5, 6, 8, 7)) p1.dist = tsp.tour_dist(p1.tour) p2.dist = tsp.tour_dist(p2.tour) gpx.test_1 = True gpx.test_2 = False gpx.test_3 = True gpx.fusion_on = True gpx.relax = True c1, c2 = gpx.recombine(p1, p2) print "Results ---------------------------------------------------------------" print print "Tour 1: ", p1.tour, ", Distance: ", p1.dist print "Tour 2: ", p2.tour, ", Distance: ", p2.dist print