Happiness is more than pleasure without pain

你只有非常努力,才能看起来毫不费力

导航

模拟退火算法

 //模拟退火算法效果稍稍好于爬山算法,核心思想是以一定的概率接受一个比当前解更差的解

public Individual<G, X> solve(final IObjectiveFunction<X> f) {

    Individual<G, X> pcur, pnew;
    Random r =new Random();
    pcur = new Individual<>();
    pnew= new Individual<>();

    pcur.g = this.nullary.create(this.random);
    pcur.x = this.gpm.gpm(pcur.g);
    pcur.v = f.compute(pcur.x); 
    X tmp= pcur.x;

    int t=1;
    double Temp;

    while (!(this.termination.shouldTerminate())) {
      pnew.g = this.unary.mutate(pcur.g, this.random);
      pnew.x = this.gpm.gpm(pnew.g);
      pnew.v = f.compute(pnew.x);
      double value=pnew.v - pcur.v;
      if (value<=0) {
        pcur.assign(pnew);
        if(f.compute(pcur.x)<f.compute(tmp))
          tmp=pcur.x;
      }
      else{
        Temp=this.temperature. getTemperature(t);


        if(r.nextDouble()<Math.exp(-(value/Temp))){
          pcur.assign(pnew);
        }
      }
      t++;
    }
    pcur.x=tmp;
    pcur.v = f.compute(pcur.x); 
    return pcur;
  }

posted on 2014-10-18 09:41  believer  阅读(142)  评论(0编辑  收藏  举报