Skip to content

Repository files navigation

MINDWORM // Artificial Phenomenology

"What I cannot create, I do not understand." — Richard Feynman


// EXPERIMENT 02: THE ALLOSTATIC AGENTS

This repository contains the simulation kernel for Experiment 02 from website https://mindworm.icu, an investigation into the emergence of primitive consciousness based on the theories of neuropsychologist Mark Solms (The Hidden Spring) and Karl Friston's Free Energy Principle.


The Concept

This is not a game. The agents in this simulation are self-evidencing systems resisting entropy. They do not follow arbitrary if/then rules; they are driven by a biological imperative to maintain homeostasis.

In this model, what we call "feelings" (hunger, cold, comfort, distress) are modeled as the subjective experience of the agent's internal state deviations. Consciousness, according to Solms, arises from the need to manage these affective states to prioritize attention.


Demo

Here is a visualization of the agents minimizing Free Energy in real-time. Notice how they form trails and change color based on their internal homeostatic error ($H$):

Swarm Simulation Demo

(White = Optimal, Blue = Cold, Red = Hot, Brown = Hungry)


Dynamics

The complexity of the swarm arises from simple, local interactions rather than global coordination.

  • Myopic Sensing: Agents can only sense the 8 adjacent cells (Moore neighborhood). They have no global knowledge of the map or the location of food patches.
  • Three-Channel Trace System: To navigate this uncertainty, agents interact with three types of decaying markers:
    • Navigation Trace: Agents mark their path as they move. This acts as a "repellent" memory, discouraging backtracking and forcing the exploration of new territory.
    • Food Pheromone: Upon discovering energy, agents release a specific, high-valence scent. This volatile signal acts as a rudimentary form of stigmergic communication.
    • Associative Thermal Memory: Agents remember their internal temperature context when they find food. This creates a "thermal attraction field" that guides them back to favorable environments. A rudimentary form of memory in the sense of "it used to be nice here and maybe it will find food here again".

🧠 The Math of "Feeling"

The simulation logic is governed by Active Inference. Agents do not merely react to stimuli; they generate predictions about the future to minimize their expected Free Energy ($G$).

1. Physiological Error ($H$)

The agent strives to maintain its internal variables (Temperature $T$ and Energy $E$) within viable bounds:

$$ H = w_T |T_{int} - T_{pref}| + w_E \max(0, E_{crit} - E_{int}) $$

2. Active Inference ($G$)

Before moving, an agent simulates all possible adjacent steps and calculates the Expected Free Energy ($G$):

$$ G(a) = \underbrace{G_{pragmatic}}_{\text{Survival}} + \underbrace{G_{epistemic}}_{\text{Curiosity}} + \underbrace{G_{social}}_{\text{Swarm}} + \underbrace{G_{memory}}_{\text{Experience}} $$

3. Associative Thermal Memory ($G_{memory}$)

The agent builds a non-parametric map of its homeostatic successes. A Sum-KDE (Kernel Density Estimation) is used to favor frequent feeding contexts:

$$ V_{hat}(T) = \sum_{k} r_k \cdot e^{-\frac{(T - T_k)^2}{2\sigma^2}} $$ $$ G_{memory}(a) = -\alpha \cdot V_{hat}(T_{pred}^{(a)}) $$

4. Affect & Precision ($\beta$)

Affect (Mood) is the rate of change of the error. This integrated valence determines the agent's Precision ($\beta$).

$$\beta_t = -\frac{H_t - H_{t-1}}{\Delta t}$$

  • Positive Affect: Error is decreasing ($H_t < H_{t-1}$), leading to a High $\beta$ (decisive behavior).
  • Negative Affect: Error is increasing ($H_t > H_{t-1}$), leading to a Low $\beta$ (volatile or exploratory behavior).

4. Action Selection (Softmax)

The agent selects its next move stochastically using a Softmax function modulated by Precision ($\beta$):

$$ P(a) = \frac{e^{\beta \cdot G(a)}}{\sum_{i} e^{\beta \cdot G(a_i)}} $$


💻 Tech Stack

  • Python 3.11
  • MESA: Agent-Based Modeling framework.
  • NumPy: Vectorized field calculations.
  • Solara: Reactive web UI for visualization.
  • Matplotlib: Backend rendering for video generation.

🚀 Running the Simulation

  1. Clone the repository:

    git clone [https://github.com/ioanfesteu/multiagent_FEP.git](https://github.com/ioanfesteu/multiagent_FEP.git)
    cd multiagent_FEP
  2. Install dependencies:

    pip install mesa numpy matplotlib plotly solara
  3. Run the basic dashboard. Uses the matplotib library, good for starting out.

    solara run multiagent_FEP.py
  4. Run the interactive dashboard. Uses Plotly library. You can select individual agents and watch their parameters change. Select individual agents from dropdown menu or by pausing simulation, selecting agent by clicking on, and then again clicking on the play button.

    solara run multiagent_FEP_i.py
  5. Make a video of the whole simulation: by uncommenting the last line of code in multiagent_FEP.py. For every simulation step a snapshot will be saved on disk. After simulation is done make a video of the whole simulation with ffmpeg.

    python multiagent_FEP.py
    
    ffmpeg -framerate 10 -i frames/frame_%04d.png -c:v libx264 -pix_fmt yuv420p swarm_simulation.mp4

⚙️ Tweaking the Simulation

Tweaks can be made in agents.py.

At the begining of agents.py you will find all the values you can play with and are pretty explanatory I hope.

Special atention should be payed for eta, mu_affect and sigma. You can find all the explanations in HOWTO.md.


📖 Want to learn more?

The main reason I started this project is because I wanted to learn about FEP and active inference. At first it seemed very intimidating to me especially when I was presented with the mathematical framework used by Friston. Then I said that there must be easier ways to understand this paradigm. Reading Mark Solms' book, "The Hidden Spring", helped me enormously to understand concepts that were unfamiliar to me. Therefore, I created some documents to help the curious reader better understand the philosophical and technical foundations of this project.

Start with INSIGHTS.md in /docs folder. Good luck!


📜 License

CODE IS LAW. This project is open for research and educational purposes.

Releases

Packages

Used by

Contributors

Languages