Skip to main content

No project description provided

Project description

Primitive Logic Directed Acyclic Graph

The Primitive Logic Directed Acyclic Graph (PL-DAG) is a data structure based on a Directed Acyclic Graph (DAG) where each node represents a logical relationship and leaf nodes correspond to literals. Each node encapsulates information about how its incoming nodes or leaves are logically related. For example, a node might represent an AND operation, meaning that if it evaluates to true, all of its incoming nodes or leaves must also evaluate to true.

How It Works

Each composite (node) is represented as a linear inequality of the form A = a + b + c >= 0. A primitive (leaf) is a named alias connected to a literal. A literal is represented as a complex number with two values: -1+3j indicates the lowest value a variable can take (-1) and the highest value (+3). For example, a boolean primitive has the literal value 1j since it can only be 0 or 1. A primitive with value 52j (representing weeks, for instance) could take any discrete value between 0 and 52, though it is expressed only by its bounds.

Example

from pldag import PLDAG

# Initialize model
model = PLDAG()

# Set x, y, and z as boolean variables in the model
model.set_primitives("xyz")

# Create a simple AND operation connected to alias "A"
# This is equivalent to A = x + y + z - 3 >= 0
# The ID for this proposition is returned. We can also assign an alias to it.
id_ref = model.set_and(["x","y","z"], alias="A")

# Later, if we forget the ID, we can retrieve it like this
id_ref_again = model.id_from_alias("A")
assert id_ref == id_ref_again

# When all x, y, and z are set to 1, we expect `id_ref` to be 1+1j
assert model.propagate({"x": 1+1j, "y": 1+1j, "z": 1+1j}).get(id_ref) == 1+1j

# When not all are set, we get 1j (meaning the model doesn't know whether it's true or false)
assert model.propagate({"x": 1+1j, "y": 1+1j, "z": 1j}).get(id_ref) == 1j

# However, if we know that any variable is not set (equal to 0), the model knows the composite is false (0j)
assert model.propagate({"x": 1+1j, "y": 1+1j, "z": 0j}).get(id_ref) == 0j

Using a Solver

There is no built-in solver, but PL-DAG supports integration with existing solvers. Install the package:

pip install pldag

Then use it as follows:

from pldag import Solver

# Maximize [x=1, y=0, z=0] such that the model's rules hold and `id_ref` must be true
solution = next(iter(model.solve(objectives=[{"x": 1}], assume={id_ref: 1+1j}, solver=Solver.DEFAULT)))

# Since x=1 and `id_ref` must be true (i.e., all(x,y,z) must be true),
# we expect all variables to be set
assert solution.get(id_ref) == 1+1j

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

pldag-0.10.17.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

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

pldag-0.10.17-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file pldag-0.10.17.tar.gz.

File metadata

  • Download URL: pldag-0.10.17.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.15 Darwin/24.6.0

File hashes

Hashes for pldag-0.10.17.tar.gz
Algorithm Hash digest
SHA256 c1d29584f093c0a3fe481a8e04711f317a2285d39fbb5f8846dd9891cba3ddc2
MD5 6c38b9eb4829a9c093e73c2115cc046a
BLAKE2b-256 476e074899b0ad1ec6d61a56b5d7c211649c1d4902dfc6aadfd3fcfe0e0d3133

See more details on using hashes here.

File details

Details for the file pldag-0.10.17-py3-none-any.whl.

File metadata

  • Download URL: pldag-0.10.17-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.15 Darwin/24.6.0

File hashes

Hashes for pldag-0.10.17-py3-none-any.whl
Algorithm Hash digest
SHA256 2804c55f132cae59d35b1c99bb842f425d5ec8f2c27bba641191da9b7ab39b0d
MD5 30270cd0ec2747eaa011fb6532cd8fd5
BLAKE2b-256 255528c7c2f40879af445d4709aa50d97e08e89e8e3d84d3e2d596031c61b350

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