Skip to main content

Python library for modeling DFAs, Moore Machines, and Transition Systems in JAX.

Project description

DFAx

A JAX-compatible Python implementation of a Deterministic Finite Automaton (DFA).

Installation

This package will soon be made pip-installable. In the meantime, pull the repo and and install locally.

git clone https://github.com/rad-dfa/dfax.git
pip install -e dfax

Usage

Create DFAs by specifying a start state, transitions matrix, which is max number of states by number of alphabet symbols, and the associated labels for each state.

from dfax import DFAx

dfax = DFAx(
	start=0, # State referred to as 0 is the initial state
	transitions=jnp.array([
		[1, 2, 0, 0, 0],
		[1, 1, 1, 1, 1],
		[2, 2, 2, 2, 2],
	]), # Max number of states is 3 and number of tokens is 5
	labels=jnp.array([False, True, False]) # State labels
) # Returns a DFA

Take transitions on the DFA using a given symbol.

dfax = dfax.advance(0) # Returns the resulting DFA after reading the symbol referred to as 0

Minimize DFAs.

dfax = dfax.minimize() # Returns a canonical minimal DFA

Canonicalize DFAs by relabeling states based on a BFS search.

dfax = dfax.canonicalize() # Returns a canonical DFA

Mutate DFAs by randomly toggling entries in the transition matrix.

import jax

key = jax.random.PRNGKey(0)
dfax = dfax.mutate(key) # Returns a mutated DFA

Perform syntactic equality check between DFAs.

dfax1 == dfax2

Perform semantic equality check between DFAs.

dfax1.minimize() == dfax2.minimize()

Use DFAs as reward functions. With ternary semantics, reward is (i) +1 if the start state has label True, (ii) -1 if the start state has label False and is a sink state, and (iii) 0 otherwise. With binary semantics, 0 is returned instead of -1.

dfax.reward() # Returns a ternary reward
dfax.reward(binary=True) # Returns a binary reward

Sample from different DFA distributions: Reach samples DFAs ordering alphabet symbols, ReachAvoid samples Reach DFAs but also includes Avoid constraints, and ReachAvoidDerived samples randomly mutated Reach and ReachAvoid DFAs.

import jax
from dfax.samplers import ReachSampler, ReachAvoidSampler, RADSampler

key = jax.random.PRNGKey(0)
sampler = ReachAvoidSampler()

dfax = sampler.sample(key)

Define your own DFA samplers by overloading DFASampler .

@struct.dataclass
class MySampler(DFASampler):
    @partial(jax.jit, static_argnums=(0,))
    def sample(self, key: chex.PRNGKey) -> DFAx:
        # Write sampling code and return sampled DFA

Visualize DFAs.

from dfax.utils import visualize
visualize(dfax)

This project is a JAX extension of dfa. Therefore, we include helper methods for translating DFAx objects to and from DFA objects.

from dfax import dfa2dfax, dfax2dfa

dfa = dfax2dfa(dfax) # Create DFA from DFAx
dfax = dfa2dfax(dfa) # Create DFAx from DFA

In progress

Currently, we are working on implementing Boolean operations on DFAx objects, e.g., conjunction, disjunction, etc. If there are other functionalities you would like to have in this package, create pull request or contact us to work together!

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

dfax-0.1.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

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

dfax-0.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file dfax-0.1.0.tar.gz.

File metadata

  • Download URL: dfax-0.1.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.18

File hashes

Hashes for dfax-0.1.0.tar.gz
Algorithm Hash digest
SHA256 84a5c9bfd5ba1eb06895f8334aee3a7b6b0e18c6e94cd0ce0424167918f8d64c
MD5 536e69e427ba09a57b07e9948ccc6ad5
BLAKE2b-256 53e5fe654b6f67c16a090c7033cef4a252e50d85a173511ff2bda3c8f2dbcc3f

See more details on using hashes here.

File details

Details for the file dfax-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dfax-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.18

File hashes

Hashes for dfax-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d75e5e66e4b49cd16d323ab1588790e1828fb10967f9c68e6b23bd38f0ebf54
MD5 578a82e720fa85fa506ecdce10e975c4
BLAKE2b-256 5bb70718bea95b0cd03cf18bf6e56c282bdf1ca6e1fdadf2d039d601d323c5c3

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