[R] linear constrained optimization in R

Hans W Borchers hwborchers at googlemail.com
Sat Mar 26 11:07:26 CET 2011


sammyny <sjain <at> caa.columbia.edu> writes:
> 
> I am trying to use
> http://rss.acs.unt.edu/Rdoc/library/stats/html/constrOptim.html in R to do
> optimization in R with some given linear constraints but not able to figure
> out how to set up the problem.
> 
> For example, I need to maximize $f(x,y) = log(x) + \frac{x^2}{y^2}$ subject
> to constraints $g_1(x,y) = x+y < 1$, $g_2(x,y) = x > 0$ and $g_3(x,y) = y >
> 0$. How do I do this in R? This is just a hypothetical example. Do not worry
> about its structure, instead I am interested to know how to set this up in
> R.
> 
> thanks!
> 

To get a reasonable solution, avoid coming near to x=0 and y=0. With
x >= 0.0001 and y >= 0.0001 constrOptim() can be called as follows:

    constrOptim(c(0.25, 0.25), function(x) -log(x[1])-x[1]^2/x[2]^2, NULL, 
                matrix(c(-1,1,0, -1,0,1), 3, 2), c(-1, 0.0001, 0.0001))

--Hans Werner



More information about the R-help mailing list