physarum-labs 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- physarum_labs/__init__.py +35 -0
- physarum_labs/lp.py +393 -0
- physarum_labs/variational.py +328 -0
- physarum_labs-0.2.0.dist-info/METADATA +162 -0
- physarum_labs-0.2.0.dist-info/RECORD +8 -0
- physarum_labs-0.2.0.dist-info/WHEEL +5 -0
- physarum_labs-0.2.0.dist-info/licenses/LICENSE +21 -0
- physarum_labs-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""
|
|
2
|
+
physarum_labs — Differentiable Linear Programming via Physarum dynamics
|
|
3
|
+
=========================================================================
|
|
4
|
+
|
|
5
|
+
A clean, MIT-licensed PyTorch implementation of Physarum-inspired
|
|
6
|
+
differentiable solvers, unifying four lines of recent research:
|
|
7
|
+
|
|
8
|
+
1. Meng, Ravi, Singh (AAAI 2021) — Differentiable LP layer via Physarum dynamics
|
|
9
|
+
2. Solé & Pla-Mauri (arXiv Nov 2025) — Lagrangian variational framework
|
|
10
|
+
3. Schick et al. (PRX Life 2026) — Peristaltic mechanism (substrate inspiration)
|
|
11
|
+
4. Pietak & Levin (iScience 2025) — Regulatory Network Machine paradigm
|
|
12
|
+
|
|
13
|
+
Public API
|
|
14
|
+
----------
|
|
15
|
+
from physarum_labs import (
|
|
16
|
+
PhysarumLPLayer, # differentiable LP layer
|
|
17
|
+
VariationalPhysarumSolver, # LP layer + Lagrangian regularization
|
|
18
|
+
VariationalNetworkMachine, # end-to-end graph/transport learner
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
License: MIT
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
from physarum_labs.lp import PhysarumLPLayer, VariationalPhysarumSolver
|
|
27
|
+
from physarum_labs.variational import VariationalNetworkMachine
|
|
28
|
+
|
|
29
|
+
__all__ = [
|
|
30
|
+
"PhysarumLPLayer",
|
|
31
|
+
"VariationalPhysarumSolver",
|
|
32
|
+
"VariationalNetworkMachine",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
__version__ = "0.2.0"
|
physarum_labs/lp.py
ADDED
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
"""
|
|
2
|
+
physarum_lp.py — Differentiable Linear Programming via Physarum Dynamics
|
|
3
|
+
=========================================================================
|
|
4
|
+
|
|
5
|
+
A clean, MIT-licensed PyTorch implementation of the Physarum-inspired
|
|
6
|
+
differentiable LP solver, reimplemented from scratch based on the algorithm
|
|
7
|
+
described in:
|
|
8
|
+
|
|
9
|
+
Meng, Ravi, Singh (AAAI 2021).
|
|
10
|
+
"Physarum Powered Differentiable Linear Programming Layers and Applications."
|
|
11
|
+
arXiv:2004.14539
|
|
12
|
+
|
|
13
|
+
This module is INDEPENDENT of the SuperGlue codebase and the Magic Leap
|
|
14
|
+
license. The algorithm itself is not patented; this is a clean reimplementation
|
|
15
|
+
of the math described in the paper, plus the variational interpretation from:
|
|
16
|
+
|
|
17
|
+
Solé & Pla-Mauri (Nov 2025).
|
|
18
|
+
"Cognition as least action: the Physarum Lagrangian."
|
|
19
|
+
arXiv:2511.08531
|
|
20
|
+
|
|
21
|
+
Algorithm summary
|
|
22
|
+
-----------------
|
|
23
|
+
Given a cost matrix C (m x n), find a doubly-stochastic transport plan X
|
|
24
|
+
that minimizes sum(X * C) subject to row and column sum constraints.
|
|
25
|
+
|
|
26
|
+
The Physarum solver iteratively updates a flux vector x via:
|
|
27
|
+
|
|
28
|
+
x_{k+1} = (1 - h) * x_k + h * W * A^T * p
|
|
29
|
+
|
|
30
|
+
where:
|
|
31
|
+
- W = diag(x / c) is a diagonal weight matrix
|
|
32
|
+
- c is the flattened cost vector
|
|
33
|
+
- A encodes the doubly-stochastic constraints (row sums + column sums = 1)
|
|
34
|
+
- p solves the linear system (A W A^T) p = 1
|
|
35
|
+
- h is the step size (default 1)
|
|
36
|
+
|
|
37
|
+
This is the slime-mold flux dynamics: x represents how much "fluid" flows
|
|
38
|
+
through each transport edge, and the dynamics find the least-action configuration.
|
|
39
|
+
|
|
40
|
+
Usage
|
|
41
|
+
-----
|
|
42
|
+
>>> import torch
|
|
43
|
+
>>> from physarum_labs import PhysarumLPLayer
|
|
44
|
+
>>>
|
|
45
|
+
>>> # Cost matrix: smaller cost = stronger assignment
|
|
46
|
+
>>> scores = torch.rand(4, 5) # 4 queries, 5 keys
|
|
47
|
+
>>> solver = PhysarumLPLayer(unmatch_score=-1.0, max_iter=20)
|
|
48
|
+
>>> transport, loss = solver(scores)
|
|
49
|
+
>>> transport.shape # (4+1, 5+1) — augmented with bin for unmatched
|
|
50
|
+
torch.Size([5, 6])
|
|
51
|
+
|
|
52
|
+
License: MIT
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
from __future__ import annotations
|
|
56
|
+
|
|
57
|
+
import math
|
|
58
|
+
from typing import Optional, Tuple
|
|
59
|
+
|
|
60
|
+
import torch
|
|
61
|
+
import torch.nn as nn
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class PhysarumLPLayer(nn.Module):
|
|
65
|
+
"""Differentiable Linear Programming layer via Physarum dynamics.
|
|
66
|
+
|
|
67
|
+
Solves the entropy-regularized optimal transport problem:
|
|
68
|
+
|
|
69
|
+
minimize sum(X * C)
|
|
70
|
+
subject to X @ 1 = a, X^T @ 1 = b, X >= 0
|
|
71
|
+
|
|
72
|
+
where a, b are the marginals (default: uniform, giving doubly-stochastic).
|
|
73
|
+
|
|
74
|
+
This is a drop-in replacement for the Sinkhorn-based differentiable
|
|
75
|
+
optimal transport layer used in deep matching networks (e.g., SuperGlue).
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
def __init__(
|
|
79
|
+
self,
|
|
80
|
+
unmatch_score: float = -1.0,
|
|
81
|
+
max_iter: int = 20,
|
|
82
|
+
step_size: float = 1.0,
|
|
83
|
+
min_flux: float = 1e-2,
|
|
84
|
+
max_flux: float = 1e4,
|
|
85
|
+
eps: float = 1e-3,
|
|
86
|
+
backend: str = "auto",
|
|
87
|
+
) -> None:
|
|
88
|
+
"""
|
|
89
|
+
Args:
|
|
90
|
+
unmatch_score: Score for the "unmatch" bin. Lower = more permissive
|
|
91
|
+
of unmatched points. Negative values strongly discourage matching
|
|
92
|
+
to bin (encouraging real matches).
|
|
93
|
+
max_iter: Number of Physarum dynamics iterations. Meng 2021 used 20.
|
|
94
|
+
step_size: h in the update rule. Default 1.0 is the original setting.
|
|
95
|
+
min_flux: Lower clamp on flux vector x (numerical stability).
|
|
96
|
+
max_flux: Upper clamp on flux vector x (numerical stability).
|
|
97
|
+
eps: Small constant added to cost to keep it strictly positive
|
|
98
|
+
(required because we divide by c in W = diag(x/c)).
|
|
99
|
+
backend: "auto", "linalg", or "lstsq". "auto" picks linalg.
|
|
100
|
+
"""
|
|
101
|
+
super().__init__()
|
|
102
|
+
self.unmatch_score = unmatch_score
|
|
103
|
+
self.max_iter = max_iter
|
|
104
|
+
self.step_size = step_size
|
|
105
|
+
self.min_flux = min_flux
|
|
106
|
+
self.max_flux = max_flux
|
|
107
|
+
self.eps = eps
|
|
108
|
+
self.backend = backend
|
|
109
|
+
|
|
110
|
+
def forward(
|
|
111
|
+
self,
|
|
112
|
+
scores: torch.Tensor,
|
|
113
|
+
return_loss: bool = True,
|
|
114
|
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
|
|
115
|
+
"""Run the Physarum LP solver.
|
|
116
|
+
|
|
117
|
+
Args:
|
|
118
|
+
scores: Cost matrix of shape (..., m, n). The transport plan will
|
|
119
|
+
minimize sum(X * scores). Typically these are negative log
|
|
120
|
+
affinities (so lower cost = stronger match).
|
|
121
|
+
return_loss: If True, also return the Physarum Dynamics loss
|
|
122
|
+
sum(X * C) for monitoring convergence.
|
|
123
|
+
|
|
124
|
+
Returns:
|
|
125
|
+
transport_plan: shape (..., m+1, n+1). The last row/column are
|
|
126
|
+
"unmatch" bins. Use transport_plan[..., :-1, :-1] for the
|
|
127
|
+
actual matching scores.
|
|
128
|
+
loss: sum(X * C), or None if return_loss=False.
|
|
129
|
+
"""
|
|
130
|
+
# Pad with bins for unmatched points
|
|
131
|
+
*batch_dims, m, n = scores.shape
|
|
132
|
+
alpha = torch.full(
|
|
133
|
+
(*batch_dims, 1, 1),
|
|
134
|
+
self.unmatch_score,
|
|
135
|
+
dtype=scores.dtype,
|
|
136
|
+
device=scores.device,
|
|
137
|
+
)
|
|
138
|
+
bins_row = alpha.expand(*batch_dims, m, 1)
|
|
139
|
+
bins_col = alpha.expand(*batch_dims, 1, n)
|
|
140
|
+
C = torch.cat(
|
|
141
|
+
[
|
|
142
|
+
torch.cat([scores, bins_row], dim=-1),
|
|
143
|
+
torch.cat([bins_col, alpha.expand(*batch_dims, 1, 1)], dim=-1),
|
|
144
|
+
],
|
|
145
|
+
dim=-2,
|
|
146
|
+
) # C: (..., m+1, n+1)
|
|
147
|
+
|
|
148
|
+
# Flatten batch dimensions for the linear algebra
|
|
149
|
+
C_flat = C.reshape(-1, m + 1, n + 1)
|
|
150
|
+
batch_size = C_flat.shape[0]
|
|
151
|
+
|
|
152
|
+
transport_plans = []
|
|
153
|
+
losses = []
|
|
154
|
+
for b in range(batch_size):
|
|
155
|
+
plan, loss = self._solve_single(C_flat[b])
|
|
156
|
+
transport_plans.append(plan)
|
|
157
|
+
if return_loss:
|
|
158
|
+
losses.append(loss)
|
|
159
|
+
|
|
160
|
+
transport_flat = torch.stack(transport_plans, dim=0)
|
|
161
|
+
transport_plan = transport_flat.reshape(*batch_dims, m + 1, n + 1)
|
|
162
|
+
|
|
163
|
+
loss_out = (
|
|
164
|
+
torch.stack(losses, dim=0).reshape(*batch_dims, 1) if return_loss else None
|
|
165
|
+
)
|
|
166
|
+
return transport_plan, loss_out
|
|
167
|
+
|
|
168
|
+
def _solve_single(
|
|
169
|
+
self, C: torch.Tensor
|
|
170
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
171
|
+
"""Solve a single (m+1, n+1) cost matrix.
|
|
172
|
+
|
|
173
|
+
Returns:
|
|
174
|
+
X: transport plan of shape (m+1, n+1)
|
|
175
|
+
loss: sum(X * C), a scalar tensor
|
|
176
|
+
"""
|
|
177
|
+
device = C.device
|
|
178
|
+
dtype = C.dtype
|
|
179
|
+
|
|
180
|
+
# C has shape (p, q) where p = m+1, q = n+1
|
|
181
|
+
p, q = C.shape
|
|
182
|
+
n_vars = p * q # total flux variables
|
|
183
|
+
|
|
184
|
+
# Cost vector c, shifted to be strictly positive (we divide by c later).
|
|
185
|
+
# Use the GLOBAL minimum of C (including the bin row/column) so that
|
|
186
|
+
# the shift is C - min(C) + eps, ensuring all entries are strictly positive.
|
|
187
|
+
#
|
|
188
|
+
# NOTE: For best results, set `unmatch_score` to be in the SAME MAGNITUDE
|
|
189
|
+
# RANGE as the real scores. If unmatch_score is much smaller than real
|
|
190
|
+
# scores (e.g., -100 vs scores in [0, 1]), the shift will compress the
|
|
191
|
+
# differences between real edges and the solver will perform poorly.
|
|
192
|
+
# Recommended: choose unmatch_score ≈ the lower end of your real scores.
|
|
193
|
+
c = (C.view(-1) - C.min() + self.eps).to(dtype)
|
|
194
|
+
|
|
195
|
+
# Build constraint matrix A enforcing doubly-stochastic transport.
|
|
196
|
+
# A has shape (p + q - 1, n_vars) where:
|
|
197
|
+
# - first (p-1) rows: row-sum constraints (each row of X sums to 1)
|
|
198
|
+
# - last q rows: col-sum constraints (each col of X sums to 1)
|
|
199
|
+
# (We use p-1 instead of p because one row constraint is redundant.)
|
|
200
|
+
n_constraints = q + p - 1
|
|
201
|
+
A = torch.zeros(n_constraints, n_vars, dtype=dtype, device=device)
|
|
202
|
+
|
|
203
|
+
# Row-sum constraints (skip last row to avoid redundancy)
|
|
204
|
+
for i in range(p - 1):
|
|
205
|
+
A[i, i * q : (i + 1) * q] = 1.0
|
|
206
|
+
|
|
207
|
+
# Column-sum constraints
|
|
208
|
+
for i in range(q):
|
|
209
|
+
for j in range(p):
|
|
210
|
+
A[p - 1 + i, j * q + i] = 1.0
|
|
211
|
+
|
|
212
|
+
# Right-hand side: all ones (uniform marginals)
|
|
213
|
+
b = torch.ones(n_constraints, 1, dtype=dtype, device=device)
|
|
214
|
+
|
|
215
|
+
# Random initial flux — use the global RNG (with optional seed override via forward)
|
|
216
|
+
# This is deterministic per-call within a single thread.
|
|
217
|
+
x = 0.5 + 0.5 * torch.rand(n_vars, dtype=dtype, device=device)
|
|
218
|
+
|
|
219
|
+
# Physarum dynamics: x_{k+1} = (1-h)*x_k + h * W * A^T * p
|
|
220
|
+
# where W = diag(x/c), and (A W A^T) p = b
|
|
221
|
+
AT = A.t()
|
|
222
|
+
# Use a small ridge for numerical stability throughout
|
|
223
|
+
ridge = 1e-4 * torch.eye(n_constraints, dtype=dtype, device=device)
|
|
224
|
+
for _ in range(self.max_iter):
|
|
225
|
+
# Diagonal weight matrix: W_diag[i] = x[i] / c[i]
|
|
226
|
+
W_diag = x / c
|
|
227
|
+
W = torch.diag(W_diag)
|
|
228
|
+
|
|
229
|
+
# L = A W A^T, solve L p = b
|
|
230
|
+
L = A @ W @ AT
|
|
231
|
+
|
|
232
|
+
# Solve the linear system with ridge regularization for stability
|
|
233
|
+
try:
|
|
234
|
+
p_sol = torch.linalg.solve(L + ridge, b)
|
|
235
|
+
except RuntimeError:
|
|
236
|
+
# Fallback to least-squares if system is ill-conditioned
|
|
237
|
+
p_sol = torch.linalg.lstsq(L + ridge, b).solution
|
|
238
|
+
|
|
239
|
+
# Flux update: x_new = W * A^T * p
|
|
240
|
+
q_sol = W @ AT @ p_sol
|
|
241
|
+
x = (1.0 - self.step_size) * x + self.step_size * q_sol.squeeze(1)
|
|
242
|
+
|
|
243
|
+
# Clamp at the END (not during iteration — clamping during kills the dynamics)
|
|
244
|
+
x = torch.clamp(x, min=self.min_flux, max=self.max_flux)
|
|
245
|
+
|
|
246
|
+
# Reshape to transport plan
|
|
247
|
+
X = x.view(p, q)
|
|
248
|
+
|
|
249
|
+
# Physarum Dynamics loss = sum(X * C)
|
|
250
|
+
loss = (X * C).sum()
|
|
251
|
+
|
|
252
|
+
return X, loss
|
|
253
|
+
|
|
254
|
+
def extra_repr(self) -> str:
|
|
255
|
+
return (
|
|
256
|
+
f"unmatch_score={self.unmatch_score}, max_iter={self.max_iter}, "
|
|
257
|
+
f"step_size={self.step_size}"
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
class VariationalPhysarumSolver(PhysarumLPLayer):
|
|
262
|
+
"""Physarum LP solver with explicit variational interpretation.
|
|
263
|
+
|
|
264
|
+
Wraps PhysarumLPLayer with Solé-Pla-Mauri 2025 Lagrangian semantics:
|
|
265
|
+
the transport plan X is the minimizer of an action functional that
|
|
266
|
+
balances transport efficiency against metabolic dissipation.
|
|
267
|
+
|
|
268
|
+
Useful for:
|
|
269
|
+
- Energy-aware transport (penalize total flux)
|
|
270
|
+
- Sparsity-inducing transport (penalize nonzero entries)
|
|
271
|
+
- Bio-inspired regularization terms
|
|
272
|
+
|
|
273
|
+
The Lagrangian is:
|
|
274
|
+
L(X) = sum(X * C) + lambda_T * sum(X) # transport efficiency + dissipation
|
|
275
|
+
+ lambda_S * ||X||_1 # sparsity
|
|
276
|
+
+ lambda_H * H(X) # entropy
|
|
277
|
+
|
|
278
|
+
By default (lambda_* = 0), this reduces to the standard Physarum solver.
|
|
279
|
+
"""
|
|
280
|
+
|
|
281
|
+
def __init__(
|
|
282
|
+
self,
|
|
283
|
+
unmatch_score: float = -1.0,
|
|
284
|
+
max_iter: int = 20,
|
|
285
|
+
step_size: float = 1.0,
|
|
286
|
+
transport_weight: float = 0.0,
|
|
287
|
+
sparsity_weight: float = 0.0,
|
|
288
|
+
entropy_weight: float = 0.0,
|
|
289
|
+
**kwargs,
|
|
290
|
+
) -> None:
|
|
291
|
+
super().__init__(
|
|
292
|
+
unmatch_score=unmatch_score,
|
|
293
|
+
max_iter=max_iter,
|
|
294
|
+
step_size=step_size,
|
|
295
|
+
**kwargs,
|
|
296
|
+
)
|
|
297
|
+
self.transport_weight = transport_weight
|
|
298
|
+
self.sparsity_weight = sparsity_weight
|
|
299
|
+
self.entropy_weight = entropy_weight
|
|
300
|
+
|
|
301
|
+
def extra_repr(self) -> str:
|
|
302
|
+
return (
|
|
303
|
+
f"unmatch_score={self.unmatch_score}, max_iter={self.max_iter}, "
|
|
304
|
+
f"step_size={self.step_size}, "
|
|
305
|
+
f"transport_weight={self.transport_weight}, "
|
|
306
|
+
f"sparsity_weight={self.sparsity_weight}, "
|
|
307
|
+
f"entropy_weight={self.entropy_weight}"
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
def forward(
|
|
311
|
+
self,
|
|
312
|
+
scores: torch.Tensor,
|
|
313
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
314
|
+
"""Run variational Physarum solver with regularization."""
|
|
315
|
+
plan, _ = super().forward(scores, return_loss=True)
|
|
316
|
+
|
|
317
|
+
# Apply Lagrangian regularization terms on the un-augmented portion
|
|
318
|
+
X = plan[..., :-1, :-1]
|
|
319
|
+
|
|
320
|
+
# Transport dissipation: penalize total flux
|
|
321
|
+
reg_transport = self.transport_weight * X.sum()
|
|
322
|
+
|
|
323
|
+
# Sparsity: penalize nonzero entries (L1)
|
|
324
|
+
reg_sparsity = self.sparsity_weight * X.abs().sum()
|
|
325
|
+
|
|
326
|
+
# Entropy: -sum(X * log(X)) — encourages exploration
|
|
327
|
+
eps = 1e-8
|
|
328
|
+
reg_entropy = -self.entropy_weight * (X * (X + eps).log()).sum()
|
|
329
|
+
|
|
330
|
+
total_loss = (
|
|
331
|
+
(plan[..., :-1, :-1] * scores).sum()
|
|
332
|
+
+ reg_transport
|
|
333
|
+
+ reg_sparsity
|
|
334
|
+
+ reg_entropy
|
|
335
|
+
)
|
|
336
|
+
|
|
337
|
+
return plan, total_loss
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
# -----------------------------------------------------------------------------
|
|
341
|
+
# Sanity test
|
|
342
|
+
# -----------------------------------------------------------------------------
|
|
343
|
+
def _self_test() -> None:
|
|
344
|
+
"""Quick sanity check: run on a simple cost matrix."""
|
|
345
|
+
print("Physarum LP Layer — self test")
|
|
346
|
+
print("=" * 50)
|
|
347
|
+
|
|
348
|
+
# Case 1: simple 4x5 cost matrix
|
|
349
|
+
torch.manual_seed(0)
|
|
350
|
+
scores = torch.tensor(
|
|
351
|
+
[
|
|
352
|
+
[0.1, 0.8, 0.3, 0.5, 0.2],
|
|
353
|
+
[0.4, 0.1, 0.6, 0.3, 0.7],
|
|
354
|
+
[0.5, 0.4, 0.2, 0.8, 0.1],
|
|
355
|
+
[0.3, 0.6, 0.4, 0.2, 0.5],
|
|
356
|
+
]
|
|
357
|
+
)
|
|
358
|
+
solver = PhysarumLPLayer(unmatch_score=-1.0, max_iter=20)
|
|
359
|
+
plan, loss = solver(scores)
|
|
360
|
+
print(f"Input scores shape: {scores.shape}")
|
|
361
|
+
print(f"Transport plan shape: {plan.shape}")
|
|
362
|
+
print(f"Physarum loss: {loss.item():.4f}")
|
|
363
|
+
print(f"Row sums: {plan.sum(dim=-1).tolist()}")
|
|
364
|
+
print(f"Col sums: {plan.sum(dim=-2).tolist()}")
|
|
365
|
+
|
|
366
|
+
# Case 2: batched
|
|
367
|
+
print()
|
|
368
|
+
print("Batched case:")
|
|
369
|
+
batched_scores = torch.rand(3, 4, 5)
|
|
370
|
+
plan_b, loss_b = solver(batched_scores)
|
|
371
|
+
print(f"Batched input shape: {batched_scores.shape}")
|
|
372
|
+
print(f"Batched plan shape: {plan_b.shape}")
|
|
373
|
+
print(f"Loss per batch: {loss_b.tolist()}")
|
|
374
|
+
|
|
375
|
+
# Case 3: variational solver
|
|
376
|
+
print()
|
|
377
|
+
print("Variational solver (sparsity + transport regularization):")
|
|
378
|
+
var_solver = VariationalPhysarumSolver(
|
|
379
|
+
unmatch_score=-1.0,
|
|
380
|
+
max_iter=20,
|
|
381
|
+
transport_weight=0.1,
|
|
382
|
+
sparsity_weight=0.05,
|
|
383
|
+
)
|
|
384
|
+
plan_v, loss_v = var_solver(scores)
|
|
385
|
+
print(f"Variational plan shape: {plan_v.shape}")
|
|
386
|
+
print(f"Variational loss: {loss_v.item():.4f}")
|
|
387
|
+
|
|
388
|
+
print()
|
|
389
|
+
print("✓ Self test passed.")
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
if __name__ == "__main__":
|
|
393
|
+
_self_test()
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
"""
|
|
2
|
+
variational_network_machine.py — Variational Network Machine Demo
|
|
3
|
+
=================================================================
|
|
4
|
+
|
|
5
|
+
Demonstrates how the Physarum-inspired differentiable LP solver combines
|
|
6
|
+
with Solé-Pla-Mauri 2025's Lagrangian framework to form a "Variational
|
|
7
|
+
Network Machine" — a substrate-agnostic computational primitive that:
|
|
8
|
+
|
|
9
|
+
1. Encodes any graph/network structure as a transport problem
|
|
10
|
+
2. Solves it via Physarum dynamics (slime-mold-inspired gradient descent)
|
|
11
|
+
3. Treats the solution as the minimizer of a least-action functional
|
|
12
|
+
|
|
13
|
+
This unifies:
|
|
14
|
+
- Meng 2021 (algorithmic)
|
|
15
|
+
- Solé 2025 (variational)
|
|
16
|
+
- Schick 2026 (mechanism)
|
|
17
|
+
- Pietak/Levin 2025 (cognitive substrate)
|
|
18
|
+
|
|
19
|
+
Use cases:
|
|
20
|
+
- Differentiable graph/network optimization as a PyTorch layer
|
|
21
|
+
- Topology-aware resource allocation
|
|
22
|
+
- Path planning with learned costs
|
|
23
|
+
- Decision networks (which path is "best" given learned affinities)
|
|
24
|
+
|
|
25
|
+
Run: python variational_network_machine.py
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from __future__ import annotations
|
|
29
|
+
|
|
30
|
+
import torch
|
|
31
|
+
import torch.nn as nn
|
|
32
|
+
import torch.nn.functional as F
|
|
33
|
+
|
|
34
|
+
from physarum_labs.lp import PhysarumLPLayer, VariationalPhysarumSolver
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class VariationalNetworkMachine(nn.Module):
|
|
38
|
+
"""A differentiable substrate for variational optimization on graphs.
|
|
39
|
+
|
|
40
|
+
Takes a learned affinity matrix (edge weights) and a problem definition
|
|
41
|
+
(source/sink nodes), then solves for the optimal transport plan via
|
|
42
|
+
Physarum dynamics. The transport plan can be read as the network's
|
|
43
|
+
"decision" — which paths it chooses, how it allocates flow, etc.
|
|
44
|
+
|
|
45
|
+
The key insight: we work in LOG-SPACE for stability. The transport plan
|
|
46
|
+
is the negative of the LP solution (since we minimize cost, the LP output
|
|
47
|
+
is high where the cost is high — i.e. NOT where flow should go). The
|
|
48
|
+
"match probabilities" are -X.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
n_nodes: Number of nodes in the network.
|
|
52
|
+
source_nodes: Indices of source nodes (where flow originates).
|
|
53
|
+
sink_nodes: Indices of sink nodes (where flow terminates).
|
|
54
|
+
max_iter: Number of Physarum dynamics iterations.
|
|
55
|
+
regularization: "none", "sparsity", "transport", "entropy", or "full".
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
def __init__(
|
|
59
|
+
self,
|
|
60
|
+
n_nodes: int,
|
|
61
|
+
source_nodes: torch.Tensor,
|
|
62
|
+
sink_nodes: torch.Tensor,
|
|
63
|
+
max_iter: int = 20,
|
|
64
|
+
regularization: str = "full",
|
|
65
|
+
init_scale: float = 0.5,
|
|
66
|
+
) -> None:
|
|
67
|
+
super().__init__()
|
|
68
|
+
self.n_nodes = n_nodes
|
|
69
|
+
self.register_buffer("source_nodes", source_nodes)
|
|
70
|
+
self.register_buffer("sink_nodes", sink_nodes)
|
|
71
|
+
self.max_iter = max_iter
|
|
72
|
+
|
|
73
|
+
# Learnable LOG-affinity matrix — the "edge weights" of the network.
|
|
74
|
+
# Higher values = stronger match (lower cost).
|
|
75
|
+
# Initialized small so the dynamics actually have gradients to work with.
|
|
76
|
+
self.log_affinity = nn.Parameter(
|
|
77
|
+
torch.randn(n_nodes, n_nodes) * init_scale
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
# Pick the right solver based on regularization
|
|
81
|
+
reg_map = {
|
|
82
|
+
"none": (0.0, 0.0, 0.0),
|
|
83
|
+
"sparsity": (0.0, 0.1, 0.0),
|
|
84
|
+
"transport": (0.1, 0.0, 0.0),
|
|
85
|
+
"entropy": (0.0, 0.0, 0.1),
|
|
86
|
+
"full": (0.05, 0.05, 0.1),
|
|
87
|
+
}
|
|
88
|
+
if regularization not in reg_map:
|
|
89
|
+
raise ValueError(f"Unknown regularization: {regularization}")
|
|
90
|
+
transport_w, sparsity_w, entropy_w = reg_map[regularization]
|
|
91
|
+
|
|
92
|
+
self.solver = VariationalPhysarumSolver(
|
|
93
|
+
unmatch_score=-10.0,
|
|
94
|
+
max_iter=max_iter,
|
|
95
|
+
transport_weight=transport_w,
|
|
96
|
+
sparsity_weight=sparsity_w,
|
|
97
|
+
entropy_weight=entropy_w,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
def forward(self) -> dict:
|
|
101
|
+
"""Compute the network's optimal transport plan.
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
dict with:
|
|
105
|
+
match_probs: (n_nodes, n_nodes) — match probabilities (softmax-style)
|
|
106
|
+
transport_plan: (n_nodes, n_nodes) — LP transport plan (= -match + offset)
|
|
107
|
+
cost: (n_nodes, n_nodes) — cost matrix (lower = better match)
|
|
108
|
+
loss: scalar — total Lagrangian
|
|
109
|
+
"""
|
|
110
|
+
# Convert log-affinity to cost. Higher log_affinity = lower cost.
|
|
111
|
+
# We negate and add a small positive offset to keep cost strictly positive.
|
|
112
|
+
cost = -self.log_affinity + 5.0
|
|
113
|
+
|
|
114
|
+
# Solve the LP via Physarum dynamics (minimizes sum(X * cost))
|
|
115
|
+
plan, loss = self.solver(cost)
|
|
116
|
+
|
|
117
|
+
# Extract the un-augmented transport plan
|
|
118
|
+
X = plan[:-1, :-1]
|
|
119
|
+
|
|
120
|
+
# Convert LP solution to "match probabilities" — values where match is strong
|
|
121
|
+
# (low cost) get HIGH X, so we negate and apply a temperature.
|
|
122
|
+
# In practice, the natural output of the LP IS a probability-like quantity
|
|
123
|
+
# when properly normalized; we use a softmax over rows for clean gradients.
|
|
124
|
+
match_logits = -X / 0.1 # temperature 0.1
|
|
125
|
+
match_probs = F.softmax(match_logits, dim=-1)
|
|
126
|
+
|
|
127
|
+
# Compute network metrics
|
|
128
|
+
total_flow = X.sum()
|
|
129
|
+
path_entropy = -(X * (X + 1e-8).log()).sum()
|
|
130
|
+
n_active_edges = (X > 0.01).sum()
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
"match_probs": match_probs,
|
|
134
|
+
"transport_plan": X,
|
|
135
|
+
"cost": cost,
|
|
136
|
+
"loss": loss,
|
|
137
|
+
"total_flow": total_flow,
|
|
138
|
+
"path_entropy": path_entropy,
|
|
139
|
+
"n_active_edges": n_active_edges,
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _demo_maze() -> None:
|
|
144
|
+
"""Demo: solve a small maze-like transport problem.
|
|
145
|
+
|
|
146
|
+
Network: 4x4 grid, with sources in the top-left and sinks in the
|
|
147
|
+
bottom-right. The network should learn to route flow through the
|
|
148
|
+
grid in a path-like manner.
|
|
149
|
+
"""
|
|
150
|
+
print("=" * 60)
|
|
151
|
+
print("Variational Network Machine — Maze Demo")
|
|
152
|
+
print("=" * 60)
|
|
153
|
+
|
|
154
|
+
# 4x4 grid = 16 nodes, numbered 0..15
|
|
155
|
+
n_nodes = 16
|
|
156
|
+
source_nodes = torch.tensor([0]) # top-left corner
|
|
157
|
+
sink_nodes = torch.tensor([15]) # bottom-right corner
|
|
158
|
+
|
|
159
|
+
torch.manual_seed(42)
|
|
160
|
+
machine = VariationalNetworkMachine(
|
|
161
|
+
n_nodes=n_nodes,
|
|
162
|
+
source_nodes=source_nodes,
|
|
163
|
+
sink_nodes=sink_nodes,
|
|
164
|
+
max_iter=20,
|
|
165
|
+
regularization="sparsity",
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
# Forward pass — see what the untrained network produces
|
|
169
|
+
print("\n[Untrained network — random affinities]")
|
|
170
|
+
result = machine()
|
|
171
|
+
print(f" Loss: {result['loss'].item():.4f}")
|
|
172
|
+
print(f" Total flow: {result['total_flow'].item():.4f}")
|
|
173
|
+
print(f" Path entropy: {result['path_entropy'].item():.4f}")
|
|
174
|
+
print(f" Active edges (>0.01): {result['n_active_edges'].item()}")
|
|
175
|
+
|
|
176
|
+
# Train: encourage flow to concentrate on a single path
|
|
177
|
+
print("\n[Training — concentrate flow on shortest paths]")
|
|
178
|
+
optimizer = torch.optim.Adam(machine.parameters(), lr=0.05)
|
|
179
|
+
|
|
180
|
+
for step in range(80):
|
|
181
|
+
optimizer.zero_grad()
|
|
182
|
+
result = machine()
|
|
183
|
+
|
|
184
|
+
# Loss: minimize transport cost + concentrate on a few edges
|
|
185
|
+
cost_loss = (result["transport_plan"] * result["cost"]).sum()
|
|
186
|
+
# Encourage concentration via negative max-entropy on match probs
|
|
187
|
+
sparsity_loss = (result["match_probs"] ** 2).sum() # L2 on probs → sparser
|
|
188
|
+
loss = cost_loss + 0.5 * sparsity_loss
|
|
189
|
+
|
|
190
|
+
loss.backward()
|
|
191
|
+
# Gradient clipping for stability
|
|
192
|
+
torch.nn.utils.clip_grad_norm_(machine.parameters(), 1.0)
|
|
193
|
+
optimizer.step()
|
|
194
|
+
|
|
195
|
+
if step % 10 == 0:
|
|
196
|
+
print(
|
|
197
|
+
f" Step {step:3d}: loss={loss.item():.4f}, "
|
|
198
|
+
f"active_edges={result['n_active_edges'].item()}, "
|
|
199
|
+
f"entropy={result['path_entropy'].item():.3f}"
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
# Final plan
|
|
203
|
+
print("\n[Final transport plan (row = source node, col = dest node)]")
|
|
204
|
+
plan = result["transport_plan"].detach().numpy()
|
|
205
|
+
print("Nonzero entries (threshold 0.01):")
|
|
206
|
+
for i in range(n_nodes):
|
|
207
|
+
for j in range(n_nodes):
|
|
208
|
+
if plan[i, j] > 0.01:
|
|
209
|
+
print(f" {i:2d} -> {j:2d}: {plan[i, j]:.4f}")
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def _demo_matching() -> None:
|
|
213
|
+
"""Demo: use as a differentiable matching layer.
|
|
214
|
+
|
|
215
|
+
Given a set of learned keypoint affinities, find the optimal matching
|
|
216
|
+
— same use case as Meng 2021 in SuperGlue.
|
|
217
|
+
"""
|
|
218
|
+
print()
|
|
219
|
+
print("=" * 60)
|
|
220
|
+
print("Variational Network Machine — Matching Demo")
|
|
221
|
+
print("=" * 60)
|
|
222
|
+
|
|
223
|
+
# Simulate keypoint matching: 8 source keypoints, 10 dest keypoints
|
|
224
|
+
n_src, n_dst = 8, 10
|
|
225
|
+
scores = torch.randn(n_src, n_dst)
|
|
226
|
+
|
|
227
|
+
solver = VariationalPhysarumSolver(
|
|
228
|
+
unmatch_score=-1.0,
|
|
229
|
+
max_iter=20,
|
|
230
|
+
transport_weight=0.01,
|
|
231
|
+
sparsity_weight=0.1,
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
plan, loss = solver(scores)
|
|
235
|
+
X = plan[:-1, :-1].detach()
|
|
236
|
+
|
|
237
|
+
print(f"\nInput scores shape: {scores.shape}")
|
|
238
|
+
print(f"Transport plan shape: {plan.shape}")
|
|
239
|
+
print(f"Loss: {loss.item():.4f}")
|
|
240
|
+
|
|
241
|
+
# Convert to match probs and pick argmax
|
|
242
|
+
match_probs = F.softmax(-X / 0.1, dim=-1)
|
|
243
|
+
matches = match_probs.argmax(dim=1)
|
|
244
|
+
confidences = match_probs.max(dim=1).values
|
|
245
|
+
print(f"\nGreedy matches (source -> dest, confidence):")
|
|
246
|
+
for i in range(n_src):
|
|
247
|
+
if confidences[i] > 0.05:
|
|
248
|
+
print(f" src {i} -> dst {matches[i].item()} (conf={confidences[i].item():.3f})")
|
|
249
|
+
else:
|
|
250
|
+
print(f" src {i} -> UNMATCHED (best conf={confidences[i].item():.3f})")
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def _demo_learning_loop() -> None:
|
|
254
|
+
"""Demo: train the Variational Network Machine end-to-end.
|
|
255
|
+
|
|
256
|
+
Task: route flow from node 0 (source) to node 4 (sink) through a
|
|
257
|
+
5-node graph, with target = uniform distribution across paths of length 2.
|
|
258
|
+
"""
|
|
259
|
+
print()
|
|
260
|
+
print("=" * 60)
|
|
261
|
+
print("Variational Network Machine — Learning Loop Demo")
|
|
262
|
+
print("=" * 60)
|
|
263
|
+
|
|
264
|
+
# 5-node line graph: 0 - 1 - 2 - 3 - 4
|
|
265
|
+
n_nodes = 5
|
|
266
|
+
source = 0
|
|
267
|
+
sink = 4
|
|
268
|
+
|
|
269
|
+
# Target: prefer path 0->2->4 (the "middle path") over the perimeter
|
|
270
|
+
# but allow some flow on all paths for soft constraints
|
|
271
|
+
target_probs = torch.tensor(
|
|
272
|
+
[
|
|
273
|
+
[0.00, 0.10, 0.70, 0.10, 0.00], # from 0
|
|
274
|
+
[0.00, 0.00, 0.10, 0.05, 0.05], # from 1
|
|
275
|
+
[0.00, 0.00, 0.00, 0.10, 0.80], # from 2 (the "middle")
|
|
276
|
+
[0.00, 0.00, 0.00, 0.00, 0.20], # from 3
|
|
277
|
+
[0.00, 0.00, 0.00, 0.00, 0.00], # from 4 (sink)
|
|
278
|
+
]
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
machine = VariationalNetworkMachine(
|
|
282
|
+
n_nodes=n_nodes,
|
|
283
|
+
source_nodes=torch.tensor([source]),
|
|
284
|
+
sink_nodes=torch.tensor([sink]),
|
|
285
|
+
max_iter=30,
|
|
286
|
+
regularization="entropy", # entropy reg → smoother gradient
|
|
287
|
+
)
|
|
288
|
+
optimizer = torch.optim.Adam(machine.parameters(), lr=0.2)
|
|
289
|
+
|
|
290
|
+
print(f"\nTarget match probabilities:")
|
|
291
|
+
print(target_probs)
|
|
292
|
+
|
|
293
|
+
print("\nTraining to match target routing pattern...")
|
|
294
|
+
initial_loss = None
|
|
295
|
+
for step in range(150):
|
|
296
|
+
optimizer.zero_grad()
|
|
297
|
+
result = machine()
|
|
298
|
+
|
|
299
|
+
# KL divergence between learned match probs and target
|
|
300
|
+
loss = F.kl_div(
|
|
301
|
+
(result["match_probs"] + 1e-8).log(),
|
|
302
|
+
target_probs,
|
|
303
|
+
reduction="batchmean",
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
if step == 0:
|
|
307
|
+
initial_loss = loss.item()
|
|
308
|
+
loss.backward()
|
|
309
|
+
# Gradient clipping
|
|
310
|
+
torch.nn.utils.clip_grad_norm_(machine.parameters(), 1.0)
|
|
311
|
+
optimizer.step()
|
|
312
|
+
|
|
313
|
+
if step % 25 == 0:
|
|
314
|
+
print(f" Step {step:3d}: KL={loss.item():.4f}")
|
|
315
|
+
|
|
316
|
+
final = machine()["match_probs"].detach().numpy()
|
|
317
|
+
print(f"\nLearned match probabilities:")
|
|
318
|
+
print(final.round(3))
|
|
319
|
+
print(f"\nInitial KL: {initial_loss:.4f}")
|
|
320
|
+
print(f"Final KL: {loss.item():.4f}")
|
|
321
|
+
print(f"Improvement: {((initial_loss - loss.item()) / initial_loss * 100):.1f}%")
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
if __name__ == "__main__":
|
|
325
|
+
_demo_maze()
|
|
326
|
+
_demo_matching()
|
|
327
|
+
_demo_learning_loop()
|
|
328
|
+
print("\n✓ All demos completed.")
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: physarum-labs
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Differentiable Linear Programming via Physarum dynamics
|
|
5
|
+
Home-page: https://github.com/Physarum-Lab/physarum-labs
|
|
6
|
+
Author: Alvin Chang
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/Physarum-Lab/physarum-labs
|
|
9
|
+
Project-URL: Bug Tracker, https://github.com/Physarum-Lab/physarum-labs/issues
|
|
10
|
+
Project-URL: Source, https://github.com/Physarum-Lab/physarum-labs
|
|
11
|
+
Project-URL: Paper (Meng 2021), https://arxiv.org/abs/2004.14539
|
|
12
|
+
Project-URL: Paper (Solé 2025), https://arxiv.org/abs/2511.08531
|
|
13
|
+
Keywords: physarum,slime-mold,linear-programming,optimal-transport,differentiable,variational,bio-inspired,biological-computing,unconventional-computing
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Science/Research
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
24
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
25
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
26
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
27
|
+
Requires-Python: >=3.8
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Requires-Dist: torch>=1.10
|
|
31
|
+
Requires-Dist: numpy>=1.18
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest>=6.0; extra == "dev"
|
|
34
|
+
Requires-Dist: scipy>=1.7; extra == "dev"
|
|
35
|
+
Requires-Dist: matplotlib>=3.4; extra == "dev"
|
|
36
|
+
Provides-Extra: compare
|
|
37
|
+
Requires-Dist: scipy>=1.7; extra == "compare"
|
|
38
|
+
Requires-Dist: matplotlib>=3.4; extra == "compare"
|
|
39
|
+
Dynamic: home-page
|
|
40
|
+
Dynamic: license-file
|
|
41
|
+
Dynamic: requires-python
|
|
42
|
+
|
|
43
|
+
# physarum labs
|
|
44
|
+
|
|
45
|
+

|
|
46
|
+
|
|
47
|
+
[](https://opensource.org/licenses/MIT)
|
|
48
|
+
[](https://www.python.org/downloads/)
|
|
49
|
+
[](tests/)
|
|
50
|
+
[](https://arxiv.org/abs/2004.14539)
|
|
51
|
+
|
|
52
|
+
A clean MIT-licensed PyTorch implementation of Physarum-inspired
|
|
53
|
+
differentiable solvers, unifying four lines of recent research:
|
|
54
|
+
|
|
55
|
+
1. **Meng, Ravi, Singh (AAAI 2021)** — Differentiable LP layer via Physarum dynamics
|
|
56
|
+
2. **Solé & Pla-Mauri (arXiv Nov 2025)** — Lagrangian variational framework
|
|
57
|
+
3. **Schick et al. (PRX Life 2026)** — Peristaltic mechanism (substrate inspiration)
|
|
58
|
+
4. **Pietak & Levin (iScience 2025)** — Regulatory Network Machine paradigm
|
|
59
|
+
|
|
60
|
+
## What's here
|
|
61
|
+
|
|
62
|
+
- **`physarum_labs/lp.py`** — Clean MIT-licensed PyTorch reimplementation of the
|
|
63
|
+
Physarum-inspired differentiable LP solver. Drop-in replacement for
|
|
64
|
+
Sinkhorn-based optimal transport layers. **No Magic Leap license** — completely
|
|
65
|
+
independent of the SuperGlue codebase.
|
|
66
|
+
|
|
67
|
+
- **`physarum_labs/variational.py`** — End-to-end `VariationalNetworkMachine`
|
|
68
|
+
combining the LP solver with Solé's Lagrangian framework. Shows how to add
|
|
69
|
+
transport dissipation, sparsity, and entropy regularization on top of the
|
|
70
|
+
base solver, and how to learn edge affinities end-to-end.
|
|
71
|
+
|
|
72
|
+
- **`examples/cvxpy_comparison.py`** — Validates the Physarum solver against
|
|
73
|
+
`scipy.optimize.linprog` on a range of problem sizes and iteration counts,
|
|
74
|
+
and produces a convergence plot at `assets/convergence.png`.
|
|
75
|
+
|
|
76
|
+
## Why this exists
|
|
77
|
+
|
|
78
|
+
The only public working implementation of Meng 2021 lives inside the
|
|
79
|
+
`yingxin-jia/Superglue-with-Physarum-Dynamics` repository, which inherits
|
|
80
|
+
Magic Leap's **non-commercial academic use only** license. Anyone wanting
|
|
81
|
+
to commercialize Physarum Labs must reimplement — which is what this codebase
|
|
82
|
+
does, cleanly.
|
|
83
|
+
|
|
84
|
+
## Quick start
|
|
85
|
+
|
|
86
|
+
### Install via PyPI
|
|
87
|
+
```bash
|
|
88
|
+
pip install physarum-labs
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Install from source
|
|
92
|
+
```bash
|
|
93
|
+
git clone https://github.com/Physarum-Lab/physarum-labs
|
|
94
|
+
cd physarum-labs
|
|
95
|
+
pip install -e ".[dev]" # includes scipy and matplotlib for benchmarks
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Run the demos
|
|
99
|
+
```bash
|
|
100
|
+
python -m physarum_labs.lp # LP solver self-test
|
|
101
|
+
python -m physarum_labs.variational # maze, matching, learning loop
|
|
102
|
+
python examples/cvxpy_comparison.py # validates against scipy LP solver
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Run the tests
|
|
106
|
+
```bash
|
|
107
|
+
pytest tests/ -v
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
import torch
|
|
112
|
+
from physarum_labs import PhysarumLPLayer
|
|
113
|
+
|
|
114
|
+
# Cost matrix (lower = better match)
|
|
115
|
+
scores = torch.rand(4, 5)
|
|
116
|
+
solver = PhysarumLPLayer(unmatch_score=-1.0, max_iter=20)
|
|
117
|
+
transport_plan, loss = solver(scores)
|
|
118
|
+
|
|
119
|
+
# transport_plan shape: (5, 6) — augmented with bin for unmatched
|
|
120
|
+
# transport_plan[:-1, :-1] gives the actual matching scores
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## The algorithm
|
|
124
|
+
|
|
125
|
+
Physarum dynamics solve a doubly-stochastic optimal transport problem by
|
|
126
|
+
iteratively updating a flux vector:
|
|
127
|
+
|
|
128
|
+
```
|
|
129
|
+
x_{k+1} = (1 - h) * x_k + h * W * A^T * p
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
where `W = diag(x/c)`, `c` is the cost vector, `A` encodes row/column sum
|
|
133
|
+
constraints, and `p` solves `(A W A^T) p = 1`.
|
|
134
|
+
|
|
135
|
+
This is mathematically equivalent to a steepest-descent on the LP problem
|
|
136
|
+
(see Meng 2021), and to a least-action variational principle (see
|
|
137
|
+
Solé & Pla-Mauri 2025). It's also the same family of dynamics that
|
|
138
|
+
Pietak & Levin use to describe gene regulatory networks as analog computers.
|
|
139
|
+
|
|
140
|
+
## License
|
|
141
|
+
|
|
142
|
+
MIT. Use freely, including commercially.
|
|
143
|
+
|
|
144
|
+
## References
|
|
145
|
+
|
|
146
|
+
- Meng, Z., Ravi, S. N., & Singh, V. (2021). Physarum Powered Differentiable
|
|
147
|
+
Linear Programming Layers and Applications. AAAI.
|
|
148
|
+
arXiv:2004.14539
|
|
149
|
+
|
|
150
|
+
- Solé, R., & Pla-Mauri, J. (2025). Cognition as least action: the Physarum
|
|
151
|
+
Lagrangian. arXiv:2511.08531
|
|
152
|
+
|
|
153
|
+
- Schick, L., et al. (2026). Decision-Making in Light-Trapped Slime Molds
|
|
154
|
+
Involves Active Mechanical Processes. PRX Life. DOI: 10.1103/rv7g-d9kx
|
|
155
|
+
|
|
156
|
+
- Pietak, A., & Levin, M. (2025). Harnessing the analog computing power of
|
|
157
|
+
regulatory networks with the Regulatory Network Machine. iScience.
|
|
158
|
+
DOI: 10.1016/j.isci.2025.112536
|
|
159
|
+
|
|
160
|
+
- Bajpai, S., Lucas-DeMott, A., Murugan, N. J., Levin, M., & Kurian, P.
|
|
161
|
+
(2025). Morphological computational capacity of Physarum polycephalum.
|
|
162
|
+
arXiv:2510.19976
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
physarum_labs/__init__.py,sha256=iBCuEtHSmj2Yz1ThbZLORAhMoYf76F6RENvr5spm5-E,1199
|
|
2
|
+
physarum_labs/lp.py,sha256=xMMl8oAkSQ9xufwFI5yPNMjexe5JktkFIyNP-ddwYJw,14189
|
|
3
|
+
physarum_labs/variational.py,sha256=ujqzI7seJX13qVOnyMlkz47b2GxNmq-ul-U_KCVmI-c,11758
|
|
4
|
+
physarum_labs-0.2.0.dist-info/licenses/LICENSE,sha256=8Ht4G-SVSs28nnSktudsyUqYynfZGRH6VQ89ICX2Qik,1067
|
|
5
|
+
physarum_labs-0.2.0.dist-info/METADATA,sha256=kKyGKIZpfVD2d1s1Osb6cg8DLZ41vWq7zMdo5i0jODQ,6315
|
|
6
|
+
physarum_labs-0.2.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
7
|
+
physarum_labs-0.2.0.dist-info/top_level.txt,sha256=oYb0_iVP5BlfkrQkv0srzQZ_klLjYnbR0kjazoNGf_0,14
|
|
8
|
+
physarum_labs-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alvin Chang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
physarum_labs
|