I am trying to test R for non linear constrained optimization.
I have 2 files: T1.csv
has
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
and T2.csv
has
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
And the code is...
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))
I have to use constraints in optimization.
Cons are in my T1.csv
file, specified as UB and LB such that
0.7<=X1<=1.3
0.7<=X2<=1.3
0.7<=X3<=1.3
How do I specify the cons in my code? (I don't want to hard code them, I want to read them from the file T1.csv.)