Re: Calculate parcel size in PHP




"C. (http://symcbean.blogspot.com/)" <colin.mckinnon@xxxxxxxxx> a écrit dans
le message de news:
786b29db-0b57-4c91-9ae6-baa0809e5227@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Jun 16, 4:55 pm, "Bob Bedford" <b...@xxxxxxxxxxx> wrote:
Hi all,

I've an internet shop and I'm facing a big problem with delivery costs.
I'd
like to find a way to calculate the littlest possible box giving other
actual purchased box size.

Does somebody have an idea on how to do so ? This is not particular to
PHP,
but if somebody has a PHP function, I'm interested for it !

Thanks for helping.

Bob
I love a challenge, so even though its rather off topic....

A simple solution might be to work out the minimal box volume by
iteratively adding the individual items to generate compound boxes.

So, given box A and box B

1) Place box A against box B, calculate volume as total length x total
width x total height, store orientation of A and B
2) rotate A 90 deg around X axis (i.e. swap length and breadth), if
resulting volume is less than stored, store orientation and volume
3) rotate A around Y axis (i.e. swap breadth and height), if resulting
volume is less than stored, store orientation and volume
4) rotate A around Z axis (i.e. swap length and height)
5) rotate B around X axis, repeat steps 2-4
6) totate B around Y axis, repeat steps 2-4
7) rotate B around Z axis, repeat steps 2-4
5) rotate B around Y axis
(i.e. there are 3x3 possible orientation for the 2 boxes).

Another way to think about is this - each box has dimensions width
(w),length(l),height(h) so all possible combinations are:

(load as a CSV file in sa spreadsheet)

box a,box b,compound width,compund length,compound height,bounding box
volume
w,w,a.w+b.w,"MAX(a.l,b.l)","max(a.h,b.h)","(a.w+b.w)*MAX(a.l,b.l)*MAX
(a.h,b.h)"
w,l,a.w+b.l,"MAX(a.l,b.h)","max(a.h,b.w)","(a.w+b.l)*MAX(a.l,b.h)*MAX
(a.h,b.l)"
w,h,a.w+b.h,"MAX(a.l,b.w)","MAX(a.h,b.l)","(A.w+b.h)*MAX(a.l,b.w)*MAX
(a.h,b.l)"
l,w,a.l+b.w,"MAX(a.h,b.l)","MAX(a.w,b.h)","(a.l+b.w)*MAX(a.h,b.l)*MAX
(a.w,b.h)"
l,l,a.l+b.l,"MAX(a.h,b.h)","MAX(a.w,b.w)","(a.l+b.1)*MAX(a.h,b.h)*MAX
(a.w,b.w)"
l,h,a.l+b.h,"MAX(a.h,b.w)","MAX(a.w,b.l)","(a.l+b.h)*MAX(a.h,b.w)*MAX
(a.l,b.l)"
h,w,a.h+b.w,"MAX(a.w,b.l)","MAX(a.l,b.h)","(a.h+b.w)*MAX(a.w,b.l)*MAX
(a.l,b.h)"
h,l,a.h+b.l,"MAX(a.w,b.h)","MAX(a.l,b.w)","(a.h+b.l)*MAX(a.w,b.h)*MAX
(a.l,b.w)"
h,h,a.h+b.h,"MAX(a.w,b.w)","MAX(a.l,b.l)","(a.h+b.h)*MAX(a.w,b.w)*MAX
(a.l,b.l)"

Then you find the minimal bounding box volume.

Then repeat with subsequent boxes.

Note that to find the true optimal packing strategy you'd need to
apply the method above to all possible orientations of all possible
boxes - i.e. finding the minimum of 3^N calculations.

Without knowing what N is before hand you'd need to use a recursive
function.

So although it won't be as good a solution, it might be simpler to
calculate the box required for A and B (AB) then calculate the size
required for AB and C....that reduces the algorithm order to (N-1)*3^2

The full set of combinations above are generally referred to as a
cartesian product. You could push all the computation cost into the
database, e.g.

where 0 is width, 1 is length and 2 is height....
sku, dimension, type
PC, 90, 0
PC, 86, 1
PC, 25, 2
KeyB, 80, 0
KeyB, 25, 1
KeyB, 5, 2
Mouse, 20, 0
Mouse, 15, 0
Mouse, 7, 0
.....

Then for 2 boxes...

SELECT a.dimension+b.dimension as compound_width,
GREATEST(a1.dimension, b1.dimension) as compound_length,
GREATEST(a2.dimension, b2.dimension) as compound_height,
(a.dimension+b.dimension as compound_width)
* GREATEST(a1.dimension, b1.dimension)
* GREATEST(a2.dimension, b2.dimension) AS compound_volume
FROM
prods a,
prods b
prods a1,
prods b1,
prods a2
prods b2
WHERE a.sku=a1.sku
AND a1.sku=b.sku
AND a1.dtype=MOD(a.dtype+1,3)
AND a2.dtype=MOD(a.dtype+2,3)
AND b.sku=b1.sku
AND b1.sku=b2.sku
AND b1.dtype=MOD(b.dtype+1,3)
AND b1.dtype=MOD(b.dtype+2,3)
AND a.sku='$first_product'
AND b.sku='$second_product'
ORDER BY compound_volume
LIMIT 1,1

But where this becomes even more messy is that you probably only have
a finite number of packing crates available in different sizes and
shapes - it doesn't follow that the smallest (cheapest) outer packing
crate required to accomodate the volume returned above is the smallest
one which will accomodate all the contents: if you try to minimise
volume using the algorithm above, it will tend towards a cube shape -
but if all your boxes are very long and thin, then you'll need a very
big box to accomodate the width and height calculated.

HTH

C.

Thank you for your code, Colin. I'll have a try to those things as I've
first to understand the philosophy of what you have done.
Thank you also for the time you have spend to create such a huge answer, I
really appreciate it.

Cheers


.



Relevant Pages

  • Re: Calculate parcel size in PHP
    ... store orientation of A and B ... rotate A around Y axis, ...
    (comp.lang.php)
  • Re: newbie: rotating meshes
    ... The problems I noticed, were if you for example rolled the ship, then tried ... not the rolled x axis, which says to me that the math for matrix ... concatenation doesnt rotate the values of the matrix correctly is some ... which meant the y roation rotated the models x axis whereas the z rotation ...
    (microsoft.public.win32.programmer.directx.graphics)
  • Re: Really understanding the Rattleback
    ... When resting on a flat surface, if it is given a push to make it rotate around its point of surface contact, it has the characteristic of moving smoothly in that direction of rotation but if given a push to rotate in the other direction, it will start to turn but then, with a rocking motion, come to a stop and begin rotating in the direction it somehow prefers. ... Take something rectangular like a pack of cards or cigarette packet, ideally something with uniform mass distribution and the 3 axes different lengths. ... even one tumble on the intermediate axis is enough to have it coming down rotated partly on another axis. ... Unfortunately since this is a rectangle it would need to be pivoted and attached to pivot with a couple of springs, or a curved surface added and rocked on the ground. ...
    (rec.puzzles)
  • Re: Object rotation
    ... > need to translate back to the origin first, then rotate, then translate to ... For example rotations are ALWAYS around an axis so if you ... then moves on the new x axis after the Z rotation. ...
    (microsoft.public.win32.programmer.directx.managed)