Skip to main content

Differentiable Linear Programming via Physarum dynamics

Project description

physarum labs

Physarum Labs — variational programming from biological dynamics

License: MIT Python 3.8+ Tests arXiv

A clean MIT-licensed PyTorch implementation of Physarum-inspired differentiable solvers, unifying four lines of recent research:

  1. Meng, Ravi, Singh (AAAI 2021) — Differentiable LP layer via Physarum dynamics
  2. Solé & Pla-Mauri (arXiv Nov 2025) — Lagrangian variational framework
  3. Schick et al. (PRX Life 2026) — Peristaltic mechanism (substrate inspiration)
  4. Pietak & Levin (iScience 2025) — Regulatory Network Machine paradigm

What's here

  • physarum_labs/lp.py — Clean MIT-licensed PyTorch reimplementation of the Physarum-inspired differentiable LP solver. Drop-in replacement for Sinkhorn-based optimal transport layers. No Magic Leap license — completely independent of the SuperGlue codebase.

  • physarum_labs/variational.py — End-to-end VariationalNetworkMachine combining the LP solver with Solé's Lagrangian framework. Shows how to add transport dissipation, sparsity, and entropy regularization on top of the base solver, and how to learn edge affinities end-to-end.

  • examples/cvxpy_comparison.py — Validates the Physarum solver against scipy.optimize.linprog on a range of problem sizes and iteration counts, and produces a convergence plot at assets/convergence.png.

Why this exists

The only public working implementation of Meng 2021 lives inside the yingxin-jia/Superglue-with-Physarum-Dynamics repository, which inherits Magic Leap's non-commercial academic use only license. Anyone wanting to commercialize Physarum Labs must reimplement — which is what this codebase does, cleanly.

Quick start

Install via PyPI

pip install physarum-labs

Install from source

git clone https://github.com/Physarum-Lab/physarum-labs
cd physarum-labs
pip install -e ".[dev]"  # includes scipy and matplotlib for benchmarks

Run the demos

python -m physarum_labs.lp                # LP solver self-test
python -m physarum_labs.variational       # maze, matching, learning loop
python examples/cvxpy_comparison.py       # validates against scipy LP solver

Run the tests

pytest tests/ -v
import torch
from physarum_labs import PhysarumLPLayer

# Cost matrix (lower = better match)
scores = torch.rand(4, 5)
solver = PhysarumLPLayer(unmatch_score=-1.0, max_iter=20)
transport_plan, loss = solver(scores)

# transport_plan shape: (5, 6) — augmented with bin for unmatched
# transport_plan[:-1, :-1] gives the actual matching scores

The algorithm

Physarum dynamics solve a doubly-stochastic optimal transport problem by iteratively updating a flux vector:

x_{k+1} = (1 - h) * x_k + h * W * A^T * p

where W = diag(x/c), c is the cost vector, A encodes row/column sum constraints, and p solves (A W A^T) p = 1.

This is mathematically equivalent to a steepest-descent on the LP problem (see Meng 2021), and to a least-action variational principle (see Solé & Pla-Mauri 2025). It's also the same family of dynamics that Pietak & Levin use to describe gene regulatory networks as analog computers.

License

MIT. Use freely, including commercially.

References

  • Meng, Z., Ravi, S. N., & Singh, V. (2021). Physarum Powered Differentiable Linear Programming Layers and Applications. AAAI. arXiv:2004.14539

  • Solé, R., & Pla-Mauri, J. (2025). Cognition as least action: the Physarum Lagrangian. arXiv:2511.08531

  • Schick, L., et al. (2026). Decision-Making in Light-Trapped Slime Molds Involves Active Mechanical Processes. PRX Life. DOI: 10.1103/rv7g-d9kx

  • Pietak, A., & Levin, M. (2025). Harnessing the analog computing power of regulatory networks with the Regulatory Network Machine. iScience. DOI: 10.1016/j.isci.2025.112536

  • Bajpai, S., Lucas-DeMott, A., Murugan, N. J., Levin, M., & Kurian, P. (2025). Morphological computational capacity of Physarum polycephalum. arXiv:2510.19976

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

physarum_labs-0.2.1.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

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

physarum_labs-0.2.1-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file physarum_labs-0.2.1.tar.gz.

File metadata

  • Download URL: physarum_labs-0.2.1.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for physarum_labs-0.2.1.tar.gz
Algorithm Hash digest
SHA256 be32acdf4b7ff178183977b917b6553094cdc41da35529a199b4d214bea49e6a
MD5 bd77d8c5f76ceb0146753803ded43d46
BLAKE2b-256 e41010c884508a967047c37dae3be88495094d27bea2f5adf5b6d3b4a5fc8ee4

See more details on using hashes here.

Provenance

The following attestation bundles were made for physarum_labs-0.2.1.tar.gz:

Publisher: release.yml on Physarum-Lab/physarum-labs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file physarum_labs-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: physarum_labs-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for physarum_labs-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 781eb0d15f02164816e633ed4cea58c11e7171f9e8c17cb84d3777853b235c38
MD5 305f3725cc26b0c8a604593a57849526
BLAKE2b-256 e1ea1caa85a206eb66e3e21582f205b923773e69d8676f5b2ba7c49616e56c22

See more details on using hashes here.

Provenance

The following attestation bundles were made for physarum_labs-0.2.1-py3-none-any.whl:

Publisher: release.yml on Physarum-Lab/physarum-labs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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