Skip to main content

No project description provided

Project description

Primitive Logic Directed Acyclic Graph

"Primitive Logic Directed Acyclic Graph" data structure, or "PL-DAG" for short, is fundamentally a Directed Acyclic Graph (DAG) where each node represents a logical relationship, and the leaf nodes correspond to literals. Each node in the graph encapsulates information about how its incoming nodes or leafs are logically related. For instance, a node might represent an AND operation, meaning that if it evaluates to true, all its incoming nodes or leafs must also evaluate to true.

How it works

Each composite (node) is a linear inequality equation on the form A = a + b + c >= 0. A primitive (leaf) is just a name or alias connected to a literal. A literal here is a complex number of two values -1+3j indicating what's the lowest value some variable could take (-1) and the highest value (+3). So a boolean primitive would have the literal value 1j, since it can take on the value 0 or 1. Another primitive having 52j (weeks for instance) could potentially take on every discrete value in between but is expressed only with the lowest and highest value.

Example

from pldag import PLDAG

# Init model
model = PLDAG()

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

# Create a simple AND connected to "A"
# This is equivalent to A = x + y + z -3 >= 0
# The ID for this proposition is returned. We can also connect an alias to it, like so.
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

# So if we check when all x, y and z are set to 1, then 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

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

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

There's also a quick way to use a solver. There's no built-in solver but is dependent on existing ones. Before using, reinstall the package with the solver variable set to the solver you'd want to use

pip install pldag

And then you can use it like following

from pldag import Solver

# Maximize [x=1, y=0, z=0] such that rules in model holds and variable `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 set (i.e. all(x,y,z) must be true), we could expect all variables
# 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.6.tar.gz (20.5 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.6-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pldag-0.10.6.tar.gz
Algorithm Hash digest
SHA256 a98356327b921c451c499dcecbf7d1199ca842ac3c3219253b65ed329083d8f9
MD5 fa6f431e6a52f2c350e79385f45f955d
BLAKE2b-256 7530d37da4bc8147ee41d6bcecb078973f342c987477e234570a3f7fbd87c225

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pldag-0.10.6-py3-none-any.whl
Algorithm Hash digest
SHA256 a5afae52b43e6e3d5a1fd27d07901307d6e715f82ae0d6248c848d82934e17ed
MD5 4c2ceb0d78d85467636600dcea3981dd
BLAKE2b-256 a6e6c052a3f52c2db2deac661ea4d2edbbb91f073e4f1fa1a0e61cce503c9e46

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