0

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.

Amandine.G
  • 181
  • 1
  • 2
  • 9

1 Answers1

0

I don't think your equation is linear and so lpSolve won't be of much help in your current formulation of your objective function.

Here is the best I can do to re-arrange your function y (I'm assuming your vectors x1 x2 x3 are each 1Xn, different sizes shouldnt really affect the analysis below):

y = P1*y+sum(x1-P1) + P2*y+sum(x2-P2) + P3*y+sum(x3-P3) 
y = y*P1 + y*P2 + y*P3 + sum(x1) + sum(x2) + sum(x3) - n*P1 - n*P2 - n*P3

I don't think sum(x1)+sum(x2)+sum(x3) are really needed as they are some sort of constant, so your objective function can be simplified to:

y = y*(P1 + P2 + P3) - n*(P1 + P2 + P3)
y = -n*(P1 + P2 + P3) / (1 - P1 - P2 - P3) 

or alternatively

y =  n*(P1 + P2 + P3) / (P1 + P2 + P3 - 1)

Take a look at the ratios section of http://lpsolve.sourceforge.net/5.5/, there are some helpful techniques on how to re-arrange ratios in your objective function and/or constraints that will allow you to use lpsolve.