def test_parser_and(self): World.reset() World._evaluation_mode = tfl.LOGIC_MODE def inside(x, y): centers_distance = tf.sqrt( tf.reduce_sum(tf.squared_difference(x[:, 0:2], y[:, 0:2]), axis=1) + 1e-6) return tf.cast((centers_distance + x[:, 2]) < y[:, 2], tf.float32) tfl.setTNorm(id=tfl.SS, p=1) circles = tfl.Domain(label="Circles", data=[[0., 0, 1], [0, 0, 2], [0, 0, 3]]) inside = tfl.Predicate(label="inside", domains=["Circles", "Circles"], function=inside) x = tfl.variable(circles, "x") y = tfl.variable(circles, "y") a = tfl.atom(inside, (x, y)) b = tfl.atom(inside, (y, x)) f = tfl.and_n(a, b) tensor = tfl.constraint("inside(x,y) and inside(y,x)") sess = tf.Session() assert np.equal(sess.run(tensor), sess.run(f)).all()
def test_transposition(self): """This test is for evaluating caching when the same variable is used as two different arguments of the same predicate""" World.reset() def inside(x, y): centers_distance = tf.sqrt( tf.reduce_sum(tf.squared_difference(x[:, 0:2], y[:, 0:2]), axis=1) + 1e-6) return tf.cast((centers_distance + x[:, 2]) < y[:, 2], tf.float32) circles = tfl.Domain(label="Circles", data=[[0., 0, 1], [0, 0, 2], [0, 0, 3]]) inside = tfl.Predicate(label="inside", domains=["Circles", "Circles"], function=inside) tfl.setTNorm(id=tfl.SS, p=1) sess = tf.Session() # Constraint 1 x = tfl.variable(circles, name="x") y = tfl.variable(circles, name="y") a = tfl.atom(inside, (x, y)) b = tfl.atom(inside, (y, x)) rule = tfl.and_n(a, b) assert np.greater(sess.run(rule), np.zeros(shape=[3, 3, 3])).all() assert len(World._predicates_cache) == 1
def test_cache(self): World.reset() a = tf.zeros([10, 10]) print(a) """Checking caching mechanism works""" # Program Model nn1 = lambda x: tf.constant([0., 1, 0]) nn2 = lambda x: tf.constant([0., 0, 0]) images = tfl.Domain(label="Images", data=[[0., 0], [1, 1], [0.2, 0.3]]) zero = tfl.Predicate(label="zero", domains=["Images"], function=nn1) one = tfl.Predicate(label="one", domains=["Images"], function=nn2) close = tfl.Predicate( label="close", domains=["Images", "Images"], function=lambda x, y: tf.reduce_sum(tf.abs(x - y), axis=1)) tfl.setTNorm(id=tfl.PRODUCT, p=None) # Constraint 1 "zero(x) and one(y)" x = tfl.variable(images) y = tfl.variable(images) a = tfl.atom(zero, (x, )) b = tfl.atom(one, (y, )) andd = tfl.and_n(a, b) # Constraint 2 z = tfl.variable(images) h = tfl.variable(images) c = tfl.atom(zero, (z, )) d = tfl.atom(one, (h, )) papapa = tfl.and_n(c, d) ab = tfl.atom(close, (x, y)) assert len(World._predicates_cache ) == 3 # one of zero, one for one and one for close