feat(layout_optimizer): DE optimizer V2 — custom loop, graduated hard constraints, broad phase

Replace scipy differential_evolution with custom DE loop for per-device
crossover, circular θ wrapping, and configurable mutation strategy
(currenttobest1bin default, best1bin as turbo mode).

Key improvements:
- Graduate ALL hard constraints during DE (proportional penalty instead of
  flat inf), giving DE smooth gradient for reachability, min_spacing, etc.
  Binary inf preserved for final pass/fail reporting.
- 2-axis sweep-and-prune AABB broad phase for collision pair pruning
- Multi-seed injection from multiple seeder presets + Gaussian variants
- snap_theta_safe: collision-check after angle snapping, revert on violation
- Weight normalization (100 distance / 60 angle / 5× hard multiplier)
- Constraint priority field (critical/high/normal/low → weight multiplier)
  with LLM intent interpreter setting priority per constraint type
- Final success field now checks user hard constraints in binary mode
- arm_slider added to mock checker reach table (1.07m)

Tests: 202 passed, 24 new tests added (optimizer 7, constraints 6, broad_phase 11)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
yexiaozhou
2026-04-01 00:32:34 +08:00
parent 64eeed56a1
commit 9ef24b7768
12 changed files with 1072 additions and 83 deletions

View File

@@ -0,0 +1,241 @@
"""Tests for broad_phase.py — 2 轴 sweep-and-prune 宽相碰撞检测。"""
from __future__ import annotations
import math
import random
import pytest
from ..broad_phase import broad_phase_device_pairs, sweep_and_prune_pairs
from ..models import Device, Placement
# ---------------------------------------------------------------------------
# 测试用辅助函数
# ---------------------------------------------------------------------------
def _make_device(device_id: str, w: float = 0.6, d: float = 0.4) -> Device:
"""创建简单测试设备。"""
return Device(id=device_id, name=device_id, bbox=(w, d))
def _make_placement(
device_id: str, x: float, y: float, theta: float = 0.0
) -> Placement:
"""创建简单测试放置。"""
return Placement(device_id=device_id, x=x, y=y, theta=theta)
# ---------------------------------------------------------------------------
# 测试类
# ---------------------------------------------------------------------------
class TestNoOverlap:
"""两台设备距离足够远,宽相不返回候选对。"""
def test_no_overlap_returns_empty(self):
"""水平方向间距远大于 AABB 尺寸 → 0 候选对。"""
devices = [_make_device("A", 1.0, 1.0), _make_device("B", 1.0, 1.0)]
placements = [
_make_placement("A", 0.0, 0.0),
_make_placement("B", 10.0, 0.0),
]
pairs = sweep_and_prune_pairs(devices, placements)
assert pairs == []
class TestOverlapping:
"""两台设备 AABB 明显重叠。"""
def test_overlapping_devices_returned(self):
"""两台 1×1 设备中心距 0.5m → 1 候选对。"""
devices = [_make_device("A", 1.0, 1.0), _make_device("B", 1.0, 1.0)]
placements = [
_make_placement("A", 0.0, 0.0),
_make_placement("B", 0.5, 0.0),
]
pairs = sweep_and_prune_pairs(devices, placements)
assert len(pairs) == 1
assert pairs[0] == (0, 1)
class TestXOverlapYNoOverlap:
"""x 轴投影交叠但 y 轴不交叠,应被 y 轴检查剪枝。"""
def test_x_overlap_y_no_overlap(self):
"""水平接近但垂直方向偏移足够大 → 0 候选对。"""
devices = [_make_device("A", 2.0, 1.0), _make_device("B", 2.0, 1.0)]
placements = [
_make_placement("A", 0.0, 0.0),
_make_placement("B", 0.5, 5.0), # x 轴交叠但 y 轴相距很远
]
pairs = sweep_and_prune_pairs(devices, placements)
assert pairs == []
class TestTouchingDevices:
"""AABB 恰好接触(边缘距离 = 0应作为候选对返回。"""
def test_touching_devices_included(self):
"""两个 1×1 设备中心距恰好为 1.0(半宽 0.5 + 0.5 = 1.0
AABB 边界接触 → 应包含在候选对中(<= 判定)。"""
devices = [_make_device("A", 1.0, 1.0), _make_device("B", 1.0, 1.0)]
placements = [
_make_placement("A", 0.0, 0.0),
_make_placement("B", 1.0, 0.0), # xmax_A = 0.5, xmin_B = 0.5 → 接触
]
pairs = sweep_and_prune_pairs(devices, placements)
# 接触算作潜在碰撞,安全起见需保留
assert len(pairs) == 1
assert pairs[0] == (0, 1)
class TestMultipleDevices:
"""4 台设备验证精确的候选对列表。"""
def test_multiple_devices_correct_pairs(self):
"""排列 4 台设备,只有特定配对 AABB 交叠。
布局1×1 设备):
A(0,0) B(0.8,0) — A-B 交叠(中心距 0.8 < 1.0
C(0,5) — 远离 A、B
D(0.9,5) — C-D 交叠(中心距 0.9 < 1.0
期望候选对: (A,B) 和 (C,D)。
"""
devices = [
_make_device("A", 1.0, 1.0),
_make_device("B", 1.0, 1.0),
_make_device("C", 1.0, 1.0),
_make_device("D", 1.0, 1.0),
]
placements = [
_make_placement("A", 0.0, 0.0),
_make_placement("B", 0.8, 0.0),
_make_placement("C", 0.0, 5.0),
_make_placement("D", 0.9, 5.0),
]
pairs = sweep_and_prune_pairs(devices, placements)
pair_set = set(pairs)
assert (0, 1) in pair_set # A-B
assert (2, 3) in pair_set # C-D
assert len(pair_set) == 2
class TestRotatedDeviceAabb:
"""旋转设备导致 AABB 变大,命中候选对。"""
def test_rotated_device_aabb(self):
"""一台窄长设备 (2.0×0.2)
- 未旋转时 AABB 半宽 = 1.0,两设备中心距 2.5 → 不交叠
- 旋转 90° 后 AABB 半宽 = 0.1,半深 = 1.0 → 仍不交叠
- 旋转 45° 后 AABB 半宽 ≈ (2*cos45 + 0.2*sin45)/2 ≈ 0.778
但另一台放在 x=1.6,半宽 = 1.0
所以 xmax_A = 0 + 0.778 = 0.778 < 1.6 - 1.0 = 0.6 → 不够
更好的方案:用中心距 1.5,未旋转时不交叠,旋转后交叠。
未旋转: half_w_A = 0.3 (bbox 0.6x2.0), half_w_B = 0.3
A: xmax = 0 + 0.3 = 0.3, B: xmin = 1.5 - 0.3 = 1.2 → 不交叠
旋转 90°: A 的 bbox (0.6, 2.0) → half_w = (0.6*0 + 2.0*1)/2 = 1.0
A: xmax = 0 + 1.0 = 1.0, B: xmin = 1.5 - 0.3 = 1.2 → 仍不交叠
用 bbox (0.4, 2.0),间距 1.2
未旋转: half_w = 0.2, xmax_A = 0.2, xmin_B = 1.2 - 0.2 = 1.0 → 不交叠
旋转 45°: half_w = (0.4*cos45 + 2.0*sin45)/2 = (0.283+1.414)/2 = 0.849
xmax_A = 0.849, xmin_B = 1.2 - 0.2 = 1.0 → 不交叠
间距 0.8
未旋转: xmax_A = 0.2, xmin_B = 0.8 - 0.2 = 0.6 → 不交叠 ✓
旋转 45°: xmax_A = 0.849, xmin_B = 0.6 → 交叠 ✓ (0.849 > 0.6)
y 轴: half_d_A_rot = (0.4*sin45 + 2.0*cos45)/2 = 0.849, half_d_B = 1.0
ymax_A = 0.849, ymin_B = -1.0 → 交叠 ✓
"""
dev_narrow = _make_device("narrow", 0.4, 2.0)
dev_normal = _make_device("normal", 0.4, 2.0)
# 未旋转:不交叠
placements_no_rot = [
_make_placement("narrow", 0.0, 0.0, theta=0.0),
_make_placement("normal", 0.8, 0.0, theta=0.0),
]
assert sweep_and_prune_pairs([dev_narrow, dev_normal], placements_no_rot) == []
# narrow 旋转 45° → AABB 变大 → 交叠
placements_rot = [
_make_placement("narrow", 0.0, 0.0, theta=math.pi / 4),
_make_placement("normal", 0.8, 0.0, theta=0.0),
]
pairs = sweep_and_prune_pairs([dev_narrow, dev_normal], placements_rot)
assert len(pairs) == 1
class TestOriginalIndices:
"""验证返回的索引对应 placements 原始顺序而非排序后顺序。"""
def test_sorted_output_preserves_original_indices(self):
"""故意让 placements 按 x 坐标逆序排列,
验证返回的索引仍是原始顺序。"""
devices = [
_make_device("A", 1.0, 1.0),
_make_device("B", 1.0, 1.0),
_make_device("C", 1.0, 1.0),
]
# 逆序排列C 在最左A 在最右
placements = [
_make_placement("A", 5.0, 0.0), # idx 0, 最右
_make_placement("B", 4.5, 0.0), # idx 1, 中间(与 A 交叠)
_make_placement("C", 0.0, 0.0), # idx 2, 最左(独立)
]
pairs = sweep_and_prune_pairs(devices, placements)
# A(idx=0) 和 B(idx=1) AABB 交叠,索引应为 (0, 1)
assert len(pairs) == 1
assert pairs[0] == (0, 1)
# 同时验证 broad_phase_device_pairs 返回正确 device_id
id_pairs = broad_phase_device_pairs(devices, placements)
assert id_pairs == [("A", "B")]
class TestPairCountReduction:
"""大规模随机测试:宽相候选对数应远小于 N*(N-1)/2。"""
def test_pair_count_reduction(self):
"""N=15 台设备随机放置在 10×10 实验室 → 候选对数显著少于全量。"""
random.seed(42)
n = 15
devices = [_make_device(f"D{i}", 0.5, 0.5) for i in range(n)]
placements = [
_make_placement(f"D{i}", random.uniform(0, 10), random.uniform(0, 10))
for i in range(n)
]
pairs = sweep_and_prune_pairs(devices, placements)
full_pairs = n * (n - 1) // 2 # = 105
# 在 10×10 区域放 15 台 0.5×0.5 设备,交叠率应很低
assert len(pairs) < full_pairs
# 额外断言:候选对数不超过全量的一半(保守判定)
assert len(pairs) < full_pairs * 0.5
class TestEdgeCases:
"""边界情况。"""
def test_empty_input(self):
"""空列表 → 空结果。"""
assert sweep_and_prune_pairs([], []) == []
def test_single_device(self):
"""单台设备 → 无候选对。"""
devices = [_make_device("A")]
placements = [_make_placement("A", 0.0, 0.0)]
assert sweep_and_prune_pairs(devices, placements) == []
def test_identical_positions(self):
"""两台设备完全重叠 → 1 候选对。"""
devices = [_make_device("A", 1.0, 1.0), _make_device("B", 1.0, 1.0)]
placements = [
_make_placement("A", 0.0, 0.0),
_make_placement("B", 0.0, 0.0),
]
pairs = sweep_and_prune_pairs(devices, placements)
assert len(pairs) == 1

View File

@@ -90,9 +90,16 @@ class TestDuplicateDeviceIDs:
lab = Lab(width=5, depth=5)
constraints = [Constraint(type="hard", rule_name="min_spacing",
params={"min_gap": 0.05})]
# graduated=True (default): 返回有限惩罚
cost = evaluate_constraints(devices, stacked, lab, constraints,
MockCollisionChecker())
assert math.isinf(cost)
assert cost > 0
assert not math.isinf(cost)
# graduated=False: binary inf
cost_binary = evaluate_constraints(devices, stacked, lab, constraints,
MockCollisionChecker(),
graduated=False)
assert math.isinf(cost_binary)
def test_create_devices_uses_uuid(self):
"""create_devices_from_list should use uuid as Device.id."""

View File

@@ -123,7 +123,7 @@ class TestUserConstraints:
assert cost == 0.0
def test_distance_less_than_violated_hard(self):
"""硬距离约束违反返回 inf。"""
"""硬距离约束违反graduated模式返回有限惩罚binary模式返回inf。"""
devices = _make_devices()
placements = [
Placement("a", 1.0, 1.0, 0.0),
@@ -134,10 +134,18 @@ class TestUserConstraints:
params={"device_a": "a", "device_b": "b", "distance": 1.0})
]
checker = MockCollisionChecker()
# graduated=True (default): 有限惩罚
cost = evaluate_constraints(
devices, placements, _make_lab(), constraints, checker
)
assert math.isinf(cost)
assert cost > 0
assert not math.isinf(cost)
# graduated=False: binary inf
cost_binary = evaluate_constraints(
devices, placements, _make_lab(), constraints, checker,
graduated=False,
)
assert math.isinf(cost_binary)
def test_minimize_distance_cost(self):
"""minimize_distance 约束应返回正比于距离的 cost。"""
@@ -184,7 +192,7 @@ class TestUserConstraints:
assert not math.isinf(cost) # reachable → no hard failure
def test_reachability_constraint_violated(self):
"""可达性约束:目标超出臂展返回 inf。"""
"""可达性约束:目标超出臂展 — graduated返回有限惩罚binary返回inf。"""
devices = [
Device(id="arm", name="Arm", bbox=(0.2, 0.2), device_type="articulation"),
Device(id="target", name="Target", bbox=(0.5, 0.5)),
@@ -199,10 +207,18 @@ class TestUserConstraints:
]
checker = MockCollisionChecker()
reachability = MockReachabilityChecker(arm_reach={"arm": 1.0})
# graduated=True (default): 有限惩罚
cost = evaluate_constraints(
devices, placements, _make_lab(), constraints, checker, reachability
)
assert math.isinf(cost)
assert cost > 0
assert not math.isinf(cost)
# graduated=False: binary inf
cost_binary = evaluate_constraints(
devices, placements, _make_lab(), constraints, checker, reachability,
graduated=False,
)
assert math.isinf(cost_binary)
def test_distance_less_than_uses_edge_to_edge():
@@ -272,3 +288,136 @@ def test_prefer_aligned_sums_over_devices():
cost = evaluate_constraints(devices, placements, lab, [constraint], checker)
# 2 devices × 1.0 × weight 2.0 = 4.0
assert cost == pytest.approx(4.0)
class TestGraduatedHardConstraints:
"""graduated 模式下硬约束返回比例惩罚而非 inf。"""
def test_hard_reachability_graduated_finite(self):
"""graduated=True: 硬可达性返回有限惩罚。"""
devices = [
Device(id="arm", name="Arm", bbox=(0.2, 0.2), device_type="articulation"),
Device(id="t", name="Target", bbox=(0.5, 0.5)),
]
placements = [
Placement("arm", 1.0, 1.0, 0.0),
Placement("t", 4.0, 3.0, 0.0),
]
constraints = [
Constraint(type="hard", rule_name="reachability",
params={"arm_id": "arm", "target_device_id": "t"}, weight=1.0)
]
checker = MockCollisionChecker()
reach = MockReachabilityChecker(arm_reach={"arm": 1.0})
cost = evaluate_constraints(
devices, placements, _make_lab(), constraints, checker, reach,
graduated=True,
)
assert cost > 0
assert not math.isinf(cost)
def test_hard_reachability_binary_inf(self):
"""graduated=False: 硬可达性返回 inf。"""
devices = [
Device(id="arm", name="Arm", bbox=(0.2, 0.2), device_type="articulation"),
Device(id="t", name="Target", bbox=(0.5, 0.5)),
]
placements = [
Placement("arm", 1.0, 1.0, 0.0),
Placement("t", 4.0, 3.0, 0.0),
]
constraints = [
Constraint(type="hard", rule_name="reachability",
params={"arm_id": "arm", "target_device_id": "t"}, weight=1.0)
]
checker = MockCollisionChecker()
reach = MockReachabilityChecker(arm_reach={"arm": 1.0})
cost = evaluate_constraints(
devices, placements, _make_lab(), constraints, checker, reach,
graduated=False,
)
assert math.isinf(cost)
def test_hard_min_spacing_graduated_sums_all_pairs(self):
"""graduated模式min_spacing 对所有违规对求和(不只第一对)。"""
devices = [
Device(id="a", name="A", bbox=(0.5, 0.5)),
Device(id="b", name="B", bbox=(0.5, 0.5)),
Device(id="c", name="C", bbox=(0.5, 0.5)),
]
# 三个设备间距都小于 min_gap=1.0
placements = [
Placement("a", 1.0, 2.0, 0.0),
Placement("b", 1.3, 2.0, 0.0), # OBB 边缘距 a 约 0.3
Placement("c", 1.6, 2.0, 0.0), # OBB 边缘距 b 约 0.3, 距 a 约 0.6
]
constraints = [
Constraint(type="hard", rule_name="min_spacing",
params={"min_gap": 1.0}, weight=1.0)
]
checker = MockCollisionChecker()
cost = evaluate_constraints(
devices, placements, _make_lab(), constraints, checker,
graduated=True,
)
# 应大于 0 且有限(累加多对违规)
assert cost > 0
assert not math.isinf(cost)
def test_hard_min_spacing_binary_inf(self):
"""graduated=False: min_spacing 违规返回 inf。"""
devices = _make_devices()
placements = [
Placement("a", 1.0, 2.0, 0.0),
Placement("b", 1.3, 2.0, 0.0),
]
constraints = [
Constraint(type="hard", rule_name="min_spacing",
params={"min_gap": 1.0}, weight=1.0)
]
checker = MockCollisionChecker()
cost = evaluate_constraints(
devices, placements, _make_lab(), constraints, checker,
graduated=False,
)
assert math.isinf(cost)
def test_hard_distance_less_than_graduated(self):
"""graduated模式distance_less_than 硬约束返回比例惩罚。"""
devices = _make_devices()
placements = [
Placement("a", 1.0, 2.0, 0.0),
Placement("b", 4.0, 2.0, 0.0),
]
constraints = [
Constraint(type="hard", rule_name="distance_less_than",
params={"device_a": "a", "device_b": "b", "distance": 0.5},
weight=2.0)
]
checker = MockCollisionChecker()
cost = evaluate_constraints(
devices, placements, _make_lab(), constraints, checker,
graduated=True,
)
# HARD_MULTIPLIER(5) × weight(2) × overshoot > 0
assert cost > 0
assert not math.isinf(cost)
def test_graduated_default_is_true(self):
"""不传 graduated 参数时默认使用 graduated 模式。"""
devices = _make_devices()
placements = [
Placement("a", 1.0, 2.0, 0.0),
Placement("b", 4.0, 2.0, 0.0),
]
constraints = [
Constraint(type="hard", rule_name="distance_less_than",
params={"device_a": "a", "device_b": "b", "distance": 0.5},
weight=1.0)
]
checker = MockCollisionChecker()
# 不指定 graduated — 默认应为 True → 有限惩罚
cost = evaluate_constraints(
devices, placements, _make_lab(), constraints, checker,
)
assert not math.isinf(cost)

View File

@@ -185,9 +185,9 @@ class TestStage3VerifyPlacements:
def test_no_hard_constraint_violation(self):
"""Full pipeline with all intents including reachability converges cleanly.
MockReachabilityChecker uses large fallback reach for unknown arms,
so arm_slider reachability constraints are satisfied in mock mode.
When real ROS checkers replace mock, this test validates the same pipeline.
MockReachabilityChecker now includes arm_slider in the default reach table
(1.07m). Binary final evaluation checks all hard constraints including
user-defined reachability.
"""
interpret_data = client.post("/interpret", json={"intents": LLM_INTENTS}).json()
@@ -197,7 +197,7 @@ class TestStage3VerifyPlacements:
"constraints": interpret_data["constraints"],
"workflow_edges": interpret_data["workflow_edges"],
"run_de": True,
"maxiter": 50,
"maxiter": 100,
"seed": 42,
})
data = optimize_resp.json()

View File

@@ -4,8 +4,9 @@ import math
from ..mock_checkers import MockCollisionChecker
from ..models import Device, Lab, Placement
import numpy as np
import pytest
from ..optimizer import optimize, snap_theta
from ..optimizer import _run_de, optimize, snap_theta
def test_optimize_three_devices_no_collision():
@@ -217,3 +218,224 @@ def test_full_pipeline_with_de():
for p in result:
assert 0 <= p.x <= lab.width
assert 0 <= p.y <= lab.depth
# ──────────────────────────────────────────────────────────────
# DE V2 新增测试
# ──────────────────────────────────────────────────────────────
def test_theta_wrapping_convergence():
"""θ 在 0/2π 边界附近应正确收敛,不发散。
构造一个简单的 cost function最优解 θ≈0即 2π 附近也可以)。
验证自定义 DE 在 θ wrapping 下能收敛。
"""
n_devices = 2
def cost_fn(x: np.ndarray) -> float:
# 最优:两设备分开放置,θ 接近 0
cost = 0.0
for d in range(n_devices):
theta = x[3 * d + 2]
# θ penalty: 偏离 0 的圆周距离
cost += (1 - math.cos(theta)) * 10.0
# 两设备距离 penalty
dx = x[0] - x[3]
dy = x[1] - x[4]
dist = math.sqrt(dx * dx + dy * dy)
if dist < 1.0:
cost += (1.0 - dist) * 100.0
return cost
bounds = np.array([
[0.5, 4.5], [0.5, 4.5], [0.0, 2 * math.pi], # 设备 0
[0.5, 4.5], [0.5, 4.5], [0.0, 2 * math.pi], # 设备 1
])
rng = np.random.default_rng(42)
pop_size = 30
init_pop = rng.uniform(bounds[:, 0], bounds[:, 1], size=(pop_size, 6))
# 故意注入 θ 接近 2π 的个体,测试 wrapping
init_pop[0, 2] = 2 * math.pi - 0.01
init_pop[0, 5] = 2 * math.pi - 0.05
best_vec, best_cost, n_gen = _run_de(
cost_fn=cost_fn,
bounds=bounds,
init_pop=init_pop,
maxiter=100,
tol=1e-6,
atol=1e-3,
mutation=(0.5, 1.0),
recombination=0.7,
seed=42,
n_devices=n_devices,
)
# θ 应在 [0, 2π) 范围内
for d in range(n_devices):
theta = best_vec[3 * d + 2]
assert 0 <= theta < 2 * math.pi, f"θ 超出 [0, 2π): {theta}"
# cost 应显著下降(不发散)
assert best_cost < 50.0, f"θ wrapping 未正确收敛cost={best_cost}"
def test_per_device_crossover_atomicity():
"""验证 per-device crossover 的原子性:同一设备的 (x, y, θ) 来自同一来源。
构造 2 设备场景,跟踪 crossover 后每个设备三元组是否完整来自 mutant 或 parent。
"""
rng = np.random.default_rng(123)
n_devices = 3
ndim = 3 * n_devices
# 构造明显不同的 parent 和 mutant
parent = np.zeros(ndim)
mutant = np.ones(ndim) * 10.0
# 模拟多次 per-device crossover检查原子性
violations = 0
n_trials = 200
for _ in range(n_trials):
trial = parent.copy()
j_rand = rng.integers(0, n_devices)
for d in range(n_devices):
if rng.random() < 0.7 or d == j_rand:
trial[3 * d: 3 * d + 3] = mutant[3 * d: 3 * d + 3]
# 检查每个设备的三元组要么全是 0parent要么全是 10mutant
for d in range(n_devices):
triple = trial[3 * d: 3 * d + 3]
all_parent = np.allclose(triple, 0.0)
all_mutant = np.allclose(triple, 10.0)
if not (all_parent or all_mutant):
violations += 1
assert violations == 0, f"Per-device crossover 原子性违反 {violations}/{n_trials * n_devices}"
def test_strategy_currenttobest1bin_converges():
"""currenttobest1bin 策略在简单 2 设备问题上应能收敛。"""
devices = [
Device(id="a", name="A", bbox=(0.6, 0.4)),
Device(id="b", name="B", bbox=(0.6, 0.4)),
]
lab = Lab(width=5.0, depth=5.0)
placements = optimize(
devices, lab, seed=42, maxiter=80, popsize=10,
strategy="currenttobest1bin",
)
assert len(placements) == 2
checker = MockCollisionChecker()
checker_placements = [
{"id": p.device_id, "bbox": next(d.bbox for d in devices if d.id == p.device_id),
"pos": (p.x, p.y, p.theta)}
for p in placements
]
collisions = checker.check(checker_placements)
assert collisions == [], f"currenttobest1bin 策略产生碰撞: {collisions}"
def test_strategy_best1bin_converges():
"""best1bin 策略在简单 2 设备问题上应能收敛。"""
devices = [
Device(id="a", name="A", bbox=(0.6, 0.4)),
Device(id="b", name="B", bbox=(0.6, 0.4)),
]
lab = Lab(width=5.0, depth=5.0)
placements = optimize(
devices, lab, seed=42, maxiter=80, popsize=10,
strategy="best1bin",
)
assert len(placements) == 2
checker = MockCollisionChecker()
checker_placements = [
{"id": p.device_id, "bbox": next(d.bbox for d in devices if d.id == p.device_id),
"pos": (p.x, p.y, p.theta)}
for p in placements
]
collisions = checker.check(checker_placements)
assert collisions == [], f"best1bin 策略产生碰撞: {collisions}"
def test_convergence_quality_two_devices():
"""2 设备无碰撞放置cost 应低于阈值,验证优化质量。"""
devices = [
Device(id="d1", name="D1", bbox=(0.8, 0.6)),
Device(id="d2", name="D2", bbox=(0.6, 0.5)),
]
lab = Lab(width=5.0, depth=5.0)
placements = optimize(devices, lab, seed=42, maxiter=100, popsize=10)
# 验证结果质量cost 应很低(无碰撞、在边界内)
from ..constraints import evaluate_default_hard_constraints
checker = MockCollisionChecker()
cost = evaluate_default_hard_constraints(devices, placements, lab, checker)
assert cost < 1.0, f"优化质量不佳cost={cost}"
def test_run_de_early_stopping():
"""验证 _run_de 的 early stopping 机制:简单 cost function 应提前终止。"""
def trivial_cost(x: np.ndarray) -> float:
# 简单二次函数,最优解在原点附近
return float(np.sum(x ** 2))
ndim = 6
n_devices = 2
bounds = np.array([[-5.0, 5.0]] * ndim)
rng = np.random.default_rng(42)
pop_size = 20
init_pop = rng.uniform(-5, 5, size=(pop_size, ndim))
_, _, n_gen = _run_de(
cost_fn=trivial_cost,
bounds=bounds,
init_pop=init_pop,
maxiter=500,
tol=1e-6,
atol=1e-3,
mutation=(0.5, 1.0),
recombination=0.7,
seed=42,
n_devices=n_devices,
)
# 简单问题应在远少于 maxiter=500 代内收敛
assert n_gen < 500, f"Early stopping 未生效,运行了 {n_gen}"
def test_run_de_returns_correct_tuple():
"""验证 _run_de 返回值格式正确。"""
def const_cost(x: np.ndarray) -> float:
return 42.0
bounds = np.array([[0.0, 1.0]] * 3)
init_pop = np.random.default_rng(0).uniform(0, 1, size=(5, 3))
result = _run_de(
cost_fn=const_cost,
bounds=bounds,
init_pop=init_pop,
maxiter=10,
tol=1e-6,
atol=1e-3,
mutation=(0.5, 1.0),
recombination=0.7,
seed=0,
n_devices=1,
)
assert isinstance(result, tuple) and len(result) == 3
best_vec, best_cost, n_gen = result
assert isinstance(best_vec, np.ndarray)
assert best_vec.shape == (3,)
assert best_cost == pytest.approx(42.0)
assert isinstance(n_gen, int) and n_gen >= 1