# [R] How to formulate constraint like abs(x) = y in constrOptim (or other)

Tue Dec 7 19:11:07 CET 2010

```Thanks a lot for your responses.
Alabama and Rsolnp packages look promising.
My first tests worked well.

For others searching for alike problems I now use something like:

#equality constraint function
eqFkt<-function(x){
#for sum(x)=sum_x
sm<-sum(x)-sum_x
return(c(sm))
}

ineqFkt<-function(x){
#for -max_x < x < max_x
ubound    <- -x+max_x
lbound    <- x+max_x
#for sum of all negative value to be > min_x_neg
negbound  <- sum(x[x<0])-min_x_neg
return(c(ubound, lbound, negbound))
}

#define start
start_weights<-seq(0, 0, length.out=len_data)
#where mini_func is the minimizing function
res <-auglag(start_weights, mini_func, heq=eqFkt, hin=ineqFkt)

Benjamin

-----Ursprüngliche Nachricht-----
Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im
Gesendet: Montag, 6. Dezember 2010 23:53
An: 'Hans W Borchers'; r-help at stat.math.ethz.ch
Betreff: Re: [R] How to formulate constraint like abs(x) = y in constrOptim
(or other)

Hi Benjamin,

If you just had abs(x_i) < c_i, it will reduce to linear inequalities, but
your constraint cannot be reduced to that.

You might try "alabama" or "Rsolnp" packages.

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
Behalf Of Hans W Borchers
Sent: Monday, December 06, 2010 1:08 PM
To: r-help at stat.math.ethz.ch
Subject: Re: [R] How to formulate constraint like abs(x) = y in constrOptim
(or other)

>
> I am trying to form some constraints for an optimization I am working on.
> I think I have understand the use of the constraints in matrix form. I use
> them like:
>
> [...]
>
> Now I would like to formulate a constraint like
>
> Sum(Abs(x_i))<=limit
>
> But I have no idea how I could get such a condition by using the matrix
> constraint formulation.
>
> I have already searched the help, web and all searchable R resources I
could
> find, but got no helping result.
>
> Is constrOptim the right function to use?
>
> Maybe you can give me a hint or can point me to a good description of
> constraint handling.
>
> Greetings and thanks in advance,
>
> Benjamin
>
> Benjamin B.
> Hamburg, Germany

With 'constrOptim' you can formulate linear constraints only, and in most
cases abs() is not linear. You might try one of the nonlinear optimization
packages.

Another possibility is to reformulate the absolute value with two binary
values, if your optimization function itself is linear; you didn't tell us
the whole story.

Hans Werner

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