Welcome to WebmasterWorld Guest from 54.227.5.198

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

memory size is too high, not sure how to delete var when done with it?

     

Max Maxington

6:28 am on Sep 22, 2012 (gmt 0)



When I change the line
echo $combination."<br>";
to anything else, I get this error:
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 35 bytes) in /home/blahblahblah.php on line blah
. 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.

$params=array($h[0],$h[1],$h[2],$h[3],$h[4],$h[5],$h[6],$h[7],$w[0],$w[1],$w[2],$w[3],$w[4],$w[5],$w[6],$w[7],$t[0],$t[1],$t[2],$t[3],$t[4],$t[5],$t[6],$t[7]);
$combinations=getCombinations($params);
function getCombinations($array)
{
$length=sizeof($array);
$combocount=pow(2,$length);
for ($i = 1;$i<$combocount;$i++)
{
$binary = str_pad(decbin($i), $length, "0", STR_PAD_LEFT);
$combination='';
for($j=0;$j<$length;$j++)
{
if($binary[$j]=="1")
$combination.=$array[$j];
}
$combinationsarray[]=$combination;
echo $combination."&lt;br&gt;";
}
return $combinationsarray;
}

Dinkar

2:33 pm on Sep 22, 2012 (gmt 0)

10+ Year Member



What exactly are you trying to do?

You may have infinite loop.

Dinkar

2:35 pm on Sep 22, 2012 (gmt 0)

10+ Year Member



BTW, try

unset ($not_needed_variable);

brotherhood of LAN

3:06 pm on Sep 22, 2012 (gmt 0)

WebmasterWorld Administrator brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Welcome to the forums Max,

$combocount=pow(2,$length);

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.

copyini_set('memory_limit','256M');

// added
decbin($i)

That is a string representation of the binary also, which will take up more bytes than it needs to.

Max Maxington

6:44 pm on Sep 22, 2012 (gmt 0)



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[0], 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 ([0],[1],[2], 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

7:40 pm on Sep 22, 2012 (gmt 0)

WebmasterWorld Administrator brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

// disclaimer
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]

Dinkar

8:20 pm on Sep 22, 2012 (gmt 0)

10+ Year Member



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 + ...)

Example:
Item Name = H x D x W
item 1 = 1 x 2 x 3
item 2 = 4 x 5 x 6

So:-
4 * 5 * (3 + 6) = ? :)

swa66

9:01 pm on Sep 22, 2012 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month