干货 | 10分钟带你全面掌握branch and bound(分支定界)算法-概念篇

本文介绍了分支定界(Branch and Bound)算法,它是一种用于离散和组合优化问题的算法设计范式。文章通过通俗易懂的方式解释了算法的基本思想,即通过搜索树进行分支,并在分支过程中设定上下界来裁剪无法产生最优解的子问题。此外,还以整数规划为例展示了算法的具体应用,并提供了算法框架和伪代码。
摘要由CSDN通过智能技术生成

00 前言

之前一直做启发式算法,最近突然对精确算法感兴趣了。但是这玩意儿说实话是真的难,刚好boss又叫我学学column generation求解VRP相关的内容。一看里面有好多知识需要重新把握,所以这段 时间就打算好好学学精确算法。届时会把学习过程记录下来,也方便大家学习!

01 什么是branch and bound(定义)?

1.1 官方一点[1]

Branch and bound (BB, B&B, or BnB) is an algorithm design paradigm for discrete and combinatorial optimization problems, as well as mathematical optimization. A branch-and-bound algorithm consists of a systematic enumeration of candidate solutions by means of state space search: the set of candidate solutions is thought of as forming a rooted tree with the full set at the root. The algorithm explores branches of this tree, which represent subsets of the solution set. Before enumerating the candidate solutions of a branch, the branch is checked against upper and lower estimated bounds on the optimal solution, and is discarded if it cannot produce a better solution than the best one found so far by the algorithm.

The algorithm depends on efficient estimation of the lower and upper bounds of regions/branches of the search space. If no bounds are available, the algorithm degenerates to an exhaustive search.

1.2 通俗一点

分支定界算法始终围绕着一颗搜索树进行的,我们将原问题看作搜索树的根节点,从这里出发,分支的含义就是将大的问题分割成小的问题。大问题可以看成是搜索树的父节点,那么从大问题分割出来的小问题就是父节点的子节点了。分支的过程就是不断给树增加子节点的过程。而定界就是在分支的过程中检查子问题的上下界,如果子问题不能产生一比当前最优解还要优的解,那么砍掉这一支。直到所有子问题都不能产生一个更优的解时,算法结束。

好的,我会尽力回答你关于分支定界法的问题。首先,让我们来看看0/1背包问题。 0/1背包问题是指有一个固定大小的背包,以及一些物品,每个物品都有自己的价值和重量。要求选择一些物品,使得在满足背包最大重量限制的前提下,所选物品的总价值最大。 使用分支定界法来解决0/1背包问题的基本思路是: 1. 将物品按照单位重量的价值从大到小排序。 2. 构造一个节点,记录当前已经选择的物品,以及当前背包中物品的重量和价值。 3. 对于每个节点,计算该节点的上界(即当前已经选择的物品的价值加上剩余物品按照单位重量价值从大到小排序所能得到的最大价值)。 4. 将节点按照上界从大到小排序,选择上界最大的节点进行分支分支的方式是选择当前节点未选择的下一个物品,然后分别构造两个新节点,一个是选择该物品放入背包的节点,另一个是不选择该物品放入背包的节点。 5. 对于新生成的节点,重复步骤3和4,直到找到最优解或者所有节点的上界都小于当前已知的最优解。 在Python中实现0/1背包问题的分支定界法可以参考以下代码: ```python class Node: def __init__(self, level, weight, value, selected): self.level = level self.weight = weight self.value = value self.selected = selected self.bound = 0.0 def bound(node, capacity, weight, value, n): if node.weight >= capacity: return 0.0 else: result = node.value j = node.level + 1 total_weight = node.weight while j < n and total_weight + weight[j] <= capacity: result += value[j] total_weight += weight[j] j += 1 if j < n: result += (capacity - total_weight) * (value[j] / weight[j]) return result def knapsack_branch_bound(capacity, weight, value, n): items = [(value[i] / weight[i], weight[i], value[i]) for i in range(n)] items.sort(reverse=True) queue = [] v = 0.0 u = Node(-1, 0, 0, []) u.bound = bound(u, capacity, weight, value, n) queue.append(u) while queue: u = queue.pop(0) if u.bound > v: if u.level == n - 1: v = u.value selected_items = u.selected else: level = u.level + 1 weight1 = u.weight + weight[level] value1 = u.value + value[level] selected1 = u.selected + [level] node1 = Node(level, weight1, value1, selected1) node1.bound = bound(node1, capacity, weight, value, n) if node1.bound > v: queue.append(node1) weight2 = u.weight value2 = u.value selected2 = u.selected node2 = Node(level, weight2, value2, selected2) node2.bound = bound(node2, capacity, weight, value, n) if node2.bound > v: queue.append(node2) return v, selected_items ``` 在这个代码中,我们定义了一个Node类来存储节点的信息,包括节点的层数、当前背包中物品的重量和价值、已经选择的物品等。我们还定义了一个bound函数来计算节点的上界。在主函数中,我们首先将物品按照单位重量的价值从大到小排序,然后构造一个初始节点,将其加入队列中。接着,我们不断从队列中取出上界最大的节点进行分支,直到找到最优解或者队列为空。最后,返回最优解的价值和已选择的物品编号。 希望这个答案能够帮助到你。如果你还有其他的问题,请随时问我!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值