feat: 添加观点传播系统
- 新增 Stance 数据结构 (optimism/fear) - 情绪影响 stance (happy→乐观, anxious→恐惧) - 实现 apply_social_influence 社交影响函数 - 确定性随机选择接触对象 - 单次变化限制 ±0.1 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
"""角色事件评论系统 - 基于规则生成观点"""
|
||||
from typing import Dict, List, Optional
|
||||
from .models import WorldState, AgentState, Opinion, Emotion, WorldEffect
|
||||
from .models import WorldState, AgentState, Opinion, Emotion, WorldEffect, Stance
|
||||
|
||||
# 情绪对 stance 的影响
|
||||
EMOTION_STANCE_EFFECTS: Dict[str, Dict[str, float]] = {
|
||||
"happy": {"optimism": 0.1, "fear": -0.05},
|
||||
"calm": {"optimism": 0.0, "fear": 0.0},
|
||||
"anxious": {"optimism": -0.05, "fear": 0.1},
|
||||
}
|
||||
|
||||
|
||||
# 观点模板:effect_type -> emotion -> 观点列表
|
||||
@@ -123,6 +130,20 @@ def get_opinion_text(
|
||||
return templates[index]
|
||||
|
||||
|
||||
def update_stance_from_emotion(agent: AgentState) -> None:
|
||||
"""根据情绪更新 stance(单次变化不超过 ±0.1)"""
|
||||
emotion_key = agent.emotion.value
|
||||
effects = EMOTION_STANCE_EFFECTS.get(emotion_key, {})
|
||||
|
||||
# 更新 optimism
|
||||
new_optimism = agent.stance.optimism + effects.get("optimism", 0.0)
|
||||
agent.stance.optimism = max(0.0, min(1.0, new_optimism))
|
||||
|
||||
# 更新 fear
|
||||
new_fear = agent.stance.fear + effects.get("fear", 0.0)
|
||||
agent.stance.fear = max(0.0, min(1.0, new_fear))
|
||||
|
||||
|
||||
def generate_opinions(state: WorldState) -> None:
|
||||
"""为所有 agent 生成对当前活跃事件的观点"""
|
||||
# 无活跃效果时,清空 opinion
|
||||
@@ -155,6 +176,9 @@ def generate_opinions(state: WorldState) -> None:
|
||||
tick=state.tick
|
||||
)
|
||||
|
||||
# 根据情绪更新 stance
|
||||
update_stance_from_emotion(agent)
|
||||
|
||||
# 记录到 memory
|
||||
memory_entry = f"[{agent_id}对{active_effect.name}的看法] {text}"
|
||||
agent.memory.append(memory_entry)
|
||||
|
||||
Reference in New Issue
Block a user