IBM / LNN

A `Neural = Symbolic` framework for sound and complete weighted real-value logic
https://IBM.github.io/LNN/
Apache License 2.0
226 stars 438 forks source link

Error running Smoking Example #60

Open buddha314 opened 1 year ago

buddha314 commented 1 year ago

From the Usage page I have abstracted the following code

from lnn import Predicates
from lnn import Variables
from lnn import Implies, Equivalent
from lnn import World
from lnn import Fact

Smokes, Cancer = Predicates('Smokes', 'Cancer')
Friends = Predicates('Friends', arity=2)

x, y = Variables('x', 'y')

Smoking_causes_Cancer = Implies(Smokes(x), Cancer(x))
Smokers_befriend_Smokers = Implies(Friends(x, y), Equivalent(Smokes(x), Smokes(y)))

formulae = [
    Smoking_causes_Cancer,
    Smokers_befriend_Smokers
]
model.add_knowledge(*formulae, world=World.AXIOM)

# add data to the model
model.add_data({
    Friends: {
        ('Anna', 'Bob'): Fact.TRUE,
        ('Bob', 'Anna'): Fact.TRUE,
        ('Anna', 'Edward'): Fact.TRUE,
        ('Edward', 'Anna'): Fact.TRUE,
        ('Anna', 'Frank'): Fact.TRUE,
        ('Frank', 'Anna'): Fact.TRUE,
        ('Bob', 'Chris'): Fact.TRUE},
    Smokes.name: {
        'Anna': Fact.TRUE,
        'Edward': Fact.TRUE,
        'Frank': Fact.TRUE,
        'Gary': Fact.TRUE},
    Cancer.name: {
        'Anna': Fact.TRUE,
        'Edward': Fact.TRUE}
    })
model.print()

I added a comma in the formulae. When I run this, I get

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In [17], line 27
     23 model.add_knowledge(*formulae, world=World.AXIOM)
     26 # add data to the model
---> 27 model.add_data({
     28     Friends: {
     29         ('Anna', 'Bob'): Fact.TRUE,
     30         ('Bob', 'Anna'): Fact.TRUE,
     31         ('Anna', 'Edward'): Fact.TRUE,
     32         ('Edward', 'Anna'): Fact.TRUE,
     33         ('Anna', 'Frank'): Fact.TRUE,
     34         ('Frank', 'Anna'): Fact.TRUE,
     35         ('Bob', 'Chris'): Fact.TRUE},
     36     Smokes.name: {
     37         'Anna': Fact.TRUE,
     38         'Edward': Fact.TRUE,
     39         'Frank': Fact.TRUE,
     40         'Gary': Fact.TRUE},
     41     Cancer.name: {
     42         'Anna': Fact.TRUE,
     43         'Edward': Fact.TRUE}
     44     })
     45 model.print()

File ~/src/pas-notebooks/env/lib64/python3.9/site-packages/lnn/model.py:346, in Model.add_data(self, data)
    344 for formula, fact in data.items():
    345     if not isinstance(formula, Formula):
--> 346         raise TypeError(
    347             "formula expected of type Formula, received "
    348             f"{formula.__class__.__name__}"
    349         )
    350     _exceptions.AssertFormulaInModel(self, formula)
    351     if formula.propositional:

TypeError: formula expected of type Formula, received str
buddha314 commented 1 year ago

I would like to note that the construction syntax seems pretty different than the Jupyter notebooks in the Examples folder.