Wanting To Optimize This Algorithm
Posted: Fri May 15, 2009 11:32 pm
Here's what I need to do in a nutshell. I'm under NDA and can't describe the client's request, and this is only one tiny piece of it that's bothering me -- one of the last few pieces.
Let's say you have a carton that holds 12 eggs. (T = 12, let's say.) And your job is to display as much of the different varieties in them as evenly as possible. Trouble is, you're only given a random bucket of eggs, each of them marked A, B, and C, and you may have in your bucket less than 12 eggs, more than 12 eggs, exactly 12 eggs, and also you may have like 14 As, 5 Bs and 18 Cs. Get it?
The only way I've figured out to solve this problem is to do it like so:
1. If A + B + C <= T, then show all of A, B, and C.
2. If not # 1, then do a loop:
a. Take 1 A if possible and put it in the carton.
b. Take 1 B if possible and put it in the carton.
c. Take 1 C if possible and put it in the carton.
d. Repeat.
e. Stop at any time when the carton hits T count of eggs.
Okay, but that gets to be processor expensive because let's say you have a gazillion users doing this in a minute -- wouldn't it be nice to get rid of that loop and use a few C functions and some Calculus, instead?
Here's the kludge I came up with:
[/size]
The end result is $nShowA, $nShowB, and $nShowC. The inputs were $nA, $nB, $nC, and $nTotal. Let's imagine that $nTotal = 12 in this case.
See how whacked out my while loop is with the if/then conditions? And you're probably thinking you can do this with some algebra and some calculus. Be my guest -- I want to learn from you.
And the only thing I can say under NDA to explain what's really going on is that A, B, and C are types of records, and Total is how many records are possible to be displayed in the dashboard gadget before someone has to click View All to see the rest of the records.
Let's say you have a carton that holds 12 eggs. (T = 12, let's say.) And your job is to display as much of the different varieties in them as evenly as possible. Trouble is, you're only given a random bucket of eggs, each of them marked A, B, and C, and you may have in your bucket less than 12 eggs, more than 12 eggs, exactly 12 eggs, and also you may have like 14 As, 5 Bs and 18 Cs. Get it?
The only way I've figured out to solve this problem is to do it like so:
1. If A + B + C <= T, then show all of A, B, and C.
2. If not # 1, then do a loop:
a. Take 1 A if possible and put it in the carton.
b. Take 1 B if possible and put it in the carton.
c. Take 1 C if possible and put it in the carton.
d. Repeat.
e. Stop at any time when the carton hits T count of eggs.
Okay, but that gets to be processor expensive because let's say you have a gazillion users doing this in a minute -- wouldn't it be nice to get rid of that loop and use a few C functions and some Calculus, instead?
Here's the kludge I came up with:
Code: Select all
$nShowA = 0; $nShowB = 0; $nShowC = 0;
$nABucket = $nA; $nBBucket = $nB; $nCBucket = $nC;
if (($nA + $nB + $nC) <= $nTotal) {
$nShowA = $nA;
$nShowB = $nB;
$nShowC = $nC;
} else {
while (($nShowA + $nShowB + $nShowC) <= $nTotal) {
if ($nABucket >= 1) {
++$nShowA;
--$nABucket;
}
if (($nShowA + $nShowB + $nShowC) == $nTotal) {
break;
}
if ($nBBucket >= 1) {
++$nShowB;
--$nBBucket;
}
if (($nShowA + $nShowB + $nShowC) == $nTotal) {
break;
}
if ($nCBucket >= 1) {
++$nShowC;
--$nCBucket;
}
if (($nShowA + $nShowB + $nShowC) == $nTotal) {
break;
}
}
}
The end result is $nShowA, $nShowB, and $nShowC. The inputs were $nA, $nB, $nC, and $nTotal. Let's imagine that $nTotal = 12 in this case.
See how whacked out my while loop is with the if/then conditions? And you're probably thinking you can do this with some algebra and some calculus. Be my guest -- I want to learn from you.
And the only thing I can say under NDA to explain what's really going on is that A, B, and C are types of records, and Total is how many records are possible to be displayed in the dashboard gadget before someone has to click View All to see the rest of the records.