I have to consider optimization problem in simulation study. An instance is given below:
library(mvtnorm)
library(alabama)
n = 200
q = 0.5
X <- matrix(0,
As said before, see Maximizing nonlinear constraints problem using r package nloptr:
You have to prevent the solver to run into an area where your objective function is not defined, here this means p_i >= 0
for each index i
. And if it does, let the objective function return some finite value. Simplifying your function (for q = 0.5
) it looks, for instance, like
f1 <- function(p) sum(sqrt(pmax(0, p)))
Better also to provide an inequality constraint for p_i > 0
as
heq1 <- function(p) c(sum(x1 * p) - x01, sum(x2 * p) - x02, sum(p) - 1)
hin1 <- function(p) p - 1e-06
Now the solver returns a plausible result:
sol <- alabama::auglag(pInit, fn = function(p) -f1(p),
heq = heq1, hin = hin1)
-1 * sol$value
## [1] 11.47805
and the equality conditions are all satisfied:
heq1(sol$par)
## [1] -4.969690e-09 5.906888e-09 1.808652e-08
All this can be done 'programmatically', with a bit of care, naturally.