Finding the best "deal" in a "group buy", given a table of values
Using PHP or Python, but I'm sure the basic functions are agnostic.
I am unsure what the proper term, mathematical theory, or algorithm is, otherwise I'm sure Google would have fixed this for me in minutes.
I have a data set similar to the following:
cost | qty | ppl | store
------------------------
30| 500| 10| 1
40| 600| 12| 2
35| 500| 14| 3
50| 700| 10| 1
30| 700| 12| 1
40| 250| 14| 2
What I'm trying to do is find the "o开发者_运维技巧ptimal" row, based on these qualifiers:
- cost: lower is better.
- qty: higher is better.
- ppl: lower is better.
- store: Doesn't matter in this case, but used later to find "best" depending on 'store'.
Essentially, I'm trying to find the best particular "deal" in a "group buy"-like situation, where the fewest amount of people are required to get the best "value" (quantity -vs- cost).
To my eye, it appears that the best-overall would be Row #5, because of the jump in quantity.
If there is a name for this, and a good (Wikipedia?) article on the subject, I'd be happy to finish this myself. Thanks for your time!
Compute qty / (cost * ppl)
and sort the list by that number. This number will be higher for higher qty
and lower cost
and ppl
.
You might want to use something like this (python):
def cmp(a, b):
return (a["qty"] / (a["cost"] * a["ppl"])) - (b["qty"] / (b["cost"] * b["ppl"]))
list = sorted(list, cmp)
Explanation: think what happens if qty
is getting bigger when cost * ppl
are constant. The ratio will increase, because a/x > b>x
if a > b
. Now with the other two values it's the other way around; if x/a > x/b
, then a < b
, so the ratio would actually get decreased when cost
or ppl
increases (think what happens if you split 100$ to two people vs three people; if you split it to two, each will get 100/2 = 50$. If you split it to three, each will get 100/3 ~= 33$, which is less). (Sorry if i'm not making it clear enough; i'm tired)
You are looking at linear programming in general, and the simplex algorithm in particular.
You need to decide what makes it optimal, define a function that depends on cost, qty, ppl and maximize/minimize it. Then its just optimization.
I'm assuming cost is cost per item, qty is amount available in store, and ppl are minimum number of people for group buy to kick in. Then minimizing cost*ppl/qty is equivalent to minimizing the minimum total amount spent before it kicks in, divided by the number of people who could participate in the group buy if it becomes available. But you still have to think does this make sense. You may say that if the cost doubles, but qty available also doubles that this group-buy is worse than a smaller one. Maybe if cost doubles, qty would have to increase by 4, or # of ppl necessary decrease by 4. Then you may want a function like cost^2 ppl/qty. E.g., something in general like cost^m ppl^n/qty^p should work; you just adjust the m, n, p (all positive numbers) depending on the weights you think appropriate.
精彩评论