Strange fuction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10439 Accepted Submission(s): 6984
Problem Description
Now, here is a fuction:
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
Sample Input
2 100 200
Sample Output
-74.4291 -178.8534
Author
Redow
模拟退火nb!!!!!!!!
先随机去一个值,然后随机移动,看是否能得到更优解,如果是,更新当前点为更优点
每次随机移动的长度在缩短,这样保证了跳出局部最优解(极值和最值的区别不用说了吧)
其实这样也有可能出现找到的是极值不是最值的情况(但是概率比较小),
所以可以一次随机好多值,更新答案,这样最后取最优解,
相当于让你自己做一道题,然后虽然你很nb,但是你也有可能做错,
那么现在拉上一群人一起做,这样出错的概率就小多了,
/*好像某个什么cpu里的纠错系统也是这个想法来着*/
复杂度分析了下每次乘delta,那么t*delta^x<=Esp
这样或者说
得,log级别的,增长肯定不快
Esp = 1e-6 1e-8
10^1 ->797 10^1 ->1025
10^10 ->1823 10^10 ->2051
10^19 ->2849 10^19 ->3077
这里计算的是单纯的while循环的执行次数,如果有n个初始值每个初始值每次瞎跑k次
那么复杂度就是O(x*n*k)咯,大概吧
原来的模拟退火好像不太对(但是A题没问题,就是那个什么什么准则没有),换掉
新写法
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double Esp = 1e-6,initT = 100,inf = 1e18,delta = 98e-2;
const int k = 10;
double Pow(double a,int b){double ans = 1;while(b){if(b&1)ans *= a;a*=a;b>>=1;}return ans;}
double Rand(){return rand()&1 ? 1.0*rand()/RAND_MAX : -1.0*rand()/RAND_MAX;}
double Fun(double x,double y){return 6*Pow(x,7)+8*Pow(x,6)+7*Pow(x,3)+5*Pow(x,2)-y*x;}
double Sovle(double y){
double t = initT,ans = inf;
double x = fabs(Rand())*100,tx;
double f1 = Fun(x,y),f2,df,r;
while(t>Esp){
for(int i=0;i<k;i++){
tx = x + Rand()*t;
if( tx - 0.0 >= Esp && tx - 100.0 <= Esp){///试探要在范围内
f2 = Fun(tx,y);///新解
df = f2 - f1;/// 以下是Metropolis准则
if(df <= Esp){
r = fabs(Rand());
if(exp(df/t) <= r){
f1 = f2;
x = tx;
}
}
}
ans = min(ans,f1);
}
t *= delta;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
double y;
scanf("%lf",&y);
printf("%.4f\n",Sovle(y));
}
return 0;
}
原来在另一个博主那看到的写法
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double Esp = 1e-6,initT = 100,inf = 1e18,delta = 98e-2;
///Esp-->精度,initT-->初始步长,inf-->最大值,delta-->步长每次缩短的系数
const int k = 10;
///每次随机跑动几次
double Pow(double a,int b){///快速幂求a^b
double ans = 1;
while(b){
if(b&1)
ans *= a;
a*=a;
b>>=1;
}
return ans;
}
double Rand(){///随机数输出一个概率范围是[-1,1]
return rand()&1 ? 1.0*rand()/RAND_MAX : -1.0*rand()/RAND_MAX;
}
double Fun(double x,double y){///题目中的函数
return 6*Pow(x,7)+8*Pow(x,6)+7*Pow(x,3)+5*Pow(x,2)-y*x;
}
double Sovle(double y){///模拟退火
double t = initT,ans = inf;///t-->初始温度(步长),ans-->答案
double x = fabs(Rand())*100;///生成原始解,[0,100]
while(t>Esp){///步长咯
double tfx = Fun(x,y);///函数值
for(int i=0;i<k;i++){///随机瞎跑,取最优解
double tx = x + Rand()*t;///跑动范围(delta x) [-t,t],即最大步长内
if( tx - 0 >= Esp && tx - 100 <= Esp){///在[0,100]范围内
double ttfx = Fun(tx,y);///这次跑出去得到的函数值
if(ttfx < tfx){///如果比较优秀
tfx = ttfx;
x = tx;
}
}
ans = min(ans,tfx);///更新答案
}
t *= delta;///步长缩短
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
double y;
scanf("%lf",&y);
printf("%.4f\n",Sovle(y));
}
return 0;
}