Re: [R] Optim with two constraints

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu 13 Oct 2005 - 16:46:08 EST

This is actually quadratic programming, so why do you want to use optim()? There are packages specifically for QP, e.g. quadprog.

A more general approach is to eliminate one variable, which gives you an inequality constrained problem in n-1 variables to which you could apply contrOptim(). Other re-parametrizations (e.g. of weights as a log-linear model) will work provided none of the parameters are going to be zero at the optimum (one cannot be one without all the others being zero).

On Wed, 12 Oct 2005, Jens Hainmueller wrote:

> Hi R-list,
>
> I am new to optimization in R and would appreciate help on the following
> question. I would like to minimize the following function using two
> constraints:
>
> ######
> fn <- function(par,H,F){
>
> fval <- 0.5 * t(par) %*% H %*% par + F%*% par
> fval
>
> }
>
> # matrix H is (n by k)
> # matrix F is (n by 1)
> # par is a (n by 1) set of weights
>
> # I need two constraints:
> # 1. each element in par needs to be between 0 and 1
> # 2. sum(par)=1 i.e. the elements in par need to sum to 1
>
> ## I try to use optim
> res <- optim(c(runif(16),fn, method="L-BFGS-B", H=H, F=f
> ,control=list(fnscale=-1), lower=0, upper=1)
> ######
>
> If I understand this correctly, using L-BFGS-B with lower=0 and upper=1
> should take care of constraint 1 (box constraints). What I am lacking is the
> skill to include constraint no 2.
>
> I guess I could solve this by reparametrization but I am not sure how
> exactly. I could not find (i.e. wasn't able to infer) the answer to this in
> the archives despite the many comments on optim and constrained optimization
> (sorry if I missed it there). I am using version 2.1.1 under windows XP.
>
> Thank you very much.
>
> Jens
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Thu Oct 13 16:52:50 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 18:49:54 EST