def compareCrossovers(): cities = tsp.load_city_locs("cities1000.txt") permutationA = tsp.rand_perm(1000) permutationB = tsp.rand_perm(1000) print("Original A: ", tsp.total_dist(permutationA, cities)) print("Original B: ", tsp.total_dist(permutationB, cities)) # Order Crossover offSpringA, offSpringB = crossovers.orderCrossover(permutationA, permutationB) print("OffspringB Order: ", tsp.total_dist(offSpringA, cities)) print("OffspringB Order: ", tsp.total_dist(offSpringB, cities)) # Partially Mapped Crossover offSpringA, offSpringB = crossovers.partiallyMappedCrossover( permutationA, permutationB) print("OffspringB Partially Mapped: ", tsp.total_dist(offSpringA, cities)) print("OffspringB Partially Mapped: ", tsp.total_dist(offSpringB, cities)) # Best Edges Crossover offSpringA, offSpringB = bestEdgesSearch(permutationA, permutationB, cities) print("OffspringB Best Edges: ", tsp.total_dist(offSpringA, cities)) print("OffspringB Best Edges: ", tsp.total_dist(offSpringB, cities))
def optimizedBestSearch(fname, max_iter=100, pop_size=50, percentToSwitch=5): city_locs = tsp.load_city_locs(fname) n = len(city_locs) curr_gen = [tsp.rand_perm(n) for i in range(pop_size)] curr_gen = [(tsp.total_dist(p, city_locs), p) for p in curr_gen] curr_gen.sort() bestScore = curr_gen[0][0] bestPermuation = curr_gen[0][1] useMutateSearch = False print( f'Optimized bestEdges("{fname}", max_iter={max_iter}, pop_size={pop_size}) ...' ) for i in range(max_iter): print("iteration: ", i) top_half = [p[1] for p in curr_gen[:int(pop_size / 2)]] next_gen = top_half[:] while len(next_gen) < pop_size: parentA = random.choice(top_half) parentB = random.choice(top_half) while parentA == parentB: parentA = random.choice(top_half) parentB = random.choice(top_half) if (useMutateSearch): first = parentA[:] second = parentB[:] else: first, second = bestEdgesSearch(parentA, parentB, city_locs) tsp.do_rand_swap(first) tsp.do_rand_swap(second) next_gen.append(first) next_gen.append(second) next_gen = next_gen[:pop_size] assert len(next_gen) == pop_size curr_gen = [(tsp.total_dist(p, city_locs), p) for p in next_gen] curr_gen.sort() useMutateSearch = ifSwitchToMutate(bestScore, curr_gen[0][0], percentToSwitch) if (curr_gen[0][0] < bestScore): bestScore = curr_gen[0][0] bestPermuation = curr_gen[0][1] print( f'... Optimized bestEdges("{fname}", max_iter={max_iter}, pop_size={pop_size})' ) print() print( f'After {max_iter} generations of {pop_size} permutations, the best is:' ) print(f'score = {bestScore}') print(bestPermuation) assert tsp.is_good_perm(bestPermuation)
def gen_rand_pop(fname, n_pop): city_locs = tsp.load_city_locs(fname) n = len(city_locs) curr_gen = [tsp.rand_perm(n) for i in range(n_pop)] curr_gen = [(tsp.total_dist(p, city_locs), p) for p in curr_gen] curr_gen.sort() assert len(curr_gen) == n_pop return curr_gen
def orderCrossoverTest(fname, max_iter, pop_size): city_locs = tsp.load_city_locs(fname) n = len(city_locs) # generate permutations for a specific population size curr_gen = [tsp.rand_perm(n) for i in range(pop_size)] # per population calculate total distance curr_gen = [(tsp.total_dist(p, city_locs), p) for p in curr_gen] curr_gen.sort() assert len(curr_gen) == pop_size print( f'orderCrossover("{fname}", max_iter={max_iter}, pop_size={pop_size}) ...' ) for i in range(max_iter): # copy the top 50% of the population to the next generation, and for the rest randomly # cross-breed pairs top_half = [p[1] for p in curr_gen[:int(pop_size / 2)]] next_gen = top_half[:] while len(next_gen) < pop_size: parentA = random.choice(top_half) parentB = random.choice(top_half) while parentA == parentB: parentA = random.choice(top_half) parentB = random.choice(top_half) first, second = orderCrossover(parentA, parentB) tsp.do_rand_swap(first) tsp.do_rand_swap(second) next_gen.append(first) next_gen.append(second) next_gen = next_gen[:pop_size] # create the next generation of (score, permutations) pairs assert len(next_gen) == pop_size curr_gen = [(tsp.total_dist(p, city_locs), p) for p in next_gen] curr_gen.sort() print( f'... orderCrossover("{fname}", max_iter={max_iter}, pop_size={pop_size})' ) print() print( f'After {max_iter} generations of {pop_size} permutations, the best is:' ) print(f'score = {curr_gen[0][0]}') # print(curr_gen[0][1]) assert tsp.is_good_perm(curr_gen[0][1])