算法-初次尝试-模拟退火

2021/4/18 20:25:16

本文主要是介绍算法-初次尝试-模拟退火,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

我对模拟退火的初步理解,还没深入了解过。这里只是用模拟退火求函数极值。
题目:https://vjudge.net/problem/HDU-2899

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
const ll MAXN=1e18;
const double eps=1e-8;

double y;

double func(double x){
    return 6*x*x*x*x*x*x*x+8*x*x*x*x*x*x+7*x*x*x+5*x*x-y*x;
}
int f[2]={-1,1};

int main(){
    srand(time(NULL));
    int t;
    cin>>t;
    while(t--){
        cin>>y;
        double T=100;
        double delta=0.98;
        double x=50;
        double ans=func(x);
        while(T>eps){
            int f[2]={-1,1};
            double newx=x+f[rand()%2]*T;
            if(newx>=0&&newx<=100){
                double next=func(newx);
                if(ans>next){
                    ans=next;
                    x=newx;
                }
            }
            T*=delta;
        }
        cout<<fixed<<setprecision(4)<<ans<<'\n';
    }
    return 0;
}


这篇关于算法-初次尝试-模拟退火的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程