Reza R.
Reza R.

Reputation: 1

Optimizing a function with two parameters (one continuous and one integer value)

Using the language R, I am trying to optimize the two-parameter function f(x,y) such that x,y are both non-negative and y is an integer. However, I do not get the simple answer one would get on paper.

Define

f = function(param){
  x=param[1] ; y=round(param[2])
  temp = (x-1.3)^2+(y-1.7)^2
  return(temp)
}

Clearly, the answer is x=1.3 and y=2, but given the set-up, any value of y in (1.5,2.49) is acceptable as round(y)=2. This is what I get from the constrOptim function though:

constrOptim(c(1,4),f,ui=rbind(c(1,0),c(0,1)),ci=c(0,0) , method="Nelder-Mead")

$par
[1] 1.299810 4.016033

$value
[1] 5.29

$counts
function gradient 
      43       NA 

$convergence
[1] 0

$message
NULL

$outer.iterations
[1] 2

$barrier.value
[1] -6.084598e-05

Upvotes: 0

Views: 140

Answers (1)

FGirosi
FGirosi

Reputation: 106

I think it is because your starting point for y is 4. if you set it around 2 it will give you what you expect. I think the problem is that your objective function is perfectly flat in certain regions and has discontinuities, so there is not always a meaningful gradient to follow to the global minimum. for x = 0.1 your function looks like this:enter image description here

so the optimization procedure is going to have a hard time to give you the correct result. one option is to substitute the round() function with a smoother version of it.

Upvotes: 1

Related Questions