我写了这段代码:
P<-4000000 #population
j<-4 #exposures
budget<-7000 #Euros
vehicles<-data.frame(A1=c(2000001,1700000,1619200),A2=c(2500000,1900000,1781120),Price=c(2000,1500,1000)) #A1: Audience1, A2: Audience2 & Price-insertion
end.i<-FALSE
for(i in seq(4,1000,1)){
for(k in 1:nrow(vehicles)){
R1=vehicles$A1[k]/P;R2=vehicles$A2[k]/P
shape1=((R1)*((R2)-(R1)))/(2*(R1)-(R1)*(R1)-(R2));shape1
shape2=(shape1*(1-(R1)))/(R1);shape2
t <- dbetabinom.ab(1:i, size = i, shape1 = shape1, shape2 = shape2)
print(t[j])
print(paste(k,"vehicles",sep=" "))
print(paste(i,"insertions", sep=" "))
price<-i*vehicles$Price[k]
print(paste(price,"Euros",sep=" "))
if((i*vehicles$Price[k])<=budget& t[j]>=0.024 & t[j]<=0.025){end.i<-TRUE;break;}
};
if (end.i) break;
}
此代码允许提取达到'X个体(t [j]概率x种群)暴露j次'所需的插入次数(i)(我的目标)。
然而,代码在达到解决方案时结束。我有兴趣知道如何编写代码来估计所有可能的解决方案,并选择一个也可以最大限度地降低插入成本(车辆$ Price [k] x i)。
亲切的问候,
Majesus
答案 0 :(得分:0)
试试这个。只需将解决方案附加到数据框(在本例中称为out_put
)
P<-4000000 #population
j<-4 #exposures
budget<-7000 #Euros
vehicles<-data.frame(A1=c(2000001,1700000,1619200),A2=c(2500000,1900000,1781120),Price=c(2000,1500,1000)) #A1: Audience1, A2: Audience2 & Price-insertion
out_put = data.frame(TJ = NA,Vehicles = NA, Insertions = NA,Price_Euros = NA)
for(i in seq(4,1000,1)){
for(k in 1:nrow(vehicles)){
R1=vehicles$A1[k]/P;R2=vehicles$A2[k]/P
shape1=((R1)*((R2)-(R1)))/(2*(R1)-(R1)*(R1)-(R2))
shape2=(shape1*(1-(R1)))/(R1)
t <- dbetabinom.ab(1:i, size = i, shape1 = shape1, shape2 = shape2)
price<-i*vehicles$Price[k]
out_put = rbind(out_put,c(t[j],k,i,price))
}
}
out_put = out_put[2:nrow(out_put),]
rownames(out_put) = NULL