集束搜索

看计算机科学中最重要的32个算法,其中有个是集束搜索(又名定向搜索,Beam Search)——最佳优先搜索算法的优化。使用启发式函数评估它检查的每个节点的能力。不过,集束搜索只能在每个深度中发现前m个最符合条件的节点,m是固定数字——集束的宽度。

泛泛的介绍,不是很能理解清楚,于是有百度又google,写篇东西备忘。先贴维基百科的地址:Beam Search

翻译过来就是:

Beam Search(集束搜索)是一种启发式图搜索算法,通常用在图的解空间比较大的情况下,为了减少搜索所占用的空间和时间,在每一步深度扩展的时候,剪掉一些质量比较差的结点,保留下一些质量较高的结点。这样减少了空间消耗,并提高了时间效率。

算法的工作流程如下:

使用广度优先策略建立搜索树,在树的每一层,按照启发代价对节点进行排序,然后仅留下预先确定的个数(Beam Width-集束宽度)的节点,仅这些节点在下一层次继续扩展,其他节点就被剪掉了。

  • 将初始节点插入到list中,
  • 将给节点出堆,如果该节点是目标节点,则算法结束;
  • 否则扩展该节点,取集束宽度的节点入堆。然后到第二步继续循环。
  • 算法结束的条件是找到最优解或者堆为空。

搞个伪代码:

<span style="font-size:18px;">/*初始化 */
g = 0;//步数
hash_table = { start };//hash表,用于标记所有已经访问过的节点。类似于visited表
BEAM = { start };//BEAM 一个容量受限的not-visited表,也就是在初始化时,需要指定not-visited表的容量


while(BEAM ≠ ∅){// 循环直到BEAM为空,也就是没有需要考察的节点了
  SET = ∅;// 设置集合为空

  for(each state in BEAM){ //对于BEAM中的每个状态state
    for(each successor of state){ // 对于state的每个后继successor
      if(successor == goal) return g + 1;// 如果后继就是目标节点,那么找到了一个路径。
      SET = SET ∪ { successor }; // 否则,后继加入到集合中
    }
  }//for

  BEAM = ∅; // 因为not-visited表中的内容已经处理完毕,清空not-visited表
  g = g + 1; // 又访问了一层

  /* fill the BEAM for the next loop */
  while((SET ≠ ∅) AND (B > |BEAM|)){ // 集合不空并且open表没有满 BEAM是容量为B的not-visited表
    state = successor in SET with smallest h value; //从集合中选出最好的点 h参考备注1
    SET = SET \ { state }; // 从集合中删除该节点
    if(state ∉ hash_table){ // 该状态还没有考察过 则state不在hash_table中
      if(hash_table is full) return ∞; // hash表满了 算法已经没法执行下去 当然没能找到路径
      hash_table = hash_table ∪ { state }; //state已经考虑过 添加到hash_table中
      BEAM = BEAM ∪ { state }; //state添加到BEAM中等待判断
    }//if
  }//while


/* 注意 有可能集合不空 但是BEAM集合已经满了
   根据算法 集合会被清空 集合中的点将不再考虑
   按照前面的解释,该算法的确不是完备的
   也就是说 有可能原问题有解,但是由于这里舍弃了一些中间过程
   导致最终无法获得解
*/

}//while

// goal was not found, and BEAM is empty - Beam Search failed to find the goal
return ∞;//没能找到路径 </span>

原文参考:Beam Search Algorithm (Draft by Andrew Jungwirth)

终上所述,Beam Search算法是在资源受限系统上运行的,处理大规模搜索问题的算法。整个算法大体上就类似广度优先搜索(BFS)+剪枝,sicily的马周游就是一种Beam Search。


集束搜索是一种启发式搜索算法,用于在大规模搜索空间中快速找到最优解。它通过保留一定数量的最有希望的候选解,来减少搜索空间。下面是一个用Python实现集束搜索的示例代码: ```python import heapq class BeamSearch: def __init__(self, beam_width): self.beam_width = beam_width self.heap = [] def search(self, start_state, goal_fn, successor_fn, heuristic_fn): # Add the start state to the heap with a priority of 0 heapq.heappush(self.heap, (0, [start_state])) while self.heap: # Pop the state with the lowest priority from the heap priority, path = heapq.heappop(self.heap) current_state = path[-1] # Check if the current state is the goal state if goal_fn(current_state): return path # Generate successor states and add them to the heap successor_states = successor_fn(current_state) for successor_state in successor_states: successor_path = path + [successor_state] successor_priority = priority + heuristic_fn(successor_state) heapq.heappush(self.heap, (successor_priority, successor_path)) # Keep only the top beam_width paths in the heap self.heap = heapq.nsmallest(self.beam_width, self.heap) # If the heap is empty, no solution was found return None ``` 这个实现使用了一个最小堆来存储候选解,每次从堆中取出当前最优路径进行扩展。在每次扩展时,生成后继状态,并计算他们的启发式值,然后将它们加入堆中。最后,保留堆中最优的 beam_width 条路径,并继续迭代,直到找到目标状态或者堆为空。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值