# [R] Optimization

Stephen Tucker brown_emu at yahoo.com
Tue Jul 17 15:43:35 CEST 2007

```My apologies, I read the post over too quickly (even the second time).

It's been a while since I've played around with anything other than box
constraints, but this one is conducive to a brute-force approach (employing
Berwin suggestions). The pseudo-code would look something like this:

delta <- 1e-3   # grid space of x3, the smaller the better
oldvalue <- -Inf # some initial value for objective function
for( x3 in seq(0,1,by=delta) ) {
## calculate x1,x2 as per Berwin's response
## if all constraints are met, feasible <- TRUE
## else feasible <- FALSE
if( !feasible ) next # if not feasible, go to next x3 value
## newvalue <- value of objective function with x1,x2,x3
if( newvalue > oldvalue ) {
oldvalue <- newvalue
max.x1 <- x1; max.x2 <- x2; max.x3 <- x3
}
}

You should end up with the desired values of max.x1, max.x2, max.x3. Hope
this helps,

ST

--- "massimiliano.talarico" <massimiliano.talarico at poste.it> wrote:

> Thanks for your suggests, but I need to obtain the MAX of
> this function:
>
> Max x1*0.021986+x2*0.000964+x3*0.02913
>
> with these conditions:
>
> x1+x2+x3=1;
>
> sqrt((x1*0.114434)^2+(x2*0.043966)^2+(x3*0.100031)^2)=0.04;
>
> x1>=0;
> x2>=0;
> x3>=0;
>
>
> Thanks and again Thanks,
> Massimiliano
>
>
>
> My apologies, didn't see the boundary constraints. Try this
> one...
>
> f <- function(x)
>   (sqrt((x[1]*0.114434)^2+(x[2]*0.043966)^2+(x[3]*0.100031)
> ^2)-0.04)^2
>
> optim(par=rep(0,3),f,lower=rep(0,3),upper=rep
> (1,3),method="L-BFGS-B")
>
> and check ?optim
>
> --- "massimiliano.talarico"
> <massimiliano.talarico at poste.it> wrote:
>
> > I'm sorry the function is
> >
> > sqrt((x1*0.114434)^2+(x2*0.043966)^2+(x3*0.100031)^2)
> =0.04;
> >
> > Have you any suggests.
> >
> > Thanks,
> > Massimiliano
> >
> >
> >
> >
> > --- "massimiliano.talarico"
> > <massimiliano.talarico at poste.it> wrote:
> >
> > > Dear all,
> > > I need a suggest to obtain the max of this function:
> > >
> > > Max x1*0.021986+x2*0.000964+x3*0.02913
> > >
> > > with these conditions:
> > >
> > > x1+x2+x3=1;
> > >
> =0.04;
> > > x1>=0;
> > > x1<=1;
> > > x2>=0;
> > > x2<=1;
> > > x3>=0;
> > > x3<=1;
> > >
> > > Any suggests ?
> > >
> > > Thanks in advanced,
> > > Massimiliano
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained,
> > > reproducible code.
> > >
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained,
> reproducible code.
> >
>
>
>
>
> ____________________________________________________________
> ________________________
> Fussy? Opinionated? Impossible to please? Perfect.  Join
> Yahoo!'s user panel and lay it on us.
>
>
>
>

____________________________________________________________________________________