後端代碼清理:移除冗餘註解和調試代碼
清理內容: - 移除所有開發元資訊(Author, Version, DocID, Rationale等) - 刪除註解掉的代碼片段(力導向演算法等24行) - 移除調試用的 logger.debug 語句 - 簡化冗餘的內聯註解(emoji、"重要"等標註) - 刪除 TDD 文件引用 清理檔案: - backend/main.py - 移除調試日誌和元資訊 - backend/importer.py - 移除詳細類型檢查調試 - backend/export.py - 簡化 docstring - backend/schemas.py - 移除元資訊 - backend/renderer.py - 移除 TDD 引用 - backend/renderer_timeline.py - 移除註解代碼和冗餘註解 - backend/path_planner.py - 簡化策略註解 保留內容: - 所有函數的 docstring(功能說明、參數、返回值) - 必要的業務邏輯註解 - 簡潔的模組功能說明 效果: - 刪除約 100+ 行冗餘註解 - 代碼更加簡潔專業 - 功能完整性 100% 保留 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -3,9 +3,6 @@
|
||||
|
||||
使用BFS算法在網格化的繪圖區域中為連接線尋找最佳路徑,
|
||||
完全避開標籤障礙物。
|
||||
|
||||
Author: AI Agent
|
||||
Version: 1.0.0
|
||||
"""
|
||||
|
||||
import logging
|
||||
@@ -151,49 +148,35 @@ class GridMap:
|
||||
path_points: 路徑點列表 [(datetime, y), ...]
|
||||
width_expansion: 寬度擴展倍數
|
||||
|
||||
策略:
|
||||
1. 標記所有線段(包括起點線段)
|
||||
2. 但是起點線段只標記離開時間軸的垂直部分
|
||||
3. 時間軸 y=0 本身不標記,避免阻擋其他起點
|
||||
"""
|
||||
if len(path_points) < 2:
|
||||
return
|
||||
|
||||
# 標記所有線段
|
||||
for i in range(len(path_points) - 1):
|
||||
dt1, y1 = path_points[i]
|
||||
dt2, y2 = path_points[i + 1]
|
||||
|
||||
# 如果是從時間軸(y=0)出發的第一段線段
|
||||
if i == 0 and abs(y1) < 0.1:
|
||||
# 只標記離開時間軸的部分(從 y=0.2 開始)
|
||||
# 避免阻擋其他事件的起點
|
||||
if abs(y2) > 0.2: # 確保終點不在時間軸上
|
||||
# 使用線性插值找到 y=0.2 的點
|
||||
if abs(y2) > 0.2:
|
||||
if abs(y2 - y1) > 0.01:
|
||||
t = (0.2 - y1) / (y2 - y1) if y2 > y1 else (-0.2 - y1) / (y2 - y1)
|
||||
if 0 < t < 1:
|
||||
# 計算 y=0.2 時的 datetime
|
||||
seconds_offset = (dt2 - dt1).total_seconds() * t
|
||||
dt_cutoff = dt1 + timedelta(seconds=seconds_offset)
|
||||
y_cutoff = 0.2 if y2 > 0 else -0.2
|
||||
|
||||
# 只標記從 cutoff 點到終點的部分
|
||||
col1 = self.datetime_to_grid_x(dt_cutoff)
|
||||
row1 = self.y_to_grid_y(y_cutoff)
|
||||
col2 = self.datetime_to_grid_x(dt2)
|
||||
row2 = self.y_to_grid_y(y2)
|
||||
self._mark_line(row1, col1, row2, col2, int(width_expansion))
|
||||
else:
|
||||
# t 不在範圍內,標記整段
|
||||
col1 = self.datetime_to_grid_x(dt1)
|
||||
row1 = self.y_to_grid_y(y1)
|
||||
col2 = self.datetime_to_grid_x(dt2)
|
||||
row2 = self.y_to_grid_y(y2)
|
||||
self._mark_line(row1, col1, row2, col2, int(width_expansion))
|
||||
# 如果終點也在時間軸上,不標記
|
||||
else:
|
||||
# 非起點線段,全部標記
|
||||
col1 = self.datetime_to_grid_x(dt1)
|
||||
row1 = self.y_to_grid_y(y1)
|
||||
col2 = self.datetime_to_grid_x(dt2)
|
||||
@@ -308,15 +291,10 @@ def find_path_bfs(
|
||||
end_row: int,
|
||||
end_col: int,
|
||||
grid_map: GridMap,
|
||||
direction_constraint: str = "up" # "up" or "down"
|
||||
direction_constraint: str = "up"
|
||||
) -> Optional[List[Tuple[int, int]]]:
|
||||
"""
|
||||
使用BFS尋找路徑(改進版:優先離開時間軸)
|
||||
|
||||
策略:
|
||||
1. 優先垂直移動(離開時間軸)
|
||||
2. 遇到障礙物才水平繞行
|
||||
3. 使用優先隊列,根據與時間軸的距離排序
|
||||
使用BFS尋找路徑
|
||||
|
||||
Args:
|
||||
start_row, start_col: 起點網格座標
|
||||
@@ -338,23 +316,16 @@ def find_path_bfs(
|
||||
|
||||
import heapq
|
||||
|
||||
# 計算時間軸的Y座標(row)
|
||||
timeline_row = grid_map.y_to_grid_y(0)
|
||||
|
||||
# 優先隊列:(優先度, row, col, path)
|
||||
# 優先度 = 與時間軸的距離(越遠越好)+ 路徑長度(越短越好)
|
||||
start_priority = 0
|
||||
heap = [(start_priority, start_row, start_col, [(start_row, start_col)])]
|
||||
visited = set()
|
||||
visited.add((start_row, start_col))
|
||||
|
||||
# 方向優先順序(垂直優先於水平)
|
||||
if direction_constraint == "up":
|
||||
# 優先往上,然後才左右
|
||||
directions = [(-1, 0), (0, 1), (0, -1)] # 上、右、左
|
||||
else: # "down"
|
||||
# 優先往下,然後才左右
|
||||
directions = [(1, 0), (0, 1), (0, -1)] # 下、右、左
|
||||
directions = [(-1, 0), (0, 1), (0, -1)]
|
||||
else:
|
||||
directions = [(1, 0), (0, 1), (0, -1)]
|
||||
|
||||
max_iterations = grid_map.grid_rows * grid_map.grid_cols * 2
|
||||
iterations = 0
|
||||
@@ -363,42 +334,30 @@ def find_path_bfs(
|
||||
iterations += 1
|
||||
_, current_row, current_col, path = heapq.heappop(heap)
|
||||
|
||||
# 到達終點
|
||||
if current_row == end_row and current_col == end_col:
|
||||
logger.info(f"找到路徑,長度: {len(path)},迭代: {iterations}")
|
||||
return path
|
||||
|
||||
# 探索鄰居(按優先順序)
|
||||
for d_row, d_col in directions:
|
||||
next_row = current_row + d_row
|
||||
next_col = current_col + d_col
|
||||
|
||||
# 檢查是否可通行
|
||||
if (next_row, next_col) in visited:
|
||||
continue
|
||||
|
||||
if not grid_map.is_free(next_row, next_col):
|
||||
continue
|
||||
|
||||
# 計算優先度
|
||||
# 1. 與時間軸的距離(主要因素)
|
||||
distance_from_timeline = abs(next_row - timeline_row)
|
||||
|
||||
# 2. 曼哈頓距離到終點(次要因素)
|
||||
manhattan_to_goal = abs(next_row - end_row) + abs(next_col - end_col)
|
||||
|
||||
# 3. 路徑長度(避免繞太遠)
|
||||
path_length = len(path)
|
||||
|
||||
# 綜合優先度:離時間軸越遠越好,離目標越近越好
|
||||
# 權重調整:優先離開時間軸
|
||||
priority = (
|
||||
-distance_from_timeline * 100 + # 負數因為要最大化
|
||||
-distance_from_timeline * 100 +
|
||||
manhattan_to_goal * 10 +
|
||||
path_length
|
||||
)
|
||||
|
||||
# 添加到優先隊列
|
||||
visited.add((next_row, next_col))
|
||||
new_path = path + [(next_row, next_col)]
|
||||
heapq.heappush(heap, (priority, next_row, next_col, new_path))
|
||||
|
||||
Reference in New Issue
Block a user