Skip to content

Commit

Permalink
A shy start for the library of problem generators mentioned in issue #43
Browse files Browse the repository at this point in the history
  • Loading branch information
gfrances committed Aug 4, 2019
1 parent a73c7e2 commit 8ba00da
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
Empty file.
60 changes: 60 additions & 0 deletions src/tarski/benchmarks/counters.py
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 added tests/benchmarks/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions tests/benchmarks/test_counters.py
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

0 comments on commit 8ba00da

Please sign in to comment.