Skip to content

Commit 148f559

Browse files
committed
Refactor portfolio selector commands for consistency in configuration_selection.sh and streamline categorical value parsing in PCSConverter
1 parent 15a7488 commit 148f559

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

Examples/configuration_selection.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,13 @@ sparkle generate report
7272

7373
# Run the portfolio selector on a *single* testing instance; the result will be printed to the command line if you add `--run-on local` to the command.
7474

75-
sparkle run portfolio selector --selection-scenario Output/Selection/MultiClassClassifier_RandomForestClassifier/PbO-CCSAT-G
76-
eneric/ --instance Examples/Resources/Instances/PTN2/plain7824.cnf --run-on local
75+
sparkle run portfolio selector --selection-scenario Output/Selection/MultiClassClassifier_RandomForestClassifier/PbO-CCSAT-Generic/ --instance Examples/Resources/Instances/PTN2/plain7824.cnf --run-on local
7776

7877
### Run on an instance set
7978

8079
# Run the portfolio selector on a testing instance *set*
8180

82-
sparkle run portfolio selector --selection-scenario Output/Selection/MultiClassClassifier_RandomForestClassifier/PbO-CCSAT-G
83-
eneric/ --instance-set Examples/Resources/Instances/PTN2/
81+
sparkle run portfolio selector --selection-scenario Output/Selection/MultiClassClassifier_RandomForestClassifier/PbO-CCSAT-Generic/ --instance-set Examples/Resources/Instances/PTN2/
8482
sparkle jobs # Wait for the portfolio selector to be done running on the testing instance set
8583

8684
#### Generate a report including results on the test set

src/sparkle/tools/parameters.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,6 @@ class PCSConverter:
6161
r"(?P<comment>#.*)?$"
6262
)
6363

64-
@staticmethod
65-
def parse_irace_categorical_values(raw_values: str) -> list[str]:
66-
"""Split unquoted IRACE categorical values into strings."""
67-
stripped = raw_values.strip()
68-
# check for enclosing like c("option1", "option2")
69-
if stripped.startswith("c(") and stripped.endswith(")"):
70-
# Remove the c( and ) and get the inner content: "option1", "option2"
71-
stripped = stripped[2:-1]
72-
# check for enclosing like ("option1", "option2") or ['option1', 'option2'] or {option1, option2}
73-
elif stripped and stripped[0] in "({[" and stripped[-1] in ")}]":
74-
# Remove the enclosing brackets and get: option1, option2
75-
stripped = stripped[1:-1]
76-
values = []
77-
for token in stripped.split(","):
78-
token = token.strip()
79-
if not token: # Empty token
80-
continue
81-
if len(token) >= 2 and token[0] == token[-1] and token[0] in ("'", '"'):
82-
# Remove enclosing quotes
83-
token = token[1:-1]
84-
values.append(token)
85-
return values
86-
8764
@staticmethod
8865
def get_convention(file: Path) -> PCSConvention:
8966
"""Determines the format of a pcs file."""
@@ -447,13 +424,31 @@ def parse_irace(content: list[str] | Path) -> ConfigurationSpace:
447424
# domain relies on another parameter: p2 "--p2" r ("p1", "p1 + 10")"
448425
# and is limited to the operators: +,-, *, /, %%, min, max
449426
raw_values = parameter.group("values")
450-
try:
427+
if parameter_type in {"c", "o"}:
428+
stripped_values = raw_values.strip()
429+
if stripped_values.startswith("c(") and stripped_values.endswith(
430+
")"
431+
):
432+
stripped_values = stripped_values[2:-1]
433+
elif (
434+
stripped_values
435+
and stripped_values[0] in "({["
436+
and stripped_values[-1] in ")}]"
437+
):
438+
stripped_values = stripped_values[1:-1]
439+
values = []
440+
categorical_token_pattern = re.compile(
441+
r'\s*(?:"([^"]*)"|\'([^\']*)\'|([^,]+?))\s*(?:,|$)'
442+
)
443+
for match in categorical_token_pattern.finditer(stripped_values):
444+
token = match.group(1) or match.group(
445+
2
446+
) # double or single quotes
447+
if token is None:
448+
token = match.group(3).strip() # unquoted
449+
values.append(token)
450+
else:
451451
values = ast.literal_eval(raw_values)
452-
except (SyntaxError, ValueError):
453-
if parameter_type in {"c", "o"}:
454-
values = PCSConverter.parse_irace_categorical_values(raw_values)
455-
else:
456-
raise
457452
scale = parameter.group("scale")
458453
conditions = parameter.group("conditions")
459454
comment = parameter.group("comment")

0 commit comments

Comments
 (0)