def __init__(self, matrix: np.ndarray, **kwargs): (self.length, self.tour), self.matrix = greedy(matrix), matrix collect = kwargs.get('collect', False) self.collector = None if not collect else Collector(['length', 'gain'], {'lkh_search': len(self.tour)}) self.data = TabuSet() self.data.append(self.length, self.tour)
def opts(opt, size, number, info, neighbours, k, mul, sb, ns): if info: logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO) average = 0. ft_start = time() for _ in range(number): tsp = generator(size) matrix = adjacency_matrix(tsp) length, tour = greedy(matrix) t_start = time() searcher = opts_type[opt](length, tour, matrix, neighbours=neighbours, k=k, mul=mul, subgradient=sb, non_seq=ns) searcher.optimize() average += (time() - t_start) click.echo(f'all: {time() - ft_start} s') click.echo(f'average: {average / number} s')
def test_greedy(): tsp = generator(size) matrix = adjacency_matrix(tsp) length, tour = greedy(matrix) assert round(get_length(tour, matrix), 2) == round(length, 2), 'generated wrong tour'
def generate_metric_tsp(): """ Генерируем данные для метрической задачи коммивояжера """ tsp = generator(size) matrix = adjacency_matrix(tsp) length, tour = greedy(matrix) return length, tour, matrix
def __init__(self, opt: str, matrix: np.ndarray, **kwargs): length, tour = greedy(matrix) self.matrix = matrix self.opt = opts_type[opt](length, tour, matrix, **kwargs) self.length, self.tour = self.opt.length, self.opt.tour self.proc = kwargs.get('proc', 4)