题目大意:定义函数 f ( x ) = 6 x 7 + 8 x 6 + 7 x 3 + 5 x 2 + a x f(x)=6x^7+8x^6+7x^3+5x^2+ax f(x)=6x7+8x6+7x3+5x2+ax, a a a已知。求当 0 ≤ x ≤ 100 0\leq x\leq 100 0≤x≤100时 f ( x ) f(x) f(x)的最小值。保留四位小数。有多组数据。
用模拟退火算法搜索 x x x。对于每个温度,求局部最优解。最后比较局部最优解,就能得出最后的结果。
code
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<cstdio>
#include<cmath>
using namespace std;
int tt,T=50;
double a,t0=100,t1,t2=1e-8,k=0.98;
double f(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-a*x;
}
double rd(double x){
return rand()*1.0/RAND_MAX*x;
}
int main()
{
srand(time(NULL));
double x,y,tx,ty,px,py;
scanf("%d",&tt);
while(tt--){
scanf("%lf",&a);
t1=t0;
x=0;
px=0;py=f(0);
while(t1>t2){
y=f(x);
for(int i=1;i<=T;i++){
tx=x+(rand()%2*2-1)*t1;
if(tx<-0.01||tx>100.01) continue;
ty=f(tx);
if(ty<y){
x=tx;y=ty;
}
else{
if(rd(1)<exp((y-ty)/t1)){
x=tx;y=ty;
}
}
if(ty<py){
px=tx;py=ty;
}
}
t1=t1*k;
}
if(px<0.0) px=0.0;
if(px>100.0) px=100.0;
printf("%.4f\n",f(px));
}
return 0;
}