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.3.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.3-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fuzzy_predicates_gdrottoli-0.1.3.tar.gz
Algorithm Hash digest
SHA256 8bc8391df8db1aaffcefbb2c3c927e5ab1c409ccd69e2a80e8c7de69d102a81a
MD5 9c20d2655db377541e06f41f36f3a680
BLAKE2b-256 4ff2454f0fb487d7159e6c7a5437829032d0cb35a8dcf1dd888610b14983b6b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fuzzy_predicates_gdrottoli-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cdce93008e812d9e8ca0e8edaf8d099d820875c0447c30658a847d5fc56babb1
MD5 5f8c0613a966a63efb6d33bdf34b6790
BLAKE2b-256 7cae95d0f7f17644b80ad821e54662d1b419290f04418595bf1f8203267626d4

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