R lpSolveAPI with non trivial coefficients in set.objfn



我想优化一个看起来像这样的函数:

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

P1、P2P3是要优化的三个参数,y 是要最小化的功能。x1、x2 x3是数据的向量。

在约束下:

P2-P1 >= 0
P3-P2 >=0

由于存在约束,我不能在 are 中使用函数 optim((,所以我在 lpSolveAPI 上看了一下。


使用lpSolveAPI,我会去:

lps.model <- make.lp(0, 3) 
add.constraint(lps.model, c(-1,1,0), ">=", 1)
add.constraint(lps.model, c(0,-1,1), ">=", 1)

但是当我想定义必须这样定义的"set.objfn"时,它就变成了一个问题:

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.

我仍然可以像这样重写函数 y:

y = P1 (y + [ (x1/P1 ) - 1 ]) + P2 (y + [ (x2/P2 ) - 1 ]) + P3 (y + [ (x3/P3 ) - 1 ])

虽然我应该如何在函数"set.objfn"的"obj"部分的参数前面写这些系数,因为我的参数P1 - P3实际上是系数的一部分?

obj = c((y + [ (x1/ P1 ) - 1 ] , ... )

lpSolveAPI 可能不是我应该用来优化这种函数的包,但我还没有真正找到任何其他包来使用。

我不认为你的方程是线性的,所以lpSolve对你目前的目标函数公式没有多大帮助。

这是我能做的最好的方法来重新排列你的函数y(我假设你的向量x1 x2 x3每个是1Xn,不同的大小不应该真正影响下面的分析(:

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

我不认为 sum(x1(+sum(x2(+sum(x3( 真的需要,因为它们是某种常量,所以你的目标函数可以简化为:

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

或者

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

看看 http://lpsolve.sourceforge.net/5.5/的比率部分,有一些有用的技术可以告诉你如何在目标函数和/或约束中重新排列比率,这些技术将允许你使用lpsolve。

最新更新