# optimize has weird behavior when I change the interval

TAGS :
Viewed: 2 - Published at: a few seconds ago

#### [ optimize has weird behavior when I change the interval ]

I have a problem with `optimize()` in R.

When I only change the interval in `optimize()`, surprisingly, the optimal parameter value will vary a lot. I found posts with similar problems before, but there is no answer for them.

I got really different values from different intervals:

``````c(-1,1): -0.819

c(-1,2): -0.729
c(0.3,0.99):0.818
c(0.2,0.99):0.803
c(0.1,0.99):0.23
c(0,0.99):0.243
``````

I really need help on this problem, thank you guys if you could help or give me any information!!

edit: here is a picture of the objective function: My code is as below:

``````dis&lt;-data[,5]
vel&lt;-data[,3]
condition&lt;-data[,2]
nrow&lt;-nrow(data)
number&lt;-500
status&lt;-0
counter&lt;-rep(0,nrow)
firstvel&lt;-rep(0,nrow)
secondvel&lt;-rep(0,nrow)
j=1
n=1
l=0
secondpoint&lt;-rep(0,nrow)
f&lt;-function(a,b,p){
for (i in 5:p){
diss&lt;-dis[1:(i-1)]
stddis&lt;-sd(diss)
lowerdis&lt;- a*stddis
upperdis&lt;- b*stddis
if (status==0&amp;&amp;dis[i]&gt;=upperdis){
status&lt;-1
firstvel[j]&lt;-vel[i]
j=j+1
}
else if (status==1&amp;&amp;condition[i]&lt;=condition[i-1]&amp;&amp;dis[i]&lt;lowerdis){
status&lt;-0
secondvel[n]&lt;-vel[i]
n=n+1
}
}
secondvel&lt;- subset(secondvel, secondvel&gt;0)
firstvel&lt;- subset(firstvel, firstvel&gt;0)
if (j==n&amp;&amp;j&gt;1){
for (k in 1:(j-1)){
unit&lt;-number/firstvel[k]
number&lt;-unit*secondvel[k]
}
}   else if(j&gt;1) {
for (k in 1:(j-2)){
unit&lt;-number/firstvel[k]
number&lt;-unit*secondvel[k]
}
unit&lt;-number/firstvel[k+1]
number&lt;-unit*vel[p]
}
return(-number)
}
for (point in 300:nrow){
diss&lt;-dis[1:(point-1)]
stddis&lt;-sd(diss)
upperdis&lt;- stddis
if (status==0&amp;&amp;dis[point]&gt;=upperdis){
status&lt;-1
firstvel[j]&lt;-vel[point]
j=j+1
last&lt;-optimize(f,c(0.2,0.99),b=1.0,p=point)
secondpoint[n]&lt;-last\$minimum      ## This is the optimal value I need, which changes a lot
lowerdis&lt;- secondpoint[n]*stddis
}
else if (status==1&amp;&amp;condition[point]&lt;=condition[point-1]&amp;&amp;dis[point]&lt;lowerdis){
status=0
secondvel[n]&lt;-vel[point]
n=n+1
}
}
secondvel&lt;-subset(secondvel,secondvel&gt;0)
firstvel&lt;-subset(firstvel,firstvel&gt;0)
secondpoint&lt;-as.numeric(secondpoint[1:(j-1)])
diff&lt;-rep(0,(j-1))
if (j==n&amp;&amp;j&gt;1){
for (k in 1:(j-1)){
unit&lt;-number/firstvel[k]
number&lt;-unit*secondvel[k]
diff[k]&lt;-unit*(secondvel[k]-firstvel[k])
}
}  else if(j&gt;1) {
for (k in 1:(j-2)){
unit&lt;-number/firstvel[k]
number&lt;-unit*secondvel[k]
diff[k]&lt;-unit*(secondvel[k]-firstvel[k])
}
unit&lt;-number/firstvel[k+1]
number&lt;-unit*vel[nrow]
diff[k+1]&lt;-unit*(vel[nrow]-firstvel[k+1])
}
``````

This optimization problem is essentially going to be impossible for any optimizer (such as `optimize()`) that assumes the objective function is smooth and has a single minimum. You didn't give a reproducible example, but here's an example of an objective function that's just about as ugly as yours:

``````set.seed(101)
r <- runif(11)
f <- function(x) r[pmin(11,pmax(1,floor(x)+1))]
``````

There are many stochastic global optimizing algorithms -- you can search the CRAN Optimization Task View for "global" to find more -- but they will all be much slower, and require a great deal more tuning of optimization control parameters, to get reliable results for any particular problem. In this case, the `"SANN"` (simulated annealing) method from `optim()` works reasonably well with the default options -- it gets the right answer 20 out of 25 times. You could adjust the control parameters (e.g. increase `maxit`: see `?optim`) and perhaps do better.

``````pvals <- replicate(25,optim(f,par=5,method="SANN")\$par)

curve(f,from=-1,to=11)
points(pvals,f(pvals),col=2)
sum(pvals>1 & pvals<2) ## 20
`````` Alternatively, for a 1D problem brute-force grid search is always an option ...