def solve(cities): N = len(cities) # 最も離れた都市を求める dist = get_all_distance(cities) max_value = 0 max_city1 = 0 for i in range(N): local_max = max(dist[i]) if max_value < local_max: max_value = local_max max_city1 = i max_city2 = dist[max_city1].index(max_value) # 2つの都市を通る直線で2つに分ける line = make_linear_function(cities[max_city1], cities[max_city2]) group1 = [max_city1] group2 = [max_city2] # yに垂直な直線の時(右か左かで分ける) if 'x' in line: for i in range(N): if i in {max_city1, max_city2}: continue # 直線よりも左側に点がある時 if cities[i][0] < line['x']: group1.append(i) # 直線よりも右側に点がある時 else: group2.append(i) # 上か下かで分ける else: for i in range(N): if i in {max_city1, max_city2}: continue # 直線よりも下側に点がある時 if cities[i][1] < line['m'] * cities[i][0] + line['n']: group1.append(i) # 直線よりも上側に点がある時 else: group2.append(i) # max_city1を開始都市として、group1に対してNN法を行う group1_cities = [cities[city] for city in group1] group1_dist = get_all_distance(group1_cities) tour1 = solver_NN.solve_each(group1_dist, 0) # max_city2を開始都市として、group2に対してNN法を行う group2_cities = [cities[city] for city in group2] group2_dist = get_all_distance(group2_cities) tour2 = solver_NN.solve_each(group2_dist, 0) # それぞれをつなげる tour = [group1[i] for i in tour1] + [group2[i] for i in tour2] return tour
def solve(cities): N = len(cities) dist = get_all_distance(cities) tours = [] # スタート地点を変えて実行する for i in range(N): tours.append(solve_each(dist, i)) tours_length = [get_tour_length(tour, dist) for tour in tours] shortest_tour = tours[tours_length.index(min(tours_length))] # 一番短い経路を返す return shortest_tour
def solve(cities): N = len(cities) start_points = range(N) # challenge6はランダムな点一つで実行 if N >= 2048: start_points = [random.randint(0, N - 1)] # 全ての距離 dist = get_all_distance(cities) tours = [] # スタート地点を変えて実行する for i in start_points: tours.append(solve_each(dist, i)) tours_length = [get_tour_length(tour, dist) for tour in tours] shortest_tour = tours[tours_length.index(min(tours_length))] # 一番短い経路を返す return shortest_tour
def solve(cities): dist = get_all_distance(cities) tour = solver_DNN.solve(cities) solver_NN_2opt.improve_tour(dist, tour) return tour
def solve(cities): # 全ての距離 dist = get_all_distance(cities) tour = solver_NN_2opt.solve(cities) solver_NN_oropt.improve_tour(dist, tour) return tour