机械优化设计随机法程序设计

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includestdio.h#includestdlib.h#includemath.hdoubleobjf(doublex[]){doubleff;ff=pow((x[0]-4),2)+pow((x[1]-5),2);return(ff);}/*约束条件子程序*/doublestrain(doublex[]){inti;doubleg[3];g[0]=x[0]-1;g[1]=x[1]-1;g[2]=8-x[0]-x[1];for(i=0;i3;i++){if(g0)return(0);}return(1);}doublern(double*r){intm;doubles,u,v,p;s=65536.0;u=2053.0;v=13849.0;*r=u*(*r)+v;m=(int)(*r/s);*r=*r-m*s;p=*r/s;return(p);}doublerandopti(doublex[],doublexl[],doublexh[],doublea0,doubleeps,intn,intnmax){inti,iw,k,jj=0;double*x0,*s,a,f0,f,rr=0,ss;x0=(double*)malloc(n*sizeof(double));s=(double*)malloc(n*sizeof(double));do{for(i=0;in;i++)*(x0+i)=xl[i]+rn(&rr)*(xh[i]-xl[i]);jj=jj+1;iw=strain(x0);if(jj2000){printf(找不到初始可行点!);return(-1);}}while(iw!=1);a=a0;f0=objf(x0);for(;;){k=1;jj=0;L1:ss=0;for(i=0;in;i++)*(s+i)=rn(&rr);for(i=0;in;i++)ss+=*(s+i)*(*(s+i));for(i=0;in;i++){*(s+i)=*(s+i)/sqrt(ss);*(x+i)=*(x0+i)+a*(*(s+i));}do{iw=strain(x);if(iw==1){f=objf(x);if(ff0){for(i=0;in;i++){*(x0+i)=*(x+i);*(x+i)=*(x0+i)+a*(*(s+i));}f0=f;jj=1;}elsebreak;}}while(iw==1);if(jj==0){k+=1;if(knmax){if(aeps)a=a*0.5;else{free(x0);free(s);return(objf(x));}}elsegotoL1;}}}voidmain(){doublea[]={0,0};doubleb[]={8,9};doublec,x[2];c=randopti(x,a,b,1,0.00001,2,50);printf(输出最优点及其目标函数值:\n);printf(x[0]=%f,x[1]=%f,ff=%f\n,x[0],x[1],c);getchar();}

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功