|
| 1 | +"""Tests for pymdp bridge integration in HGF, DDM, metacognition, hierarchical.""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import jax.numpy as jnp |
| 5 | +import pytest |
| 6 | + |
| 7 | +try: |
| 8 | + from pymdp.agent import Agent as PyMDP_Agent |
| 9 | + from alf.compat import alf_to_pymdp |
| 10 | + HAS_PYMDP = True |
| 11 | +except ImportError: |
| 12 | + HAS_PYMDP = False |
| 13 | + |
| 14 | +from alf.generative_model import GenerativeModel |
| 15 | + |
| 16 | +pytestmark = pytest.mark.skipif(not HAS_PYMDP, reason="pymdp not installed") |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def simple_model(): |
| 21 | + """3-state, 3-obs, 2-action model.""" |
| 22 | + A = np.array([[0.9, 0.05, 0.05], |
| 23 | + [0.05, 0.9, 0.05], |
| 24 | + [0.05, 0.05, 0.9]]) |
| 25 | + B = np.zeros((3, 3, 2)) |
| 26 | + B[:, :, 0] = np.array([[0.8, 0.1, 0.1], |
| 27 | + [0.1, 0.8, 0.1], |
| 28 | + [0.1, 0.1, 0.8]]) |
| 29 | + B[:, :, 1] = np.array([[0.1, 0.1, 0.8], |
| 30 | + [0.8, 0.1, 0.1], |
| 31 | + [0.1, 0.8, 0.1]]) |
| 32 | + C = np.array([2.0, 0.0, -1.0]) |
| 33 | + D = np.array([1/3, 1/3, 1/3]) |
| 34 | + return GenerativeModel(A=[A], B=[B], C=[C], D=[D]) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def pymdp_agent(simple_model): |
| 39 | + return alf_to_pymdp(simple_model) |
| 40 | + |
| 41 | + |
| 42 | +class TestHGFBridgePymdp: |
| 43 | + def test_accepts_pymdp_agent(self, pymdp_agent): |
| 44 | + from alf.hgf.bridge import HGFPerceptualAgent |
| 45 | + from alf.hgf.updates import BinaryHGFParams |
| 46 | + |
| 47 | + params = BinaryHGFParams(omega_2=-2.0, mu_2_0=0.0, sigma_2_0=1.0) |
| 48 | + agent = HGFPerceptualAgent(pymdp_agent, params, gamma=4.0) |
| 49 | + assert agent.gm is not None |
| 50 | + assert isinstance(agent.gm, GenerativeModel) |
| 51 | + |
| 52 | + def test_step_works_with_pymdp(self, pymdp_agent): |
| 53 | + from alf.hgf.bridge import HGFPerceptualAgent |
| 54 | + from alf.hgf.updates import BinaryHGFParams |
| 55 | + |
| 56 | + params = BinaryHGFParams(omega_2=-2.0, mu_2_0=0.0, sigma_2_0=1.0) |
| 57 | + agent = HGFPerceptualAgent(pymdp_agent, params, gamma=4.0) |
| 58 | + action, info = agent.step(1.0) |
| 59 | + assert isinstance(action, int) |
| 60 | + assert "G" in info |
| 61 | + |
| 62 | + |
| 63 | +class TestDDMBridgePymdp: |
| 64 | + def test_neg_efe_to_ddm(self): |
| 65 | + from alf.ddm.bridge import neg_efe_to_ddm, efe_to_ddm |
| 66 | + |
| 67 | + G = jnp.array([-1.5, -0.5]) |
| 68 | + neg_efe = jnp.array([1.5, 0.5]) |
| 69 | + |
| 70 | + params_from_G = efe_to_ddm(G) |
| 71 | + params_from_neg_efe = neg_efe_to_ddm(neg_efe) |
| 72 | + |
| 73 | + np.testing.assert_allclose(float(params_from_G.v), |
| 74 | + float(params_from_neg_efe.v), atol=1e-5) |
| 75 | + np.testing.assert_allclose(float(params_from_G.a), |
| 76 | + float(params_from_neg_efe.a), atol=1e-5) |
| 77 | + |
| 78 | + def test_neg_efe_to_ddm_with_batch_dim(self): |
| 79 | + from alf.ddm.bridge import neg_efe_to_ddm |
| 80 | + |
| 81 | + neg_efe_batched = jnp.array([[1.5, 0.5]]) |
| 82 | + params = neg_efe_to_ddm(neg_efe_batched) |
| 83 | + assert jnp.isfinite(params.v) |
| 84 | + |
| 85 | + |
| 86 | +class TestMetacognitionPymdp: |
| 87 | + def test_accepts_pymdp_agent(self, pymdp_agent): |
| 88 | + from alf.metacognition import MetacognitiveAgent |
| 89 | + |
| 90 | + agent = MetacognitiveAgent(pymdp_agent, gamma=4.0) |
| 91 | + assert agent.gm is not None |
| 92 | + |
| 93 | + def test_step_works_with_pymdp(self, pymdp_agent): |
| 94 | + from alf.metacognition import MetacognitiveAgent |
| 95 | + |
| 96 | + agent = MetacognitiveAgent(pymdp_agent, gamma=4.0) |
| 97 | + action, info = agent.step([0]) |
| 98 | + assert isinstance(action, int) |
| 99 | + assert "metacognitive_confidence" in info |
| 100 | + assert 0.0 <= info["metacognitive_confidence"] <= 1.0 |
| 101 | + |
| 102 | + def test_learn_works_with_pymdp(self, pymdp_agent): |
| 103 | + from alf.metacognition import MetacognitiveAgent |
| 104 | + |
| 105 | + agent = MetacognitiveAgent(pymdp_agent, gamma=4.0) |
| 106 | + agent.step([0]) |
| 107 | + agent.learn(1.0) |
| 108 | + assert len(agent.accuracy_history) == 1 |
| 109 | + |
| 110 | + |
| 111 | +class TestHierarchicalPymdp: |
| 112 | + def test_from_pymdp(self, pymdp_agent): |
| 113 | + from alf.hierarchical import HierarchicalGenerativeModel |
| 114 | + |
| 115 | + hierarchy = HierarchicalGenerativeModel.from_pymdp(pymdp_agent) |
| 116 | + assert hierarchy.num_levels == 1 |
| 117 | + assert hierarchy.levels[0].num_states == 3 |
| 118 | + assert hierarchy.levels[0].num_actions == 2 |
| 119 | + |
| 120 | + def test_from_pymdp_with_higher_levels(self, pymdp_agent): |
| 121 | + from alf.hierarchical import HierarchicalGenerativeModel, HierarchicalLevel |
| 122 | + |
| 123 | + context_A = np.eye(2) |
| 124 | + context_B = np.stack([np.eye(2)] * 2, axis=-1) |
| 125 | + context_C = np.array([1.0, 0.0]) |
| 126 | + context_D = np.array([0.5, 0.5]) |
| 127 | + context_level = HierarchicalLevel( |
| 128 | + A=context_A, B=context_B, C=context_C, D=context_D, |
| 129 | + temporal_scale=5, level_name="context", |
| 130 | + ) |
| 131 | + |
| 132 | + hierarchy = HierarchicalGenerativeModel.from_pymdp( |
| 133 | + pymdp_agent, higher_levels=[context_level] |
| 134 | + ) |
| 135 | + assert hierarchy.num_levels == 2 |
| 136 | + assert hierarchy.levels[0].level_name == "sensorimotor" |
| 137 | + assert hierarchy.levels[1].level_name == "context" |
0 commit comments