[R] question about constraint minimization

2010-11-19 Thread dhacade...@gmail.com

Hi, 

I am a beginner of R. There is a question about constraint minimization. A
function, y=f(x1,x2,x3x12), 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.

__
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] question about constraint minimization

2010-11-20 Thread dhacade...@gmail.com

Sorry for the unclear description in my last post. It is an error in my last
post, that x4 should be in the range of [-1, 0] as it is identical to x1.
Actually I think both "constraint" (for the requirement 1 and 2, so there
are only 9 variables) and "bounded" (for the requirement 3) opt are needed
in my case.  Does it means that the "optim" function can not meet my
requirements, and that the "constrOptim" function is needed (as Joanthon
mentioned)?

Many thanks for the reply!

Best,
Hao

On Sat, Nov 20, 2010 at 3:17 AM, Hans W Borchers [via R] <
ml-node+3051338-309339578-202...@n4.nabble.com
> wrote:


> 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] 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.
>
>
> --
>  View message @
> http://r.789695.n4.nabble.com/question-about-constraint-minimization-tp3050880p3051338.html
>
> To unsubscribe from question about constraint minimization, click 
> here.
>
>

-- 
View this message in context: 
http://r.789695.n4.nabble.com/question-about-constraint-minimization-tp3050880p3051663.html
Sent from the R help mailing list archive at Nabble.com.

[[alternative HTML version deleted]]

__
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] question about constraint minimization

2010-11-22 Thread dhacade...@gmail.com

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] <
ml-node+3051338-309339578-202...@n4.nabble.com
> wrote:

> dhacademic  gmail.com  gmail.com> writes:
>
> >
> >
> > Hi,
> >
> > I am a beginner of R. There is a question about constraint minimization.
> A
> > function, y=f(x1,x2,x3x12), 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
>
> __
> [hidden email] 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.
>
>
> --
>  View message @
> http://r.789695.n4.nabble.com/question-about-constraint-minimization-tp3050880p3051338.html
>
> To unsubscribe from question about constraint minimization, click 
> here.
>
>

-- 
View this message in context: 
http://r.789695.n4.nabble.com/question-about-constraint-minimization-tp3050880p3053912.html
Sent from the R help mailing list archive at Nabble.com.

[[alternative HTML version deleted]]

__
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] question about constraint minimization

2010-11-22 Thread dhacade...@gmail.com

Dear Prof. Ravi Varadhan,

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.

I have downloaded your code, but I even do not know how to call your code in
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
work well. Can you please help to revise the input file that can work by
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-202...@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.
>
> ---
> Ravi Varadhan, Ph.D.
> Assistant Professor,
> Division of Geriatric Medicine and Gerontology School of Medicine Johns
> Hopkins University
>
> Ph. (410) 502-2619
> email: [hidden email]
>
>
> -Original Message-
> From: [hidden 
> email][mailto:[hidden
> email] ] On
> Behalf Of [hidden email]
> Sent: Monday, November 22, 2010 11:10 AM
> To: [hidden email] 
> 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] 
> 
> [hidden email] >
>  > wrote:
>
> > dhacademic  gmail.com  gmail.com> writes:
> >
> > >
> > >
> > > Hi,
> > >
> > > I am a beginner of R. There is a question about constraint
> minimization.
> > A
> > > function, y=f(x1,x2,x3x12), 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
> >
> > __
> > [hidden email]
> 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.
> >
> >
> > --
> >  View message @
> >
>
> http://r.789695.n4.nabble.com/question-about-constraint-minimization-tp30508

Re: [R] question about constraint minimization

2010-11-24 Thread dhacade...@gmail.com

Hi Hans,

I really appreciate your help and the detailed reply. Many thanks and have a
nice Thanks giving!

Best,
Hao

On Tue, Nov 23, 2010 at 11:46 AM, Hans W Borchers [via R] <
ml-node+3055782-1866096000-202...@n4.nabble.com
> wrote:

> dhacademic  gmail.com  gmail.com> writes:
>
> > 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
>
> You did not disclose the the function f, is it linear or nonlinear, does it
>
> have (many) local minima, do you know its gradient?, etc.
>
> With some bloody tricks it is possible to emulate equality and inequality
> constraints with 'optim', too. But in general, I would suggest to apply
> 'constrOptim.nl' in the alabama package (or 'solnp' in Rsolnp). These are
> newer implementations and will handle equality constraints nicely.
>
> Assuming your original function f12 is a function of 12 variables, e.g.
>
> f12 <- function(x) sum((1:12)*(x - 1:12)^2)
>
> define a new function f eliminating x3, x4, and x12 through
>
> f <- function(x) {
> xx1r  <- 1.5 - x[1] - sum(x)
> x12  <- c(x[1], x[2], x[1], x[1], x[3:9], xx1r)
> f12(x12)
> }
>
> I would suggest 'solnp' in package "Rsolnp", as the bound constraints can
> be
> formulated somewhat easier; the start value has to be feasible(!):
>
> lower <- c(-1, 0, -1, 0, 0, 0, 0, 0, 0)
> upper <- c( 0, 1,  0, 1, 1, 1, 1, 1, 1)
>
> fun <- function(x) 1.5 - 2*x[1] - x[2] - sum(x[3:9])
> start <- c(-0.2, 0.2, -0.2, rep(0.2, 6))
>
> S <- solnp(start, f, LB=lower, UB=upper, ineqfun=fun, ineqLB=0,
> ineqUB=1)
>
> This will return a (local?) minimum (-1, 0, -1, 0, 0, 0.5, 1, 1, 1) as:
>
> S$pars
>   # [1] -1.00e+00  1.209474e-08 -9.99e-01  4.801754e-08
>  1.930926e-07
>   # [6]  4.99e-01  9.98e-01  1.00e+00  1.00e+00
>
> Hans Werner
>
> 
> P. S.:  Sorry, Ravi, but I could not resist the temptation to at least
> **indicate** one complete solution.
> 
>
> __
> [hidden email] 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.
>
>
> --
>  View message @
> http://r.789695.n4.nabble.com/question-about-constraint-minimization-tp3050880p3055782.html
>
> To unsubscribe from question about constraint minimization, click 
> here.
>
>

-- 
View this message in context: 
http://r.789695.n4.nabble.com/question-about-constraint-minimization-tp3050880p3057370.html
Sent from the R help mailing list archive at Nabble.com.

[[alternative HTML version deleted]]

__
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.