我正在尝试测试R的非线性约束优化。
我有2个文件:T1.csv
有
DV LB UB Hist_Inv
X1 0.7 1.3 28462739.43
X2 0.7 1.3 177407.18
X3 0.7 1.3 1423271.12
和T2.csv
已
Count DV weight_f weight_v p coef
1 X1 2.310281831 3.661156016 0.5 1828.105881
2 X1 0.693084549 2.20503016 0.5 1460.686147
3 X1 0.207925365 2.030522789 0.5 1436.277144
4 X1 0 5.248353307 0.8 1050.493355
5 X1 0 1.591805116 0.8 983.9964128
6 X1 0 1.933056056 0.8 459.9371809
7 X2 7.322516444 138 0.5 387.4659072
8 X2 3.661258222 139 0.5 606.8684771
9 X2 1.830629111 176.5 0.5 358.8902965
10 X3 164294.4758 77024 0.2 282.0477107
11 X3 98576.68545 122261.4 0.2 345.9217482
12 X3 59146.01127 166242.84 0.2 364.9587162
代码是......
df <- read.csv("C:/Users/prashant.mudgal/Downloads/T1.csv")
dv <- read.csv("C:/Users/prashant.mudgal/Downloads/T2.csv")
decVars <- dv$DV
coeff <- dv$coef
wf<-dv$weight_f
wv<-dv$weight_v
p<-dv$p
y<-0
inv<-df$Hist_Inv
fr <- function(decVars) {
for(i in 1:length(decVars)){
k <-coeff[i]*(wf[i] + wv[i] * decVars[i])**p[i]
y <- y + k
}
y
}
temp<-0
for(j in 1:length(inv)){
temp[j]<- 1
}
optim(temp, fr, NULL,control=list(fnscale=-1))
##constrOptim(inv,fr,NULL,Control=list(fnscale=-1))
我必须在优化中使用约束。
缺点在我的T1.csv
文件中,指定为UB和LB,以便
0.7<=X1<=1.3
0.7<=X2<=1.3
0.7<=X3<=1.3
如何在代码中指定缺点? (我不想硬编码,我想从T1.csv文件中读取它们。)