A lightweight emotional engine simulating emotional state transitions in an AI-like agent using vector dynamics, short-term memory, and personality thresholds.
Follow the link for the simplified Medium Blog👇
- Works on 5 basic emotions: Joy, Angry, Sad, Fear, Trust (JASFT)
- Simulates emotional reactions based on event inputs
- Incorporates emotional memory and personality thresholds
- Visualizes if/when emotions become "visible" (i.e., surpass personality thresholds)
- Models how emotions influence each other via a relationship map
The emotional state of the agent is represented as a 5D vector in the JASFT format. Each value is between 0 (inactive) and 1 (fully activated).
available_emotions = ["Joy", "Angry", "Sad", "Fear", "Trust"]Each emotion has a threshold, called personality bias, which determines when that emotion becomes visible or is expressed outwardly.
personality_bias = np.array([0.3, 0.7, 0.5, 0.4, 0.7])This key component models how one dominant emotion can influence the others. A high Joy, for instance, can slightly increase Trust but suppress Sadness or Fear.
relationship_map = {
"Joy": [ 1.0, -0.5, -0.4, -0.3, 0.6],
"Angry": [-0.6, 1.0, 0.5, 0.7, -0.4],
"Sad": [-0.4, 0.6, 1.0, 0.6, -0.5],
"Fear": [-0.3, 0.6, 0.5, 1.0, -0.6],
"Trust": [ 0.7, -0.4, -0.4, -0.3, 1.0],
}- Initialize memory to keep track of recent emotional states (max 3).
- Generate an event (a JASFT vector).
- Find the dominant emotion in the event.
- Apply its influence on the other emotions using the relationship map.
- Blend it with the average of the emotional memory.
- Clip the result to stay between 0 and 1.
- Compare each value with its personality bias.
- Print emotions that surpass the threshold (i.e., expressed emotions).
- Update memory, rolling out older states.
- Python 3.x
- NumPy installed (
pip install numpy) - Any IDE (VSCode, PyCharm, etc.) or a Jupyter Notebook
- Open your Python environment (Jupyter, VSCode, or terminal).
- Copy-paste the full code into a new Python file or notebook.
- Run the script. It will simulate 10 emotional updates.
- You'll be prompted to press enter to move through each step.
- Emotion vectors and visible emotions will be printed in each iteration.
Inside the script, you'll find two versions of the event line:
event = np.round(np.random.uniform(0, 1, size=5), 1) # Random event
event = np.round(np.array([0.8, 0.2, 0.1, 0.3, 0.5]), 1) # Custom eventYou can switch between random or custom input just by commenting/uncommenting these lines.
- Want random input every time? → Keep the first, comment the second.
- Want to test a fixed emotion? → Comment the first, edit the second.
Iteration number: 1
Joy
Trust
Final: [0.8 0. 0. 0. 0.5]
Personality: [0.3 0.7 0.5 0.4 0.7]
[0.8 0. 0. 0. 0.5]
This simulation helps explore:
- How emotions affect one another dynamically
- How emotional expression can be threshold-dependent
- How short-term memory influences emotional state
- A foundational step toward emotional AI or NPC design
- Add decay to simulate emotion fading over time
- Add emotional history for long-term trends
- Integrate with NLP inputs (e.g., emotion extraction from text)
- Build agents that evolve their personality with feedback
- Visualize emotion shifts with Matplotlib
This is Version 1 — intentionally simple and interpretable.
You’re encouraged to tweak weights, personality, and input to experiment with emergent behaviors.
The code is readable, testable, and made to inspire experimentation.