声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1910|回复: 0

[优化设计] 机械优化设计程序

[复制链接]
发表于 2016-6-16 15:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
  今天终于交了机械优化设计作业,程序贴出来,那位要用就拿去吧,资源共享了,O(∩_∩)O
  下面是利用外点惩罚函数(Powell)法求解三维目标函数最优解与最优值的程序,用C++编写。

1.jpg

  1.   #include

  2.   #include

  3.   double lamta[10]={0, 1.0 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,1};//鲍威尔方法初始化方向,线性无关

  4.   double lamta1[3]={0, 0 , 0};//暂存新的搜索方向

  5.   double x1[4]={0, 0 ,0, 0 };//x1到x3用于存储各共轭方向的点

  6.   double x2[4]={0, 0 ,0, 0 };

  7.   double x3[4]={0, 0 ,0, 0 };

  8.   double x4[4]={0, 0 ,0, 0 };//x4用于中间判断

  9.   double x5[4]={0, 0 ,0, 0 };//x5用存放于更换方向后产生的新点

  10.   int m=0;//标志

  11.   double x_[4]={0, 0, 0, 0};//暂存鲍威尔最优解

  12.   double x0[4]={0, 2, 2 , 2};//初值

  13.   double c=10;//递减系数

  14.   double e=0.00001;//精度控制

  15.   double r0=1;//初始惩罚因子

  16.   double r=1;

  17.   //函数声明部分

  18.   void Powell(double r); //鲍威尔方法函数

  19.   double fxy(double x1,double x2,double x3,double r); //待求函数

  20.   double ysearch(double x); //一维搜索的目标函数

  21.   void search(double &a,double &b,double h); //区间搜索

  22.   double yellowcut(double &a,double &b); //黄金分割

  23.   void sort(double *p,int size);//选择法排序

  24.   void main() //约束优化方法主函数入口

  25.   {

  26.   cout<<"请输入精度"<

  27.   cin>>e;

  28.   changyan:Powell(r);

  29.   double cmpare[4];

  30.   int flag1=0;

  31.   for (int i=1;i<=3;i++)

  32.   {

  33.   cmpare[i]=x_[i]-x0[i];

  34.   if (fabs(cmpare[i])

  35.   {flag1++;}

  36.   }

  37.   if (flag1==3)

  38.   {

  39.   cout<<"最优解为:"<<"x1="<

  40.   }

  41.   else

  42.   {

  43.   for (int j=1;j<=3;j++)

  44.   {

  45.   x0[j]=x_[j];

  46.   }

  47.   r=c*r;

  48.   goto changyan;

  49.   }

  50.   }

  51.   //子函数定义部分

  52.   double fxy(double x1,double x2,double x3,double r)//待求函数

  53.   {

  54.   double m,n,p;

  55.   m=(-x1>0)?(-x1):0;

  56.   n=(-x2>0)?(-x2):0;

  57.   p=(-x3>0)?(-x3):0;

  58.   return //惩罚函数

  59.   1000-x1*x1-2*x2*x2-x3*x3-x1*x2-x1*x3+r*(m*m+n*n+p*p)+r*((x1*x1+x2*x2+x3*x3-25)*(x1*x1+x2*x2+x3*x3-25)+(8*x1+14*x2+7*x3-56)*(8*x1+14*x2+7*x3-56));

  60.   }

  61.   void Powell(double r) //鲍威尔方法函数定义

  62.   {

  63.   double det=0.0001; //迭代精度

  64.   int k;

  65.   my1: for (k=1;k<=3;k++)

  66.   {

  67.   m=3*k-2;

  68.   double a=0,b=0,xo=0;

  69.   search(a,b,1); //完成区间搜索

  70.   double temp;

  71.   temp=yellowcut(a,b);//黄金分割法

  72.   int n=3*k-2;

  73.   for (int i=1;i<=3;i++)

  74.   {

  75.   switch (k)

  76.   {

  77.   case 1:x1[i]=x0[i]+temp*lamta[n++];break;

  78.   case 2:x2[i]=x1[i]+temp*lamta[n++];break;

  79.   case 3:x3[i]=x2[i]+temp*lamta[n++];break;

  80.   default :break;

  81.   }

  82.   }

  83.   }

  84.   double cmp[4];

  85.   int flag=0;

  86.   for (int i=1;i<=3;i++)

  87.   {

  88.   cmp[i]=x3[i]-x0[i];

  89.   if (fabs(cmp[i])

  90.   {flag++;}}

  91.   if (flag==3) //找到最优解

  92.   {

  93.   x_[1]=x3[1];

  94.   x_[2]=x3[2];

  95.   x_[3]=x3[3];

  96.   }

  97.   else

  98.   {

  99.   double fy[4];

  100.   fy[0]=fxy(x0[1],x0[2],x0[3],r);

  101.   fy[1]=fxy(x1[1],x1[2],x1[3],r);

  102.   fy[2]=fxy(x2[1],x2[2],x2[3],r);

  103.   fy[3]=fxy(x3[1],x3[2],x3[3],r); double fyy[3];

  104.   for (int ii=0;ii<3;ii++)

  105.   {fyy[ii]=fy[ii]-fy[ii+1];}

  106.   sort(fyy,3);

  107.   for (ii=1;ii<=3;ii++)

  108.   {x4[ii]=2*x3[ii]-x0[ii];}

  109.   double f0,f3,f4;

  110.   f0=fy[0];

  111.   f3=fy[3];

  112.   f4=fxy(x4[1],x4[2],x4[3],r);

  113.   if ((f0+f4-2*f3)/2>=fyy[2])

  114.   {

  115.   if (f3

  116.   {

  117.   for (int t=1;t<=3;t++)

  118.   {x0[t]=x3[t];}

  119.   }

  120.   else

  121.   {

  122.   for (int t=1;t<=3;t++)

  123.   {x0[t]=x4[t];

  124.   }}

  125.   goto my1;

  126.   }

  127.   else{

  128.   for (int t=0;t<3;t++)

  129.   {lamta1[t]=x3[t+1]-x0[t+1];}

  130.   m=0; //switch 标志!

  131.   double aa=0,bb=0;

  132.   search(aa,bb,1);

  133.   double temp1;

  134.   temp1=yellowcut(aa,bb);

  135.   for (int i=1;i<=3;i++)

  136.   {x5[i]=x3[i]+temp1*lamta1[i-1];}

  137.   for (i=1;i<=3;i++)

  138.   {x0[i]=x5[i];}

  139.   for (i=1;i<=6;i++)

  140.   {lamta[i]=lamta[i+3];}

  141.   for (i=1;i<=3;i++)

  142.   {

  143.   lamta[6+i]=lamta1[i-1];}

  144.   goto my1;

  145.   }}}

  146.   double ysearch(double x) //一维搜索的目标函数

  147.   {

  148.   switch (m)

  149.   {

  150.   case 1: return fxy(x0[1]+x*lamta[m],x0[2]+x*lamta[m+1],x0[3]+x*lamta[m+2],r);break;

  151.   case 4: return fxy(x1[1]+x*lamta[m],x1[2]+x*lamta[m+1],x1[3]+x*lamta[m+2],r);break;

  152.   case 7: return fxy(x2[1]+x*lamta[m],x2[2]+x*lamta[m+1],x2[3]+x*lamta[m+2],r);break;

  153.   case 0: return fxy(x3[1]+x*lamta1[0],x3[2]+x*lamta1[1],x3[3]+x*lamta1[2],r);break;//更改方向后的一维搜索

  154.   default:return 0; break;

  155.   }

  156.   }

  157.   void search(double &a,double &b,double h) //区间搜索

  158.   {double a1,a2,a3,y1,y2,y3;

  159.   h=1;

  160.   a1=a,y1=ysearch(a1);

  161.   a2=a+h,y2=ysearch(a2);

  162.   if(y2>=y1){

  163.   h=-h,a3=a1,y3=y1;

  164.   a1=a2,y1=y2,a2=a3,y2=y3;}

  165.   a3=a2+h,y3=ysearch(a3);

  166.   while(y3<=y2){

  167.   h=2*h;

  168.   a1=a2,y1=y2,a2=a3,y2=y3;

  169.   a3=a2+h,y3=ysearch(a3);

  170.   }

  171.   if(h<0)a=a3,b=a1;

  172.   else a=a1,b=a3;}

  173.   double yellowcut(double &a,double &b){

  174.   double e; //黄金分割法求解

  175.   e=0.001;

  176.   double c,fc;

  177.   c=a+0.382*(b-a);

  178.   fc=ysearch(c);

  179.   double d,fd;

  180.   double xo;

  181.   d=a+0.618*(b-a);

  182.   fd=ysearch(d);

  183.   label2: if (fc<=fd)

  184.   {b=d;

  185.   d=c;

  186.   fd=fc;

  187.   c=a+0.382*(b-a);

  188.   fc=ysearch(c);}

  189.   else

  190.   {a=c;

  191.   c=d;

  192.   fc=fd;

  193.   d=a+0.618*(b-a);

  194.   fd=ysearch(d);}

  195.   if ((b-a)<=e)

  196.   {xo=(a+b)/2;}

  197.   else

  198.   goto label2;

  199.   return xo;

  200.   }

  201.   void sort(double *p,int size){//选择法排序

  202.   int i,j;

  203.   double k;

  204.   for(i=0;i

  205.   for(j=i+1;j

  206.   if(*(p+i)>*(p+j)){k=*(p+i);*(p+i)=*(p+j);*(p+j)=k;}

  207.   }
复制代码




转自:http://blog.sina.com.cn/s/blog_49d083e50100cojl.html

回复
分享到:

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-4-29 07:59 , Processed in 0.130974 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表