The branch and bound method of the five algorithms

Question: There is a batch of n containers to be loaded on 2 ships of c1 and c2 respectively. The weight of container i is wi, and it is required to determine whether there is a reasonable loading plan to load these n containers on this ship. 2 ships.

Abstract: Put n items into 2 containers, each container cannot be overweight, and find a feasible solution.

Idea: Fill the first ship as much as possible, and then load the remaining containers on the second ship. If the second ship cannot be loaded, the problem is unsolved. Then the problem turns into finding the maximum load of the first ship. The solution space search is performed using the branch and bound method.

code show as below:

package test;
 
import java.util.LinkedList;
 
/**
 * Use the branch and bound method to solve the loading problem
 *
 * @author yanghang
 *
 */
public class Fenzhidingjie {
 
    private static float[] w = { 40, 40, 10 }; // Array of container masses
    private static int n = w.length; // number of boxes
    private static float c1 = 50; // first ship's deadweight
    private static float c2 = 50; // second ship's deadweight
 
    private static float bestw; // max load for the first ship
    private static float ew; // current ship's load
    private static LinkedList<Float> mq = new LinkedList<Float>(); // FIFO队列
 
    /**
     * optimal loading value
     *
     * @param c
     */
    public static float maxLoading(float c) {
        mq.addLast(new Float(-1)); // Initialize the node queue, mark the layer
        int i = 0; // layer of E-node
        ew = 0; // current ship's load
        bestw = 0; // current best value
 
        while (!mq.isEmpty()) { // search the subset space tree
            if (ew + w[i] <= c) { // Check if the left child of E-node, container i can be loaded
                addLiveNode(ew + w[i], i); // Container i can be loaded
            }
 
            addLiveNode(ew, i); // right child is always available, no cargo i is loaded
 
            ew = (Float) mq.removeFirst(); // remove the next node
 
            if (ew == -1) { // reach the end of the layer
                if (mq.isEmpty()) {
                    return bestw;
                }
                mq.addLast(new Float(-1));
                ew = (Float) mq.removeFirst(); // remove the next node
                i++; // layer of ew
            }
        }
        return bestw;
    }
 
    /**
     * Join the team
     *
     * @param wt
     * @for me
     */
    public static void addLiveNode(float wt, int i) {
        if (i == n - 1) { // subscript starts from 0, it is a leaf
            if (wt > bestw) {
                bestw = wt;
            }
        } else { // not a leaf
            mq.addLast(new Float(wt));
        }
    }
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // sum of weights of all boxes
        float s = 0;
        for (float f : w) {
            s += f;
        }
        if (s <= c1 || s <= c2) {
            System.out.println("need only one ship!");
        }
        if (s > c1 + c2) {
            System.out.println("no solution!");
            return;
        }
 
        float bestw = Fenzhidingjie.maxLoading(c1);
 
        if (s - bestw <= c2) {
            System.out.println("The first ship loading " + bestw);
            System.out.println("The second ship loading " + (s - bestw));
        } else {
            System.out.println("no solution!");
        }
 
    }
 
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325594829&siteId=291194637