# [R] question about constraint minimization

Tue Nov 23 15:42:22 CET 2010

```You need to spend some time and learn the basics of R.  There are several
excellent tutorials available on the CRAN R-project website. This will be a
very good investment of your time.  After you have spent a couple of days
learning the basics, try using my package to solve your problem.

Best,
Ravi.

-------------------------------------------------------
Assistant Professor,
Division of Geriatric Medicine and Gerontology School of Medicine Johns
Hopkins University

Ph. (410) 502-2619

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Sent: Tuesday, November 23, 2010 12:26 AM
To: r-help at r-project.org
Subject: Re: [R] question about constraint minimization

Many thanks for the reply. In my case, besides x1=x3, x1=x4 ("x1=x3=x4" was
used in last post), another constraint is needed, x2+x3+x4+...+x12=1.5. So
there are 9 variables.

R program. Actually, I know very little about R.  I spent lots of time to
read the R help files as well as lots of post on line, and finally prepared
the input file that I pasted in my last post. Unfortunately, it does not
using the constrOptim function? Or can you plese show me how to call your
code in R and send me the input file?

Many thanks for your kind considerations!

Best,
Hao

On Mon, Nov 22, 2010 at 2:31 PM, Ravi Varadhan [via R] <
ml-node+3054297-1984476990-202837 at n4.nabble.com<ml-node%2B3054297-1984476990
-202837 at n4.nabble.com>
> wrote:

> I do not understand the constraint x1 = x3 = x4.  If this is correct, you
> only have 10 unknown parameters.
>
> If you can correctly formulate your problem, you can have a look at the
> packages "alabama" or "BB".  The function `auglag' in "alabama" or the
> function `spg' in "BB" may be useful.
>
> Ravi.
>
> -------------------------------------------------------
> Assistant Professor,
> Division of Geriatric Medicine and Gerontology School of Medicine Johns
> Hopkins University
>
> Ph. (410) 502-2619
> email: [hidden
email]<http://user/SendEmail.jtp?type=node&node=3054297&i=0>
>
>
> -----Original Message-----
> From: [hidden
email]<http://user/SendEmail.jtp?type=node&node=3054297&i=1>[mailto:[hidden
> email] <http://user/SendEmail.jtp?type=node&node=3054297&i=2>] On
> Behalf Of [hidden
email]<http://user/SendEmail.jtp?type=node&node=3054297&i=3>
> Sent: Monday, November 22, 2010 11:10 AM
> To: [hidden email] <http://user/SendEmail.jtp?type=node&node=3054297&i=4>
> Subject: Re: [R] question about constraint minimization
>
>
> Hi,
>
> I have struggled on this "bound optimization with equality constraint" by
> using optim function for two days, but still fail to prepare a good input.
> Can anyone help to prepare the input for my specific case? Many thanks.
>
> Best,
> Hao
>
>
> On Sat, Nov 20, 2010 at 3:17 AM, Hans W Borchers [via R] <
> [hidden email]
<http://user/SendEmail.jtp?type=node&node=3054297&i=5><ml-node%2B3051338-309
339578-2
>
> [hidden email] <http://user/SendEmail.jtp?type=node&node=3054297&i=6>>
>  > wrote:
>
> >
> > >
> > >
> > > 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
> > > 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
> >
> > ______________________________________________
> > [hidden email]
> <http://user/SendEmail.jtp?type=node&node=3051338&i=0>mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> >
http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting
-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >
> >
> > ------------------------------
> >  View message @
> >
>
>
8?by-user=t>
> 80p3051338.html
> >
> > To unsubscribe from question about constraint minimization, click
> here<
>
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscrib<http
://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscrib&by-user=
t>
>
e_by_code&node=3050880&code=ZGhhY2FkZW1pY0BnbWFpbC5jb218MzA1MDg4MHwtNjM2Nzc0
>
> NA==>.
> >
> >
>
> --
> View this message in context:
>
>
8?by-user=t>
> 80p3053912.html
> Sent from the R help mailing list archive at Nabble.com.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email]
<http://user/SendEmail.jtp?type=node&node=3054297&i=7>mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
>
http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting
-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email]
<http://user/SendEmail.jtp?type=node&node=3054297&i=8>mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
>
http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting
-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>
> ------------------------------
>  View message @
>
80p3054297.html
>
> To unsubscribe from question about constraint minimization, click
here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscrib
e_by_code&node=3050880&code=ZGhhY2FkZW1pY0BnbWFpbC5jb218MzA1MDg4MHwtNjM2Nzc0
NA==>.
>
>

--
View this message in context:
80p3054850.html
Sent from the R help mailing list archive at Nabble.com.

[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help