簡體   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