资源描述:
《求函数f(x,y)_=_5sin(xy)_+_x^2_+_y^2的》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、[原创]模拟退火算法/**模拟退火法求函数f(x,y)=5sin(xy)+x^2+y^2的最小值*日期:2004-4-16*作者:ARMYLAU* EMAIL:armylau2@163.com*结束条件为两次最优解之差小于某小量*/usingSystem;namespaceSimulateAnnealing{classClass1{//要求最优值的目标函数staticdoubleObjectFunction(doublex,doubley){doublez=0.0;z=5.0*Math.Sin(x*y)+x*x+y*y;returnz;}[STATh
2、read]staticvoidMain(string[]args){//搜索的最大区间constdoubleXMAX=4;constdoubleYMAX=4;//冷却表参数intMarkovLength=10000;//马可夫链长度doubleDecayScale=0.95;//衰减参数double StepFactor=0.02;//步长因子doubleTemperature=100;//初始温度double Tolerance=1e-8;//容差doublePreX,NextX;//priorandnextvalueofxdoublePreY,N
3、extY;//priorandnextvalueofydouble PreBestX,PreBestY;//上一个最优解doubleBestX,BestY;//最终解doubleAcceptPoints=0.0;//Metropolis过程中总接受点Randomrnd=newRandom();//随机选点PreX=-XMAX*rnd.NextDouble();PreY=-YMAX*rnd.NextDouble();PreBestX=BestX=PreX;PreBestY=BestY=PreY;//每迭代一次退火一次(降温),直到满足迭代条件为止do{
4、Temperature*=DecayScale;AcceptPoints=0.0;//在当前温度T下迭代loop(即MARKOV链长度)次for(inti=0;i{//1)在此点附近随机选下一点do{NextX=PreX+StepFactor*XMAX*(rnd.NextDouble()-0.5);NextY=PreY+StepFactor*YMAX*(rnd.NextDouble()-0.5);}while(!(NextX>=-XMAX&&NextX=-YMAX&&NextY//2)是否全局最优解if(ObjectFunction(BestX,Be
5、stY)>ObjectFunction(NextX,NextY)){//保留上一个最优解PreBestX=BestX;PreBestY=BestY;//此为新的最优解BestX=NextX;BestY=NextY;}//3)Metropolis过程if(ObjectFunction(PreX,PreY)-ObjectFunction(NextX,NextY)>0){//接受,此处lastPoint即下一个迭代的点以新接受的点开始PreX=NextX;PreY=NextY;AcceptPoints++;}else{doublechange=-1*(Ob
6、jectFunction(NextX,NextY)-ObjectFunction(PreX,PreY))/Temperature;if(Math.Exp(change)>rnd.NextDouble()){PreX=NextX;PreY=NextY;AcceptPoints++;}//不接受,保存原解}}Console.WriteLine("{0},{1},{2},{3}",PreX,PreY,ObjectFunction(PreX,PreY),Temperature);} while(Math.Abs(ObjectFunction(BestX,Be
7、stY)–ObjectFunction(PreBestX,PreBestY))>Tolerance);Console.WriteLine("最小值在点:{0},{1}",BestX,BestY);Console.WriteLine("最小值为:{0}",ObjectFunction(BestX,BestY));}}}