Skip to content

KlepAgent

Roll4d4 edited this page Feb 23, 2025 · 1 revision

๐Ÿง  KLEPAgent: The Decision-Making Brain of the Neuron

The KLEPAgent is the core decision-maker within the KLEP system. It is responsible for evaluating available executables, selecting the most appropriate action, and managing the execution cycle of KLEP behaviors.


๐Ÿšฆ What Does the KLEPAgent Do?

The KLEPAgent serves as the AI controller, performing the following key functions:

  1. Initialization:

    • Binds to a parent neuron and registers necessary events.
    • Prepares the agent to react to key changes and trigger behavior evaluations.
  2. Update Management:

    • Handles standard Unity update cycles (Update and FixedUpdate).
    • Manages in-tandem and solo executables, ensuring appropriate execution order.
  3. Behavior Selection:

    • Evaluates all available executables and selects the best option based on validation and execution checks.
  4. Execution Control:

    • Executes in-tandem behaviors as background tasks.
    • Prioritizes and executes a single solo action if available and valid.

๐Ÿ” Key Components of KLEPAgent:

๐Ÿง  1. Initialization: Setting Up the Agent

public void Initialize(KLEPNeuron neuron)
{
    parentNeuron = neuron;
    parentNeuron.bridge.RegisterEvent("KeyAdded", OnKeyAddedEvent);
}
  • Registers events with the SLASHBridge, enabling the agent to react to key changes.
  • Prepares for dynamic behavior changes by rechecking in-tandem executables when new keys are added.

๐Ÿšฆ 2. Handling Update Cycles:

๐Ÿ”„ AgentUpdate:

public void AgentUpdate()
{
    if (parentNeuron == null) return;

    var currentKeys = parentNeuron.heldKeys;
    var allExecutables = parentNeuron.heldExecutables;

    var inTandemExecutables = allExecutables.Where(exe => !exe.IsManagedByGoal && exe.InTandem).ToList();
    var soloExecutables = allExecutables.Where(exe => !exe.InTandem && !exe.IsManagedByGoal).ToList();

    foreach (var exe in inTandemExecutables)
    {
        if (exe.parentNeuron == null || !exe.gameObject.activeInHierarchy) continue;
        exe.ExecutableUpdates();
    }

    ExecuteInTandemExecutables(inTandemExecutables);
    DecideAndExecuteSoloAction(soloExecutables, currentKeys);
}
  • Executes in-tandem behaviors immediately if conditions are met.
  • Selects and executes a single solo action based on validation and execution checks.

โฒ๏ธ 3. FixedUpdate Cycle:

public void AgentFixedUpdate(HashSet<KLEPKey> fixedKeys)
{
    if (parentNeuron == null) return;

    var allExecutables = parentNeuron.heldExecutables;
    var inTandemExecutables = allExecutables.Where(exe => !exe.IsManagedByGoal && exe.InTandem).ToList();
    var soloExecutables = allExecutables.Where(exe => !exe.InTandem && !exe.IsManagedByGoal).ToList();

    foreach (var exe in inTandemExecutables)
    {
        if (exe.parentNeuron == null || !exe.gameObject.activeSelf) continue;
        exe.ExecutableFixedUpdate();
    }

    ExecuteInTandemExecutablesFixed(inTandemExecutables, fixedKeys);
    DecideAndExecuteSoloActionFixed(soloExecutables, fixedKeys);
}
  • Handles physics-related execution during FixedUpdate.
  • Ensures timing-sensitive behaviors are synchronized with Unity's physics engine.

๐Ÿง  4. In-Tandem Execution:

private void ExecuteInTandemExecutables(List<KLEPExecutableBase> inTandemExecutables)
{
    recheckInTandemExecutables = true;
    int val = 0;

    while (recheckInTandemExecutables)
    {
        recheckInTandemExecutables = false;
        if (val > inTandemRecheckLimit) break;

        foreach (var executable in inTandemExecutables.ToList())
        {
            if (executable.parentNeuron == null || !executable.gameObject.activeInHierarchy)
                continue;

            if (executable.CanValidate(parentNeuron.heldKeys) && executable.CanExecute(parentNeuron.heldKeys))
            {
                executable.Execute();
                recheckInTandemExecutables = true;
            }
        }

        if (inTandemExecutables.Count == 0) recheckInTandemExecutables = false;
        val++;
    }
}
  • In-tandem executables are executed immediately when conditions are met.
  • The system rechecks in-tandem actions if new keys are generated, allowing for dynamic reactions within a single frame.

๐ŸŽฏ 5. Selecting the Best Solo Action:

private KLEPExecutableBase SelectBestAction(HashSet<KLEPKey> currentElements, List<KLEPExecutableBase> soloActions)
{
    float highestValue = float.MinValue;
    KLEPExecutableBase selectedAction = null;

    foreach (var action in soloActions)
    {
        if (!action.gameObject.activeInHierarchy) continue;

        float attractionValue = action.CalculateAttraction(currentElements);
        float combinedValue = attractionValue;

        if (combinedValue > highestValue)
        {
            highestValue = combinedValue;
            selectedAction = action;
        }
    }

    return selectedAction;
}
  • Evaluates all solo actions and selects the most attractive option.
  • Attraction values are calculated based on keys present and lock conditions, enabling smart prioritization.

โœ… Key Takeaways:

  • In-Tandem vs. Solo Execution:

    • In-tandem executables: Always fire if valid and executable.
    • Solo executables: Undergo a selection process, with only one action firing per frame.
  • Reactivity to Key Changes:

    • Listens for key additions and rechecks behavior options dynamically.
  • Execution Layers:

    • Update cycle: For real-time behaviors.
    • FixedUpdate cycle: For physics-based actions.
  • Smart Decision-Making:

    • Uses validation and execution checks to prioritize actions.
    • Supports adaptive AI behavior by balancing desirability and feasibility.

๐ŸŽฎ Practical Example:

Scenario: A Zombie AI in a survival game:

  1. In-Tandem Executables:

    • Always checks for the player's presence using a sensor.
    • Updates patrol behavior if no target is found.
  2. Solo Executables:

    • Evaluates whether to attack, hide, or search for resources.
    • Only the most attractive behavior is executed per frame, preventing erratic behavior.

This explanation covers the KLEPAgent's role, its execution flow, and key mechanisms for behavior selection. If you need further focus on specific features, like goal handling or event-driven actions, let me know! ๐Ÿ˜Š

KLEP Wiki Navigation

๐Ÿš€ Getting Started

๐Ÿง  Core Concepts

๐ŸŽฎ Examples and Tutorials

๐Ÿ› ๏ธ Scripts and Components

๐Ÿ’ก Experimental/Project Projections

๐Ÿงฌ Contributing

Clone this wiki locally