Skip to content

Commit

Permalink
Export utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomSerg committed Jun 6, 2024
1 parent f9f3143 commit d817f44
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
10 changes: 5 additions & 5 deletions xcsp3/executable/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
29 changes: 12 additions & 17 deletions xcsp3/executable/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
{
Expand All @@ -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")
Expand All @@ -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,
Expand All @@ -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`.
"""
Expand All @@ -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):
"""
Expand All @@ -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):
Expand All @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions xcsp3/executable/test/export.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit d817f44

Please sign in to comment.