if __name__ == '__main__': # set up arguments parser = argparse.ArgumentParser( description='utility test cases in Manhattan Graph format') parser.add_argument('-p', '--points', nargs=2, type=int, default=[DEFAULT_NUM_POINTS_START, DEFAULT_NUM_POINTS_END], help='Range of number of points per test case') parser.add_argument('-s', '--size', nargs='?', default=DEFAULT_SIZE, type=int, help='Size of the map to use') parser.add_argument('-n', '--num', nargs='?', default=DEFAULT_NUM_CASES, help='Number of test cases to generate', type=int) args = parser.parse_args() # remove old cases, write new ones and build the project utility.write_cases(args.num, args.points, args.size, overwrite=True) utility.build_project() # print the header header_format = "{:>5} | {:>16} | {:>16} | {:>16} | {:>16}" print(header_format.format('Case', 'Naive Time (s)', 'Optimal Time (s)', 'Difference (s)', 'Speedup ')) print('{:->5}-+-{:->16}-+-{:->16}-+-{:->16}-+-{:->16}'.format('', '', '', '', '')) # run the binary on all cases for case_filename in utility.iterate_cases(): # benchmark naive and optimal solutions naive_time = utility.benchmark_solution([utility.PROJECT_BINARY, '-m', 'NAIVETSP'], case_filename)
DEFAULT_NUM_CASES = 100 DEFAULT_NUM_POINTS = (14, 16) DEFAULT_MAP_SIZE = 100 if __name__ == '__main__': PARSER = argparse.ArgumentParser( description='Benchmark the little-tsp binary') PARSER.add_argument('--generate', dest='generate', action='store_true', help='Whether to generate new cases or not') PARSER.add_argument('--no-generate', dest='generate', action='store_false', help='Whether to generate new cases or not') PARSER.set_defaults(generate=False) ARGS = PARSER.parse_args() # generate cases if necessary if ARGS.generate: utility.write_cases(DEFAULT_NUM_CASES, DEFAULT_NUM_POINTS, DEFAULT_MAP_SIZE, overwrite=True) # run the binary on all the cases, print the time it takes for case_filename in utility.iterate_cases(): print(utility.benchmark_solution(utility.PROJECT_BINARY, case_filename))
import argparse import utility DEFAULT_NUM_CASES = 100 DEFAULT_NUM_POINTS = (14, 16) DEFAULT_MAP_SIZE = 100 if __name__ == '__main__': PARSER = argparse.ArgumentParser( description='Benchmark the little-tsp binary') PARSER.add_argument( '--generate', dest='generate', action='store_true', help='Whether to generate new cases or not') PARSER.add_argument( '--no-generate', dest='generate', action='store_false', help='Whether to generate new cases or not') PARSER.set_defaults(generate=False) ARGS = PARSER.parse_args() # generate cases if necessary if ARGS.generate: utility.write_cases( DEFAULT_NUM_CASES, DEFAULT_NUM_POINTS, DEFAULT_MAP_SIZE, overwrite=True) # run the binary on all the cases, print the time it takes for case_filename in utility.iterate_cases(): print(utility.benchmark_solution(utility.PROJECT_BINARY, case_filename))
'--size', nargs='?', default=DEFAULT_SIZE, type=int, help='Size of the map to use') parser.add_argument('-n', '--num', nargs='?', default=DEFAULT_NUM_CASES, help='Number of test cases to generate', type=int) args = parser.parse_args() # remove old test cases, write new ones, and bootstrap them utility.write_cases(args.num, args.points, args.size, overwrite=True) utility.build_project() for case_filename in utility.iterate_cases(): # run both naive tsp and little tsp with open(case_filename, 'r') as case_file, open(NAIVE_FILENAME, 'w') as naive_file: naive_process = subprocess.Popen( [utility.PROJECT_BINARY, '-m', 'NAIVETSP'], stdin=case_file, stdout=naive_file) with open(case_filename, 'r') as case_file, open(OPT_FILENAME, 'w') as opt_file: opt_process = subprocess.Popen( [utility.PROJECT_BINARY, '-m', 'OPTTSP'],