当我更改间隔时,优化具有怪异的行为 [英] optimize has weird behavior when I change the interval

查看:125
本文介绍了当我更改间隔时,优化具有怪异的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中的optimize()有问题.

当我仅更改optimize()中的间隔时,令人惊讶的是,最佳参数值会有很大不同.我以前发现过类似问题的帖子,但没有答案.

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:

我的代码如下:

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

推荐答案

对于任何假设目标函数是平滑且具有单个最小值的优化器(例如optimize()),此优化问题基本上都是不可能的.您没有给出可复制的示例,但是下面是一个目标函数的示例,它和您的函数一样丑陋:

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))]

有许多随机的全局优化算法-您可以搜索 CRAN优化Task View 可让全局"找到更多信息-但它们都将变慢得多,并且需要对优化控制参数进行大量调整,以针对任何特定问题获得可靠的结果.在这种情况下,optim()"SANN"(模拟退火)方法在默认选项下可以很好地工作-在25次中,有20次得到正确的答案.您可以调整控制参数(例如增加maxit:请参阅?optim),并且可能会做得更好.

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 ...

这篇关于当我更改间隔时,优化具有怪异的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆