import argparse import os import subprocess import time if __name__ == '__main__': import solver else: from solvers.twd import solver proc = None stop_flag = False def stop(): global proc global stop_flag stop_flag = True if not proc is None: proc.terminate() print('stopped') def solve(params): global proc global stop_flag stop_flag = False # 必要な変数があるか確認 assert('problem' in params) assert('timeout' in params) problem = params['problem'] timeout = params['timeout'] seed = params.get('seed', int(time.time())) basedir = os.path.abspath(os.path.dirname(__file__)) problem_filepath = f'{basedir}/tmp.txt' with open(problem_filepath, 'w') as fp: fp.write(params['problem']) if not stop_flag: Q = solver.readQ(problem_filepath) if not stop_flag: nodes, cnfs = solver.generate_sat(Q) cnf_filepath = f'{basedir}/p.txt' if not stop_flag: solver.print_cnf(nodes, cnfs, filename=cnf_filepath) returncode = 0 if not stop_flag: solution_filepath = f'{basedir}/a.txt' cmds = f'minisat {cnf_filepath} {solution_filepath}'.split() proc = subprocess.Popen( cmds, # stderr=subprocess.PIPE, # stdout=subprocess.PIPE ) try: # outs, errs = proc.communicate() proc.communicate() status = 'done' except Exception as ex: status = 'failed' finally: returncode = proc.returncode # outs = outs.decode() # errs = errs.decode() # print(outs) # print(errs) solution = '' if returncode == 10: # 10 when SAT status = 'done' if not stop_flag: A = solver.read_satA(solution_filepath, Q, nodes) if not stop_flag: solution += f'SIZE {A["w"]}X{A["h"]}\n' for r in A['map']: solution += ','.join(str(v) for v in r) + '\n' for bi, bv in A['BLOCK'].items(): solution += f'BLOCK#{bi} @({bv["x"]},{bv["y"]})\n' solution += '\n' else: status = 'failed' return { 'status': status, 'solution': solution } def main(args): with open(args['problem'], 'r') as fp: problem = fp.read() params = { 'problem': problem, 'timeout': args['timeout'] } ret = solve(params) print(ret) if __name__ == "__main__": parser = argparse.ArgumentParser(description='Wrapper Script for ADC2019 Solver') parser.add_argument('problem', type=str, help='File path to the problem file') parser.add_argument('-t', '--timeout', type=int, default=10, help='Timeout for the process of the solver') args = vars(parser.parse_args()) main(args)