views:

386

answers:

2

Hi, in another post, MSN gave me a good guide on solving my algebra problem (http://stackoverflow.com/questions/639215/calculating-bid-price-from-total-cost). Now, even though I can calculate it by hand, I'm completely stuck on how to write this in pseudocode or code. Anyone could give me a quick hint? By the way, I want to calculate the bid given the final costs .

usage cost(bid) = PIN(bid*0.10, 10, 50)
seller cost(bid) = bid*.02
added cost(bid) = PIN(ceiling(bid/500)*5, 5, 10) + PIN(ceiling((bid - 1000)/2000)*5, 0, 10)
storing cost(bid) = 100
So the final cost is something like:

final cost(bid) = PIN(bid*.1, 10, 50) + pin(ceiling(bid/500)*5, 5, 20) + PIN(ceiling((bid - 1000)/2000)*10, 0, 20) + bid*.02 + 100 + bid
Solve for a particular value and you're done.

For example, if you want the total cost to be $2000:

2000 = PIN(bid*.1, 10, 50) + pin(ceiling(bid/500)*5, 5, 10) + PIN(ceiling((bid - 1000)/2000)*5, 0, 10) + bid*.02 + 100 + bid.
Bid must be at least > 1500 and < 2000, which works out nicely since we can make those PIN sections constant:

2000 = 50 + 10 + 5 + 100 + bid*1.02
1835 = bid*1.02
bid = 1799.0196078431372549019607843137
+2  A: 

Due to the use of PIN and ceiling, I don't see a easy way to invert the calculation. Assuming that bid has a fixed precision (I'd guess two decimals behind the dot) you can always use a binary search (as the functions are monotone).

Edit: After thinking about it some more, I observed that, taking x = bid*1.02 + 100, we have that the final costs are between x+15 (exclusive) and x+70 (inclusive) (i.e. x+15 < final cost < x+70). Given the size of this range (70-15=55) and the fact that the special values (see note below) for bid are all apart more than this, you can take x+15 = final cost and x+70 = final cost, get the right cases/values of usage and added costs and simply solve that equation (which no longer has either PIN or ceiling in it).

To illustrate, let the final cost be 222. From x+15 = 222 it follows that bid = 107/1.02 = 104.90. Then we have that the usage costs are given by bid*0.1 and that the additional costs are 5. In other words, we get final cost = bid*0.1 + bid*0.02 + 5 + 100 + bid = bid*1.12 + 105 and therefore bid = (222-105)/1.12 = 104.46. As this value of bid means the right values for usage and additional costs were taken, we know that this is the solution.

However, if we would have first looked at x+70 = 222, we would get the following. First we get that for this assumption that bid = 52/1.02 = 50.98. This means that usage costs are 10 and the additional costs are 5. So we get final costs = 10 + bid*0.02 + 5 + 100 + bid = bid*1.02 + 115 and therefore bid = (222-115)/1.02 = 104.90. But if bid is 104.90 then the usage costs are not 10 but bid*0.1, so this isn't the right solution.

I hope I explained it clearly enough. If not, please let me know.

N.B.: With special values I mean those for which the function defining the values of usage and added costs change. For example, for usage cost these values are 100 and 500: below 100 you use 10, above 500 you use 50 and in between you use bid*0.1.

mweerden
great explanation. thank you for your time! Sad thing is MizardX made a perfect one .. :P
Ownatik
No problem. I'll just cry for a week and afterwards I'll probably forget all about it. ;)
mweerden
+3  A: 

The function simplifies to:

                  / 1.02 * bid + 115   bid <   100
                  | 1.12 * bid + 105   bid <=  500
final cost(bid) = | 1.02 * bid + 160   bid <= 1000
                  | 1.02 * bid + 165   bid <= 3000
                  \ 1.02 * bid + 170   otherwise

If you consider each piece as a separate function, they can be inverted:

bid_a(cost) = (cost - 115) / 1.02
bid_b(cost) = (cost - 105) / 1.12
bid_c(cost) = (cost - 160) / 1.02
bid_d(cost) = (cost - 165) / 1.02
bid_e(cost) = (cost - 170) / 1.02

If you plug your cost into each function you get an estimated bid value for that range. You must check that this value indeed is within that functions valid range.

Example:

cost = 2000

bid_a(2000) = (2000 - 115) / 1.02 = 1848  Too big! Need to be < 100
bid_b(2000) = (2000 - 105) / 1.12 = 1692  Too big! Need to be <= 500
bid_c(2000) = (2000 - 160) / 1.02 = 1804  Too big! Need to be <= 1000
bid_d(2000) = (2000 - 165) / 1.02 = 1799  Good. It is <= 3000
bid_e(2000) = (2000 - 170) / 1.02 = 1794  Too small! Need to be > 3000

Just to check:

final cost(1799) = 1.02 * 1799 + 165 = 2000   Good!

Since the original function is strictly increasing, at most one of those functions will give an acceptable value. But for some inputs none of them will give a good value. This is because the original function jumps over those values.

final cost(1000) = 1.02 * 1000 + 160 = 1180
final cost(1001) = 1.02 * 1001 + 165 = 1186

So no function will give an acceptable value for cost = 1182 for example.

MizardX
You beat me by 32 seconds! Your explanation is probably a bit clearer and just calculating all values instead of first figuring out the one or two most relevant functions is probably also easier. +1
mweerden
One thing: you wrote bid instead of cost in the definitions of bid_X.
mweerden
@mweerden: Good catch.
MizardX
+1 for being thorough
Daniel LeCheminant
Excellent explanation. thanks a lot!!!
Ownatik