# Identifying Rack Cabinets by Devices

Hello,

I can determine Rack Cabinets according to the number of devices with the code below. But there is a problem

The “U” indicates how much space will be used by the places where the devices will be mounted inside the rack cabinets.

It is clear how many U of each selected device, I calculate the total U value of the selected devices
No problem here

When I use the code I use for Patch Panel here, it gives the result:
`\$rackcabinetarray = array("12","16","20","26","32","36","42","44","47");`
If we search for rack cabinet for 48 U, it will give the following result
47+12
One is the biggest and the other is the smallest.
example is 48, but even if this value is much higher, rack cabinets should be close to each other.
for example 48. 48/2= 24. However, there is no 24 U cabinet, instead, 2 pieces of 26 U cabinets would be more suitable.
How to calculate for this?

Thank you

Adem, another puzzle? Nice!

Are you just wanting to select the next value up? Or do you wish to locate the best fit?
Searching with 48 would be best fit with one panel of 12 and one of 36 giving exactly 48.
Two 26 panels would give you 52 and you would waste four positions.

Let us know which you prefer, best fit or just the higher value.

[quote=“ErnieAlex, post:2, topic:33721”]
Adem, another puzzle? Nice!
[/quote]

A few extra U-values don’t matter, there must already be unusable gaps
Here, if there is more than one cabinet, it is not appropriate to have one 47 U and the other 12 U
It is not preferred as it will not look good, It is preferable to have the same dimensions as possible.

Well, you could use a function that has an input of the value you are looking for.
If an exact fit, return that number. If not, divide by 2 and return the next higher value.
This might work:

``````<?php
//  Function to locate best value or higher
function best_rack(\$rack) {
\$rackcabinetarray = array(12, 16, 20, 26, 32, 36, 42, 44, 47);
//  See if rack matches exactly, if not use two panels
\$index = 0;
foreach (\$rackcabinetarray as \$key=>\$rackcabinet) {
if (\$rackcabinet==\$rack) {
//  Rack number is in array, use it
return \$rackcabinetarray[\$key];
}
}
if (\$index==0) {
//  Use two racks
\$rack = ceil(\$rack/2);
foreach (\$rackcabinetarray as \$key => \$rackcabinet) {
if (\$rack >= \$rackcabinet) {
\$index = \$key;
}
}
return \$rackcabinetarray[\$index] . "-" . \$rackcabinetarray[\$index];
}
}

//  Test with exact values
echo "input: 36 = " . best_rack(36) . "<br>";
//  Test with multiple value
echo "input: 40 = " . best_rack(40) . "<br>";
echo "input: 48 = " . best_rack(48) . "<br>";
echo "input: 57 = " . best_rack(57);
?>
``````

I used integer values for the array, not strings as you did. Using int’s, you can round up the numbers when you divide by 2. Also, I had to add some code to first check for exact matches and the rounding up of values so that you select the next highest value. Copy this into a test file and run as-is to see if it is what you want. You will need to alter it to indicate two panels. I just used a dash between them to indicate two panel values. Hope this works for you!

Thank you very much for your effort
Didn’t give the desired result

input: 36 = 36 → OK, correct result
input: 40 = 20-20 → OK, correct result
input: 48 = 20-20 → 20+20 = 40 U, but the desired 48 U
input: 57 = 26-26 → OK, correct result

Also, what if more than two RACK CABINETS are required?

Something came to my mind amateurishly, I tried to write the code as follows.
I did a few tests and it seems to give the desired result.
However, it will give all cabinets equal.
I have no idea how it will be when the situation like 47+47+47+12 is needed
So, this code is not the best, bad to good

``````function closest(\$number, \$array) {
sort(\$array);
foreach (\$array as \$a) {
if (\$a >= \$number) return \$a;
}
return NULL;
}

// total how many "u" to search
\$total_how_many_u_to_search = 48;

// Rack cabinets
\$rackcabinetarray = array(12, 16, 20, 26, 32, 36, 42, 44, 47);

// To start from the largest rack cabinet
rsort(\$rackcabinetarray);

\$which_cabinet = 0;
\$quantity = 0;
for (\$i = 1; \$i <= 100; \$i++) {

// We do division starting from 1
\$rack = \$total_how_many_u_to_search/\$i;

// Does the result of the partitioning process match or have a superior in the rack cabinet array?
\$rack_kabin = closest(\$rack, \$rackcabinetarray);

// If the return from the function is not null
if(!empty(\$rack_kabin)){

echo \$which_cabinet = \$rack_kabin; // How many "U" cabinets found
echo "<br />";
echo \$quantity = \$i; // Cabinet quantity

// If the return from the function is not null, stop the loop
break;
}
\$i++;
}``````

While copying the code, I missed a line. This DOES work!

``````<?php
//  Function to locate best value or higher
function best_rack(\$rack) {
\$rackcabinetarray = array(12, 16, 20, 26, 32, 36, 42, 44, 47);
//  See if rack matches exactly, if not use two panels
\$index = 0;
foreach (\$rackcabinetarray as \$key=>\$rackcabinet) {
if (\$rackcabinet==\$rack) {
//  Rack number is in array, use it
return \$rackcabinetarray[\$key];
}
}
if (\$index==0) {
//  Use two racks
\$rack = ceil(\$rack/2);
foreach (\$rackcabinetarray as \$key => \$rackcabinet) {
if (\$rackcabinet >= \$rack) {
\$index = \$key;
break;
}
}
return \$rackcabinetarray[\$index] . "-" . \$rackcabinetarray[\$index];
}
}
//  Test with exact values
echo "input: 36 = " . best_rack(36) . "<br>";
//  Test with multiple value
echo "input: 40 = " . best_rack(40) . "<br>";
echo "input: 48 = " . best_rack(48) . "<br>";
echo "input: 57 = " . best_rack(57);
?>
``````

Somehow I missed the the BREAK line. Also, you did NOT mention you might need more that two panels. I only set it up for one or two panels. For more than two panels, you can divide by 3 or more as needed. This code would work. You would just figure out the max number of panels first and then divide by that number. To create the max number of panels, you would find the division results and add one to it. Should work. Here is a better example that allows any number of panels: ( And is smaller code! )

``````<?PHP
//  Function to locate best value or higher
function best_rack(\$rack) {
\$rackcabinetarray = array(12, 16, 20, 26, 32, 36, 42, 44, 47);
//  Locate maximum number of panels needed
\$max = ceil( \$rack / max(\$rackcabinetarray) );
//  Locate correct rack
\$rack = ceil(\$rack/\$max);
foreach (\$rackcabinetarray as \$key => \$rackcabinet) {
if (\$rackcabinet >= \$rack) {
return \$rackcabinetarray[\$key] . " ( times " . \$max . " )";
}
}
}
//  Test with exact values
echo "input: 36 = " . best_rack(36) . "<br>";
//  Test with multiple value
echo "input: 40 = " . best_rack(40) . "<br>";
echo "input: 48 = " . best_rack(48) . "<br>";
echo "input: 57 = " . best_rack(57) . "<br>";
echo "input: 127 = " . best_rack(127) . "<br>";
echo "input: 249 = " . best_rack(249);
?>
``````
1 Like

I’m sorry I didn’t fully foresee everything.
With the code I just wrote and given above, it gave 3 16Us for 48 “U” cabinets.
Yes, 16x3=48, but 2 26U cabinets are preferred instead of 3 rack cabinets.
Rack cabinets may not have enough space to install.
In addition, it is not appropriate to distribute the products in too many cabinets.
According to this, the result should be like this
The first is to have as few cabinets as possible.
Second, there is no rule that the desired “U” cannot be passed, this value can be exceeded.
Sample:
For 48U, 26+26=52 is preferred over 16+16+16=48, because, instead of 3 cabinets, 2 cabinets are preferred since less space and products are distributed less.
Necessary consequences are inevitable, 1,2,3,4,5… cabinet
I’m trying your code now

This gave excellent results
Thank you so much

I want to ask something
For 48U, 16x3=48 exactly match, how does 26x2=52 result?
Instead of 3 cabinets, 2 cabinets are much better

Very sorry, Adem, you did not ask for that either. I will see if there is a way to fix that, too.
I will need time to test it for you.

I do not understand your logic. First, you want the best rack number. You also want to use the less number of racks possible. But, you do not mind wasting empty slots in the racks. This is confusing to me.

Let me ask one question on this. What is the maximum number of devices you would ever need in a full panel of racks? This will let me create a routine for you. If you do not mind wasting devices, and if you know the maximum number of devices possible, then you can create a routine that wastes a small amount and uses less panels. Tell me the max device count.

no no you misunderstood
the code is perfect just what i wanted, works perfectly

I asked to find out
for 48U
3 does not give 16, whereas 16x3=48 is an exact match
Gives 2 26s just like I wanted
what is the code that gives 26 instead of 16

Well, that would be backwards. The way this code works is it starts at the smallest rack-cabinet-array.
Then, it works UP to find the first minimum number that divides correctly and matches up. This is why it came up with 16x3. If you want to use larger amounts you would have to start with the higher numbers of units-per-rack and move down instead of up. Does that make sense? Here is that version…

``````<?PHP
//  Function to locate best value or higher
function best_rack(\$rack) {
\$rackcabinetarray = array(12, 16, 20, 26, 32, 36, 42, 44, 47);
//  Locate maximum number of panels needed
\$max = ceil( \$rack / min(\$rackcabinetarray) );
//  Locate correct rack
\$rack = ceil(\$rack/\$max);
foreach (array_reverse(\$rackcabinetarray) as \$key=>\$rackcabinet) {
if (\$rackcabinet >= \$rack) {
return \$rackcabinetarray[\$key] . " ( times " . \$max . " )";
}
}
}
//  Test with exact values
echo "input: 36 = " . best_rack(36) . "<br>";
//  Test with multiple value
echo "input: 40 = " . best_rack(40) . "<br>";
echo "input: 48 = " . best_rack(48) . "<br>";
echo "input: 57 = " . best_rack(57) . "<br>";
echo "input: 127 = " . best_rack(127) . "<br>";
echo "input: 249 = " . best_rack(249);
?>
``````

But, it might be much easier to just create a table with all the possible unit numbers and the corresponding rack sizes and counts. Then, you could just do a simple lookup instead of calculations.

1 Like

I was wondering how the code works, I wanted to learn the logic
I’ve tried a lot, it’s amazing, it works exactly as I want it. Thank You
The important thing here is not the closest to the desired result, but the least number of cabinets.

I was using the following switch before. But your code is much better, thank you again

``````    case \$rack_kabin_kac_u_olacak <= 138 : \$hedefsecenek= array(
"47-47-44", "32-32-32-44"); break;
case \$rack_kabin_kac_u_olacak <= 141 : \$hedefsecenek= array(
"47-47-47", "32-32-32-47"); break;
case \$rack_kabin_kac_u_olacak <= 144 : \$hedefsecenek= array(
"36-36-36-36", "42-42-42-42"); break;
case \$rack_kabin_kac_u_olacak <= 168 : \$hedefsecenek= array(
"42-42-42-42", "44-44-44-44"); break;
case \$rack_kabin_kac_u_olacak <= 176 : \$hedefsecenek= array(
"44-44-44-44", "47-47-47-47"); break;``````