Skip to content

Commit 86293eb

Browse files
author
baiqing
committed
更改了描述信息
1 parent b622950 commit 86293eb

12 files changed

Lines changed: 5819 additions & 0 deletions

File tree

.output.txt

Lines changed: 4488 additions & 0 deletions
Large diffs are not rendered by default.

configs/constitution_example.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# A-YLM 宪法配置示例
2+
# 用户可以根据自己的需求修改此配置
3+
4+
# 宪法原则列表
5+
principles:
6+
# 碰撞检测 - 最高优先级
7+
- name: no_collision
8+
severity: critical
9+
enabled: true
10+
params:
11+
check_trajectory: true
12+
prediction_horizon: 2.0 # 预测时间范围(秒)
13+
14+
# TTC 安全 - 高优先级
15+
- name: ttc_safety
16+
severity: high
17+
enabled: true
18+
params:
19+
warning_threshold: 3.0 # 警告阈值(秒)
20+
critical_threshold: 1.5 # 关键阈值(秒)
21+
22+
# 安全跟车距离
23+
- name: safe_following
24+
severity: high
25+
enabled: true
26+
params:
27+
time_gap: 2.0 # 时间间隔(秒)
28+
29+
# 车道合规
30+
- name: lane_compliance
31+
severity: medium
32+
enabled: true
33+
34+
# 速度限制
35+
- name: speed_limit
36+
severity: medium
37+
enabled: true
38+
params:
39+
max_speed: 120 # km/h
40+
41+
# 安全阈值
42+
thresholds:
43+
ttc_warning: 3.0 # TTC 警告阈值(秒)
44+
ttc_critical: 1.5 # TTC 关键阈值(秒)
45+
min_safe_distance: 2.0 # 最小安全距离(米)
46+
speed_distance_factor: 0.5 # 速度-距离系数
47+
48+
# 打分权重
49+
weights:
50+
collision: 1.0 # 碰撞权重
51+
ttc: 0.8 # TTC 权重
52+
boundary: 0.5 # 边界权重
53+
54+
# 训练信号配置
55+
training:
56+
generate_positive_signals: false # 是否生成正样本
57+
export_format: json # 导出格式: json, tfrecord, parquet

src/aylm/constitution/__init__.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""A-YLM 几何宪法式 AI 核心模块。
2+
3+
本模块提供开放式接口,允许用户自定义:
4+
- 宪法原则(ConstitutionPrinciple)
5+
- 安全打分器(SafetyScorer)
6+
- 训练信号生成器(TrainingSignalGenerator)
7+
8+
设计理念:
9+
- 我们提供基础设施和接口定义
10+
- 具体的打分逻辑和训练方式由用户根据需求实现
11+
- 支持插件机制,便于第三方扩展
12+
"""
13+
14+
from .base import (
15+
ConstitutionPrinciple,
16+
Severity,
17+
ViolationResult,
18+
)
19+
from .config import ConstitutionConfig
20+
from .registry import ConstitutionRegistry
21+
from .scorer import SafetyScore, SafetyScorer
22+
from .training import TrainingSignal, TrainingSignalGenerator
23+
24+
__all__ = [
25+
# 基础类型
26+
"Severity",
27+
"ViolationResult",
28+
# 抽象基类
29+
"ConstitutionPrinciple",
30+
"SafetyScorer",
31+
"TrainingSignalGenerator",
32+
# 数据类
33+
"SafetyScore",
34+
"TrainingSignal",
35+
"ConstitutionConfig",
36+
# 注册机制
37+
"ConstitutionRegistry",
38+
]

src/aylm/constitution/base.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
"""宪法原则基类定义。
2+
3+
本模块定义了几何宪法式 AI 的核心抽象:
4+
- Severity: 违规严重性等级
5+
- ViolationResult: 违规检测结果
6+
- ConstitutionPrinciple: 宪法原则基类
7+
8+
用户可以继承 ConstitutionPrinciple 实现自定义的安全规则。
9+
"""
10+
11+
from abc import ABC, abstractmethod
12+
from dataclasses import dataclass, field
13+
from enum import Enum
14+
from typing import TYPE_CHECKING, Any
15+
16+
if TYPE_CHECKING:
17+
from .types import AIDecision, SceneState
18+
19+
20+
class Severity(Enum):
21+
"""违规严重性等级。
22+
23+
用于定义宪法原则被违反时的严重程度,
24+
影响最终安全分数的计算和推荐动作。
25+
"""
26+
27+
CRITICAL = 1.0 # 关键:必须立即阻止(如即将碰撞)
28+
HIGH = 0.8 # 高:强烈警告,建议干预
29+
MEDIUM = 0.5 # 中:一般警告,需要注意
30+
LOW = 0.2 # 低:轻微提示,可忽略
31+
32+
33+
@dataclass
34+
class ViolationResult:
35+
"""违规检测结果。
36+
37+
Attributes:
38+
violated: 是否违反原则
39+
severity: 违规严重性
40+
confidence: 检测置信度 (0.0-1.0)
41+
description: 违规描述(人类可读)
42+
metrics: 相关度量值(如距离、TTC 等)
43+
correction_hint: 纠正建议(可选,用于生成训练信号)
44+
"""
45+
46+
violated: bool
47+
severity: Severity
48+
confidence: float
49+
description: str
50+
metrics: dict[str, float] = field(default_factory=dict)
51+
correction_hint: dict[str, Any] | None = None
52+
53+
def to_dict(self) -> dict[str, Any]:
54+
"""转换为字典格式。"""
55+
return {
56+
"violated": self.violated,
57+
"severity": self.severity.name,
58+
"severity_weight": self.severity.value,
59+
"confidence": self.confidence,
60+
"description": self.description,
61+
"metrics": self.metrics,
62+
"correction_hint": self.correction_hint,
63+
}
64+
65+
66+
class ConstitutionPrinciple(ABC):
67+
"""宪法原则基类。
68+
69+
所有安全规则都应继承此类并实现 evaluate 方法。
70+
这是 A-YLM 几何宪法式 AI 的核心抽象。
71+
72+
Example:
73+
>>> class NoCollisionPrinciple(ConstitutionPrinciple):
74+
... @property
75+
... def name(self) -> str:
76+
... return "no_collision"
77+
...
78+
... @property
79+
... def severity(self) -> Severity:
80+
... return Severity.CRITICAL
81+
...
82+
... def evaluate(self, state, decision) -> ViolationResult:
83+
... # 实现碰撞检测逻辑
84+
... collision = check_collision(state.obstacles, decision.trajectory)
85+
... return ViolationResult(
86+
... violated=collision,
87+
... severity=self.severity,
88+
... confidence=0.95,
89+
... description="检测到碰撞风险" if collision else "安全",
90+
... )
91+
"""
92+
93+
@property
94+
@abstractmethod
95+
def name(self) -> str:
96+
"""原则名称(唯一标识符)。"""
97+
pass
98+
99+
@property
100+
@abstractmethod
101+
def severity(self) -> Severity:
102+
"""默认严重性等级。"""
103+
pass
104+
105+
@property
106+
def description(self) -> str:
107+
"""原则描述(可选覆盖)。"""
108+
return f"宪法原则: {self.name}"
109+
110+
@property
111+
def enabled(self) -> bool:
112+
"""是否启用(可动态控制)。"""
113+
return True
114+
115+
@abstractmethod
116+
def evaluate(
117+
self,
118+
state: "SceneState",
119+
decision: "AIDecision",
120+
) -> ViolationResult:
121+
"""评估 AI 决策是否违反此原则。
122+
123+
Args:
124+
state: 当前场景状态(包含障碍物、自车状态等)
125+
decision: AI 的决策(包含规划轨迹、控制指令等)
126+
127+
Returns:
128+
ViolationResult: 违规检测结果
129+
"""
130+
pass
131+
132+
def __repr__(self) -> str:
133+
return f"{self.__class__.__name__}(name={self.name}, severity={self.severity.name})"

0 commit comments

Comments
 (0)