Skip to content

SlashMemory

Roll4d4 edited this page Feb 23, 2025 · 1 revision

๐Ÿง  SLASHMemory: A Memory System for KLEP AI Agents

The SLASHMemory class is an experimental module for KLEP, introducing a memory system to AI agents. It allows agents to remember experiences, evaluate key importance, and transfer significant experiences to long-term memory. This feature is designed to support adaptive behavior by leveraging past experiences.


๐Ÿšฆ What Does SLASHMemory Do?

  1. Short-Term Memory Management:

    • Stores active experiences with heat values, indicating recent importance.
    • Cools down over time, simulating natural memory decay.
  2. Long-Term Memory Storage:

    • Transfers highly significant experiences to long-term memory.
    • Maintains a fixed capacity, automatically discarding the oldest memories.
  3. Memory Scoring System:

    • Evaluates key importance using positive and negative scores.
    • Aids decision-making by influencing the desirability of actions.
  4. Emotional Integration:

    • Can interface with the KLEPEmotion system to create emotionally charged memories.

๐Ÿ” Core Components of SLASHMemory:


๐Ÿ“š 1. Memory Data Structures:

private Dictionary<string, (float heat, float positiveScore, float negativeScore)> shortTermMemory = new Dictionary<string, (float, float, float)>();
private List<HashSet<string>> longTermMemory;
  • Short-Term Memory (Dictionary<string, (float heat, float positiveScore, float negativeScore)>):

    • Stores active keys with associated metadata, including:
      • Heat: Indicates recency and importance.
      • Positive & Negative Scores: Tracks success or failure linked to keys.
  • Long-Term Memory (List<HashSet<string>>):

    • Holds significant snapshots of key states, typically triggered by important events.
    • Snapshots are represented as sets of key names, allowing contextual recall.

๐Ÿง  2. Memory Initialization & Setup (Initialize)

public void Initialize(KLEPNeuron neuron)
{
    currentNeuron = neuron;        
}
  • Associates the memory system with a specific KLEPNeuron.
  • Prepares the system to store and evaluate keys based on the neuron's experiences.

๐Ÿ”ฅ 3. Memory Heat & Scoring (AddOrUpdateKey)

public void AddOrUpdateKey(string keyName, bool success)
{
    if (!shortTermMemory.ContainsKey(keyName))
    {
        shortTermMemory[keyName] = (1.0f, 0f, 0f); // Initial heat, positive, and negative scores
    }

    var (heat, positiveScore, negativeScore) = shortTermMemory[keyName];
    heat = 1.0f; // Reset heat to max
    positiveScore += success ? 1 : 0;
    negativeScore += success ? 0 : 1;
    shortTermMemory[keyName] = (heat, positiveScore, negativeScore);

    EvaluateForKeyTransfer(keyName);
    OnMemoryUpdated?.Invoke(keyName);
}
  • Manages key heat:

    • Heat represents recency, resetting to max on key reactivation.
    • Higher heat values make keys candidates for long-term memory transfer.
  • Score Management:

    • Increments positive/negative scores based on success parameter.
    • Allows KLEP agents to evaluate the historical reliability of keys.

๐ŸงŠ 4. Memory Decay (CoolDownKeys)

private void CoolDownKeys()
{
    foreach (var key in shortTermMemory.Keys.ToList())
    {
        var (heat, positiveScore, negativeScore) = shortTermMemory[key];
        heat -= coolDownRate * Time.deltaTime;
        if (heat <= 0)
        {
            shortTermMemory.Remove(key);
            OnMemoryUpdated?.Invoke(key); // Notify removal
        }
        else
        {
            shortTermMemory[key] = (heat, positiveScore, negativeScore);
        }
    }
}
  • Gradually reduces heat, simulating natural memory decay.
  • Removes keys when heat reaches zero, cleaning up short-term memory.
  • Emulates forgetfulness, ensuring only relevant keys persist.

๐Ÿ“ฆ 5. Long-Term Memory Transfer (TransferToLongTermMemory)

private void TransferToLongTermMemory()
{
    HashSet<string> snapshot = new HashSet<string>(shortTermMemory.Keys);

    if (longTermMemory.Count >= longTermMemoryCapacity)
    {
        longTermMemory.RemoveAt(0); // Remove the oldest snapshot
    }

    longTermMemory.Add(snapshot);
    OnMemoryTransferredToLongTerm?.Invoke("Snapshot added");

    foreach (var key in snapshot)
    {
        shortTermMemory.Remove(key);
    }
}
  • Captures a snapshot of current short-term memory.
  • Adds significant experiences to long-term memory, enforcing capacity limits.
  • Cleans up short-term memory by removing transferred keys.

๐Ÿ“ธ 6. Traumatic Experience Capture (CaptureSnapshotForTraumaticExperience)

public void CaptureSnapshotForTraumaticExperience()
{
    HashSet<string> snapshot = new HashSet<string>(currentNeuron.heldElements.Select(key => key.Name));
    if (longTermMemory.Count >= longTermMemoryCapacity)
    {
        longTermMemory.RemoveAt(0);
    }
    longTermMemory.Add(snapshot);
}
  • Manually captures memory snapshots, ideal for traumatic or significant experiences.
  • Overrides normal thresholds, forcing memory storage.
  • Useful for critical events, such as near-death experiences or major successes.

๐ŸŽฎ Example Scenario: Memory in a Game:

๐Ÿค– Scenario: An NPC Learning From Combat:

  1. During combat, the SLASHMemory system tracks keys such as "Enemy_KEY", "LowHealth_KEY", and "FindCover_KEY".
  2. If the NPC survives, the keys are positively reinforced, increasing their heat and positive score.
  3. If the NPC fails, negative scores are increased, decreasing desirability for future use.
  4. Highly significant experiences, such as escaping with 1% health, are immediately transferred to long-term memory.
  5. In future encounters, the NPC might prioritize cover if similar keys are present, showing learned behavior.

โœ… Key Takeaways:

  • SLASHMemory introduces a memory model to KLEP AI, allowing agents to learn from experiences.
  • Heat values and positive/negative scores enable adaptive decision-making.
  • Short-term memory decay ensures only relevant experiences are retained, avoiding memory bloat.
  • Long-term memory captures significant events, enabling agents to adapt over extended periods.
  • This module is experimental, but illustrates the potential for AI agents to develop learned behaviors over time.

Would you like further integration of SLASHMemory with KLEPEmotion, goals, or reinforcement learning in KLEP? ๐Ÿ˜Š

KLEP Wiki Navigation

๐Ÿš€ Getting Started

๐Ÿง  Core Concepts

๐ŸŽฎ Examples and Tutorials

๐Ÿ› ๏ธ Scripts and Components

๐Ÿ’ก Experimental/Project Projections

๐Ÿงฌ Contributing

Clone this wiki locally