与 constrOptim 中的简单约束作斗争

Pau*_*koo 3 optimization r constraints

我在 R 中有一个函数,我希望在optimor 中的一些简单约束下最大化该函数constrOptim,但我正在努力解决问题ciui适应我的约束。

我的功能是:

negexpKPI <- function(alpha,beta,spend){

  -sum(alpha*(1-exp(-spend/beta)))

}
Run Code Online (Sandbox Code Playgroud)

其中alphabeta是固定向量,并且spendc(sp1,sp2,...,sp6)我想要改变以最大化negexpKPI. 我想以spend三种不同的方式进行约束:

1) 每个 的最小值和最大值sp1,sp2,...,sp6,即

0 < sp1 < 10000000 5000 < sp2 < 10000000 ...

2)总和:

sum(spend)=90000000

3) 一些单独组件的总和:

sum(sp1,sp2)=5000000

请问有什么帮助吗?对任何其他可行的方法持开放态度,但如果可能的话,更喜欢使用基础 R。

李哲源*_*李哲源 5

根据?constrOptim

The feasible region is defined by ‘ui %*% theta - ci >= 0’. The
starting value must be in the interior of the feasible region, but
the minimum may be on the boundary.
Run Code Online (Sandbox Code Playgroud)

所以这只是以矩阵格式重写约束的问题。请注意,身份约束只是两个不等式约束。

在此处输入图片说明

现在我们可以在 R 中定义:

## define by column
ui = matrix(c(1,-1,0,0,1,-1,1,-1,
              0,0,1,-1,1,-1,1,-1,
              0,0,0,0,0,0,1,-1,
              0,0,0,0,0,0,1,-1,
              0,0,0,0,0,0,1,-1,
              0,0,0,0,0,0,1,-1), ncol = 6)

ci = c(0, -1000000, 5000, -1000000, 5000000, 90000000, -90000000)
Run Code Online (Sandbox Code Playgroud)

附加说明

我认为这里有问题。sp1 + sp2 = 5000000,但两者sp1sp2不能大于1000000。所以没有可行域!请先解决您的问题。

抱歉,我使用的样本数据尚未完全检查;真正的优化是针对sp具有 92 个约束的40 个值,如果我在这里完整复制,会使问题更难以解释。我添加了一些额外的零以使其现在可行。