求 F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)的最小值
模拟退火,每次根据温度随机下个状态,再根据温度转移
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define inf (double)0x3f3f3f3f3f3f3f3fll
using namespace std;
double y;
double cal(double x){
return 6*pow(x,7.0)+8*pow(x,6.0)+7*pow(x,3.0)+5*pow(x,2.0)-y*x;
}
double Rand(){
return (rand()%1000)/1000.0;
}
double work(){
double T=100,now=0;
double fnow=cal(now);
double minf=min(inf,fnow);
while(T>0.0000001){
double nxt=now+T*(Rand()*2-1);
if(nxt>=0&&nxt<=100){
double fnxt=cal(nxt);
double dE=fnow-fnxt;
if((dE>0)||(exp(dE/T))>Rand()){
now=nxt;fnow=fnxt;}
}
T*=0.99;
}
return fnow;
}
int main(){
srand(20001101);
int T;
scanf("%d",&T);
while(T--){
scanf("%lf",&y);
printf("%0.4lf\n",work());
}
}