Skip to main content

Un solveur d'expressions booléenes

Project description

bsolver

bsolver est une bibliothèque Python pour la résolution de problèmes logiques et de contraintes, avec un accent particulier sur la recherche de solutions minimales.

Installation

Installez bsolver et ses dépendances avec pip :

pip install .

Utilisation

Exemple d'importation et d'utilisation :

from bsolver import Var

A, B, C, D = Var("A"), Var("B"), Var("C"), Var("D")
expr = ((A & B)|(~A & C)) & D

# Affichage de la formule (forme FND réduite)
print(expr) # (!A & C & D)|(B & C & D)|(A & B & D)

# Affiche l'arbre ROBDD
expr.print()
# A
# ├── T: B
# │   ├── T: D
# │   └── F: FALSE
# └── F: C
#     ├── T: D
#     └── F: FALSE

# Affiche la table de verité de l'expression
expr.print_table()
# A | B | C | D | Résultat
# --------------------------
# 0 | 0 | 0 | 0 |   0
# 0 | 0 | 0 | 1 |   0
# 0 | 0 | 1 | 0 |   0
# 0 | 0 | 1 | 1 |   1
# 0 | 1 | 0 | 0 |   0
# 0 | 1 | 0 | 1 |   0
# 0 | 1 | 1 | 0 |   0
# 0 | 1 | 1 | 1 |   1
# 1 | 0 | 0 | 0 |   0
# 1 | 0 | 0 | 1 |   0
# 1 | 0 | 1 | 0 |   0
# 1 | 0 | 1 | 1 |   0
# 1 | 1 | 0 | 0 |   0
# 1 | 1 | 0 | 1 |   1
# 1 | 1 | 1 | 0 |   0
# 1 | 1 | 1 | 1 |   1

# Affiche les solutions de l'expression
expr.print_table(solutions_only=True)
# A | B | C | D | Résultat
# --------------------------
# 0 | * | 1 | 1 |   1
# * | 1 | 1 | 1 |   1
# 1 | 1 | * | 1 |   1

# les solutions peuvent être obtenues et parcourues via la propriété "solutions"
for s in expr.solutions:
    env = {var: val for var, val in s}
    # Il est possible d'évaluer une expression pour des valeurs de variable données
    assert expr.eval(env), f"{env} n'est pas une solution de {expr} ??"
    print(f"{env} est bien une solution de {expr}")
# {'D': True, 'B': True, 'A': True} est bien une solution de (A & B & D)|(B & C & D)|(!A & C & D)
# {'D': True, 'C': True, 'B': True} est bien une solution de (A & B & D)|(B & C & D)|(!A & C & D)
# {'D': True, 'C': True, 'A': False} est bien une solution de (A & B & D)|(B & C & D)|(!A & C & D)

# Fixer des variables à des valeurs données
print(expr.fix({'A': True})) # (B & D)
print(expr.fix({'A': True, 'B': True})) # (D)
print(expr.fix({'D': False})) # FALSE

# Verifier l'implication de deux expressions
print(expr.implies(D)) # True
print(expr.implies(A & B)) # False
print(expr.implies(~A | B)) # True

Tests

Lancez les tests avec :

pytest

Licence

Ce projet est sous licence MIT.

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

bsolver-0.1.1.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

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

bsolver-0.1.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file bsolver-0.1.1.tar.gz.

File metadata

  • Download URL: bsolver-0.1.1.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for bsolver-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f162412d6d9b26ed83097b006d48bc266bc61fd78373e12f3b3783ca00ff1f73
MD5 392022b57c00f9a83d797a859ce61469
BLAKE2b-256 b49d42852d88a49cef36a9ce792686b24c1c778bcc1c4949234d122adb72c847

See more details on using hashes here.

File details

Details for the file bsolver-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: bsolver-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for bsolver-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9eac88bfbcc301d520eb33c7b1f0dba598c5324b1ff120c08d8fd03f81e5cdc6
MD5 e42578aafe43e0f7a44ea7df9db4c9c4
BLAKE2b-256 1e6157498ad778fcfefdb9530505f3d6d93bc1f0725622c64b5c7d308f6dcbaf

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