Re: [R] constrained optimization -which package?
Leonardo Monasterio leonardo.monasterio at gmail.com writes: Dear R users, In the function bellow I want to find the maximum value of v, subject to the constrain that the sum of x is equal to 1. I want to maximize: v-t(x)%*%distance%*%x Subject to: sum(x)=1 I do not see why you would take the trouble to use constraint optimization here. Use x_n as a dependent variable, x_n - 1 - sum(x), x an (n-1)-dim. vector, define another function f1(x) - f(c(x, 1-sum(x))) appropriately, and apply optim() without constraints. Hans Werner Where: x is a vector n X 1 distance is a matrix n*n and it is given. (In practice, the number of n can go up to hundreds.) I have a bit of experience using R but not much on optimization techniques or on R optimization packages. I have taken a look at optim and nlminb, but I am quite confused. Do you have any suggestion on how to do it? Thank you very much, Leo Monasterio. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] constrained optimization -which package?
Dear R users, I want to find the maximum value of v, subject to the constrain that the sum of x is equal to 1. So, I want to maximize: v-t(x)%*%distance%*%x Subject to: sum(x)=1 Where: x is a vector n X 1 distance is a matrix n*n and it is given. (In practive, the number of n can go up to hundreds.) I have a bit of experience using R but not much on optimization techniques or on R optimization packages. I have taken a look at optim and nlminb, but I am confused. Do you have any suggestion on how to do it? Thank you very much, Leo Monasterio. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] constrained optimization -which package?
?constrOptim -- View this message in context: http://r.789695.n4.nabble.com/constrained-optimization-which-package-tp2717677p2717719.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] constrained optimization -which package?
Dear R users, In the function bellow I want to find the maximum value of v, subject to the constrain that the sum of x is equal to 1. I want to maximize: v-t(x)%*%distance%*%x Subject to: sum(x)=1 Where: x is a vector n X 1 distance is a matrix n*n and it is given. (In practice, the number of n can go up to hundreds.) I have a bit of experience using R but not much on optimization techniques or on R optimization packages. I have taken a look at optim and nlminb, but I am quite confused. Do you have any suggestion on how to do it? Thank you very much, Leo Monasterio. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] constrained optimization -which package?
No. That would not work for equality constraints. This is quadratic programming problem. Check out `quadprog' or any other quad programming packages in R. If you have more general, nonlinearly constrained optimization you can use any one of the 3 following packages: 1. `spg' function in BB package 2. `constrOptim.nl' or `auglag' functions in alabama package 3. `solnp' in Rsolnp package. Hope this helps, Ravi. -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Peng, C Sent: Tuesday, September 28, 2010 2:54 PM To: r-help@r-project.org Subject: Re: [R] constrained optimization -which package? ?constrOptim -- View this message in context: http://r.789695.n4.nabble.com/constrained-optimization-which-package-tp27176 77p2717719.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] constrained optimization -which package?
constrOptim() can do linear and quadratic programming problems! See the following example from the help document. ## Solves linear and quadratic programming problems ## but needs a feasible starting value # # from example(solve.QP) in 'quadprog' # no derivative fQP - function(b) {-sum(c(0,5,0)*b)+0.5*sum(b*b)} Amat - matrix(c(-4,-3,0,2,1,0,0,-2,1),3,3) bvec - c(-8,2,0) constrOptim(c(2,-1,-1), fQP, NULL, ui=t(Amat),ci=bvec) # derivative gQP - function(b) {-c(0,5,0)+b} constrOptim(c(2,-1,-1), fQP, gQP, ui=t(Amat), ci=bvec) ## Now with maximisation instead of minimisation hQP - function(b) {sum(c(0,5,0)*b)-0.5*sum(b*b)} constrOptim(c(2,-1,-1), hQP, NULL, ui=t(Amat), ci=bvec, control=list(fnscale=-1)) -- View this message in context: http://r.789695.n4.nabble.com/constrained-optimization-which-package-tp2717677p2718136.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] constrained optimization -which package?
Please read the help page carefully, the very first line that describes the function says, Minimise a function subject to linear inequality constraints. OP has a problem with equality constraints: sum(x) = 1. Furthermore, if you want to solve a QP problem then it is better to use a dedicated QP algorithm than to use a general-purpose nonlinear optimization algorithm. Ravi. Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu - Original Message - From: Peng, C cpeng@gmail.com Date: Tuesday, September 28, 2010 7:58 pm Subject: Re: [R] constrained optimization -which package? To: r-help@r-project.org constrOptim() can do linear and quadratic programming problems! See the following example from the help document. ## Solves linear and quadratic programming problems ## but needs a feasible starting value # # from example(solve.QP) in 'quadprog' # no derivative fQP - function(b) {-sum(c(0,5,0)*b)+0.5*sum(b*b)} Amat - matrix(c(-4,-3,0,2,1,0,0,-2,1),3,3) bvec - c(-8,2,0) constrOptim(c(2,-1,-1), fQP, NULL, ui=t(Amat),ci=bvec) # derivative gQP - function(b) {-c(0,5,0)+b} constrOptim(c(2,-1,-1), fQP, gQP, ui=t(Amat), ci=bvec) ## Now with maximisation instead of minimisation hQP - function(b) {sum(c(0,5,0)*b)-0.5*sum(b*b)} constrOptim(c(2,-1,-1), hQP, NULL, ui=t(Amat), ci=bvec, control=list(fnscale=-1)) -- View this message in context: Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] constrained optimization -which package?
On Tue, Sep 28, 2010 at 9:47 PM, Leonardo Monasterio leonardo.monaste...@gmail.com wrote: In the function bellow I want to find the maximum value of v, subject to the constrain that the sum of x is equal to 1. I want to maximize: v-t(x)%*%distance%*%x Subject to: sum(x)=1 Where: x is a vector n X 1 distance is a matrix n*n and it is given. (In practice, the number of n can go up to hundreds.) I have a bit of experience using R but not much on optimization techniques or on R optimization packages. I have taken a look at optim and nlminb, but I am quite confused. Do you have any suggestion on how to do it? Your optimization problem corresponds to a quadratic programming problem. So, you can use, e.g., the package quadprog. Hope this helps you, Paul __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.