I want to optimize a function that looks like this :
y= P1*y+sum(x1-P1) + P2*y+sum(x2-P2) + P3*y+sum(x3-P3)
with P1, P2 and P3 the three parameters to be optimized and y the function to be minimzed. x1, x2 x3 are vectors of data.
Under the constraints :
P2-P1 >= 0
P3-P2 >=0
Since there are constraints, I can't use the function optim() in R so I had a look on lpSolveAPI.
Using lpSolveAPI I would go :
lps.model <- make.lp(0, 3)
add.constraint(lps.model, c(-1,1,0), ">=", 1)
add.constraint(lps.model, c(0,-1,1), ">=", 1)
But then it becomes a problem when I want to define "set.objfn" which must be defined that way :
set.objfn(lprec, obj, indices) with
lprec: an lpSolve linear program model object.
obj: a numeric vector of length n (where n is the number of decision variables in lprec) containing the coefficients of the objective function. Alternatively, if indices is also provided, a numeric vector of the same length as indices containing only the nonzero coefficients.
indices: optional for sparse obj. A numeric vector the same length as obj of unique values from the set {1, ..., n} where n is the number of decision variables in lprec; obj[i] is entered into column indices[i] in objective function. The coefficients for the columns not in indices are set to zero. This argument should be omitted when length(obj) == n.
I can still re-write the function y like this :
y = P1 (y + [ (x1/P1 ) - 1 ]) + P2 (y + [ (x2/P2 ) - 1 ]) + P3 (y + [ (x3/P3 ) - 1 ])
Though how am I supposed to write those coefficients in front of my parameters in the "obj" part of the function "set.objfn" since my parameters P1 - P3 are actually part of the coefficients ?
obj = c((y + [ (x1/ P1 ) - 1 ] , ... )
It is possible that lpSolveAPI is not the package I should use to optimize this kind of function but I haven't really found any other packages to use.