from .celoe import CELOE
from ..knowledge_base import KnowledgeBase
from typing import Optional
import owlapy
from ..abstracts import AbstractScorer, BaseRefinement, AbstractHeuristic
from ..search import OENode, LBLNode
from owlapy.class_expression import OWLClassExpression
from ..heuristics import OCELHeuristic
[docs]
class OCEL(CELOE):
"""A limited version of CELOE.
Attributes:
best_descriptions (EvaluatedDescriptionSet[OENode, QualityOrderedNode]): Best hypotheses ordered.
best_only (bool): If False pick only nodes with quality < 1.0, else pick without quality restrictions.
calculate_min_max (bool): Calculate minimum and maximum horizontal expansion? Statistical purpose only.
heuristic_func (AbstractHeuristic): Function to guide the search heuristic.
heuristic_queue (SortedSet[OENode]): A sorted set that compares the nodes based on Heuristic.
iter_bound (int): Limit to stop the algorithm after n refinement steps are done.
kb (KnowledgeBase): The knowledge base that the concept learner is using.
max_child_length (int): Limit the length of concepts generated by the refinement operator.
max_he (int): Maximal value of horizontal expansion.
max_num_of_concepts_tested (int) Limit to stop the algorithm after n concepts tested.
max_runtime (int): Limit to stop the algorithm after n seconds.
min_he (int): Minimal value of horizontal expansion.
name (str): Name of the model = 'ocel_python'.
_number_of_tested_concepts (int): Yes, you got it. This stores the number of tested concepts.
operator (BaseRefinement): Operator used to generate refinements.
quality_func (AbstractScorer) The quality function to be used.
reasoner (AbstractOWLReasoner): The reasoner that this model is using.
search_tree (Dict[OWLClassExpression, TreeNode[OENode]]): Dict to store the TreeNode for a class expression.
start_class (OWLClassExpression): The starting class expression for the refinement operation.
start_time (float): The time when :meth:`fit` starts the execution. Used to calculate the total time :meth:`fit`
takes to execute.
terminate_on_goal (bool): Whether to stop the algorithm if a perfect solution is found.
"""
__slots__ = ()
name = 'ocel_python'
def __init__(self,
knowledge_base: KnowledgeBase,
reasoner: Optional[owlapy.abstracts.AbstractOWLReasoner] = None,
refinement_operator: Optional[BaseRefinement[OENode]] = None,
quality_func: Optional[AbstractScorer] = None,
heuristic_func: Optional[AbstractHeuristic] = None,
terminate_on_goal: Optional[bool] = None,
iter_bound: Optional[int] = None,
max_num_of_concepts_tested: Optional[int] = None,
max_runtime: Optional[int] = None,
max_results: int = 10,
best_only: bool = False,
calculate_min_max: bool = True):
""" Create a new instance of OCEL.
Args:
best_only (bool): If False pick only nodes with quality < 1.0, else pick without quality restrictions.
Defaults to False.
calculate_min_max (bool): Calculate minimum and maximum horizontal expansion? Statistical purpose only.
Defaults to True.
refinement_operator (BaseRefinement[OENode]): Operator used to generate refinements.
Defaults to `ModifiedCELOERefinement`.
heuristic_func (AbstractHeuristic): Function to guide the search heuristic. Defaults to `OCELHeuristic`.
iter_bound (int): Limit to stop the algorithm after n refinement steps are done. Defaults to 10'000.
knowledge_base (KnowledgeBase): The knowledge base that the concept learner is using.
max_num_of_concepts_tested (int) Limit to stop the algorithm after n concepts tested. Defaults to 10'000.
max_runtime (int): Limit to stop the algorithm after n seconds. Defaults to 5.
max_results (int): Maximum hypothesis to store. Defaults to 10.
quality_func (AbstractScorer) The quality function to be used. Defaults to `F1`.
reasoner (AbstractOWLReasoner): Optionally use a different reasoner. If reasoner=None, the reasoner of
the :attr:`knowledge_base` is used.
terminate_on_goal (bool): Whether to stop the algorithm if a perfect solution is found. Defaults to True.
"""
if heuristic_func is None:
heuristic_func = OCELHeuristic()
super().__init__(knowledge_base=knowledge_base,
reasoner=reasoner,
refinement_operator=refinement_operator,
quality_func=quality_func,
heuristic_func=heuristic_func,
terminate_on_goal=terminate_on_goal,
iter_bound=iter_bound,
max_num_of_concepts_tested=max_num_of_concepts_tested,
max_runtime=max_runtime,
max_results=max_results,
best_only=best_only,
calculate_min_max=calculate_min_max)
[docs]
def make_node(self, c: OWLClassExpression, parent_node: Optional[OENode] = None, is_root: bool = False) -> OENode:
"""
Create a node for OCEL.
Args:
c: The class expression of this node.
parent_node: Parent node.
is_root: Is this the root node?
Returns:
OENode: The node.
"""
assert parent_node is None or isinstance(parent_node, LBLNode)
r = LBLNode(c, self.kb.concept_len(c), self.kb.individuals_set(c), parent_node=parent_node, is_root=is_root)
if parent_node is not None:
parent_node.add_child(r)
return r