Fuzzy Logic
The fuzzy_logic module provides a set of classes for performing fuzzy logic operations. Fuzzy logic is an approach to computing based on "degrees of truth" ([0, 1]) rather than the usual "true or false" (1 or 0) Boolean logic. This module includes classes for fuzzy AND, OR, and NOT operations, as well as linguistic modifiers. It also allows you to set custom truth functions for these operations.
Please refer to the API doc for some usage examples.
Class Hierarchy
The class hierarchy in this module is designed to encapsulate various fuzzy logic operators and modifiers in a structured, extensible, and reusable way. The primary design choices reflect the need for clear abstraction, the separation of concerns, and the flexibility to define and apply different fuzzy logic operations within a common framework.
Here’s an overview of the class hierarchy and the rationale behind its design:

-
FuzzyOperator(Enum)Purpose:
FuzzyOperatoris an abstract base class designed as an enumeration (Enum). It serves as the foundational class for all fuzzy operators (unary and binary) by providing a common interface (call() method) that derived classes must implement.Rationale: By using an
Enumas a base class, the design allows for defining multiple concrete implementations of fuzzy operators in a single class, with each implementation corresponding to a different value of theEnum. This approach provides flexibility and extensibility while keeping the implementation organized. -
FuzzyUnaryOperator(FuzzyOperator)Purpose:
FuzzyUnaryOperatoris an abstract class that extendsFuzzyOperator. It specifically handles unary fuzzy operations, which take a single membership value and return another membership value.Rationale: The separation into unary and binary operators allows for a clear distinction between operations that act on a single input (
FuzzyUnaryOperator) versus those that act on two inputs (FuzzyBinaryOperator). This separation enforces correct usage and enhances code clarity. -
FuzzyBinaryOperator(FuzzyOperator)Purpose:
FuzzyBinaryOperatoris an abstract class that extendsFuzzyOperator. It defines a common interface for all binary fuzzy operations, which take two membership values and return another membership value.Rationale: Just like
FuzzyUnaryOperator, this class enforces a clear structure for binary operations, ensuring that all binary operators adhere to a common interface, thereby promoting consistency and reducing the potential for errors in implementation. -
FuzzyAnd(FuzzyBinaryOperator)Purpose:
FuzzyAndis a concrete implementation ofFuzzyBinaryOperator, defining various t-norms (truth functions) that represent fuzzy AND operations.Rationale: By encapsulating all AND-related operations within a single class, the design simplifies the management and usage of different t-norms. Users can easily switch between different fuzzy AND operations by changing the enum value used.
-
FuzzyOr(FuzzyBinaryOperator)Purpose:
FuzzyOris a concrete implementation ofFuzzyBinaryOperator, providing different t-conorms (truth functions) for fuzzy OR operations.Rationale: Similar to
FuzzyAnd, this class groups all OR-related operations, making it easy to manage and apply different fuzzy OR operations. -
FuzzyNot(FuzzyUnaryOperator)Purpose:
FuzzyNotis a concrete implementation ofFuzzyUnaryOperator, defining various truth functions for fuzzy NOT operations.Rationale: This class centralizes the definition of fuzzy NOT operations, allowing for easy extension and modification of negation functions. The user can select the appropriate negation method based on the specific requirements of their fuzzy logic system.
-
Purpose:
FuzzyLogicis a static class that manages the current truth functions for fuzzy AND, OR, and NOT operations. It provides methods to set and retrieve the current fuzzy logic operations, ensuring consistency across the system.Rationale: The
FuzzyLogicclass abstracts the management of fuzzy operations, allowing the underlying operations to be swapped out or modified without affecting the rest of the system. This promotes flexibility and enables users to customize their fuzzy logic operations easily. -
LinguisticModifiers(FuzzyUnaryOperator)Purpose:
LinguisticModifiersis a concrete implementation ofFuzzyUnaryOperator, providing methods to apply linguistic modifiers (e.g., 'VERY', 'MORE_OR_LESS') to fuzzy sets. These modifiers adjust the degree of membership to reflect linguistic semantics.Rationale: By encapsulating linguistic modifiers within their own class, the design ensures that these operations are clearly separated from the basic fuzzy logic operations. This enhances readability and maintains a clean separation of concerns, making the system more modular and easier to maintain.
Custom Truth Functions
Custom truth functions such as t-norms and t-conorms can be defined by extending the corresponding class (such as FuzzyAnd, FuzzyOr and FuzzyNot) and by adding new Enum values corresponding to new custom implementations to add. The following is an example for each operation:
class CustomFuzzyAnd(FuzzyAnd):
@member
def NEW_IMPLEMENTATION(a: float, b:float) -> float:
# calculate and return
pass
def __call__(self, a: float, b: float) -> float:
return super().__call__(a, b)
class CustomFuzzyOr(FuzzyOr):
@member
def NEW_IMPLEMENTATION(a: float, b:float) -> float:
# calculate and return
pass
def __call__(self, a: float, b: float) -> float:
return super().__call__(a, b)
class CustomFuzzyNot(FuzzyNot):
@member
def NEW_IMPLEMENTATION(a: float) -> float:
# calculate and return
pass
def __call__(self, a: float) -> float:
return super().__call__(a)
By doing so it is possible to set them as the current AND, OR and NOT (by calling the methods set_and_fun, set_or_fun and set_not_fun) truth functions in the static class FuzzyLogic in order to change the behaviour of the DiscreteFuzzySet methods by using custom truth functions depending on the context of use.