DEV Community

Cover image for Optimizing Container Loading for a Logistics Company with the LAFF Algorithm
Jacky
Jacky

Posted on

Optimizing Container Loading for a Logistics Company with the LAFF Algorithm

When I worked with a large Singaporean logistics customer, we encountered a problem regarding the optimization of box placement within containers to reduce wasted space. This is the approach I applied while working as a Backend Engineer.

Shipping products efficiently is critical for reducing costs in logistics. One way to optimize shipping is through effective 3D bin packing — fitting boxes and products efficiently into shipping containers. This allows you to use fewer containers and reduce wasted space.

Manually packing boxes is inefficient. Humans struggle to visualize the optimal arrangement in 3D space. This leads to suboptimal container loading and wasted capacity.

Instead, we can apply algorithms that are designed to optimize 3D bin packing. One effective approach is the Largest Area First Fit (LAFF) algorithm.

How the LAFF Algorithm Works

The LAFF algorithm packs boxes in this order:

  • 1. Sort boxes by maximum face area, largest to smallest. Pack the largest box into the container first. Position it to minimize height.
  • 2. Try to fit smaller boxes into any remaining open spaces around the packed box.
  • 3. Repeat steps 2–3, packing the next largest box and fitting smaller boxes in open spaces.
  • 4. Continue until all boxes are packed or no more fit.
  • 5. Benefits for Shipping Logistics

The LAFF algorithm provides these key benefits for shipping operations:

  • Maximizes space utilization — Reduces wasted capacity and fits more boxes per container.
  • Minimizes containers needed — By fully optimizing box packing, you can ship the same amount with fewer containers.
  • Saves on shipping costs — Fewer containers means lower total shipping expenses.
  • Easy to implement — The LAFF logic is straightforward to code and integrate into packing software.
  • Fast packing times — Simple heuristics run efficiently even for large box sets.

Recommendation for Implementation

To optimize your shipping operations, we recommend implementing the LAFF packing algorithm in your packing software. The algorithm can be coded in any language like Java, C++, Python, etc.

Simple snippet code in Java:

// Box class 
class Box {
  int width;
  int depth;
  int height;

  // Constructor, getters, etc.
}

// Bin class
class Bin {
  int width;
  int depth;
  int height;

  List<Box> packedBoxes;

  // Constructor, getters, setters
}

class LaffPacker {

  // Sort boxes by max face area
  void sortBoxes(List<Box> boxes) {
    boxes.sort((b1, b2) ->  
      Integer.compare(maxFaceArea(b2), maxFaceArea(b1))); 
  }

  int maxFaceArea(Box box) {
    return Math.max(box.width * box.depth, 
               Math.max(box.width * box.height, 
                        box.depth * box.height));
  }

  // Pack boxes into bin
  void packBoxes(List<Box> boxes, Bin bin) {
    sortBoxes(boxes);

    for (Box box : boxes) {
      packBox(box, bin);
      fitRemainingBoxes(boxes, bin);
    }
  }

  // Pack single box into bin
  void packBox(Box box, Bin bin) {
    bin.packedBoxes.add(box);
    bin.height = Math.max(bin.height, box.height);
  }

  // Try to fit remaining boxes around packed box
  void fitRemainingBoxes(List<Box> boxes, Bin bin) {
    for (Box box : boxes) {
      if (fitsWidth(box, bin) || fitsDepth(box, bin)) {
        packBox(box, bin);
      }
    }
  }

  // Check if box fits in empty width or depth
  boolean fitsWidth(Box box, Bin bin) {
    // Check if box fits in width  
  }

  boolean fitsDepth(Box box, Bin bin) {
    // Check if box fits in depth
  }

}
Enter fullscreen mode Exit fullscreen mode

How to use it:

List<Box> boxes = new ArrayList<>(); // add boxes
Bin bin = new Bin(10, 10);

LaffPacker packer = new LaffPacker();
packer.packBoxes(boxes, bin);
Enter fullscreen mode Exit fullscreen mode

This only simple code to start working with our idea. In real projects, we will also implement many complex logic and operations. But this will be the starting point…We approach everything in the simplest way possible in our thinking

For best results, integrate LAFF with your order management and container loading processes. As new orders come in, run the algorithm to recommend the optimal box packing for those products. Then load containers based on the LAFF output.

This approach will maximize your container space utilization and reduce overall shipping costs. There will be many other algorithms to optimize costs that we can refer to and discuss.

*Referral:
*
An Efficient Algorithm for 3D Rectangular Box Packing

Top comments (1)

Collapse
 
gizmowannabe profile image
Teck Seng Loo

Hi, I was reading up on the LAFF algorithm and was wondering if load balancing is considered as well?