"What I cannot create, I do not understand." — Richard Feynman
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.
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.
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 (
(White = Optimal, Blue = Cold, Red = Hot, Brown = Hungry)
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 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 (
The agent strives to maintain its internal variables (Temperature
Before moving, an agent simulates all possible adjacent steps and calculates the Expected Free Energy (
The agent builds a non-parametric map of its homeostatic successes. A Sum-KDE (Kernel Density Estimation) is used to favor frequent feeding contexts:
Affect (Mood) is the rate of change of the error. This integrated valence determines the agent's Precision (
-
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).
The agent selects its next move stochastically using a Softmax function modulated by Precision (
- Python 3.11
- MESA: Agent-Based Modeling framework.
- NumPy: Vectorized field calculations.
- Solara: Reactive web UI for visualization.
- Matplotlib: Backend rendering for video generation.
-
Clone the repository:
git clone [https://github.com/ioanfesteu/multiagent_FEP.git](https://github.com/ioanfesteu/multiagent_FEP.git) cd multiagent_FEP -
Install dependencies:
pip install mesa numpy matplotlib plotly solara
-
Run the basic dashboard. Uses the matplotib library, good for starting out.
solara run multiagent_FEP.py
-
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
-
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
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.
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!
CODE IS LAW. This project is open for research and educational purposes.
