Skip to main content

Specification pattern implementation with fuzzy predicates

Project description

FuzzyPredicates

FuzzSpec is a Python library for defining and evaluating fuzzy predicate specifications. It allows you to define complex logic rules based on fuzzy set theory, returning a degree of truth (between 0 and 1) instead of binary values.

Features

  • Flexible Membership Functions:
    • Triangular (3 parameters)
    • Trapezoidal (4 parameters)
  • Configurable Logic: Choose between different T-Norms (AND), S-Norms (OR), and Complements (NOT).
  • YAML Configuration: Decouple logic from code using declarative logic.yaml files.
  • Pydantic Validation: Automatic schema validation for your logic configurations.
  • Compositional API: Easily combine atomic predicates using Python operators (&, |, ~).

Installation

uv sync

Configuration (logic.yaml)

You can configure the global fuzzy logic behavior and define your predicates in a single file.

1. Global Logical Operators

Configure how the library handles AND, OR, and NOT operations.

config:
    tnorm: "product"      # Options: min, product, lukasiewicz
    snorm: "probabilistic" # Options: max, probabilistic, lukasiewicz
    complement: "standard" # Options: standard (1 - x)

2. Atomic Predicates

Define attributes and their fuzzy ranges.

atomic:
    low_att:
        variable: "att1"
        fuzzy_number: [0.1, 1.4, 4.3] # Triangular
    active_range:
        variable: "att2"
        fuzzy_number: [0.1, 0.9, 4.3, 5.5] # Trapezoidal

3. Composed Predicates

Link atomic predicates using logical operators.

composed:
    low_and_active:
        operator: "and"
        operands: ["low_att", "active_range"]
    not_low:
        operator: "not"
        operands: ["low_att"]

Usage Example

Create your data model

from dataclasses import dataclass

@dataclass
class SensorData:
    att1: float
    att2: float
    att3: int

Load and Evaluate

from FuzzyPredicates.utils import load_rules

# Load rules from config (validates against Pydantic schema)
rules = load_rules('logic.yaml')

# Evaluate against data
data = SensorData(att1=1.0, att2=4.4, att3=43)
result = rules['low_and_active'](data)

print(f"Confidence Degree: {result:.2f}")

Advanced Usage (Command Line)

The main.py entry point supports configurable logging and custom configuration paths.

uv run main.py --log-level DEBUG --config my_logic.yaml

Available log levels: DEBUG, INFO, WARNING, ERROR, CRITICAL.


## Technical Details

### Supported Operators

| Logic Type | Name | Formula |
|------------|------|---------|
| **T-Norm (AND)** | `min` | $\min(a, b)$ |
| | `product` | $a \times b$ |
| | `lukasiewicz` | $\max(0, a + b - 1)$ |
| **S-Norm (OR)** | `max` | $\max(a, b)$ |
| | `probabilistic` | $a + b - (a \times b)$ |
| | `lukasiewicz` | $\min(1, a + b)$ |

### Project Structure
- `src/FuzzyPredicates/fuzzy/`: Core fuzzy membership functions and operators.
- `src/FuzzyPredicates/predicates.py`: High-level `Predicate` class with operator overloading.
- `src/FuzzyPredicates/models.py`: Pydantic schemas for logic validation.
- `src/FuzzyPredicates/utils.py`: Utility functions (e.g., `load_rules`).
- `main.py`: Example entry point.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fuzzy_predicates_gdrottoli-0.1.2.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fuzzy_predicates_gdrottoli-0.1.2-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file fuzzy_predicates_gdrottoli-0.1.2.tar.gz.

File metadata

File hashes

Hashes for fuzzy_predicates_gdrottoli-0.1.2.tar.gz
Algorithm Hash digest
SHA256 df715f205b669619ff7c3fcf7269bbd7cf0250884877ccf2ce9f70e38eaab4a5
MD5 ae076a9ba38f2749d71f1986d9056217
BLAKE2b-256 d2e302178f811219c8977b2eda1a0c5d7ff3bbecf8d33a0d3e531931645359e2

See more details on using hashes here.

File details

Details for the file fuzzy_predicates_gdrottoli-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fuzzy_predicates_gdrottoli-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3c72db6d1215e6d42eaad99c44fbdb4f061a3f953bce0f2adb12365d04053634
MD5 fc28321004f65280af8c286502581e31
BLAKE2b-256 f31b6c3a0a83f2a2739f951514ad30fbd7e0d85ab070de40a4894e8638581195

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page