I think this is a planning problem, but I'm not even sure about it! I want to find the optimal sequence of disjoint purchase decisions when I fully get acquainted with their cost and opportunities for the future.
Imagine a wholesaler who sells various products that I want to buy for my store. At any time, they can run several special offers; I will sell at full price, so their discount is my profit.
I want to maximize profits, but the catch is that I can only buy one thing at a time, and there is no such thing as a loan, and, even worse, there is a delivery delay. The good news is that I will sell items as soon as they are delivered and I can spend the money again. Thus, one way through all the options could be: I buy 100 kg of apples on Monday, they are delivered on Tuesday. Then I buy 20 monastic costumes, respectively, on Sunday. I miss a couple of days, as I know, on Wednesday they will have a Ferrari at a big discount. Therefore, I buy one of them, it will be delivered next Tuesday. Etc.
You can think about multiplying profits or not. The algorithm comes down to a decision at each stage between choosing one of today's special offers or waiting for the day, because tomorrow something better will come.
Let it be abstract a little. Purchase and delivery are becoming days-s-era. Profit is written as the selling price divided by the purchase price. That is, 1.00 means break-even, 1.10 means 10% profit, 2.0 means that I doubled my money.
buy delivery profit 1 2 1.10 Apples 1 3 1.15 Viagra 2 3 1.15 Notebooks 3 7 1.30 Nun costumes 4 7 1.28 Priest costumes 6 7 1.09 Oranges 6 8 1.11 Pears 7 9 1.16 Yellow shoes 8 10 1.15 Red shoes 10 15 1.50 Red Ferrari 11 15 1.40 Yellow Ferrari 13 16 1.25 Organic grapes 14 19 1.30 Organic wine
NOTES: Opportunities exist only on the day of purchase (for example, organic grapes turn into wine if nobody buys it!), And I get the opportunity to sell on the same day as delivery, but I can’t buy my next product until bye the next day. Therefore, I cannot sell my monastic costumes at t = 7 and immediately buy yellow shoes at t = 7.
I was hoping that a well-known better algorithm exists, and that an R-module already exists for it, but the algorithms or academic literature would also be good, as in any other language. Speed matters, but mainly when the data gets big, so I would like to know if it is O (n 2 ) or something else.
By the way, does a better algorithm improve if there is the maximum possible delivery delay? For instance. if delivery - buy <= 7
Here is the above data as a CSV:
buy,delivery,profit,item 1,2,1.10,Apples 1,3,1.15,Viagra 2,3,1.15,Notebooks 3,7,1.30,Nun costumes 4,7,1.28,Priest costumes 6,7,1.09,Oranges 6,8,1.11,Pears 7,9,1.16,Yellow shoes 8,10,1.15,Red shoes 10,15,1.50,Red Ferrari 11,15,1.40,Yellow Ferrari 13,16,1.25,Organic grapes 14,19,1.30,Organic wine
Or like JSON:
{"headers":["buy","delivery","profit","item"],"data":[[1,2,1.1,"Apples"],[1,3,1.15,"Viagra"],[2,3,1.15,"Notebooks"],[3,7,1.3,"Nun costumes"],[4,7,1.28,"Priest costumes"],[6,7,1.09,"Oranges"],[6,8,1.11,"Pears"],[7,9,1.16,"Yellow shoes"],[8,10,1.15,"Red shoes"],[10,15,1.5,"Red Ferrari"],[11,15,1.4,"Yellow Ferrari"],[13,16,1.25,"Organic grapes"],[14,19,1.3,"Organic wine"]]}
Or as a data frame R:
structure(list(buy = c(1L, 1L, 2L, 3L, 4L, 6L, 6L, 7L, 8L, 10L, 11L, 13L, 14L), delivery = c(2L, 3L, 3L, 7L, 7L, 7L, 8L, 9L, 10L, 15L, 15L, 16L, 19L), profit = c(1.1, 1.15, 1.15, 1.3, 1.28, 1.09, 1.11, 1.16, 1.15, 1.5, 1.4, 1.25, 1.3), item = c("Apples", "Viagra", "Notebooks", "Nun costumes", "Priest costumes", "Oranges", "Pears", "Yellow shoes", "Red shoes", "Red Ferrari", "Yellow Ferrari", "Organic grapes", "Organic wine")), .Names = c("buy", "delivery", "profit", "item"), row.names = c(NA, -13L), class = "data.frame")
LINK
Are there any R-packages for graphs (shortest path, etc.)? ( igraph offers the function shortest.paths and, in addition to the C library, has an R package and a python interface)