-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A shy start for the library of problem generators mentioned in issue #43
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
Generate FSTRIPS Counters languages and problems | ||
""" | ||
from ..fstrips import create_fstrips_problem, language | ||
from ..syntax import land | ||
from ..theories import Theory | ||
|
||
|
||
BASE_DOMAIN_NAME = "counters-fn" | ||
|
||
|
||
def generate_fstrips_counters_language(ncounters=3, upper_bound=None): | ||
""" The (typed) FSTRIPS Counters encoding """ | ||
upper_bound = ncounters * 2 if upper_bound is None else upper_bound | ||
|
||
lang = language(BASE_DOMAIN_NAME, theories=[Theory.EQUALITY, Theory.ARITHMETIC]) | ||
lang.sort('counter') | ||
lang.interval('val', lang.Integer, lower_bound=0, upper_bound=upper_bound) | ||
|
||
lang.function('value', 'counter', 'val') | ||
lang.function('max_int', 'val') | ||
|
||
[lang.constant(f'c{k}', 'counter') for k in range(1, ncounters+1)] | ||
return lang | ||
|
||
|
||
def generate_fstrips_counters_problem(ncounters=3, upper_bound=None): | ||
""" Generate a (typed) FSTRIPS Counters problem with the given number of counters """ | ||
upper_bound = ncounters * 2 if upper_bound is None else upper_bound | ||
lang = generate_fstrips_counters_language(ncounters=ncounters, upper_bound=upper_bound) | ||
|
||
problem = create_fstrips_problem(domain_name=BASE_DOMAIN_NAME, problem_name='test-instance', language=lang) | ||
counter_t = lang.get_sort('counter') | ||
counters = sorted(counter_t.domain(), key=lambda x: x.symbol) | ||
|
||
value = lang.get_function('value') | ||
max_int = lang.get_function('max_int') | ||
|
||
inequalities = [value(c1) < value(c2) for c1, c2 in zip(counters, counters[1:])] | ||
problem.goal = land(*inequalities, flat=True) | ||
|
||
c = lang.variable('c', 'counter') | ||
|
||
problem.action( | ||
"increment", [c], | ||
precondition=value(c) < max_int(), | ||
effects=[value(c) << value(c) + 1] | ||
) | ||
|
||
problem.action( | ||
"decrement", [c], | ||
precondition=value(c) > 0, | ||
effects=[value(c) << value(c) - 1] | ||
) | ||
|
||
problem.init.set(max_int, upper_bound) | ||
for c in counters: | ||
problem.init.set(value, c, 0) | ||
|
||
return problem |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
from tarski.benchmarks.counters import generate_fstrips_counters_problem | ||
from tarski.evaluators.simple import evaluate | ||
from tarski.syntax import is_and | ||
|
||
|
||
def test_counters(): | ||
problem = generate_fstrips_counters_problem(ncounters=3) | ||
lang = problem.language | ||
value, c1 = lang.get('value', 'c1') | ||
|
||
problem.init.evaluator = evaluate | ||
|
||
# More than testing some particular property, here we want to test that the generator can run correctly | ||
assert is_and(problem.goal) and len(problem.goal.subformulas) == 2 | ||
assert problem.init[value(c1)] == 0 |