Hi, I am a beginner of R. There is a question about constraint minimization. A function, y=f(x1,x2,x3....x12), needs to be minimized. There are 3 requirements for the minimization: (1) x2+x3+...+x12=1.5 (x1 is excluded); (2) x1=x3=x4; (3) x1, x3 and x5 are in the range of -1~0, respectively. The rest variables (x2, x4, x6, x7, ...., x12) are in the range of 0~1, respectively. The "optim" function is used. And part of my input is as follow, where "xx1r" represents the x12: xx1r=1.5-x[2]-x[1]-x[1]-x[3]-x[4]-x[5]-x[6]-x[7]-x[8]-x[9] start=rnorm(9) up=1:9/1:9*1 lo=1:9/1:9*-1 out=optim(start,f,lower=lo,upper=up,method="L-BFGS-B",hessian=TRUE, control=list(trace=6,maxit=1000)) There are two problems in this input. the "up" and "lo" only define a range of -1~1 for x1 to x11, which can not meet the requirement (3). In addition, there is not any constraint imposed on x12. I have no idea how to specify a matrix that can impose different constraints on individual variables in a function. Any suggestion is highly appreciated. Best, Hao -- View this message in context: http://r.789695.n4.nabble.com/question-about-constraint-minimization-tp3050880p3050880.html Sent from the R help mailing list archive at Nabble.com.
Does ?constrOptim look as though it will handle your needs? -------------------------------------- Jonathan P. Daily Technician - USGS Leetown Science Center 11649 Leetown Road Kearneysville WV, 25430 (304) 724-4480 "Is the room still a room when its empty? Does the room, the thing itself have purpose? Or do we, what's the word... imbue it." - Jubal Early, Firefly r-help-bounces at r-project.org wrote on 11/19/2010 02:51:46 PM:> [image removed] > > [R] question about constraint minimization > > dhacademic at gmail.com > > to: > > r-help > > 11/19/2010 03:15 PM > > Sent by: > > r-help-bounces at r-project.org > > > Hi, > > I am a beginner of R. There is a question about constraint minimization.A> function, y=f(x1,x2,x3....x12), needs to be minimized. There are 3 > requirements for the minimization: > > (1) x2+x3+...+x12=1.5 (x1 is excluded); > (2) x1=x3=x4; > (3) x1, x3 and x5 are in the range of -1~0, respectively. The restvariables> (x2, x4, x6, x7, ...., x12) are in the range of 0~1, respectively. > > The "optim" function is used. And part of my input is as follow, where > "xx1r" represents the x12: > > xx1r=1.5-x[2]-x[1]-x[1]-x[3]-x[4]-x[5]-x[6]-x[7]-x[8]-x[9] > start=rnorm(9) > up=1:9/1:9*1 > lo=1:9/1:9*-1 > out=optim(start,f,lower=lo,upper=up,method="L-BFGS-B",hessian=TRUE, > control=list(trace=6,maxit=1000)) > > There are two problems in this input. the "up" and "lo" only define arange> of -1~1 for x1 to x11, which can not meet the requirement (3). Inaddition,> there is not any constraint imposed on x12. I have no idea how tospecify a> matrix that can impose different constraints on individual variables ina> function. Any suggestion is highly appreciated. > > Best, > Hao > > > -- > View this message in context: http://r.789695.n4.nabble.com/ > question-about-constraint-minimization-tp3050880p3050880.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
dhacademic <at> gmail.com <dhacademic <at> gmail.com> writes:> > > Hi, > > I am a beginner of R. There is a question about constraint minimization. A > function, y=f(x1,x2,x3....x12), needs to be minimized. There are 3 > requirements for the minimization: > > (1) x2+x3+...+x12=1.5 (x1 is excluded); > (2) x1=x3=x4; > (3) x1, x3 and x5 are in the range of -1~0, respectively. The rest variables > (x2, x4, x6, x7, ...., x12) are in the range of 0~1, respectively. > > The "optim" function is used. And part of my input is as follow, where > "xx1r" represents the x12: > > xx1r=1.5-x[2]-x[1]-x[1]-x[3]-x[4]-x[5]-x[6]-x[7]-x[8]-x[9] > start=rnorm(9) > up=1:9/1:9*1 > lo=1:9/1:9*-1 > out=optim(start,f,lower=lo,upper=up,method="L-BFGS-B",hessian=TRUE, > control=list(trace=6,maxit=1000)) > > There are two problems in this input. the "up" and "lo" only define a range > of -1~1 for x1 to x11, which can not meet the requirement (3). In addition, > there is not any constraint imposed on x12. I have no idea how to specify a > matrix that can impose different constraints on individual variables in a > function. Any suggestion is highly appreciated. > > Best, > Hao >I don't see any direct need for real 'constraint' optimization here, it is a 'bounded' optimization where you are allowed to use lower <- c(-1,0,-1,0,-1,0,0,0,0,0,0,0) upper <- c( 0,1, 0,0, 0,1,1,1,1,1,1,1) Otherwise, your description is confusing: (1) Did you change f to a new function with 9 variables, eliminating x3, x4, and x12 ? (2) x4 (being equal to x1) has to be in [-1, 0] but also in [0, 1]? (3) If you need to restrict x12 to [0, 1] also, you cannot eliminate it. Either keep x12 and use an equality constraint, or use inequality constraints on xxlr. Hans Werner