**数据是3个商店的20个订单的样本数据。想要编写一个算法,根据规则确定哪个商店可以满足哪个订单**
如果有超过1家商店有资格获得订单,则累计满足最少量订单的商店将满足当前订单。 例如,如果S1,S2和& S3提供100,150,75个订单,直到订单#3和S1& S2有资格获得#4订单,然后S1将迎合订单#4
OrderNum|OrderQty|StoresEligible
------- |--------|--------------
1|12|S3
2|10|S1
3|19|S1,S2
4|7|S1,S2
5|14|S1,S2,S3
6|19|S2
7|5|S1,S2
8|17|S2
9|13|S3
10|5|S1
实际上,美国有2,000家商店和1M订单
答案 0 :(得分:1)
确定这可以解决部分问题,它可以为您的数据添加新行,将过去的数据(我添加了ChoosenStore信息)作为输入数量和符合条件的商店;它输出完成N + 1订单的data.frame:
您必须迭代地将此功能应用于您过去的数据才能完成它。
library(dplyr); library(tidyr)
# initialise the data.frame with just the first 3 lines
df=data.frame(OrderNum=1:3, OrderQty=c(12,10,19),
StoresEligible=c("S3", "S1", "S1, S2"),
ChoosenStore=c("S3", "S1", "S2"),stringsAsFactors=FALSE)
# function to compute new incoming order
NewOrder = function(df, quantity, eligible){
choosen <- df %>% filter(ChoosenStore %in% eligible) %>%
group_by(ChoosenStore) %>%
summarise(CumQty=sum(OrderQty)) %>%
top_n(-1, CumQty) %>% .$ChoosenStore
return(rbind(df, c(tail(df$OrderNum, 1)+1,
quantity,
paste0(eligible, collapse=", "),
choosen)))}
# try it with the 4th line
NewOrder(df, 7, c("S1", "S2"))