|memory size is too high, not sure how to delete var when done with it?|
When I change the line
echo $combination."<br>"; to anything else, I get this error:
. I get the error if I delete the line, or if I replace it with anything else. I want to replace that line with rules that say which resulting combinations should have more stuff done with them. After that, that resulting combination can disappear.
|Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 35 bytes) in /home/blahblahblah.php on line blah |
for ($i = 1;$i<$combocount;$i++)
$binary = str_pad(decbin($i), $length, "0", STR_PAD_LEFT);
What exactly are you trying to do?
You may have infinite loop.
|brotherhood of LAN|
Welcome to the forums Max,
With your current array that's 16 million combinations, for your for loop has to iterate that many times. Make sure this is necessary as there may be a more efficient way.
In RE: unset a variable, using unset() for a variable doesn't free the memory up instantly, it will only tell PHP that 'it can if it likes when it has the time'. Some of the comments on the manual page [php.net] may shed some more light.
PHP isn't exactly efficient with memory when it comes to variables as it's flexible about the size and type of them, so if memory is a constraint you may want to look into some of the memory functions [php.net] that will use your memory more optimally.
If the server you're working on allows you to, you can also change the amount of memory a PHP invocation is allowed using ini set [php.net], e.g.
That is a string representation of the binary also, which will take up more bytes than it needs to.
What I am trying to do overall is to get combinations of height ($h[?], width ($w[?]), and thickness ($t[?]) of 8 items in the shopping cart. I am doing this to stack the items to get an idea of what pkg size to charge people for. People rarely buy more than 2 or 3 items from me at a time. if it matters, $h, $w, and $t are all numbers in session vars ($_SESSION['productHeight1'] is $h, etc.). Some have decimal points with 4 or fewer numbers after the decimal point. In some cases $h, $w, and $t are not set, "", or 0. Sometimes different variables contain the same number, which makes me think maybe I should be getting permutations instead of combinations?
I need every combination where
1. There are 8 elements in the resulting combination
2. The resulting combination has 1 of every number 0-7 (,,, etc.)
3. The resulting combination does not contain more than 1 of each number 0-7
When I add the array elements from a resulting combination, that is 1 dimension of a possible package size. From there I can figure out myself how to get dimensions 2 and 3. How can I get dimension 1 without running out of memory though? I need to include the 3 rules to get the right combinations.
|brotherhood of LAN|
If it's a case of doing this to work out the optimal storage space then I think you're overcomplicating it a bit.
Instead of thickness I'll call it breadth, so you have length, breadth and height.
Add up all lengths, all breadths and all heights and see which one is the largest of the 3. Use that to 'stack' the items on top of each other.
So if height is the largest value, then take the maximum breadth and maximum width to work out what volume of space is needed.
it's been a while since I was at school but I'm sure the answer is relatively easy to find the best fit, if I understand the problem correctly.
[edited by: brotherhood_of_LAN at 8:46 pm (utc) on Sep 22, 2012]
So you are trying to get the area of the package that can contain the selected items, right?
In that case I agree with brotherhood of LAN - you're overcomplicating it.
My way to find solution:-
STOP, thinking in programming language. Open paint or any similar program and create 2 or 3 items with different height, width, depth (breadth / thickness) and keep them nearby to make one package. Try different combinations and you will get the answer. It's that simple :)
So, if you arrange all selected items width wise then try ->
Highest Height of selected items x Highest Depth (breadth / thickness) of selected items x (width of item 1 + width of item 2 + ...)
Item Name = H x D x W
item 1 = 1 x 2 x 3
item 2 = 4 x 5 x 6
4 * 5 * (3 + 6) = ? :)
The normal way to program these things is either as an LP problem (if you have many boxes to stack), or using backtracking (if the total number of combinations is manageable.
Trying to build a data structure that holds all possible combinations will quickly overwhelm resources.
Linear Programming is too difficult to explain in here - It's the same sort of thing used by e.g. a GPS to determine the "optimal" route - well they find a "good enough" one, it's not guaranteed to be optimal.
Backtracking is basically how you solve a maze when being directionally challenged: you chose at an intersection always the rightmost choice you've not yet tried, when you run into a deadend - you "backtrack" to the last intersection, when you run out of untried options at an intersections, you backtrack to the previous intersection.
You can use this to solve a maze (stop at the first solution) - or if you keep track of solutions, you can determine a list of solution as well as the number of (different) solutions, or if you find a cost or other metric, you can optimize and chose the best solution (Guaranteed best).
In modern languages this is implemented by a recursive function (a function that calls itself).
But: I'd agree it's probably overkill, unless you're the size of amazon; but by then you go to free shipping and it all means nothing anymore.