1

I have problems to write my first simple pseudocode.

The input of the algorithm is an ontology which contains axioms and an inference set, exactly an inference is defined as an object with set of premises and an conclusion.

The output of the algorithm should be the set of conclusion that we can derive from the given ontology using the inference set.

A conclusion is derivable if there exits an inference which has as conclusion such conclusion and in turn its premises are derivable.

I wrote the pseudocode, but i think it is not the best solution due to the goto instruction. I think it can be remove and i can add some data structure that takes care of the phase of propagation. I hope that i was clear:)

enter image description here

Christophe
  • 81,699

1 Answers1

3

Improving the pseudocode structure

In line 7 it appears that you restart every time you find a new element to add to D. However, in pseudocode and mathematical language, you have no guarantee about the order in which foreach takes the elements of I.

Therefore, you could as well continue the loop to the end of the foreach and add an outer loop:

D ← { O }
repeat 
  retry ← false
  foreach  ∈ I do
     P ← getPremises()
     if P ∈ D then
        C ← getConclusion()
        if C ∉ D then 
           D ← D ∪ { C }
           retry ← true
  until not retry
return D

If you do not like repeat/until, you could also go for a while but you'd need to initalize retry in consequence.

If the order of evaluation matters, you'd replace the set I with an ordered list or a priority queue, and use the same approach than before but break the inner loop after setting retry to true.

Correcting the notation

Instead of !, you'd better use the more readable not, or the more comprehensive .

This being said, if O is a set, and if D is a set that should at start contain all the elements of O, O should not be shown as a set element, and line 1 should be:

D ← O

If P is in fact a set and not elements, line 5 should use set operators instead of element operators:

if P ⊂ D then
   ...

Improving algorithm

The restart in your original algorithm after the firing of a rule leads to think that the intent is to re-evaluate all the rules due to changed of situation.

However from the comments, it appears that a rule may have an effect on D only once. So there's no need to fire again a rule that already fired. This may lead to an improved algorithm:

D ← O          
K ← I       // K is the set of rules that could still fire
n ← true    // n is true when new facts were added to D
while n do 
  n ← false
  foreach  ∈ K do
     P ← getPremises()
     if P ⊂ D then
        K  ← K - {  }          // or K ← {x∈K|x≠ }
        C ← getConclusion()
        if C ∉ D then 
           D ← D ∪ { C }
           n ← true
return D

Now K contains the rules which did not fire yet.

Depending on the number of rules and the complexity of firing them, you could even go further and remove from K any other rule that could give the same C as conclusion, whether they are ready to be fired or not, since they would never change the final D.

Christophe
  • 81,699