繁体   English   中英

如何找到最佳解决方案? For循环[r]

[英]How to find the best possible solution? For-loop [r]

我写了这段代码:

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] xi)。

亲切的问候,

Majesus

尝试这个。 只需将解决方案附加到数据框(在本例中称为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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM