我具有以下功能:
Proposed <- function(N_b,Lanes,m,A,x.sqr,e_1,e_2,e_3,e_4,e_5,S,a,b) {
e <- data.frame(e_1,e_2,e_3,e_4,e_5)
CSi <- m * ((Lanes/N_b) + (A * (combn(e,Lanes,sum)) / x.sqr) * (b*S^a))
return(max(CSi))
}
将其应用于示例数据行时:
> DATA[1, ]
Lanes N_b N_l A x.sqr e_1 e_2 e_3 e_4 e_5 S Proposed.Girder1 UG CSi m
1 3 5 4 -12 1440 21.8 9.8 -2.2 -14.2 0 12 0.6261667 100 0.5918969 0.85
使用:
x <- DATA[1, ]
Proposed1(x[["N_b"]], x[["Lanes"]], x[["m"]], x[["A"]], x[["x.sqr"]], x[["e_1"]], x[["e_2"]],
x[["e_3"]], x[["e_4"]], x[["e_5"]],x[["S"]], a = 0.01, b = 0.5)
# [1] 0.5695447
我得到正确的答案。但是如果我将代码更改为
Proposed2 <- function(N_b,Lanes,m,A,x.sqr,e_1,e_2,e_3,e_4,e_5,S,a,b) {
e <- data.frame(e_1,e_2,e_3,e_4,e_5)
CSi <- m * ((Lanes/N_b) + (A * max(combn(e,Lanes,sum)) / x.sqr) * (b*S^a))
return(CSi)
}
我预期会有相同结果的,我得到了错误的答案:
Proposed2(x[["N_b"]], x[["Lanes"]], x[["m"]], x[["A"]], x[["x.sqr"]], x[["e_1"]], x[["e_2"]],
x[["e_3"]], x[["e_4"]], x[["e_5"]],x[["S"]], a = 0.01, b = 0.5)
# [1] 0.3952675
答案 0 :(得分:2)
由于A
为负数,因此,当combn(e,Lanes,sum)
最小时,函数将最大化。
x <- readr::read_table2("
Lanes N_b N_l A x.sqr e_1 e_2 e_3 e_4 e_5 S Proposed.Girder1 UG CSi m
3 5 4 -12 1440 21.8 9.8 -2.2 -14.2 0 12 0.6261667 100 0.5918969 0.85
")
Proposed3 <- function(N_b,Lanes,m,A,x.sqr,e_1,e_2,e_3,e_4,e_5,S,a,b) {
e <- data.frame(e_1,e_2,e_3,e_4,e_5)
# Note "min"
CSi <- m * ((Lanes/N_b) + (A * min(combn(e,Lanes,sum)) / x.sqr) * (b*S^a))
return(CSi)
}
Proposed3(x[["N_b"]], x[["Lanes"]], x[["m"]], x[["A"]], x[["x.sqr"]], x[["e_1"]], x[["e_2"]],
x[["e_3"]], x[["e_4"]], x[["e_5"]],x[["S"]], a = 0.01, b = 0.5)
#> [1] 0.5695447