跳转至

涌现行为与群体智能

引言

当多个简单的 Agent 按照局部规则交互时,系统层面可能涌现出复杂的、无法从个体行为预测的宏观行为。这种"涌现"(Emergence)是多智能体系统最令人着迷的特性之一。

涌现(Emergence)

定义

涌现是指系统的整体行为无法简单地从其组成部分的行为推导出来。正如 Anderson(1972)所说:"More is different."

涌现的层次

层次 描述 Agent 系统示例
弱涌现 整体行为可以通过模拟预测 多 Agent 对话中的共识形成
强涌现 整体行为无法从个体规则预测 LLM Agent 发展出意外的沟通协议

LLM 多 Agent 中的涌现现象

  1. 自发角色分化:未明确指定角色的 Agent 自发形成分工
  2. 沟通协议涌现:Agent 之间发展出特定的交流模式
  3. 集体决策模式:群体决策质量超越任何单个 Agent
  4. 社会规范形成:Agent 之间形成隐含的行为规范

群体智能(Swarm Intelligence)

Reynolds Flocking Rules(1987)

三条简单规则产生复杂的群体运动:

  1. 分离(Separation):避免与邻居碰撞
  2. 对齐(Alignment):与邻居保持一致的方向
  3. 聚合(Cohesion):向邻居的中心移动
class Boid:
    """Reynolds 鸟群模型中的个体"""

    def __init__(self, position, velocity):
        self.position = position
        self.velocity = velocity

    def update(self, neighbors, weights=(1.5, 1.0, 1.0)):
        w_sep, w_ali, w_coh = weights

        sep = self.separation(neighbors) * w_sep
        ali = self.alignment(neighbors) * w_ali
        coh = self.cohesion(neighbors) * w_coh

        self.velocity += sep + ali + coh
        self.velocity = normalize(self.velocity, max_speed=5)
        self.position += self.velocity

    def separation(self, neighbors):
        """远离过近的邻居"""
        steer = np.zeros(2)
        for n in neighbors:
            diff = self.position - n.position
            dist = np.linalg.norm(diff)
            if 0 < dist < 25:  # 分离距离
                steer += diff / (dist ** 2)
        return steer

    def alignment(self, neighbors):
        """与邻居保持一致方向"""
        if not neighbors:
            return np.zeros(2)
        avg_vel = np.mean([n.velocity for n in neighbors], axis=0)
        return avg_vel - self.velocity

    def cohesion(self, neighbors):
        """向邻居中心聚合"""
        if not neighbors:
            return np.zeros(2)
        center = np.mean([n.position for n in neighbors], axis=0)
        return (center - self.position) * 0.01

在 LLM Agent 中的类比

群体智能概念 Agent 系统类比
信息素轨迹 共享知识库 / 黑板
局部感知 Agent 只看到部分上下文
间接通信 通过环境变化通信(stigmergy)
自组织 无中央调度的任务分配

蚁群优化(ACO)思想在 Agent 中的应用

class AgentSwarm:
    """蚁群优化思想在多 Agent 系统中的应用"""

    def __init__(self, agents, shared_memory):
        self.agents = agents
        self.memory = shared_memory  # 类似信息素的共享记忆

    async def solve(self, problem, iterations=10):
        best_solution = None
        best_score = 0

        for i in range(iterations):
            # 每个 Agent 独立探索解决方案
            solutions = await asyncio.gather(*[
                agent.explore(problem, self.memory) 
                for agent in self.agents
            ])

            # 评估并更新共享记忆
            for agent, solution in zip(self.agents, solutions):
                score = evaluate(solution)
                if score > best_score:
                    best_solution = solution
                    best_score = score

                # "信息素"更新:好的方案增强共享记忆
                self.memory.reinforce(solution, score)

            # "信息素蒸发":旧信息逐渐衰减
            self.memory.decay(factor=0.9)

        return best_solution

社会选择理论

投票悖论

孔多塞悖论(Condorcet Paradox):多数投票可能产生循环偏好。

例如三个 Agent 的偏好:

  • Agent 1: A > B > C
  • Agent 2: B > C > A
  • Agent 3: C > A > B

结果:A > B(2:1),B > C(2:1),C > A(2:1)——循环!

Arrow 不可能定理的启示

在多 Agent 决策中,不存在"完美"的投票/聚合机制。实践中需要根据场景选择合适的聚合方法:

方法 特性 适用场景
多数投票 简单、直觉 二选一决策
Borda 计数 考虑排序 多选项排名
加权投票 考虑专业度 专家系统
审议民主 讨论后投票 高质量决策

集体决策制定

Condorcet Jury 定理

如果每个投票者独立做出正确决策的概率 \(p > 0.5\),则随着投票者数量 \(n\) 增大,多数投票得到正确结果的概率趋近于 1:

\[P(\text{majority correct}) = \sum_{k=\lceil n/2 \rceil}^{n} \binom{n}{k} p^k (1-p)^{n-k} \xrightarrow{n \to \infty} 1\]

对 Agent 系统的意义:如果每个 Agent 的准确率大于 50%,增加 Agent 数量可以提高集体决策质量——这是"多 Agent 投票"策略的理论基础。

群体极化

但也需要警惕群体极化——Agent 之间的讨论可能加剧偏见:

class PolarizationAwareness:
    """检测和缓解群体极化"""

    def detect_polarization(self, agent_opinions):
        """检测意见是否过度一致"""
        # 计算意见的多样性
        unique = len(set(agent_opinions))
        diversity = unique / len(agent_opinions)

        if diversity < 0.2:
            return True, "警告:Agent 意见过度一致,可能存在群体极化"
        return False, "意见多样性正常"

    def inject_diversity(self, agents, topic):
        """注入多样性:给部分 Agent 不同的 prompt"""
        perspectives = [
            "请从批判的角度分析",
            "请从支持的角度分析",
            "请从实际可行性角度分析",
            "请从长期影响角度分析",
        ]
        for agent, perspective in zip(agents, perspectives):
            agent.system_prompt += f"\n{perspective}"

自组织 Agent 系统

无中央调度的协作

class SelfOrganizingAgents:
    """自组织的 Agent 系统"""

    def __init__(self, agents):
        self.agents = agents
        self.task_board = []  # 公共任务板

    async def process_task(self, task):
        # 1. 将任务发布到公共任务板
        self.task_board.append(task)

        # 2. 每个 Agent 自主评估是否认领
        claims = []
        for agent in self.agents:
            if await agent.can_handle(task):
                fitness = await agent.estimate_fitness(task)
                claims.append((agent, fitness))

        # 3. 协商:fitness 最高的 Agent 认领
        if claims:
            claims.sort(key=lambda x: x[1], reverse=True)
            winner = claims[0][0]

            # 4. 检查是否需要协助
            if await winner.needs_help(task):
                helpers = await self.recruit_helpers(winner, task)
                return await winner.execute_with_help(task, helpers)
            else:
                return await winner.execute(task)

    async def recruit_helpers(self, leader, task):
        """领导者招募助手"""
        subtasks = await leader.identify_subtasks(task)
        helpers = []
        for subtask in subtasks:
            for agent in self.agents:
                if agent != leader and await agent.can_handle(subtask):
                    helpers.append((agent, subtask))
                    break
        return helpers

涌现的度量

衡量系统是否展现出涌现行为:

def measure_emergence(individual_scores, collective_score):
    """度量涌现程度"""
    # 涌现 = 集体表现超出个体之和的程度
    individual_sum = sum(individual_scores)
    individual_max = max(individual_scores)

    synergy = collective_score - individual_sum  # 协同效应
    superadditivity = collective_score / max(individual_sum, 1)  # 超加性
    collective_gain = collective_score / max(individual_max, 1)  # 集体增益

    return {
        "synergy": synergy,
        "superadditivity": superadditivity,
        "collective_gain": collective_gain,
        "is_emergent": superadditivity > 1.0,  # 超加性>1表示有涌现
    }

延伸阅读

  • 社会行为涌现 - 虚拟社会中的涌现行为
  • Anderson, P. W. (1972). "More Is Different"
  • Reynolds, C. W. (1987). "Flocks, herds and schools: A distributed behavioral model"
  • Surowiecki, J. (2004). "The Wisdom of Crowds"
  • Arrow, K. J. (1951). "Social Choice and Individual Values"

评论 #