java - 装箱暴力破解法

标签 java brute-force bin packing

我需要编写解决装箱问题的程序,但我已经编写了优先拟合和贪心算法,但我的讲师说在某些情况下它找不到问题的最小解。所以我决定尝试暴力破解,但我不知道它应该如何检查所有可能的解决方案。所以是的......有人可以向我解释或提供伪代码或其他东西。我将不胜感激。

最佳答案

请注意 bin-packing是一个 NP-hard 问题,基本上意味着它会花费过长的时间对其进行蛮力计算,即使对于相对较小的输入也是如此,因此对 NP-hard 问题进行蛮力计算几乎从来都不是一个好主意。上面的链接显示了一些替代方案(或近似值)。但我会继续...

Recursion使蛮力变得容易。一旦你明白a basic recursive algorithm , 继续阅读...

基本思路:(对于 3 个项目,2 个箱子,假设一切都合适,如果它不只是跳过那个分支)

Put the first item in the first bin.
  Put the second item in the first bin.
    Put the third item in the first bin.
      Woo-hoo! We have a solution!
    Remove the third item from the first bin and put it into the second bin.
      Woo-hoo! We have a solution!
    Remove the third item from the second bin.
  Remove the second item from the first bin and put it into the second bin.
    Put the third item in the first bin.
      Woo-hoo! We have a solution!
    Remove the third item from the first bin and put it into the second bin.
      Woo-hoo! We have a solution!
    Remove the third item from the second bin.
  Remove the second item from the second bin.
Remove the first item from the first bin and put it into the second bin.
  Put the second item in the first bin.
    Put the third item in the first bin.
      Woo-hoo! We have a solution!
    Remove the third item from the first bin and put it into the second bin.
      Woo-hoo! We have a solution!
    Remove the third item from the second bin.
  Remove the second item from the first bin and put it into the second bin.
    Put the third item in the first bin.
      Woo-hoo! We have a solution!
    Remove the third item from the first bin and put it into the second bin.
      Woo-hoo! We have a solution!
    Remove the third item from the second bin.
  Remove the second item from the second bin.
Remove the first item from the second bin.

(看看已经有多少步了?这只是 3 件元素和 2 个箱子)

伪代码:

recurse(int itemID)
  if pastLastItem(itemID)
    if betterThanBestSolution
      bestSolution = currentAssignment
    return
  for each bin i:
    putIntoBin(itemID, i)
    recurse(itemID+1)
    removeFromBin(itemID, i)

关于java - 装箱暴力破解法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16083977/

相关文章:

c++ - 带有恢复选项的 C++ 中的暴力字符串生成器

地穴暴力永无休止的循环

file - 如何使用 VBScript 读取二进制文件

java -/bin 在 JDK 中丢失

java - 如何在 Play 框架中运行异步/非阻塞 MySQL 查询?

java - 在不同线程、JVM 和服务器之间使用 Hibernate

java - 在实例化的 boolean 数组上获取 NPE

java - 字典算法,计算所有案例选项

node.js - docker 容器中的 NPM 安装失败并返回 "The command '/bin/sh -c npm install' 返回非零代码 : 1"

java - Spring @Transactional 传播属性