def test_model_list_extensions(): lang = tarski.language(theories=[]) p = lang.predicate('p', lang.Object, lang.Object) f = lang.function('f', lang.Object, lang.Object) o1 = lang.constant("o1", lang.Object) o2 = lang.constant("o2", lang.Object) model = Model(lang) model.evaluator = evaluate model.set(f, o1, o2) model.add(p, o1, o2) extensions = model.list_all_extensions() ext_f = extensions[f.signature] ext_p = extensions[p.signature] # We want to test that the `list_all_extensions` method correctly unwraps all TermReferences in the internal # representation of the model and returns _only_ actual Tarski terms. Testing this is a bit involved, as of # course we cannot just check for (o1, o2) in ext_f, because that will trigger the wrong __eq__ and __hash__ # methods - in other words, to test this we precisely need to wrap things back into TermReferences, so that we can # compare them properly assert len(ext_f) == 1 and len(ext_p) == 1 p, v = ext_f[0] assert TermReference(p) == TermReference(o1) and TermReference(v) == TermReference(o2) v1, v2 = ext_p[0] assert TermReference(v1) == TermReference(o1) and TermReference(v2) == TermReference(o2)
def test_matrix_evaluation_case_1(): from tarski.syntax.arithmetic import one lang = tarski.language('double_integrator', [Theory.EQUALITY, Theory.ARITHMETIC]) I = lang.matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]], lang.Real) x0 = Model(lang) x0.evaluator = evaluate assert x0[I][0, 0].is_syntactically_equal(one(lang.Real))
def test_special_function_min(): from tarski.syntax.arithmetic.special import min lang = tarski.fstrips.language( theories=[Theory.ARITHMETIC, Theory.SPECIAL]) model = Model(lang) model.evaluator = evaluate reals = lang.Real a, b = lang.constant(3.14, reals), lang.constant(1.24, reals) assert model[min(a, b)].symbol == 1.24
def test_special_function_erfc(): from tarski.syntax.arithmetic.special import erfc lang = tarski.fstrips.language( theories=[Theory.ARITHMETIC, Theory.SPECIAL]) model = Model(lang) model.evaluator = evaluate reals = lang.Real x = lang.constant(0.5, reals) assert model[erfc(x)].symbol == sci.erfc(0.5)
def test_blocksworld_set_via_square_brackets(): lang = blocksworld.generate_small_fstrips_bw_language() model = Model(lang) model.evaluator = evaluate loc = lang.get_function('loc') b1, table = (lang.get_constant(s) for s in ('b1', 'table')) model.set(loc, b1, table) assert model[loc(b1)] == table
def test_zero_ary_predicate_set(): L = tarski.language() P = L.predicate('P') M = Model(L) M.add(P) assert evaluate(P(), M) is True
def test_arcsin(): import numpy as np from tarski.syntax.arithmetic.special import asin lang = tarski.fstrips.language( theories=[Theory.ARITHMETIC, Theory.SPECIAL]) model = Model(lang) model.evaluator = evaluate reals = lang.Real alpha = lang.constant(0.5, reals) assert model[asin(alpha)].symbol == np.arcsin(0.5)
def test_special_function_sgn(): import numpy as np from tarski.syntax.arithmetic.special import sgn lang = tarski.fstrips.language( theories=[Theory.ARITHMETIC, Theory.SPECIAL]) model = Model(lang) model.evaluator = evaluate reals = lang.Real x = lang.constant(0.5, reals) assert model[sgn(x)].symbol == np.sign(0.5)
def test_special_function_pow(): import numpy as np from tarski.syntax.arithmetic import pow lang = tarski.fstrips.language( theories=[Theory.ARITHMETIC, Theory.SPECIAL]) model = Model(lang) model.evaluator = evaluate reals = lang.Real alpha = lang.constant(0.5, reals) assert model[pow(alpha, 2.0)].symbol == np.power(0.5, 2.0)
def test_random_function_gamma(): import numpy as np from tarski.syntax.arithmetic.random import gamma np.random.seed(1234) # for repeatability lang = tarski.fstrips.language( theories=[Theory.ARITHMETIC, Theory.SPECIAL, Theory.RANDOM]) model = Model(lang) model.evaluator = evaluate reals = lang.Real shape = lang.constant(1.0, reals) scale = lang.constant(5.0, reals) the_term = gamma(shape, scale) assert isinstance(the_term, tarski.syntax.Term) assert model[gamma(shape, scale)].symbol == 1.0629932880924005
def test_random_function_normal(): import numpy as np from tarski.syntax.arithmetic.random import normal np.random.seed(1234) # for repeatability lang = tarski.fstrips.language( theories=[Theory.ARITHMETIC, Theory.SPECIAL, Theory.RANDOM]) model = Model(lang) model.evaluator = evaluate reals = lang.Real mu = lang.constant(0.5, reals) sigma = lang.constant(1.0, reals) the_term = normal(mu, sigma) assert isinstance(the_term, tarski.syntax.Term) assert model[normal(mu, sigma)].symbol == 0.9714351637324931
def test_special_function_abs(): from tarski.syntax.arithmetic.special import abs lang = tarski.fstrips.language( theories=[Theory.ARITHMETIC, Theory.SPECIAL]) model = Model(lang) model.evaluator = evaluate reals = lang.Real a = lang.constant(5.01, reals) b = lang.constant(-5.01, reals) c = lang.constant(0.001, reals) assert model[abs(a)].symbol == 5.01 assert model[abs(b)].symbol == 5.01 assert model[abs(a) > c] is True assert model[abs(b) > c] is True
def test_blocksworld_add(): lang = blocksworld.generate_small_fstrips_bw_language() model = Model(lang) clear = lang.get_predicate('clear') b1 = lang.get_constant('b1') model.add(clear, b1) assert evaluate(clear(b1), model) is True
def test_predicate_extensions(): lang = tarski.language() pred = lang.predicate('pred', lang.Object, lang.Object) o1 = lang.constant("o1", lang.Object) o2 = lang.constant("o2", lang.Object) model = Model(lang) with pytest.raises(errors.ArityMismatch): # This should raise an error, as the predicate is binary model.add(pred) with pytest.raises(ValueError): # This should raise an error, as the predicate sort does not coincide with the sort of the parameters model.add(pred, 1, 2) model.add(pred, o1, o2) assert not model.holds(pred, (o2, o1)) # Make sure the order in which the elements were added is respected! assert model.holds(pred, (o1, o2)) with pytest.raises(KeyError): # This should raise an error, as the tuple does not belong to the predicate's extension model.remove(pred, o2, o1) model.remove(pred, o1, o2) with pytest.raises(KeyError): # This should raise an error, as the tuple has been removed model.remove(pred, o1, o2)
def read_file(format_, file_): """ Read in the world as specified in `file`. """ with open(file_) as stream: reader = format_(stream=stream) world = reader.read_world() return Model(world=world)
def test_blocksworld_set(): lang = blocksworld.generate_small_fstrips_bw_language() model = Model(lang) loc = lang.get_function('loc') b1, table = (lang.get_constant(s) for s in ('b1', 'table')) model.set(loc, b1, table) assert evaluate(loc(b1), model) == table
def test_predicate_without_equality(): lang = tarski.language(theories=[]) leq = lang.predicate('leq', lang.Integer, lang.Integer) f = lang.function('f', lang.Object, lang.Integer) o1 = lang.constant("o1", lang.Object) o2 = lang.constant("o2", lang.Object) model = Model(lang) model.evaluator = evaluate model.set(f, o1, 1) model.set(f, o2, 2) for x in range(0, 5): for y in range(x, 5): model.add(leq, x, y) assert model[leq(f(o1), f(o2))] is True assert model[leq(f(o2), f(o1))] is False
def test_blocksworld_add(): lang = tarski.benchmarks.blocksworld.generate_fstrips_bw_language() model = Model(lang) clear = lang.get_predicate('clear') b1 = lang.get_constant('b1') b2 = lang.get_constant('b2') model.add(clear, b1) assert evaluate(clear(b1), model) is True assert evaluate(clear(b2), model) is False
def test_predicate_without_equality_reals(): import numpy lang = tarski.language(theories=[]) leq = lang.predicate('leq', lang.Real, lang.Real) w = lang.function('w', lang.Object, lang.Real) o1 = lang.constant("o1", lang.Object) o2 = lang.constant("o2", lang.Object) model = Model(lang) model.evaluator = evaluate model.setx(w(o1), 1.0) model.setx(w(o2), 2.0) for x in numpy.arange(0.0, 5.0): for y in numpy.arange(x, 5.0): model.add(leq, x, y) assert model[leq(w(o1), w(o2))] is True assert model[leq(w(o2), w(o1))] is False
def test_model_as_atoms(): lang = tarski.language(theories=[]) p = lang.predicate('p', lang.Object, lang.Object) f = lang.function('f', lang.Object, lang.Object) o1 = lang.constant("o1", lang.Object) o2 = lang.constant("o2", lang.Object) model = Model(lang) model.evaluator = evaluate model.set(f, o1, o2) model.add(p, o1, o2) atoms = model.as_atoms() patom, fatom = atoms assert patom.is_syntactically_equal(p(o1, o2)) term, value = fatom assert term.is_syntactically_equal( f(o1)) and value.is_syntactically_equal(o2)
def test_ite(): lang = tarski.language('arith', [Theory.EQUALITY, Theory.ARITHMETIC]) c = lang.constant(1, lang.Integer) x = lang.function('x', lang.Integer) y = lang.function('y', lang.Integer) phi = (x() <= y()) & (y() <= x()) t1 = x() + 2 t2 = y() + 3 tau = ite(phi, t1, t2) model = Model(lang) model.evaluator = evaluate model.setx(x(), 1) model.setx(y(), 2) assert model[tau].symbol == 5
def test_matrix_evaluation_case_2(): lang = tarski.language('double_integrator', [Theory.EQUALITY, Theory.ARITHMETIC]) I = lang.matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]], lang.Real) x = lang.function('x', lang.Real) y = lang.function('y', lang.Real) z = lang.function('z', lang.Real) v = lang.vector([x(), y(), z()], lang.Real) x0 = Model(lang) x0.evaluator = evaluate x0.setx(x(), 1.0) x0.setx(y(), 2.0) x0.setx(z(), 3.0) # print(x0[I @ v][2, 0]) assert x0[I @ v][2, 0].is_syntactically_equal(lang.constant(3.0, lang.Real))
def test_instance_creation(): L = tsk.language("mylang", theories=[Theory.EQUALITY, Theory.ARITHMETIC]) int_t = L.Integer obj_t = L.Object platform_t = L.sort('platform') rov1 = L.constant('rov1', platform_t) direction = L.function('direction', platform_t, int_t) sensor_sort = L.sort('sensor') camera, range, bearing = [L.constant(name, sensor_sort) for name in ('camera', 'range', 'bearing')] engaged = L.function('engaged', sensor_sort, int_t) region_t = L.sort('region') p0 = L.constant('p0', region_t) p1 = L.constant('p1', region_t) p2 = L.constant('p2', region_t) t0 = L.constant('t0', obj_t) t1 = L.constant('t1', obj_t) t2 = L.constant('t2', obj_t) #t0, t1, t2 = [L.constant('t{}'.format(i), obj_t) # for i in range(3)] position = L.predicate('position', obj_t, region_t) observed = L.predicate('observed', obj_t) estimated_range = L.predicate('estimated_range', obj_t) estimated_bearing = L.predicate('estimated_bearing', obj_t) req_1 = temporal.ResourceLock(**{ "ts": 0.0, "td": 10.0, "r": engaged(camera) }) req_2 = temporal.ResourceLock(**{ "ts": 0.0, "td": 10.0, "r": engaged(range) }) req_3 = temporal.ResourceLock(**{ "ts": 0.0, "td": 10.0, "r": engaged(bearing) }) req_4 = temporal.ResourceLevel(**{ "ts": 0.0, "td": 20.0, "r": direction(rov1), "n": L.constant(0, int_t) }) a1 = temporal.Action( name='localize_t0', parameters=[], precondition=position(rov1, p0), requirements=[ TimedEffect(0.0, req_2), TimedEffect(0.0, req_3)], timed_effects=[ TimedEffect(15.0, estimated_range(t0)), TimedEffect(20.0, estimated_bearing(t0))], untimed_effects=[] ) a2 = temporal.Action( name='observed_t0', parameters=[], precondition=land(position(rov1, p0), estimated_range(t0), estimated_bearing(t0)), requirements=[ TimedEffect(0.0, req_1), TimedEffect(0.0, req_2)], timed_effects=[ TimedEffect(21.0, observed(t0))], untimed_effects=[] ) initial = Model(L) initial.add(position, rov1, p0) initial.evaluator = evaluate inst = temporal.Instance( L=L, X=[position(rov1, p0), estimated_range(t0), estimated_bearing(t0), observed(t0), estimated_range(t1), estimated_bearing(t1), observed(t1), estimated_range(t2), estimated_bearing(t2), observed(t2)], I=initial, A=[a1, a2], G=observed(t0) ) assert len(inst.R) == 3