3 ms·
You can actually improve this a little with a better scoring function. Suppose we currently produce p, and A costs cA, for a power gain of pA. Likewise for B. U
by TusanHomichi 10y ago
You can actually improve this a little with a better scoring function. Suppose we currently produce p, and A costs cA, for a power gain of pA. Likewise for B. Under the assumption that we will buy at least one more of A and B, we can look at whether A then B or B then A is faster, which is:
cA/p + cB/(p+pA) < cB/p + cA/(p+pA)
Solving this, we get cA * (p+pA)/pA < cB * (p+pB)/pB
Since the left side is all in A, if we pick the item minimizing that quantity, it is faster to buy it before any other item. So changing res.score to be res.cost * (cur_power + res.powerGain)/res.powerGain is going to be a bit faster
- christiangenco 10y agoAhh of course! I had the thought to take this into account but couldn't wrap my head around the math last night. cA/p is just the time it would take to earn the money to buy A at the current production level - seems much simpler now. I think the last term should be `cA/(p+pB)`. When I solve from there[1] I get `cB * pB < cA * pA`, which means that the most optimal item A will have the largest product `cA * pA`. This would translate to: res.score = res.cost*res.powerGain ... }).sort(function(a,b){return b.score - a.score}) Does that check out? It seems too simple, and now that I'm thinking more about it I think this would be functionally equal to what I already have. Did I do my inequality[1] wrong? 1. http://i.imgur.com/pyMY3zr.jpg http://i.imgur.com/pyMY3zr.jpg