news 2026/6/23 22:50:44

[机器学习]搜索碰撞点以及反向微调退避(0619)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
[机器学习]搜索碰撞点以及反向微调退避(0619)

在$initialize\_trees$函数的几何布局算法中,核心机制是通过“从远及近搜索碰撞点,再反向微调退避”来为新增的圣诞树找到紧邻现有树群的最短合法距离。设当前已放置的树集合为 $P=\{p_1,p_2,\dots,p_k\}$,每棵树有其多边形 $A_i$。待放置的新树初始位于极坐标 $(R,\theta)$,其中 $R=20$(单位:缩放前的抽象距离),$\theta$ 由加权随机角度生成,方向向量 $v=(\cos\theta,\sin\theta)$。算法沿射线 $p(t)=t\cdot v$($t\in[0,R]$)以步长 $\Delta_{\text{in}}=0.5$ 向前扫描,寻找第一个使 $A_{\text{new}}(t)$与任一 $A_i$发生真相交(intersects 且不仅为 touches)的临界半径 $t_c$。若存在 $t_c$,则退回到 $t_c-\Delta_{\text{in}}$(即最后一个无碰撞位置),然后以步长 $\Delta_{\text{out}}=0.05$向外微调,直到再次刚好脱离碰撞,得到最终半径 $t_f$。该过程等价于求解方程:

$$t_f = \inf\{ t \ge t_c - \Delta_{\text{in}} \mid \forall i,\; A_{\text{new}}(t) \cap A_i = \emptyset \;\lor\; A_{\text{new}}(t) \text{ touches } A_i \}$$

由于多边形为凸包(圣诞树简化形状),交点检测可由STRtree加速至 $O(\log k)$。若整个扫描过程未发现任何碰撞(即 $t_f=0$ 时仍安全),则新树置于原点。为增强稳健性,算法重复10次独立随机角度尝试,选择其中 $t_f$ 最小的位置(即最贴近现有树群的方案),从而在保持紧凑布局的同时兼顾随机多样性。最终整个配置的外接正方形边长由所有树的并集边界确定,

$$\text{side_length} = \max(\max x - \min x,\; \max y - \min y)$$

这为后续迭代缩放提供了归一化基准。

def initialize_trees(num_trees, existing_trees=None): """ This builds a simple, greedy starting configuration, by using the previous n-tree placements, and adding more tree for the (n+1)-tree configuration. We place a tree fairly far away at a (weighted) random angle, and the bring it closer to the center until it overlaps. Then we back it up until it no longer overlaps. You can easily modify this code to build each n-tree configuration completely from scratch. """ if num_trees == 0: return [], Decimal('0') if existing_trees is None: placed_trees = [] else: placed_trees = list(existing_trees) num_to_add = num_trees - len(placed_trees) if num_to_add > 0: unplaced_trees = [ ChristmasTree(angle=random.uniform(0, 360)) for _ in range(num_to_add)] if not placed_trees: # Only place the first tree at origin if starting from scratch placed_trees.append(unplaced_trees.pop(0)) for tree_to_place in unplaced_trees: placed_polygons = [p.polygon for p in placed_trees] tree_index = STRtree(placed_polygons) best_px = None best_py = None min_radius = Decimal('Infinity') # This loop tries 10 random starting attempts and keeps the best one for _ in range(10): # The new tree starts at a position 20 from the center, at a random vector angle. angle = generate_weighted_angle() vx = Decimal(str(math.cos(angle))) vy = Decimal(str(math.sin(angle))) # Move towards center along the vector in steps of 0.5 until collision radius = Decimal('20.0') step_in = Decimal('0.5') collision_found = False while radius >= 0: px = radius * vx py = radius * vy candidate_poly = affinity.translate( tree_to_place.polygon, xoff=float(px * scale_factor), yoff=float(py * scale_factor)) # Looking for nearby objects possible_indices = tree_index.query(candidate_poly) # This is the collision detection step if any((candidate_poly.intersects(placed_polygons[i]) and not candidate_poly.touches(placed_polygons[i])) for i in possible_indices): collision_found = True break radius -= step_in # back up in steps of 0.05 until it no longer has a collision. if collision_found: step_out = Decimal('0.05') while True: radius += step_out px = radius * vx py = radius * vy candidate_poly = affinity.translate( tree_to_place.polygon, xoff=float(px * scale_factor), yoff=float(py * scale_factor)) possible_indices = tree_index.query(candidate_poly) if not any((candidate_poly.intersects(placed_polygons[i]) and not candidate_poly.touches(placed_polygons[i])) for i in possible_indices): break else: # No collision found even at the center. Place it at the center. radius = Decimal('0') px = Decimal('0') py = Decimal('0') if radius < min_radius: min_radius = radius best_px = px best_py = py tree_to_place.center_x = best_px tree_to_place.center_y = best_py tree_to_place.polygon = affinity.translate( tree_to_place.polygon, xoff=float(tree_to_place.center_x * scale_factor), yoff=float(tree_to_place.center_y * scale_factor), ) placed_trees.append(tree_to_place) # Add the newly placed tree to the list all_polygons = [t.polygon for t in placed_trees] bounds = unary_union(all_polygons).bounds minx = Decimal(bounds[0]) / scale_factor miny = Decimal(bounds[1]) / scale_factor maxx = Decimal(bounds[2]) / scale_factor maxy = Decimal(bounds[3]) / scale_factor width = maxx - minx height = maxy - miny # this forces a square bounding using the largest side side_length = max(width, height) return placed_trees, side_length
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/23 22:49:27

【AI应用实战-WorkBuddy】工作流搭建:从需求到自动化全流程(十三)

目录 一、这篇文章适合谁? 1、适合人群 二、什么是工作流? 1、定义 2、例子 1:每日数据汇报工作流 3、例子 2:用户注册工作流 三、场景 1:设计工作流 1、痛点 2、解决方案 四、场景 2:实现数据处理工作流 1、痛点 2、解决方案 五、场景 3:实现文件处理工作流…

作者头像 李华
网站建设 2026/6/23 22:45:05

基于 Harmony 6.0 应用的游戏时长统计与防沉迷提醒应用首页实现

基于 Harmony 6.0 应用的游戏时长统计与防沉迷提醒应用首页实现 前言 游戏适度有益、过度有害——青少年沉迷、成年人耽误工作、家长束手无策。一款好的防沉迷应用要把"今日游戏时长 / 周报对比 / 家长查看 / 健康提醒"四件事在一屏内全部铺到。Harmony 6.0 时代&…

作者头像 李华
网站建设 2026/6/23 22:35:20

Harness 中的智能轮询:自适应退避策略

Harness 中的智能轮询:自适应退避策略深度解析与实战指南 引言 痛点引入 在现代 DevOps 和持续交付(CD)平台中,实时同步与资源效率往往是一对难以调和的矛盾: 若采用高频固定轮询(如每秒/10秒检查一次外部服务状态),确实能第一时间捕获部署失败、云资源创建完成、测试…

作者头像 李华
网站建设 2026/6/23 22:34:00

Tango框架:视频大语言模型的高效令牌剪枝技术

1. 项目概述&#xff1a;Tango框架的核心价值在视频大语言模型&#xff08;Video LLMs&#xff09;的实际部署中&#xff0c;计算效率与模型性能的平衡一直是工程实践的痛点。传统方法处理视频数据时&#xff0c;往往面临两个关键挑战&#xff1a;首先&#xff0c;视频帧包含大…

作者头像 李华
网站建设 2026/6/23 22:31:54

多模态深度学习在系外行星搜寻中的应用:ExoNet系统设计与实战

1. 项目概述&#xff1a;当AI遇见星空 在过去的十几年里&#xff0c;我参与过不少数据分析项目&#xff0c;但把目光投向星辰大海&#xff0c;用深度学习去“大海捞针”般寻找系外行星&#xff0c;这绝对是最酷、也最富挑战性的经历之一。这个项目的核心&#xff0c;就是 ExoN…

作者头像 李华
网站建设 2026/6/23 22:22:15

Ubuntu 20.04 配置 MongoDB 远程访问三步法:bindIp、ufw、权限

1. 项目概述&#xff1a;为什么在 Ubuntu 20.04 上开放 MongoDB 远程访问是个高频但高风险操作“Como configurar o acesso remoto ao MongoDB no Ubuntu 20.04”——这个葡萄牙语标题直译是“如何在 Ubuntu 20.04 上配置 MongoDB 远程访问”。它背后藏着一个非常典型的开发运维…

作者头像 李华