From d817f449fb4f675430cf7e7bbeeaff878e67b3e4 Mon Sep 17 00:00:00 2001 From: Thomas Sergeys Date: Thu, 6 Jun 2024 17:44:45 +0200 Subject: [PATCH] Export utilities --- xcsp3/executable/main.py | 10 +++++----- xcsp3/executable/test/conftest.py | 29 ++++++++++++----------------- xcsp3/executable/test/export.py | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 xcsp3/executable/test/export.py diff --git a/xcsp3/executable/main.py b/xcsp3/executable/main.py index b1a663b54..68e38a519 100644 --- a/xcsp3/executable/main.py +++ b/xcsp3/executable/main.py @@ -325,13 +325,13 @@ def z3_arguments(args: Args, model:cp.Model): # Global parameters res = { "random_seed": args.seed, - "max_memory": bytes_as_mb(remaining_memory(args.mem_limit)) if args.mem_limit is not None else None, # hard upper limit, given in MB } # Sat parameters if args.sat: res |= { "threads": args.cores, # TODO what with hyperthreadding, when more threads than cores + "max_memory": bytes_as_mb(remaining_memory(args.mem_limit)) if args.mem_limit is not None else None, # hard upper limit, given in MB } # Opt parameters if args.opt: @@ -611,7 +611,7 @@ def run_helper(args:Args): signal.signal(signal.SIGABRT, sigterm_handler) # Main program - try: - main() - except Exception as e: - error_handler(e) + # try: + main() + # except Exception as e: + # error_handler(e) diff --git a/xcsp3/executable/test/conftest.py b/xcsp3/executable/test/conftest.py index 6bc5b96f7..d9b26909d 100644 --- a/xcsp3/executable/test/conftest.py +++ b/xcsp3/executable/test/conftest.py @@ -13,7 +13,7 @@ SUPPORTED_SOLVERS = ["choco", "ortools"] # Solvers supported by this testsuite (and the executable) INSTANCES_DIR = os.path.join(pathlib.Path(__file__).parent.resolve(), "..", "..") -def get_all_instances(instance_dir: os.PathLike, year:str): +def get_all_instances(instance_dir: os.PathLike, year:str, relative=False): """ Collects all problem instances in a dict: { @@ -39,13 +39,13 @@ def get_all_instances(instance_dir: os.PathLike, year:str): location = os.path.join(root, file) if "MiniCOP" in location: - MiniCOP[name] = location + MiniCOP[name] = location if not relative else location[len(instance_dir):] elif "MiniCSP" in location: - MiniCSP[name] = location + MiniCSP[name] = location if not relative else location[len(instance_dir):] elif "COP" in location: - COP[name] = location + COP[name] = location if not relative else location[len(instance_dir):] elif "CSP" in location: - CSP[name] = location + CSP[name] = location if not relative else location[len(instance_dir):] elif year == "2023": instance_dir = os.path.join(instance_dir, "XCSP23_V2") @@ -58,13 +58,13 @@ def get_all_instances(instance_dir: os.PathLike, year:str): location = os.path.join(root, file) if "MiniCOP23" in location: - MiniCOP[name] = location + MiniCOP[name] = location if not relative else location[len(instance_dir):] elif "MiniCSP23" in location: - MiniCSP[name] = location + MiniCSP[name] = location if not relative else location[len(instance_dir):] elif "COP23" in location: - COP[name] = location + COP[name] = location if not relative else location[len(instance_dir):] elif "CSP23" in location: - CSP[name] = location + CSP[name] = location if not relative else location[len(instance_dir):] return { "COP": COP, @@ -79,7 +79,7 @@ def get_all_instances(instance_dir: os.PathLike, year:str): cop_instance_types = ["COP", "MiniCOP"] csp_instance_types = ["CSP", "MiniCSP"] -def instances(type, year, filter) -> list: +def instances(type, year) -> list: """ Filters and aggregates problem instances based on the provided `type`. """ @@ -103,13 +103,9 @@ def instances(type, year, filter) -> list: instances = get_all_instances(INSTANCES_DIR, year)["COP"] else: raise() - # return instances.keys(), instances.values() - res = list(instances.items()) - if filter is not None: - res = [i for i in res if filter in i[0]] - return res + return list(instances.items()) def pytest_addoption(parser): """ @@ -128,7 +124,6 @@ def pytest_addoption(parser): parser.addoption("--only_transform", action="store_true") parser.addoption("--check", action="store_true") parser.addoption("--year", action="store", default="2022") - parser.addoption("--filter", action="store", default=None) def pytest_generate_tests(metafunc): @@ -137,7 +132,7 @@ def pytest_generate_tests(metafunc): """ # Get the test instances based on the provided filter - instance = instances(type=metafunc.config.getoption("type"), year=metafunc.config.getoption("year"), filter=metafunc.config.getoption("filter")) + instance = instances(type=metafunc.config.getoption("type"), year=metafunc.config.getoption("year")) # The test instances to solve if "instance" in metafunc.fixturenames: diff --git a/xcsp3/executable/test/export.py b/xcsp3/executable/test/export.py new file mode 100644 index 000000000..e9ba4ccaa --- /dev/null +++ b/xcsp3/executable/test/export.py @@ -0,0 +1,19 @@ +import json +import os +import pathlib +from conftest import get_all_instances, INSTANCES_DIR + +instance_dir = os.path.join(INSTANCES_DIR, "XCSP23_V2") +all_instances = get_all_instances(INSTANCES_DIR, year="2023") +all_instances_2 = get_all_instances(INSTANCES_DIR, year="2023", relative=True) +for instance_type in all_instances.items(): + for instance in instance_type[1].items(): + with open(instance[1]) as f: + instance_str = "\n".join(f.readlines()) + if "maximize" in instance_str: + all_instances_2[instance_type[0]][instance[0]] = (instance[1], "max") + elif "minimize" in instance_str: + all_instances_2[instance_type[0]][instance[0]] = (instance[1], "min") + +with open(os.path.join(pathlib.Path(__file__).parent.resolve(), 'instances23.json'), 'w') as f: + json.dump(all_instances_2, f, indent=4) \ No newline at end of file