gpt4 book ai didi

r - 在 constrOptim 中与简单的约束作斗争

转载 作者:行者123 更新时间:2023-12-03 16:46:36 26 4
gpt4 key购买 nike

我在 R 中有一个函数,我希望在 optim 中的一些简单约束条件下最大化该函数或 constrOptim , 但我正在努力解决 ciui以适应我的限制。

我的职能是:

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

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

}

哪里alphabeta是固定向量,spend是花费的向量 c(sp1,sp2,...,sp6)我想改变以最大化 negexpKPI 的输出.我想约束 spend以三种不同的方式:

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

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

2)总和:

sum(spend)=90000000

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

sum(sp1,sp2)=5000000

有什么帮助吗?对任何其他可行的方法开放,但如果可能的话更喜欢 base R。

最佳答案

根据?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.

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

enter image description here

现在我们可以在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)

附加说明

我觉得这里有问题。 sp1 + sp2 = 5000000,但是sp1sp2都不能大于1000000。所以不存在可行域!请先解决您的问题。

Sorry, I was using sample data that I hadn't fully checked; the true optimisation is for 40 sp values with 92 constraints which would if I'd replicated here in full would have made the problem more difficult to explain. I've added a few extra zeroes to make it feasible now.

关于r - 在 constrOptim 中与简单的约束作斗争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40411078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com