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

Rule not functioning #91

Open KaminiAggarwal opened 4 months ago

KaminiAggarwal commented 4 months ago

Hi Team,

I am working on a rule that doesn't seem to give expected results. The data and rule are as below:

Friends.add_data({ ("P", "A"): Fact.TRUE, ("P", "B"): Fact.TRUE, ("Q", "C"): Fact.TRUE, ("Q", "D"): Fact.TRUE }) Popular.add_data({ "A": Fact.TRUE, "B": Fact.FALSE, "C": Fact.FALSE, "D": Fact.FALSE })

Rule: P is a Star only if ALL friends of P are popular.

rule = Forall(x, Implies(Forall(y, And(Friends(x, y), Popular(y))), Star(x)))

Here is the code:

from lnn import Predicate, Variable, Forall, Fact, Model, And, Implies, World

Friends = Predicate('Friends', arity=2) Popular = Predicate('Popular') Star = Predicate('Star')

x, y = Variable('x'), Variable('y')

Friends.add_data({ ("P", "A"): Fact.TRUE, ("P", "B"): Fact.TRUE, ("Q", "C"): Fact.TRUE, ("Q", "D"): Fact.TRUE })

Popular.add_data({ "A": Fact.TRUE, "B": Fact.FALSE, "C": Fact.FALSE, "D": Fact.FALSE })

rule = Forall(x, Implies(Forall(y, And(Friends(x, y), Popular(y))), Star(x)))

model = Model() model.add_knowledge(Friends, Popular, Star, rule)

model.infer()

model.print()

The result here gives me Star("P") True and Star("Q") = True for all values of popular of a person.

Expected result from above: Star("P") False and Star("Q") = False ( Since only A is popular)

If both A and B are popular: Star("P") True and Star("Q") = False

If all are popular: Star("P") True and Star("Q") = True