Skip to content

Neural Network

Muhammed Zafar edited this page Jul 7, 2025 · 4 revisions

Neural Network

The Neural Network in TheLifeOfPy determines how creatures process sensory input and choose actions. Each sensor and its connections function as a forest of trees, where each tree independently processes inputs.

The actuators with values equal to the highest activation in a tree are selected, and their corresponding actions are performed.


Forward Propagation Algorithm

Below is a simplified algorithm for processing sensory input and selecting actions:

Input:

  • A list of input values corresponding to sensor nodes.
  • Predefined sets of:
    • Nodes categorized as sensor, bias, hidden, and actuator.
    • Connections between nodes with associated weights and enabled/disabled states.
    • Sorted node list indicating the order of computation.
    • Grouped actuator nodes for activation resolution.

Algorithm:

  1. Initialize Activations:

    • Set each sensor node’s activation to its corresponding input value.
    • Set each bias node’s activation to 1.
    • Set all other nodes’ activations to 0.
  2. Propagate Activations:
    For each node in sorted order:

    • Skip if it is a sensor or bias node.
    • Initialize value as 0.
    • For each input connection to this node:
      • If the connection is enabled:
        • Add the product of the input node’s activation and the connection weight to value.
    • Store value as this node's activation.
  3. Apply Activation Function (Winner-Takes-All):
    For each group of nodes:

    • Identify the actuator nodes in the group.
    • If none exist, skip.
    • Find the maximum activation value among them.
      • If the maximum value is negative:
        • Set all activations in the group to 0.
      • Otherwise:
        • Set the activation of nodes with the maximum value to 1, and all others to 0.
  4. Return Activated Actuators:

    • Return the list of actuator nodes whose activation value is 1.

Sensors & Actuators

Sensors (Inputs)

Sensors provide environmental data to the neural network, influencing decision-making. If an actuator is marked (if visible), the relevant proximity sensor must be selected first, as it stores context for actuator calculations.

Random & Environmental Sensors

Code Description Output Range
RNs Random noise generator -1 (Low) → 1 (High)
FDi Distance to nearest plant shrub -1 (On it) → 1 (Farthest)
FAm Density of plant shrubs nearby -1 (None) → 1 (More than 10)

Proximity & Density Sensors

Code Description Output Range
ADi Distance to nearest same-species critter -1 (On it) → 1 (Farthest)
ODi Distance to nearest other-species critter -1 (On it) → 1 (Farthest)
CDi Distance to nearest any-species critter -1 (On it) → 1 (Farthest)
AAm Density of same-species critters nearby -1 (None) → 1 (More than 10)
OAm Density of other-species critters nearby -1 (None) → 1 (More than 10)
CAm Density of any-species critters nearby -1 (None) → 1 (More than 10)

Interaction & Context Sensors

Code Description Output Range
MsD Distance to mouse pointer (if visible) -1 (On it) → 1 (Farthest)
MSa Whether mating signal was accepted -1 (No) → 1 (Yes)

Self-State Sensors

Code Description Output Range
CEn Current energy level -1 (Empty) → 1 (Full)
CAg Current age -1 (Newborn) → 1 (Old)
CFi Current fitness vs average -1 (Low) → 1 (High)
RSt Reproduction state -1 (Not ready) → 0 (Ready) → 1 (Mating)
DSt Defense state -1 (Not activated) → 1 (Activated)

Actuators (Outputs)

Actuators determine how the critter responds to its environment. If an actuator is marked (if visible), the relevant proximity sensor must be selected first, as it stores context for actuator calculations.

Movement Actions

Code Action
Mv Move in a random direction
MvS Move towards same-species critter (if visible)
MvO Move towards other-species critter (if visible)
MvA Move towards any-species critter (if visible)
MvF Move towards nearest food source (if visible)
MvM Move towards mouse pointer (if visible)

Defensive Actions

Code Action
ADe Activate defense mechanism
DDe Deactivate defense mechanism

Avoidance Actions

Code Action
AvS Move away from same-species critter (if visible)
AvO Move away from other-species critter (if visible)
AvA Move away from any-species critter (if visible)
AvF Move away from nearest food source (if visible)
AvM Move away from mouse pointer (if visible)

Other Actions

Code Action
Eat Consume food if available
SMS Send a mating signal to a nearby same-species critter (if visible)
Mte Attempt to mate if a potential mate is found

Example Neural Networks

Example 1: Basic Food Detection

Description:
If food is nearby, the creature eats food; otherwise, it wanders.

Weights:

Sensor Weight Actuator
FDi 1 Mv
FDi -1 Eat

Example 2: Interactive Feeding

Description:
If the mouse pointer is near, the creature moves towards it; if food is nearby, the creature eats the food.

Weights:

Sensor Weight Actuator
MsD -1 MvM
FDi -1 Eat

Example 3: Hostile interaction

Description:
If an organism of another species is nearby, the creature moves towards it and activates its defense mechanism. This behavior is designed for entities like Swordlings to attack other organisms. If no other species is near, it will wander and deactivate its defense mechanism.

Weights:

Sensor Weight Actuator
ODi -1 MvO
ODi -1 ADe
ODi 1 DDe
ODi 1 Mv