Re: proportionality problem
- From: Marius Huse Jacobsen <loop@xxxxxxxxxxxxx>
- Date: Sun, 26 Feb 2006 16:06:13 +0100
Digital Puer wrote:
Suppose I have a list of resources, where each resource is
assigned a quality value 1 to 10, inclusive, where 1 is the
highest quality. I want to assign resource requests to each
of these resources in proportion to their quality value.
For example, if I have two resources A and B with quality
values 1 and 4, respectively, I would assign requests to
A and B with a 4:1 ratio because A has 4x the value of B.
How can I extend this to the general case with multiple
resources? For example, if A, B, and C have values 1, 2,
3, how would requests be assigned to A, B, and C? I think
it would be 6:3:2. What's a good way to compute this?
Doing a different class depending on what resource it is, makes for an extensibility headache, but either way you're gonna need a list (or deque) for every type from which it is to be fetched separately. (Could probably be autoextensibly done by getting creative with arrays/vectors, all-representative class/base classes/any/variant, etc.)
If you're fetching by quality not by resource (if A and B are both quality 1, C is 2, then 112 (==ABC) instead of ABABC) then you could create a container as below for each quality instead of each resource.
(If you have a unified "request" class, then you just have to sort them in by which resource they're requesting.)
// Doing this in C++. Exchange the standard library container
// (std::deque<> - double ended queue) to get normal C.
// Defined somewhere
std::deque<Arequest> A;
const int Aquality = 1; // or static int calculated on something
std::deque<Brequest> B;
const int Bquality = 2;
std::deque<Crequest> C;
const int Cquality = 3;
// In a function, probably somewhere else
for (int i=1; i<=10; i++) {
if (! Aquality%i && !A.empty()) A.pop_front().AcceptRequest());
if (! Bquality%i && !B.empty()) B.pop_front().AcceptRequest());
if (! Cquality%i && !C.empty()) C.pop_front().AcceptRequest());
}
PS. I think maybe quality may be a bad choice of words. Perhaps (borrowing from tcp/ip) "metric" would be a better word?
Also, I haven't tried to compile above code, but I expect it will work anyway.
.
- References:
- proportionality problem
- From: Digital Puer
- proportionality problem
- Prev by Date: Re: Assembly: direct video access acts strange
- Next by Date: Re: init
- Previous by thread: Re: proportionality problem
- Next by thread: partition signature
- Index(es):
Relevant Pages
|