Skip to content

Commit d08f4e7

Browse files
committed
Move on stop_criterion to CLI
1 parent e011ec4 commit d08f4e7

3 files changed

Lines changed: 8 additions & 10 deletions

File tree

src/fandango/cli/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,9 @@ def _populate_fuzz_parser(parser: argparse.ArgumentParser) -> None:
460460
parser.add_argument(
461461
"--stop-criterion",
462462
type=str,
463+
default=None,
463464
dest="stop_criterion",
464-
default="lambda t: False",
465-
help='stop criterion to be used. This is a lambda function which is run on every new solution. Example: `lambda t: t.to_string().startswith("abc")`',
465+
help='stop criterion to be used. This is a lambda function which is run on every new solution. Example: `lambda t: t.to_string().startswith("abc")`. Is `eval()`ed, so be careful!',
466466
)
467467
parser.add_argument(
468468
"--stop-after-seconds",

src/fandango/cli/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def make_fandango_settings(
9393
_copy_setting(args, settings, "max_repetitions")
9494
_copy_setting(args, settings, "max_nodes")
9595
_copy_setting(args, settings, "max_node_rate")
96-
_copy_setting(args, settings, "stop_criterion")
96+
if hasattr(args, "stop_criterion") and args.stop_criterion is not None:
97+
# previously is a str, we eval it into a function
98+
settings["stop_criterion"] = eval(args.stop_criterion)
9799
_copy_setting(args, settings, "stop_after_seconds")
98100

99101
if hasattr(args, "start_symbol") and args.start_symbol is not None:

src/fandango/evolution/evaluation.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(
3030
diversity_k: int,
3131
diversity_weight: float,
3232
warnings_are_errors: bool = False,
33-
stop_criterion: Optional[Callable[..., bool]] = None,
33+
stop_criterion: Optional[Callable[[DerivationTree], bool]] = None,
3434
):
3535
self._grammar = grammar
3636
self._soft_constraints: list[SoftValue] = []
@@ -258,9 +258,7 @@ def evaluate_individual(
258258
self._fitness_cache[key] = (fitness, failing_trees, suggestion)
259259
return fitness, failing_trees, suggestion
260260

261-
def evaluate_population(
262-
self, population: list[DerivationTree]
263-
) -> Generator[
261+
def evaluate_population(self, population: list[DerivationTree]) -> Generator[
264262
DerivationTree,
265263
None,
266264
list[tuple[DerivationTree, float, list[FailingTree], Suggestion]],
@@ -442,9 +440,7 @@ def evaluate_individual(
442440
self._fitness_cache[key] = (fitness, failing_trees, suggestion)
443441
return fitness, failing_trees, suggestion
444442

445-
def evaluate_population(
446-
self, population: list[DerivationTree]
447-
) -> Generator[
443+
def evaluate_population(self, population: list[DerivationTree]) -> Generator[
448444
DerivationTree,
449445
None,
450446
list[tuple[DerivationTree, float, list[FailingTree], Suggestion]],

0 commit comments

Comments
 (0)