Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion csp.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ def topological_sort(X, root):
visited shows the state (visited - not visited) of nodes
"""
nodes = X.variables
neighbors = X.neighbors

visited = defaultdict(lambda: False)
Expand Down
20 changes: 18 additions & 2 deletions logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,24 @@ def subst(s, x):
return Expr(x.op, *[subst(s, arg) for arg in x.args])


def fol_fc_ask(KB, alpha): # TODO
raise NotImplementedError
def fol_fc_ask(KB, alpha):
"""A simple forward-chaining algorithm. [Figure 9.3]"""
new = []
while new is not None:
for rule in KB.clauses:
p, q = parse_definite_clause(standardize_variables(rule))
for p_ in KB.clauses:
if p != p_:
for theta in KB.clauses:
if subst(theta, p) == subst(theta, p_):
q_ = subst(theta, q)
if not unify(q_, KB.sentence in KB) or not unify(q_, new):
new.append(q_)
phi = unify(q_, alpha)
if phi is not None:
return phi
KB.tell(new)
return None


def standardize_variables(sentence, dic=None):
Expand Down