import copy import random from time import perf_counter import argparse import sys import math class TimeKeeper: def __init__(self): self.start_time = perf_counter() def is_time_over(self, LIMIT): return (perf_counter() - self.start_time) >= LIMIT def time_now(self): return (perf_counter() - self.start_time) ############## def main(DEBUG): # ユークリッド距離 def euclidean_distance(point1, point2): return sum((a - b) ** 2 for a, b in zip(point1, point2)) ** 0.5 # K-meansクラスタリング def initialize_centroids(points, k): return random.sample(points, k) def assign_clusters(points, centroids): clusters = [] for point in points: distances = [euclidean_distance(point, centroid) for centroid in centroids] cluster = distances.index(min(distances)) clusters.append(cluster) return clusters def update_centroids(points, clusters, k): new_centroids = [] for i in range(k): cluster_points = [points[j] for j in range(len(points)) if clusters[j] == i] if cluster_points: new_centroid = [sum(dim) / len(cluster_points) for dim in zip(*cluster_points)] new_centroids.append(tuple(new_centroid)) return new_centroids def kmeans(points, k, max_iters=100): centroids = initialize_centroids(points, k) for _ in range(max_iters): clusters = assign_clusters(points, centroids) new_centroids = update_centroids(points, clusters, k) if centroids == new_centroids: break centroids = new_centroids return clusters, centroids def HeldKarp(dists): N = len(dists) dp = [[10**18] * N for _ in range(2**N)] for i in range(N): dp[1< bestsc: print('best', loop, sc, file=sys.stderr) bestsc = sc besttour = tour beststations = stations loop += 1 # スコア print('SC', bestsc, file=sys.stderr) # 出力(ステーション) for x, y in beststations: print(x, y) # 出力(訪問箇所) print(len(besttour)) for type, id in besttour: print(type, id) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Debug mode') parser.add_argument('--debug', action='store_true', help='Enable debug mode') args = parser.parse_args() main(args.debug)