Buckets:

download
raw
3.06 kB
"""
Module.
Constitutional Hash: 608508a9bd224290
"""
import os
import sys
import pytest
# Governance and constitutional compliance test markers
pytestmark = [pytest.mark.governance, pytest.mark.constitutional]
# Add project root to sys.path
sys.path.append("/home/martin/ACGS")
from enhanced_agent_bus.governance.stability import mhc as mhc_module
if not mhc_module.TORCH_AVAILABLE:
pytest.skip("mHC governance tests require PyTorch", allow_module_level=True)
torch = mhc_module.torch
ManifoldHC = mhc_module.ManifoldHC
sinkhorn_projection = mhc_module.sinkhorn_projection
def test_sinkhorn_projection():
# Set seed for reproducibility (random matrices can cause convergence issues)
torch.manual_seed(42)
# Create a random positive matrix (using exp as in the implementation)
W = torch.randn(5, 5)
W_proj = sinkhorn_projection(W, iters=100)
# Check if rows sum to 1
row_sums = W_proj.sum(dim=-1)
# Check if cols sum to 1
col_sums = W_proj.sum(dim=-2)
assert torch.allclose(row_sums, torch.ones_like(row_sums), atol=1e-5)
assert torch.allclose(col_sums, torch.ones_like(col_sums), atol=1e-5)
def test_manifold_hc():
# Set seed for reproducibility
torch.manual_seed(42)
mhc = ManifoldHC(dim=4)
x = torch.randn(1, 4)
residual = torch.randn(1, 4)
# Forward pass
out = mhc(x, residual)
# Verify weights are doubly stochastic
W_proj = mhc.get_projected_weights()
assert out.shape == x.shape
assert torch.allclose(W_proj.sum(dim=-1), torch.ones(4), atol=1e-5)
def test_weighted_sinkhorn():
# Set seed for reproducibility
torch.manual_seed(42)
# 5 agents with different trust scores
trust_v = torch.tensor([0.1, 0.4, 0.2, 0.2, 0.1])
W = torch.randn(5, 5)
# Project with custom marginals
W_proj = sinkhorn_projection(W, row_marginal=trust_v, col_marginal=trust_v, iters=100)
row_sums = W_proj.sum(dim=-1)
col_sums = W_proj.sum(dim=-2)
assert torch.allclose(row_sums, trust_v, atol=1e-5)
assert torch.allclose(col_sums, trust_v, atol=1e-5)
def test_alpha_capping():
# Set seed for reproducibility
torch.manual_seed(42)
W = torch.randn(4, 4)
alpha = 0.3 # Max weight any agent can have per connection
W_proj = sinkhorn_projection(W, alpha=alpha, iters=50)
max_val = W_proj.max()
# Note: Sinkhorn might pull values slightly above alpha after row/col norm,
# but the clamp happens pre-normalization to damp "peaks".
# For a stricter invariant, we'd need a constrained Sinkhorn variants.
# However, for governance, damping is often sufficient.
# Check marginals still sum to 1
assert torch.allclose(W_proj.sum(dim=-1), torch.ones(4), atol=1e-5)
if __name__ == "__main__":
try:
test_sinkhorn_projection()
test_weighted_sinkhorn()
test_alpha_capping()
test_manifold_hc()
except (RuntimeError, ValueError, TypeError, AssertionError) as e:
import traceback
traceback.print_exc()
sys.exit(1)

Xet Storage Details

Size:
3.06 kB
·
Xet hash:
c6d99b8fbf6da1a676018a7398b3c6727004e6c89a3c0c54a6f2590d3d6a8fef

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.