I have the following method:
static bool ForwardChaining(List<Clause> clauses, string goal)
{
Queue<string> agenda = new Queue<string>();
Dictionary<string, bool> inferred = new Dictionary<string, bool>();
Dictionary<Clause, long> count = new Dictionary<Clause, long>();
foreach (Clause clause in clauses)
{
count.Add(clause, clause.Premises.Count);
if (!inferred.ContainsKey(clause.Conclusion))
inferred.Add(clause.Conclusion, false);
foreach (string symbol in clause.Premises )
{
if (!inferred.ContainsKey(symbol))
inferred.Add(symbol, false);
}
}
agenda.Enqueue("true");
if(!inferred.ContainsKey("true"))
inferred.Add("true", true);
while (agenda.Count > 0)
{
string p = agenda.Dequeue();
if (p == goal)
{
return true;
}
if (inferred[p] == false)
{
inferred[p] = true;
foreach (Clause clause in clauses)
{
if (clause.Premises.Contains(p))
{
if (count[clause] > 0)
{
count[clause]--;
if (count[clause] == 0)
{
agenda.Enqueue(clause.Conclusion);
}
}
}
}
}
}
return false;
}
It should be capable of determining if a given goal, follows logically from a list of definite clauses by returning true of it does. The class Clauses that I made has a Conclusion variable and a list of premises. I think it should work but somehow in some testcases it fails.
I googled how to implement forward chaining in C# and tried to apply it. But I cannot find the mistake in my work.
New contributor
Pete Formann is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.