Struggling with simple constraints in constrOptim

此生再无相见时 提交于 2019-12-30 07:15:11

问题


I have a function in R that I wish to maximise subject to some simple constraints in optim or constrOptim, but I'm struggling to get my head around ci and uito fit my constraints.

My function is:

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

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

}

where alpha and beta are fixed vectors, and spend is a vector of spends c(sp1,sp2,...,sp6) which I want to vary in order to maximise the output of negexpKPI. I want to constrain spend in three different ways:

1) Min and max for each sp1,sp2,...,sp6, i.e

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

2) A total sum:

sum(spend)=90000000

3) A sum for some individual components:

sum(sp1,sp2)=5000000

Any help please? Open to any other methods that would work but would prefer base R if possible.


回答1:


According to ?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.

So it is just a matter of rewriting your constraints in matrix format. Note, an identity constraint is just two inequality constraints.

Now we can define in 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)

Additional Note

I think there is something wrong here. sp1 + sp2 = 5000000, but both sp1 and sp2 can not be greater than 1000000. So there is no feasible region! Please fix your question first.

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.



来源:https://stackoverflow.com/questions/40411078/struggling-with-simple-constraints-in-constroptim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!