# Help with using lp() package

I am struggling to use the lp() function, from lpSolve() with a series of constraints. In particular, I want to add a non-zero constraint to all elements of the solution.
The data:

``````q <- c(6,3,6,6,6,6,3,5)
Q <- 5
``````

I want to find a set of 8 weights that make Q a weighted sum of q. The following works OK:

``````obj.fun <- rep(1,8)
constr <- rbind(q,rep(1,8))
constr.dir <- c("==","==")
rhs <- c(Q,1)
p.start <- lp("max",obj.fun,constr,constr.dir,rhs)\$sol
``````

But I want the solution to be all 8 weights (p.start) as non-zero. I have tried:

``````obj.fun <- rep(1,8)
constr <- rbind(q,rep(1,8),diag(nrow=8))
constr.dir <- c("==","==",">")
rhs <- c(Q,1,rep(0,8))
p.start <- lp("max",obj.fun,constr,constr.dir,rhs)\$sol
``````

But I get an error, and the solution still has 6 of the weights as zero. Can anyone help me get this constraint in correctly?

There are solutions that don't require lp()

``````q <- c(6,3,6,6,6,6,3,5)
Q <- 5

#I want to find a set of 8 weights that make Q a weighted sum of q.
(weights <- Q/(length(q) *q))
#  0.1041667 0.2083333 0.1041667 0.1041667 0.1041667 0.1041667 0.2083333 0.1250000

Q ==  sum(weights*q)
# True``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.