zoukankan      html  css  js  c++  java
  • 模拟退火算法

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

    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;
      }
  • 相关阅读:
    MVC 学习(二)之Linq to Sql 简单Demo
    MVC 学习(一)Linq to Entities 简单Demo
    MVC学习(三)Code-First Demo
    pickle 模块
    json 模块
    sys 模块
    os 模块
    random(随机)模块
    time 模块
    python之函数基础
  • 原文地址:https://www.cnblogs.com/gaoxiangde/p/4379878.html
Copyright © 2011-2022 走看看