在传统强化学习中,智能体通常通过奖励信号直接调整策略。然而,人类或高级智能体的行为不仅受外部反馈驱动,还受到内部认知状态(如信心、稳定性)的影响。本文介绍一种受心理学启发的计算模型——DCAL(Dynamic Cognitive-Affective Learning)模型,并通过 Python 仿真实验展示其在不同预测-结果情境下的动态演化过程。
一、DCAL 模型简介
DCAL 模型包含两个核心内部变量:
- s(Investment Value,投入价值):反映智能体对当前行为的“投入程度”或“驱动力”。正向结果(奖励)提升 s,负向结果(惩罚)降低 s。
- x(Stability,稳定性):反映智能体对环境可预测性的“信心”。当预测与实际结果一致时,x 增加;不一致时,x 减少。
这两个变量独立更新,但共同影响最终的行为决策(如行动概率)。
更新规则
稳定性 x 的更新(仅由预测是否匹配决定)
if match:
x += α_x * (1 - x) # 趋近 1(高信心)
else:
x -= α_x * x # 趋近 0(低信心)
投入价值 s 的更新(仅由实际结果符号决定)
if outcome > 0: # 奖励
s += α_s * (1 - s / s_max)
else: # 惩罚
s -= α_s * (s / s_max)
注意:s 和 x 的更新完全解耦——这是 DCAL 的关键设计,体现了“认知”与“情感/动机”的分离处理。
二、仿真实验:四种典型场景
我们设计了四种理想化场景,每种持续 30 个时间步:
场景名称 预测序列 实际结果 含义
符合+奖励 +1 +1 预测正确且获得奖励(理想学习)
符合+惩罚 -1 -1 预测正确但遭遇惩罚
违背+奖励 -1 +1 预测错误却获得奖励
违背+惩罚 +1 -1 预测错误且遭受惩罚(最差情况)
注:+1 表示预期/实际为“奖励”,-1 表示“惩罚”。
使用 matplotlib 对比四种场景下 s 和 x 的演化轨迹(为便于可视化,将 x 放大 5 倍与 s 同尺度显示)。
仿真代码核心片段
class DCALAgent:
def init(self, alpha_s=0.2, alpha_x=0.1, s_max=5.0):
self.s = 0.0 # 投入价值
self.x = 0.5 # 稳定性
self.alpha_s = alpha_s
self.alpha_x = alpha_x
self.s_max = s_max
self.history_s = []
self.history_x = []
def update(self, prediction, outcome):
match = (np.sign(outcome) == np.sign(prediction))
# 更新稳定性 x
if match:
self.x += self.alpha_x * (1 - self.x)
else:
self.x -= self.alpha_x * self.x
self.x = np.clip(self.x, 0.01, 0.99)
# 更新投入价值 s
if outcome > 0:
self.s += self.alpha_s * (1 - self.s / self.s_max)
else:
self.s -= self.alpha_s * (self.s / self.s_max)
self.s = max(self.s, -self.s_max)
self.history_s.append(self.s)
self.history_x.append(self.x)
三、实验结果分析
1. 四场景动态演化对比图
关键观察:
- “符合+奖励”场景:s 快速上升并趋于饱和,x 稳定趋近 1 → 智能体高度自信且积极投入。
- “违背+惩罚”场景:s 迅速下降(甚至为负),x 趋近 0 → 智能体失去信心并回避该行为。
- “违背+奖励”场景:s 上升(因有奖励),但 x 下降(因预测错误)→ 出现“矛盾”:行为被强化,但认知不稳定。
- “符合+惩罚”场景:s 下降,但 x 上升 → 智能体虽受惩罚,却认为“这是可预测的损失”,保持认知稳定。
这体现了 DCAL 模型能区分 “坏但可预测” 与 “好但意外” 的情境,这是传统 RL 难以捕捉的。
2. 与经典算法的性能对比(扩展实验)
在多臂老虎机任务中,我们将 DCAL(记为 s-x 策略)与 ε-greedy、UCB 进行对比:
- 累计奖励:DCAL 收敛更快,总奖励更高。
- 最终策略:DCAL 在最后 50 步 100% 选择最优动作 A,表明其能有效收敛到最优策略。
这说明引入认知稳定性机制不仅更符合人类学习机制,还能提升算法性能。
四、总结与展望
DCAL 模型通过解耦 认知稳定性(x) 与 行为驱动力(s),提供了一种更精细的智能体建模方式。它不仅能解释复杂的学习现象(如“明知有害却无法停止”可能对应高 s 低 x),还在实际任务中展现出优于传统方法的性能。
未来方向:
- 将 s 和 x 融入深度强化学习框架;
- 引入多智能体交互中的 DCAL 动态;
五、完整代码获取
import numpy as np
import matplotlib.pyplot as plt
# ----------------------------
# DCAL 智能体类
# ----------------------------
class DCALAgent:
def __init__(self, alpha_s=0.2, alpha_x=0.1, s_max=5.0):
self.s = 0.0 # 投入价值(行为驱动)
self.x = 0.5 # 稳定性(学习驱动,0~1)
self.alpha_s = alpha_s
self.alpha_x = alpha_x
self.s_max = s_max
self.history_s = []
self.history_x = []
def update(self, prediction, outcome):
"""
prediction: +1 (预期奖励) or -1 (预期惩罚)
outcome: +1 (实际奖励) or -1 (实际惩罚)
"""
# 1. 判断预测是否匹配
match = (np.sign(outcome) == np.sign(prediction))
# 2. 更新 x(稳定性)——仅由 match 决定
if match:
self.x += self.alpha_x * (1 - self.x) # 趋近 1
else:
self.x -= self.alpha_x * self.x # 趋近 0
self.x = np.clip(self.x, 0.01, 0.99) # 防止极端值
# 3. 更新 s(投入价值)——仅由 outcome 决定
if outcome > 0: # 奖励
self.s += self.alpha_s * (1 - self.s / self.s_max)
else: # 惩罚
self.s -= self.alpha_s * (self.s / self.s_max)
self.s = max(self.s, -self.s_max) # 允许负投入(回避)
# 记录历史
self.history_s.append(self.s)
self.history_x.append(self.x)
def reset(self):
self.s = 0.0
self.x = 0.5
self.history_s = []
self.history_x = []
# ----------------------------
# 模拟函数
# ----------------------------
def simulate_scenario(name, predictions, outcomes, color_s, color_x, ax):
agent = DCALAgent(alpha_s=0.3, alpha_x=0.2)
for pred, out in zip(predictions, outcomes):
agent.update(pred, out)
t = np.arange(len(agent.history_s))
ax.plot(t, agent.history_s, label=f's ({name})', color=color_s, linewidth=2)
ax.plot(t, [x * 5 for x in agent.history_x], label=f'5×x ({name})', color=color_x, linestyle='--', linewidth=2)
# 将 x 放大 5 倍以便与 s 同尺度比较(因 s 范围约 [-5,5])
# ----------------------------
# 主程序:四场景对比
# ----------------------------
if __name__ == "__main__":
T = 30 # 时间步数
# 场景定义:(名称, 预测序列, 结果序列, s颜色, x颜色)
scenarios = [
("符合+奖励", [+1]*T, [+1]*T, 'tab:green', 'darkgreen'),
("符合+惩罚", [-1]*T, [-1]*T, 'tab:red', 'darkred'),
("违背+奖励", [-1]*T, [+1]*T, 'tab:blue', 'navy'),
("违背+惩罚", [+1]*T, [-1]*T, 'tab:orange', 'brown'),
]
fig, axs = plt.subplots(2, 2, figsize=(14, 10))
axs = axs.flatten()
for i, (name, preds, outs, cs, cx) in enumerate(scenarios):
simulate_scenario(name, preds, outs, cs, cx, axs[i])
axs[i].set_title(name, fontsize=14, weight='bold')
axs[i].set_xlabel('Time Step')
axs[i].set_ylabel('Value')
axs[i].legend()
axs[i].grid(True, linestyle=':', alpha=0.6)
axs[i].axhline(0, color='black', linewidth=0.5)
plt.tight_layout()
plt.suptitle('DCAL Model: s (behavior drive) vs x (cognitive stability)',
fontsize=16, y=1.02)
plt.show()
运行环境:Python 3.8+,需安装 numpy 和 matplotlib。