From 9605a723266cf99de0a648d91b19ad9f4d5f4d5b Mon Sep 17 00:00:00 2001 From: Kento HASEGAWA Date: Tue, 27 Aug 2019 12:23:13 +0900 Subject: [PATCH] Update scripts for adc2019-system --- kwmr_solver_pynq.py | 98 +++++++++++++++++++++++++++++++++++++++++++++ scripts/main.py | 3 ++ 2 files changed, 101 insertions(+) create mode 100644 kwmr_solver_pynq.py diff --git a/kwmr_solver_pynq.py b/kwmr_solver_pynq.py new file mode 100644 index 0000000..33a9208 --- /dev/null +++ b/kwmr_solver_pynq.py @@ -0,0 +1,98 @@ +import argparse +import os +import random +import subprocess +import uuid + +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) + + problem = params['problem'] + + basedir = os.path.abspath(os.path.dirname(__file__)) + + pid = str(uuid.uuid4()) + problem_filepath = f'{basedir}/P{pid}.txt' + with open(problem_filepath, 'w') as fp: + fp.write(problem) + + aid = str(uuid.uuid4()) + solution_filepath = f'{basedir}/A{aid}.txt' + if os.path.exists(solution_filepath): + os.remove(solution_filepath) + + seed = random.randint(0, 2147483647) # seed: uint32_t <= 2147483647 + + cmds = f'python3 {basedir}/solver_soft/scripts/main.py -p {problem_filepath} -s {seed} -o {solution_filepath}'.split() + + proc = subprocess.Popen( + cmds, + stdin=subprocess.PIPE, + # stderr=subprocess.PIPE, + # stdout=subprocess.PIPE + ) + + try: + proc.communicate(problem.encode()) + status = 'done' + except Exception as ex: + status = 'failed' + finally: + returncode = proc.returncode + + solution = '' + if returncode == 0: + status = 'done' + if os.path.exists(solution_filepath): + with open(solution_filepath, 'r') as fp: + solution = fp.read() + else: + status = 'failed' + 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) diff --git a/scripts/main.py b/scripts/main.py index 4f135e8..7e2da33 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -1,5 +1,6 @@ import numpy as np from argparse import ArgumentParser +import sys import time import mySolverModule @@ -105,9 +106,11 @@ def main(args): if W_ext[0] > info['W'][0] or H_ext[0] > info['H'][0]: print('Fail satisfying constraint T_T') + sys.exit(1) else: print('Satisfy constraint ^_^') if args['output'] is not None: mySolverModule.output_to_file(args['output'], W_ext[0], H_ext[0], info['blocks'][0], info['line_num'][0], info['block_info'], result) + sys.exit(0) if __name__ == '__main__': -- 2.22.0