Reputation: 13
Say I am trying to optimise the parameters of a function given some data. Say the parameter is a 3-element vector. Using the constrOptim function I could set the conditions that the sum of the parameters must be greater than or equal to 1,...(using the ui and ci arguments of the constrOptim function).
But how would I go about restricting the 3-element parameter vector to have only 2 non-zero elements? I cannot figure out a way - I thought of specifying a 4-element parameter vector where the 4th element is a function of the first 3 elements of the vector (ie. counts the number of non-zeroes in the first 3 elements of the vector and then restrict that to a certain number) but cannot get anything working. Any ideas (much appreciated)?
#Some simple code as context:
data=cbind(rnorm(5)+1,rnorm(5)+1,rnorm(5)+1)
par=c(0.5,0.3,0.2)
fn=function(par,data) {return(as.numeric(rep(1,times=5)%*%(data%*%par)))}
#setting the conditions that the sum of the parameters must be 1:
u1<-rbind(c(1,1,1),c(-1,-1,-1))
c1<-c(0.999, -1.001)
constr.optim1<-constrOptim(c(0.3,0.3,0.4), f=fn, data=data,grad=NULL, ui=u1, ci=c1)
sum(constr.optim1$par) #=0.99997 which is close enough to 1 as specified
#But how would I set/restrict:
length(which(constr.optim1$par!=0)) #to equal 2 and not 3?
Upvotes: 1
Views: 1165
Reputation: 16724
I am afraid that "k out of n can be non-zero" is a difficult constraint. Sometimes this is called a cardinality constraint. Basically we need to add additional binary decision variables to indicate if a variable is zero or not. This will make the model a so-called MINLP (Mixed Integer Nonlinear Programming) model. To solve this you would need an MINLP solver (which are readily available, e.g. through NEOS).
Upvotes: 1
Reputation: 11518
R has packages for non-linear optimisation with constraints, where you can specify constrainst in terms of inequalities. Look in the Optimisation view for packages corresponding to Nonlinear programming.
Upvotes: 0