How to do an optimization with inequality restriction?

Asked

Viewed 969 times

7

Suppose I want to minimize the following function:

-(5-(x1-2)^2-2*(x2-1)^2)
s.a. x1+4*x2 < 3

For optimization problems without restriction I can use the following code.

fr <- function(x){
  x1 <- x[1]
  x2 <- x[2]
  -(5-(x1-2)^2-2*(x2-1)^2)
}

optim(c(1,1),fr)

How to do optimization with restriction?

2 answers

2

In this example, you can define in the function that if the values exceed the constraint, it receives infinite value:

fr <- function(x){
  if(x[1]+4*x[2] > 3 ){value<-Inf}
  else{
    x1 <- x[1]
    x2 <- x[2]
    -(5-(x1-2)^2-2*(x2-1)^2)
  }
}

Solving:

optim(c(1,0),fr)
$par
[1] 1.6666193 0.3333452

$value
[1] -4

2


I am learning Portuguese, but this is a solution without inf explicita.

ui = c(-1, -4)
ci = c(-3)

fr <- function(x) {
    x1 = x[1]
    x2 = x[2]
    -(5-(x1-2)^2-2*(x2-1)^2)
}

constrOptim(c(1.6,.2), fr, ui=ui, ci=ci, method='Nelder-Mead')

$par
[1] 1.6668 0.3333

$value
[1] -4

$counts
function gradient 
     192       NA 

$convergence
[1] 0

$message
NULL

$outer.iterations
[1] 3

$barrier.value
[1] -0.000

Browser other questions tagged

You are not signed in. Login or sign up in order to post.