fuzzy_dl_owl2.fuzzydl.milp.solution

Encapsulates the outcome of a query performed on a fuzzy knowledge base, distinguishing between a numerical degree of satisfaction and the consistency status of the base itself.

Description

The design allows the object to represent two distinct states: a valid query result where the knowledge base is consistent, or a state indicating inconsistency. When initialized with a floating-point number, the instance stores the satisfaction value and assumes the underlying knowledge base is consistent, providing a container for the numerical outcome. Conversely, initialization with a boolean flag explicitly marks the knowledge base as inconsistent, rendering the numerical solution irrelevant and resetting the internal state to reflect this failure. To support detailed analysis, the structure maintains a dictionary of variable bindings that can be populated and retrieved, allowing users to inspect specific values determined during the resolution process. Accessor methods provide a clean interface for retrieving the consistency status, the primary solution value, and the associated variable mappings, while standard string representation methods offer human-readable summaries of the result state.

Classes

Solution

This class encapsulates the outcome of a query performed on a fuzzy knowledge base, distinguishing between a numerical degree of satisfaction and the consistency status of the base itself. When initialized with a floating-point number, it represents a valid query result where the knowledge base is consistent, storing the satisfaction value and allowing the retrieval of specific variable bindings. Alternatively, it can be instantiated with a boolean flag to explicitly represent an inconsistent knowledge base, in which case the satisfaction value is disregarded. Users can access the consistency status, the numerical solution, and any associated variable values through dedicated accessor methods, while the string representation provides a human-readable summary of the result.

Module Contents

UML Class Diagram for Solution

UML Class Diagram for Solution

class Solution(consistent: bool)[source]
class Solution(sol: float)

This class encapsulates the outcome of a query performed on a fuzzy knowledge base, distinguishing between a numerical degree of satisfaction and the consistency status of the base itself. When initialized with a floating-point number, it represents a valid query result where the knowledge base is consistent, storing the satisfaction value and allowing the retrieval of specific variable bindings. Alternatively, it can be instantiated with a boolean flag to explicitly represent an inconsistent knowledge base, in which case the satisfaction value is disregarded. Users can access the consistency status, the numerical solution, and any associated variable values through dedicated accessor methods, while the string representation provides a human-readable summary of the result.

Parameters:
  • CONSISTENT_KB (bool) – Constant indicating a consistent fuzzy Knowledge Base.

  • INCONSISTENT_KB (bool) – Constant flag indicating an inconsistent fuzzy Knowledge Base, used to initialize a solution object.

Raises:

ValueError – Raised if the argument provided to the constructor is neither a boolean nor a numeric type.

__hash__() int[source]

Computes a hash value for the instance by converting the object to its string representation and hashing the resulting string. This enables instances of the class to be used as dictionary keys or stored in sets. The hash value is entirely dependent on the output of the __str__ method, meaning that the efficiency of this operation is tied to the complexity of generating the string representation. If the object is mutable, modifying it after it has been added to a hash-based collection will lead to inconsistent behavior, as the hash value will change while the object remains in its original bucket.

Returns:

An integer hash value derived from the string representation of the object.

Return type:

int

__repr__() str[source]

Returns the official string representation of the object, which is primarily intended for debugging and developer feedback. This implementation delegates directly to the informal string conversion logic, meaning the output is identical to the result of calling str() on the instance. Consequently, the returned string may not strictly adhere to the convention of being a valid Python expression that can be used to recreate the object.

Returns:

The string representation of the object.

Return type:

str

__solution_init_1(consistent: bool) None

Initializes the core state attributes for a solution instance, setting the numerical solution value (sol) to a default float of 0.0 and assigning the provided boolean consistency status to the consistent attribute. Additionally, it creates an empty dictionary to store variable values (showed_variables). This method effectively resets or establishes the initial state of these specific instance variables, overwriting any pre-existing data.

Parameters:

consistent (bool) – Indicates whether the fuzzy knowledge base is consistent.

__solution_init_2(sol: float) None

Initializes the internal state of the solution object by setting the numerical solution value, establishing the consistency of the fuzzy knowledge base, and preparing a container for displayed variables. It assigns the provided numerical value to the solution attribute, defaults the consistency flag to true, and creates an empty dictionary to track the values of variables that have been shown. This method serves as a secondary initialization routine to reset or configure the object’s state before further operations.

Parameters:

sol (float) – Numerical value of the solution.

__str__() str[source]

Returns a human-readable string representation of the solution object, which varies based on the consistency of the underlying knowledge base. If the solution is consistent, the method returns the string representation of the solution itself; otherwise, it returns a message indicating that the knowledge base is inconsistent.

Returns:

Returns the string representation of the solution if the knowledge base is consistent, or ‘Inconsistent KB’ otherwise.

Return type:

str

add_showed_variable(var_name: str, value: float) None[source]

Updates the internal collection of variables designated for display by associating a specific name with a floating-point value. This method modifies the instance’s showed_variables dictionary in place, adding a new entry if the name does not exist or overwriting the existing value if it does. It is typically used to record metrics or parameters that need to be tracked or visualized later in the solution process.

Parameters:
  • var_name (str) – The name of the showed variable to set.

  • value (float) – Numeric content to assign to the showed variable.

get_showed_variables() dict[str, float][source]

Returns a dictionary containing the values of variables that were highlighted or determined during the query resolution process. This method is intended to be used after a query has been successfully solved over a consistent Knowledge Base. The keys of the dictionary are variable names, and the values are their corresponding floating-point representations. Note that this method returns a direct reference to the internal dictionary, so modifying the returned object will affect the state of the Solution instance.

Returns:

A dictionary mapping variable names to their float values, representing the variables resulting from a solved query over a consistent knowledge base.

Return type:

dict[str, float]

get_solution() bool | float[source]

Retrieves the solution stored within the instance, which represents the outcome of a query over a consistent knowledge base. The returned value can be either a boolean, indicating satisfiability or truth, or a float, representing a numerical metric such as a probability or cost. This method acts as a simple accessor and does not perform any computation or modify the object’s state.

Returns:

The solution to the query, returned as either a boolean (True/False) or a float (e.g., a probability or confidence score).

Return type:

Union[bool, float]

is_consistent_kb() bool[source]

Returns a boolean flag indicating the consistency status of the original Knowledge Base (KB). This method acts as a direct accessor to the internal consistent attribute, reflecting whether the KB was found to be logically sound during prior processing. It does not perform any new computations or modify the object’s state.

Returns:

True if the original Knowledge Base is consistent, False otherwise.

Return type:

bool

CONSISTENT_KB: bool = True
INCONSISTENT_KB: bool = False