分支限界法求解01背包(优先队列)【java】

本文介绍了如何使用分支限界法解决0-1背包问题,通过优先队列管理活节点,根据上界函数剪枝,以广度优先策略找到最优解。给出完整代码示例和计算上界函数的方法。
摘要由CSDN通过智能技术生成
实验内容:运用分支限界法解决0-1背包问题

实验目的:分支限界法按广度优先策略遍历问题的解空间树,在遍历过程中,对已经处理的每一个结点根据限界函数估算目标函数的可能取值,从中选取使目标函数取得极值的结点优先进行广度忧先搜索,从而不断调整搜索方向,尽快找到问题的解。因为限界函数常常是基于向题的目标函数而确定的,所以,分支限界法适用于求解最优化问题。本次实验利用分支限界法解决0-1背包问题。 

算法核心思想

  1. 首先对物品按照单位重量价值排序 
  2. 计算上界值
  3. 计算装入背包的真实价值bestvalue
  4. 使用优先队列存储活节点
  5. 根据bestvalue和重量进行剪枝
  6. 根据优先队列先出队的节点选择最接近最优的结果的情况

详细过程可参考文章:0-1背包问题-分支限界法(优先队列分支限界法)_0-1背包问题-优先队列式分支界限法的基础思想和核心步骤-CSDN博客

解空间树: 

 完整代码:

import java.util.PriorityQueue;
//排列树
class Node implements Comparable<Node> {
    int level; // 当前层级
    int weight; // 当前重量
    int value; // 当前价值
    int bound; // 上界

    public Node(int level, int weight, int value, int bound) {
        this.level = level;
        this.weight = weight;
        this.value = value;
        this.bound = bound;
    }

    @Override
    public int compareTo(Node other) {
        // 按照bound的降序排列
        return other.bound - this.bound;
    }
}

public class Knapsack {
    int capacity; // 背包容量
    int n; // 物品数量
    int[] weights; // 物品重量
    int[] values; // 物品价值
    int bestvalue;

    public Knapsack(int capacity, int n, int[] weights, int[] values) {
        this.capacity = capacity;
        this.n = n;
        this.weights = weights;
        this.values = values;
    }

    public int maxValue() {
        // 初始化优先队列
        PriorityQueue<Node> queue = new PriorityQueue<>();
        queue.add(new Node(0, 0, 0, bound(0, 0,0)));
        int maxValue = 0;
        this.bestvalue = 0;
        while (!queue.isEmpty()) {
            Node node = queue.poll(); // 取出队首元素--扩展节点
            if (node.level == n) { // 达到叶子节点,更新最大值
                maxValue = Math.max(maxValue, node.value);
            } else {
                // 左子树:选择当前物品
                if (node.weight + weights[node.level] <= capacity) {
                    int leftbound  = bound(node.level + 1, node.weight + weights[node.level] ,node.value + values[node.level]);
                    if(this.bestvalue<node.value + values[node.level]){
                        this.bestvalue = node.value + values[node.level];
                    }
                    if (leftbound<this.bestvalue){
                        continue;
                    }
                    queue.add(new Node(node.level + 1, node.weight + weights[node.level],
                            node.value + values[node.level],leftbound));
                }
                // 右子树:不选择当前物品
                int rightbound =bound(node.level + 1, node.weight,node.value);
                if (rightbound<this.bestvalue){
                    continue;
                }
                queue.add(new Node(node.level + 1, node.weight, node.value,rightbound));
            }
        }
        return maxValue;
    }

    // 计算上界函数
    private int bound(int i, int weight,int val) {
        int remainingWeight = capacity - weight; // 剩余重量
        int remainingValue = 0; // 剩余价值
        int j = i;
        for (; j < n; j++) {
            if (weights[j] > remainingWeight) { // 当前物品装不下,跳出循环
                break;
            }
            remainingWeight -= weights[j]; // 减去当前物品的重量
            remainingValue += values[j]; // 加上当前物品的价值
        }
        if (j<n){ //使用了double类型进行除法运算来保留小数部分的价值
            remainingValue = (int) (remainingValue + remainingWeight*(double)(values[j]/weights[j]));
        }
        return remainingValue+val;
    }


    public static void main(String[] args) {
        //int[] wt = {4,7,5,3};
        //int[] val = {40,42,25,12};
        //必须按照单位单位价值从大到小
        int[] wt = {4,1,1,2,12};
        int[] val = {10,2,1,2,4};
        int capacity = 15;
        int n = wt.length;
        Knapsack knapsack = new Knapsack(capacity,n,wt,val);
        int res = knapsack.maxValue();
        System.out.println(res);
    }
}

输出结果:15

下面是01背包问题的分支界限法的Java实现: ```java import java.util.PriorityQueue; class Node implements Comparable<Node>{ int level; int profit; int weight; double bound; Node parent; public Node(int level, int profit, int weight, double bound, Node parent){ this.level = level; this.profit = profit; this.weight = weight; this.bound = bound; this.parent = parent; } public int compareTo(Node node){ return Double.compare(node.bound, this.bound); } } public class BranchAndBound { static int n = 4; // 物品数量 static int[] w = {2, 3, 4, 5}; // 物品重量 static int[] p = {3, 4, 5, 6}; // 物品价值 static int c = 8; // 背包容量 public static void main(String[] args) { PriorityQueue<Node> queue = new PriorityQueue<Node>(); Node u = new Node(-1, 0, 0, bound(-1, 0, 0), null); queue.offer(u); int maxValue = 0; while (!queue.isEmpty()){ Node node = queue.poll(); if (node.bound > maxValue && node.level < n - 1){ Node left = new Node(node.level + 1, node.profit + p[node.level + 1], node.weight + w[node.level + 1], bound(node.level + 1, node.profit + p[node.level + 1], node.weight + w[node.level + 1]), node); if (left.weight <= c && left.profit > maxValue) maxValue = left.profit; queue.offer(left); Node right = new Node(node.level + 1, node.profit, node.weight, bound(node.level + 1, node.profit, node.weight), node); if (right.bound > maxValue) queue.offer(right); } } System.out.println("最大价值是:" + maxValue); } public static double bound(int i, int profit, int weight){ if (weight > c) return 0; double bound = profit; int j = i + 1; int totalWeight = weight; while (j < n && totalWeight + w[j] <= c){ totalWeight += w[j]; bound += p[j]; j++; } if (j < n) bound += (c - totalWeight) * (p[j] * 1.0 / w[j]); return bound; } } ``` 这个实现中,我们使用了一个优先队列来维护所有的节点,并按照上界从大到小的顺序进行搜索。在搜索过程中,我们首先取出上界最大的节点,然后分别计算选择该节点的左右儿子节点,将这两个节点加入队列中。在计算左右儿子节点的上界时,我们使用了贪心策略,即尽量选择价值最高的物品。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小俱的一步步

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值