在 Java 中使用暴力破解 3D 背包

发布于 2024-12-27 08:23:24 字数 593 浏览 0 评论 0原文

我想解决一个3维背包问题。

我有许多不同宽度、高度、长度和价值的盒子。我有一个指定的空间,我想把盒子放在那个空间里,这样我就能获得最优的利润。我想用暴力来做到这一点。

我正在用 Java 编程。 我尝试用递归来做到这一点,所以:

public void solveBruteforce(double freeX, double freeY, double freeZ) {
   for(int i = 0; i < numOfBoxes; i++) {
      for(int j = 0; j < BoxObject.numOfVariations; j++) {
         if(possible to place box) {
            place(box);
            add(value);
            solveBruteforce(newX, newY, newZ);
         }
      }
   }
   remove(box);
   remove(value);
}

但我会遇到每行都有不同的自由 x、y 和 z 的问题。

有人可以帮我找到另一种方法吗?

I want to solve a 3-dimesional knapsack problem.

I got a number of boxes with a different width, height, length and value. I got a specified space and I want to put the boxes into that space, such that I will get the optimal profit. I would like to do it with using bruteforce.

I'm programming in Java.
I tried to do it with recursion, so:

public void solveBruteforce(double freeX, double freeY, double freeZ) {
   for(int i = 0; i < numOfBoxes; i++) {
      for(int j = 0; j < BoxObject.numOfVariations; j++) {
         if(possible to place box) {
            place(box);
            add(value);
            solveBruteforce(newX, newY, newZ);
         }
      }
   }
   remove(box);
   remove(value);
}

But I will get the problem that each line has a different free x, y and z.

Could someone help me to find another way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尛丟丟 2025-01-03 08:23:24

首先,使用八叉树来跟踪事物在空间中的位置。占用树是一棵 3D 4 出度树,每个节点都有占用标志,将您的空间划分为可有效搜索的位置。如果您想使用某种启发式搜索来放置框,即使您正在尝试所有可能性,这也会很有用。它可以快捷地禁止(拥挤)的位置。

暴力破解将需要很长时间。但如果这就是您想要的,您需要定义一个顺序来尝试展示位置的排列。

由于您将需要多次迭代,因此递归并不是很好,因为您会遇到堆栈溢出。

第一个草案的替代方案将涉及贪婪算法。取出使您的利润最大化的盒子(例如最大的盒子),放置它,然后取出下一个最大的盒子,并找到最适合的盒子,依此类推。

但是,假设您想尝试所有可能的组合:

def maximize_profit(boxes,space):
    max_profit = 0
    best_fits = list()
    while(Arranger.hasNext()):
        a_fit,a_profit = Arranger.next(boxes,space)
        if (a_profit == max_profit):
            best_fits.append(a_fit)
        elif (a_profit > max_profit):
            max_profit = a_profit
            best_fits = [ a_profit ]
    return best_fits, max_profit  

有关如何定义编曲器的想法,请考虑从 #{space} 可能性中选择 #{box} 插槽,尊重与对称性相同的排列。或者,也许“洪水填充”方法会给你一些想法。

First thing is, use an octree to keep track of where things are in the space. Occupancy tree is a 3D 4-out-degree tree, with occupancy flags at every node, dividing your space into a place that is efficient to search over. This would be useful if you want to use some kind of heuristic search to place the boxes, and even if you are trying all possibilities. It can shortcut the forbidden (crowded) placements.

Brute force will take a long time. But if that's what you want you need to define an ordering for trying out permutations of placements.

Since you will need many iterations, recursion is not so great since you will get a stack overflow.

A first-draft alternative would involve a greedy algorithm. Take the box that maximizes your profit (say, the largest), place that, then take the next largest box, and find the best fit for that, and so on.

But, say you wanted to try all possible combinations:

def maximize_profit(boxes,space):
    max_profit = 0
    best_fits = list()
    while(Arranger.hasNext()):
        a_fit,a_profit = Arranger.next(boxes,space)
        if (a_profit == max_profit):
            best_fits.append(a_fit)
        elif (a_profit > max_profit):
            max_profit = a_profit
            best_fits = [ a_profit ]
    return best_fits, max_profit  

For ideas on how to define the Arranger, think about choosing #{box} slots from #{space} possibilities, respecting arrangements that are identical w.r.t. symmetry. Alternately maybe a "flood fill" method will give you ideas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文