Modi
Modi

Reputation: 143

Inequalityconstraints in the Optimx/Optim R packages

Can Optim or Optimx take inequality constraints in a non-linear optimization problem?

I have a nonlinear problem in which there are two types of constraints to be imposed:

Upvotes: 1

Views: 970

Answers (2)

Arthur Yip
Arthur Yip

Reputation: 6230

Using L-BFGS-B in optimx,

  • For Variables >= 0 , you would set the lower argument in optimx to rep.int(0, n_variables)
  • For Sum(variables) <= 1, I don't know of a direct way to program this in with optimx. I would suggest upper = rep.int(1, n_variables), modifying your objective function to include: if Sum(variables) > 1, objectiveValue = objectiveFunction + large penalty, and then set a starting point that meets all your criteria Vars >=0, Sum(Vars) <=1, (and Vars <=1 as implied by your constraints)

Upvotes: 0

Ott Toomet
Ott Toomet

Reputation: 1956

You can do this with maxLik. The inequality constraints there must be specified as $Ax + B > 0$ where x is the parameter.

For example, assume you have two parameters and you maximize the exponential hat:

f <- function(x) exp( - (x[1]-2)^2 - (x[2]-2)^2)
A <- matrix(c(1,1,-1,-1), 2, 2, byrow=TRUE)
B <- c(0,1)

You can check that now $Ax + B > 0$ is equivalent to your inequality conditions (well, except that you wrote >= instead of > but that does not really matter for numerical solutions).

You can use BFGS or other optimizers. You need the list of ineqA and ineqB for the constraints:

a <- maxBFGS(f, start=c(0.2, 0.2), constraints=list(ineqA=A, ineqB=B))
summary(a)

--------------------------------------------
BFGS maximization 
Number of iterations: 50 
Return code: 0 
successful convergence  
Function value: 0.01104892 
Estimates:
      estimate   gradient
[1,] 0.4990966 0.03316674
[2,] 0.4990966 0.03316674

Constrained optimization based on constrOptim 
1  outer iterations, barrier value -0.0003790423 
--------------------------------------------

As you see the constraints are binding, the unconstrained maximum would be at (2,2).

Upvotes: 1

Related Questions