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;
      }
  • 相关阅读:
    python中的内置函数的思维导图
    练习(面试题):关于生成器函数的求和问题
    推导式, 生成器表达式
    生成器
    静态代码块
    java中内存的划分
    静态方法
    Chapter07Scanner类、Random类、ArrayList类
    泛型
    对象数组
  • 原文地址:https://www.cnblogs.com/gaoxiangde/p/4379878.html
Copyright © 2011-2022 走看看