Skip to content

Commit c3699dd

Browse files
authored
Merge pull request #551 from dice-group/FixIssue550
Fix issue550
2 parents 0193a73 + 9833fd4 commit c3699dd

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed

ontolearn/learners/drill.py

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -706,61 +706,79 @@ def generate_learning_problems(self,
706706
707707
Time complexity: O(n^2) n = named concepts
708708
"""
709-
counter = 0
710-
size_of_examples = 3
711-
examples = []
712-
# C: Iterate over all named OWL concepts
709+
# Initialize counters and containers
710+
counter = 0
711+
size_of_examples = 3 # Minimum number of examples required for positive/negative sets
712+
examples = []
713+
# C: Iterate over all named OWL concepts
713714
for i in self.kb.get_concepts():
714-
# Retrieve(C)
715+
# Retrieve all individuals that belong to concept i (positive examples)
715716
individuals_i = set(self.kb.individuals(i, True))
717+
718+
# Skip concepts with insufficient individuals for sampling
716719
if len(individuals_i) < size_of_examples:
717720
continue
721+
718722
for j in self.kb.get_concepts():
723+
# Skip if same concept (can't use same concept for both positive and negative examples)
719724
if i == j:
720725
continue
721726
str_dl_concept_i = owl_expression_to_dl(i)
727+
728+
# Retrieve all individuals that belong to concept j (negative examples)
722729
individuals_j = set(self.kb.individuals(j, True))
730+
731+
# Skip concepts with insufficient individuals for sampling
723732
if len(individuals_j) < size_of_examples:
724733
continue
725734

726735
# Generate Learning problems from a single target
727736
for _ in range(num_of_target_concepts):
737+
# Randomly sample positive examples from concept i
728738
sampled_positives = set(random.sample(individuals_i, size_of_examples))
739+
740+
# Randomly sample negative examples from concept j
729741
sampled_negatives = set(random.sample(individuals_j, size_of_examples))
730-
if sampled_negatives== sampled_positives:
742+
743+
# Validate that positive and negative examples are different
744+
if sampled_negatives == sampled_positives:
731745
print("Sampled Positives and negatives are same. We need to ignore this example")
732746
continue
733-
lp = (str_dl_concept_i,sampled_positives,sampled_negatives)
747+
748+
lp = (str_dl_concept_i, sampled_positives, sampled_negatives)
734749
examples.append(lp)
735750
counter += 1
751+
752+
# Check if we've generated enough learning problems
736753
if counter == num_learning_problems:
737754
break
738755

756+
# Early termination if we've reached the desired number of learning problems
739757
if counter == num_learning_problems:
740758
break
741-
742-
return examples
743-
"""
744-
# if |Retrieve(C|>3
745-
if len(individuals_i) > size_of_examples:
746-
str_dl_concept_i = owl_expression_to_dl(i)
747-
for j in self.kb.get_concepts():
748-
if i == j:
749-
continue
750-
individuals_j = set(self.kb.individuals(j))
751-
if len(individuals_j) > size_of_examples:
752-
for _ in range(num_learning_problems):
753-
lp = (str_dl_concept_i,
754-
set(random.sample(individuals_i, size_of_examples)),
755-
set(random.sample(individuals_j, size_of_examples)))
756-
yield lp
757-
758-
counter += 1
759-
if counter == num_of_target_concepts:
760-
break
759+
# Return the generated learning problems
760+
return examples
761+
"""
762+
# if |Retrieve(C|>3
763+
if len(individuals_i) > size_of_examples:
764+
str_dl_concept_i = owl_expression_to_dl(i)
765+
for j in self.kb.get_concepts():
766+
if i == j:
767+
continue
768+
individuals_j = set(self.kb.individuals(j))
769+
if len(individuals_j) > size_of_examples:
770+
for _ in range(num_learning_problems):
771+
lp = (str_dl_concept_i,
772+
set(random.sample(individuals_i, size_of_examples)),
773+
set(random.sample(individuals_j, size_of_examples)))
774+
yield lp
775+
776+
counter += 1
761777
if counter == num_of_target_concepts:
762778
break
763-
"""
779+
if counter == num_of_target_concepts:
780+
break
781+
"""
764782

765783
def learn_from_illustration(self, sequence_of_goal_path: List[RL_State]):
766784
"""

0 commit comments

Comments
 (0)