Skip to content

Commit 0b5ece0

Browse files
author
Markus Iser
committed
towards good
1 parent 3d77b0d commit 0b5ece0

9 files changed

Lines changed: 166 additions & 31 deletions

File tree

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ include gbd_server/static/img/*.png
55
include gbd_server/static/img/*.jpg
66
include gbd_server/static/*.css
77
include gbd_server/static/*.js
8+
include default_config.toml
89

default_config.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[contexts]
2+
default = "cnf"
3+
cnf = { suffix = ".cnf", idfunc = "cnf_hash", description = "DIMACS Conjunctive Normal Form (CNF)" }
4+
sancnf = { suffix = ".sanitized.cnf", idfunc = "cnf_hash", description = "Sanitized CNF" }
5+
kis = { suffix = ".kis", idfunc = "cnf_hash", description = "k-Independent Set Problem Graph" }
6+
opb = { suffix = ".opb", idfunc = "opb_hash", description = "Pseudo-Boolean Optimization Problem" }
7+
wcnf = { suffix = ".wcnf", idfunc = "wcnf_hash", description = "Weighted CNF (WCNF)" }
8+
wecnf = { suffix = ".wecnf", idfunc = "cnf_hash", description = "Weighted Extended CNF (WECNF)" }
9+
10+
[transformers]
11+
12+
[extractors]

gbd_core/config.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import toml
2+
import os
3+
import importlib.resources as pkg_resources
4+
5+
### Default Context
6+
default = "cnf"
7+
8+
### Load Configuration from Files
9+
def load_config(default_config_path, user_config_path=None):
10+
# Load the default configuration file
11+
with pkg_resources.open_text('gbd_tools', default_config_path) as f:
12+
config = toml.load(f)
13+
14+
# If a user configuration file is provided, load it and update the config
15+
if user_config_path and os.path.exists(user_config_path):
16+
with open(user_config_path, 'r') as f:
17+
user_config = toml.load(f)
18+
config.update(user_config)
19+
20+
return config
21+
22+
### Convert ConfigParser to Dictionary
23+
def config_to_dict(config):
24+
config_dict = {}
25+
for context, details in config['contexts'].items():
26+
config_dict[context] = {
27+
"description": details["description"],
28+
"suffix": details["suffix"],
29+
"idfunc": globals()[details["idfunc"]],
30+
}
31+
return config_dict
32+
33+
### Paths to Configuration Files
34+
default_config_path = "default_config.toml"
35+
user_config_path = "user_config.toml" # Adjust this path as needed
36+
37+
### Load and Convert Configuration
38+
config_parser = load_config(default_config_path, user_config_path)
39+
config = config_to_dict(config_parser)

gbd_core/contexts.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ def description(context):
5656
return config[context]['description']
5757

5858
def suffixes(context):
59-
packed = [ "", ".gz", ".lzma", ".xz", ".bz2" ]
60-
return [ config[context]['suffix'] + p for p in packed ]
59+
return [ config[context]['suffix'] + p for p in [ "", ".gz", ".lzma", ".xz", ".bz2" ] ]
6160

6261
def idfunc(context):
6362
return config[context]['idfunc']

gbd_init/feature_extractors.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@
2323
from gbd_core.util import eprint, confirm
2424
from gbd_init.initializer import Initializer, InitializerException
2525

26-
gbdc_available = True
2726
try:
28-
from gbdc import extract_base_features, base_feature_names, extract_gate_features, gate_feature_names, isohash, wcnfisohash, wcnf_base_feature_names, extract_wcnf_base_features, opb_base_feature_names, extract_opb_base_features
27+
from gbdc import extract_base_features, base_feature_names, extract_gate_features, gate_feature_names, isohash, wcnfisohash, wcnf_base_feature_names, extract_wcnf_base_features, opb_base_feature_names, extract_opb_base_features, checksani, checksani_feature_names
2928
except ImportError:
30-
gbdc_available = False
31-
warnings.warn("gbdc not found. Please install using 'pip install gbdc'.")
3229
def extract_base_features(path, tlim, mlim):
3330
raise ModuleNotFoundError("gbdc not found", name="gbdc")
3431

@@ -55,6 +52,12 @@ def extract_opb_base_features(path, tlim, mlim):
5552

5653
def opb_base_feature_names():
5754
return [ ]
55+
56+
def checksani(path, tlim, mlim):
57+
raise ModuleNotFoundError("gbdc not found", name="gbdc")
58+
59+
def checksani_feature_names():
60+
return [ ]
5861

5962
## GBDHash
6063
def compute_hash(hash, path, limits):
@@ -96,23 +99,35 @@ def compute_opb_base_features(hash, path, limits, tp=None):
9699
rec = extract_opb_base_features(path, limits['tlim'], limits['mlim'])
97100
return [ (key, hash, int(value) if isinstance(value, float) and value.is_integer() else value) for key, value in rec.items() ]
98101

102+
## SANI Features
103+
def compute_sani_features(hash, path, limits, tp=None):
104+
eprint('Extracting SANI features from {} {}'.format(hash, path))
105+
rec = checksani(path, limits['tlim'], limits['mlim'])
106+
return [ (key, hash, int(value) if isinstance(value, float) and value.is_integer() else value) for key, value in rec.items() ]
107+
99108

100109
generic_extractors = {
101110
"base" : {
102111
"description" : "Extract base features from CNF files. ",
103-
"contexts" : [ "cnf" ],
112+
"contexts" : [ "cnf", "sancnf" ],
104113
"features" : [ (name, "empty") for name in base_feature_names() ],
105114
"compute" : compute_base_features,
106115
},
116+
"checksani" : {
117+
"description" : "Extract sanitise status from CNF files. ",
118+
"contexts" : [ "cnf", "sancnf" ],
119+
"features" : [ (name, "empty") for name in checksani_feature_names() ],
120+
"compute" : compute_sani_features,
121+
},
107122
"gate" : {
108123
"description" : "Extract gate features from CNF files. ",
109-
"contexts" : [ "cnf" ],
124+
"contexts" : [ "cnf", "sancnf" ],
110125
"features" : [ (name, "empty") for name in gate_feature_names() ],
111126
"compute" : compute_gate_features,
112127
},
113128
"isohash" : {
114129
"description" : "Compute ISOHash for CNF or WCNF files. ",
115-
"contexts" : [ "cnf", "wcnf" ],
130+
"contexts" : [ "cnf", "wcnf", "sancnf" ],
116131
"features" : [ ("isohash", "empty") ],
117132
"compute" : compute_isohash,
118133
},

gbd_init/instance_transformers.py

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,36 @@
2525
from gbd_init.initializer import Initializer, InitializerException
2626

2727
try:
28-
from gbdc import cnf2kis, sanitize
28+
from gbdc import cnf2kis, sanitise, normalise
2929
except ImportError:
30-
def cnf2kis(ipath, opath, maxedges, maxnodes, tlim, mlim, flim):
30+
def cnf2kis(ipath, opath):
3131
raise ModuleNotFoundError("gbdc not found", name="gbdc")
3232

33-
def sanitize(ipath, tlim, mlim):
33+
def sanitise(ipath, opath):
34+
raise ModuleNotFoundError("gbdc not found", name="gbdc")
35+
36+
def normalise(ipath, opath):
3437
raise ModuleNotFoundError("gbdc not found", name="gbdc")
3538

3639

37-
# Transform SAT Problem to k-Independent Set Problem
3840
def kis_filename(path):
3941
kispath = reduce(lambda path, suffix: path[:-len(suffix)] if path.endswith(suffix) else path, contexts.suffixes('cnf'), path)
4042
return kispath + '.kis'
4143

42-
# Sanitize CNF
43-
def sanitized_filename(path):
44+
def sanitised_filename(path):
4445
sanpath = reduce(lambda path, suffix: path[:-len(suffix)] if path.endswith(suffix) else path, contexts.suffixes('cnf'), path)
4546
return sanpath + '.sanitized.cnf'
4647

48+
def normalised_filename(path):
49+
normpath = reduce(lambda path, suffix: path[:-len(suffix)] if path.endswith(suffix) else path, contexts.suffixes('cnf'), path)
50+
return normpath + '.normalised.cnf'
51+
4752

4853
def wrap_cnf2kis(hash, path, limits):
4954
kispath = kis_filename(path)
5055
util.eprint('Transforming {} to k-ISP {}'.format(path, kispath))
5156
try:
52-
result = cnf2kis(path, kispath, 2**32, 2**32, limits['tlim'], limits['mlim'], limits['flim'])
57+
result = cnf2kis(path, kispath)
5358
if "local" in result:
5459
kishash = result['hash']
5560
return [ ('local', kishash, result['local']), ('to_cnf', kishash, hash),
@@ -63,20 +68,39 @@ def wrap_cnf2kis(hash, path, limits):
6368

6469
return [ ]
6570

66-
def wrap_sanitize(hash, path, limits):
67-
sanname = sanitized_filename(path)
68-
util.eprint('Sanitizing {}'.format(path))
71+
def wrap_sanitise(hash, path, limits):
72+
sanpath = sanitised_filename(path)
73+
util.eprint('Sanitising {}'.format(path))
6974
try:
70-
with open(sanname, 'w') as f, util.stdout_redirected(f):
71-
if sanitize(path):
72-
sanhash = identify(sanname)
73-
return [ ('local', sanhash, sanname), ('to_cnf', sanhash, hash) ]
75+
with open(sanpath, 'w') as f, util.stdout_redirected(f):
76+
result = sanitise(path, sanpath)
77+
if "local" in result:
78+
sanhash = result['hash']
79+
return [ ('local', sanhash, result['local']), ('to_cnf', sanhash, hash) ]
7480
else:
7581
raise GBDException("Sanitization failed for {}".format(path))
7682
except Exception as e:
7783
util.eprint(str(e))
78-
if os.path.exists(sanname):
79-
os.remove(sanname)
84+
if os.path.exists(sanpath):
85+
os.remove(sanpath)
86+
87+
return [ ]
88+
89+
def wrap_normalise(hash, path, limits):
90+
normpath = normalised_filename(path)
91+
util.eprint('Normalising {}'.format(path))
92+
try:
93+
with open(normpath, 'w') as f, util.stdout_redirected(f):
94+
result = normalise(path, normpath)
95+
normhash = result['hash']
96+
if "local" in result and hash == normhash:
97+
return [ ('local', normhash, result['local']) ]
98+
else:
99+
raise GBDException("Normalisation failed for {}".format(path))
100+
except Exception as e:
101+
util.eprint(str(e))
102+
if os.path.exists(normpath):
103+
os.remove(normpath)
80104

81105
return [ ]
82106

@@ -98,13 +122,21 @@ def transform_instances_generic(key: str, api: GBD, rlimits, query, hashes, targ
98122

99123

100124
generic_transformers = {
101-
"sanitize" : {
102-
"description" : "Sanitize CNF files. ",
125+
"sanitise" : {
126+
"description" : "Sanitise CNF files. ",
103127
"source" : [ "cnf" ],
104128
"target" : [ "sancnf" ],
105129
"features" : [ ('local', None) , ('to_cnf', None) ],
106-
"compute" : wrap_sanitize,
107-
"filename" : sanitized_filename,
130+
"compute" : wrap_sanitise,
131+
"filename" : sanitised_filename,
132+
},
133+
"normalise" : {
134+
"description" : "Normalise CNF files. ",
135+
"source" : [ "cnf" ],
136+
"target" : [ "cnf" ],
137+
"features" : [ ('local', None) ],
138+
"compute" : wrap_normalise,
139+
"filename" : normalised_filename,
108140
},
109141
"cnf2kis" : {
110142
"description" : "Transform CNF files to k-ISP instances. ",

pyproject.toml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[build-system]
2+
requires = ["setuptools>=42", "wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "gbd_tools"
7+
version = "4.9.7"
8+
description = "GBD Tools: Maintenance and Distribution of Benchmark Instances and their Attributes"
9+
readme = "README.md"
10+
requires-python = ">=3.6"
11+
license = { text = "MIT License" }
12+
authors = [
13+
{ name = "Markus Iser", email = "markus.iser@kit.edu" }
14+
]
15+
urls = { Homepage = "https://github.com/Udopia/gbd" }
16+
classifiers = [
17+
"License :: OSI Approved :: MIT License",
18+
"Programming Language :: Python :: 3"
19+
]
20+
dependencies = [
21+
"flask",
22+
"tatsu",
23+
"pandas",
24+
"waitress",
25+
"pebble",
26+
"gbdc"
27+
]
28+
scripts = { gbd = "gbd:main" }
29+
30+
[tool.setuptools.packages.find]
31+
where = ["gbd_core", "gbd_init", "gbd_server"]
32+
33+
[tool.setuptools]
34+
include-package-data = true

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup, find_packages
22

33
setup(name='gbd_tools',
4-
version='4.9.7',
4+
version='4.9.8',
55
description='GBD Tools: Maintenance and Distribution of Benchmark Instances and their Attributes',
66
long_description=open('README.md', 'rt').read(),
77
long_description_content_type="text/markdown",

update_tool.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
sudo rm -rf dist/
2-
sudo python3 setup.py develop sdist bdist_wheel
2+
# sudo python3 setup.py develop sdist bdist_wheel
3+
# twine upload dist/*
4+
sudo python3 -m pip install --upgrade build twine
5+
sudo python3 -m build
36
twine upload dist/*
47
sudo rm -Rf gbd_tools.egg-info dist build

0 commit comments

Comments
 (0)