Skip to content

feat: add injectable Python behavior strategies for Organism#22

Merged
YJack0000 merged 1 commit into
mainfrom
feat/python-behavior-system
Feb 24, 2026
Merged

feat: add injectable Python behavior strategies for Organism#22
YJack0000 merged 1 commit into
mainfrom
feat/python-behavior-system

Conversation

@YJack0000

Copy link
Copy Markdown
Owner

Summary

  • Add ReactionStrategy and InteractionStrategy as injectable std::function types on Organism
  • Python users can now fully customize how organisms behave without touching C++ code
  • Strategies are inherited by offspring via reproduce()
  • Default behavior unchanged when no custom strategy is set

Usage Example (Python)

from simevopy import Organism, Genes, Food

# Custom reaction: flee from everything
def flee_strategy(organism, nearby_objects):
    if not nearby_objects:
        return (0.0, 0.0)
    my_pos = organism.get_position()
    obj_pos = nearby_objects[0].get_position()
    return (my_pos[0] - obj_pos[0], my_pos[1] - obj_pos[1])

# Custom interaction: pacifist (only eat food, never kill)
def pacifist_strategy(organism, nearby_objects):
    for obj in nearby_objects:
        if hasattr(obj, 'can_be_eaten') and obj.can_be_eaten():
            organism.add_life_span(500)

org = Organism(Genes(chr(40) * 4))
org.set_reaction_strategy(flee_strategy)
org.set_interaction_strategy(pacifist_strategy)

New Python API

Method Description
organism.set_reaction_strategy(fn) Set custom reaction to nearby objects. fn(organism, objects) -> (dx, dy)
organism.set_interaction_strategy(fn) Set custom interaction logic. fn(organism, objects) -> None
organism.add_life_span(amount) Modify lifespan from custom strategies

Discussion Points

  • handleInteractionshandleReactions 改為 single-threaded 以避免 Python callback 的 GIL deadlock。未來如果要恢復多線程,需要在 C++ 端做 GIL release/acquire 管理
  • 目前 nearby_objects 包含所有 EnvironmentObject(Food + Organism),Python 端需要用 hasattrisinstance 判斷類型。考慮是否要提供 typed accessor 來簡化
  • Strategies 是 per-organism 的。如果想要 per-species 共用,可以在 Python 端用同一個 function reference

Test plan

  • All 3 existing Python tests pass
  • Custom Python strategy smoke test passes
  • Test strategy inheritance through reproduce()
  • Test mixed default/custom organisms in same environment

🤖 Generated with Claude Code

@YJack0000
YJack0000 force-pushed the feat/python-behavior-system branch from 7461285 to 22634bf Compare February 24, 2026 18:59
Add ReactionStrategy and InteractionStrategy callbacks that allow
Python callers to override organism behavior without modifying C++.
Strategies are propagated to offspring via reproduce().

Multi-threading in handleReactions is preserved when all organisms use
default C++ strategies; falls back to single-threaded when any organism
has a custom strategy to avoid GIL deadlocks.

Includes custom_behavior.py example demonstrating herbivore/predator
species with distinct Python-defined behaviors.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@YJack0000
YJack0000 force-pushed the feat/python-behavior-system branch from 22634bf to 25f518a Compare February 24, 2026 19:11
@YJack0000
YJack0000 merged commit f99fd07 into main Feb 24, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant