0

I wrote a heuristic algorithm for the bin packing problem using best-fit aproach, itens S=(i1,...,in), bins size T, and a want to create a real exact exponential algorithm witch calculates the optimal solution(minimum numbers of bins to pack all the itens), but I have no idea how to check every possibility of packing, I'm doing in C.

Somebody can tell me any ideas what structs I have to use? How can I test all de combinations of itens? It has to be a recursive algorithm? Have some book ou article that may help me?

sorry for my bad english

MeatPuppet
  • 13
  • 4

1 Answers1

0

The algorithm given will find one packing, usually one that is quite good, but not necessarily optimal, so it does not solve the problem.

For NP complete problems, algorithms that solve them are usually easiest to describe recursively (iterative descriptions mostly end up making explicit all the book-keeping that is hidden by recursion). For bin packing, you may start with a minimum number of bins (upper Gaussian of sum of object sizes divided by bin size, but you can even start with 1), try all combinations of assignments of objects to bins, check for each such assignment that it is legal (sum of bin content sizes <= bin size for each bin), return accepting (or outputing the found assignment) if it is, or increase number of bins if no assignment was found.

You asked for structures, here is one idea: Each bin should somehow describe the objects contained (list or array) and you need a list (or array) of all your bins. With these fairly simple structures, a recursive algorithm looks like this: To try out all possible assignments you run a loop for each object that will try assigning it to each available bin. Either you wait for all objects to be assigned before checking legality, or (as a minor optimization) you only assign an object to the bins it fits in before going on to the next object (that's the recursion that ends when the last object has been assigned), going back to the previous object if no such bin is found or (for the first object) increasing the number of bins before trying again.

Hope this helps.

UlFie
  • 99
  • 3
  • Here is a dp solution it's faster then your algorithm: http://stackoverflow.com/questions/8310385/bin-packing-brute-force-recursive-solution-how-to-make-it-faster – Gigamegs Mar 27 '13 at 15:03
  • Yeah, for *NP complete problems* it is of utmost importance to know complicated, by no means instructive algorithms because their complexity is ... still exponential. That is truly what has been asked for... – UlFie Mar 27 '13 at 15:27
  • Can you elaborate? What is complete and what is np-hard? – Gigamegs Mar 27 '13 at 17:19
  • Summarizing what can be found in Wikipedia: NP is the class of decision problems for which *non-deterministic* Turing machines exist that will accept (or reject) any input in polynomial runtime (i.e. number of steps of the Turing machine is bounded by a polynomial in the size of the input). – UlFie Mar 27 '13 at 17:42
  • A problem is NP hard if any problem in NP can be reduced to it in polynomial time, i.e. a *deterministic* Turing machine can convert the input of the problem in NP to an input of the other problem in a number of steps bounded by a polynomial in the size of the input such that the converted input is accepted by a Turing machine for that problem if and only if it is accepted by a Turing machine for the problem in NP. – UlFie Mar 27 '13 at 17:44
  • Finally, a problem is NP complete if it is NP hard and in NP. – UlFie Mar 27 '13 at 17:46
  • Ok. Thanks but what I want to know if the code from the solution I posted is a dp algorithm? I think it just a recursive solution. It's not about np hard or complete. It was just a reflex from me? – Gigamegs Mar 27 '13 at 17:46
  • It is unknown whether P = NP (where P is the class of problems for which *deterministic* polynomial runtime Turing machine accepting them exist). It is assumed that this equation does *not* hold, one reason being that so far no algorithms were found that would *deterministically* solve NP complete problems in less than exponential time. – UlFie Mar 27 '13 at 17:50
  • DP (as far as I know) means Dynamic Programming, a fairly general approach to optimization problems. As the associated decision problems are usually NP complete, you cannot expect any better runtime behaviour from this approach. You are right, essentially, it is merely a recursive solution (established in one specific framework). – UlFie Mar 27 '13 at 17:55
  • Yes but this code stackoverflow.com/questions/8310385/bin-packing-brute-force-recursive-solution-how-to-make-it-faster is this dp or not dp? – Gigamegs Mar 27 '13 at 17:56
  • Yes, that's what it looks like. – Gigamegs Mar 27 '13 at 18:02
  • According to the reference given, it is. But I am not an expert in dynamic programming, so I won't judge. Sometimes people put a label on something not really knowing what they are doing... – UlFie Mar 27 '13 at 18:04
  • As i cannot edit older comments: The definition of NP hard above needs to be fixed near the end: ... if and only if *the original input* is accepted ... – UlFie Mar 27 '13 at 18:09
  • How can I try all the possibilities? I'm kinda stuck in this part – MeatPuppet Mar 28 '13 at 13:51
  • Start with all bins empty. For object number i, loop over all bin numbers j, check if object i fits in bin j, if so, place it there and recurse to object i+1. After returning, remove from bin (and continue with bin loop). Trying to place object n+1 means all objects have been placed in bins, so recursion ends. Obviously, you start with the first object, your recursive function gets the object to be placed as a parameter as well as your data structure for the bins. – UlFie Mar 29 '13 at 15:50
  • Keep in mind you need a return value which could be either just a Boolean or a copy of the bins with all the objects in them or even some list containing all found assignments (or something like NULL if no such assignment was found so far, which will be the case if the bin loop found no bin object i could be placed in). – UlFie Mar 29 '13 at 15:50