How to do an optimization with restriction of inequality?

6

Suppose I want to minimize the following function:

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

For unrestricted optimization problems 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 optimize with the constraint?

    
asked by anonymous 05.04.2014 / 00:03

2 answers

2

I'm learning Portuguese, but this is a solution without inf explicit.

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
    
05.04.2014 / 09:10
2

In this example, you can define in the function that, if the values exceed the constraint, it gets 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
    
05.04.2014 / 04:51