声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1855|回复: 3

[C/C++] 求约束最优化算法

[复制链接]
发表于 2007-3-27 09:47 | 显示全部楼层 |阅读模式

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

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

x
哪位能给我一个求解约束最优化问题的算法,如梯度投影法,乘子法等的程序,我已经有罚函数法了,需要另外一种算法进行比较,谢谢了。也可发到我的邮箱dyjun888666@163.com
回复
分享到:

使用道具 举报

发表于 2007-3-27 10:24 | 显示全部楼层
乘子法,网上收集到的一个c程序

  1. #include  <stdio.h>
  2. #include  <math.h>
  3. #define  L  10

  4. void FX(int m,int n,float x[L][L],float z,float s[L][L],float x0[L][L])
  5. {int i,j;
  6. for(i=0;i<m;i++)        {for(j=0;j<n;j++)
  7.                 { x0[i][j]=x[i][j]+z*s[i][j];}}}       
  8. void grad(int m,float input[L],float x[L][L],float g0[L][L])
  9. {int i,j;
  10.   for(i=0;i<m;i++)
  11.   { for(j=0;j<m;j++)        { if(j!=i)
  12.                     g0[i][0]=2*input[i]*x[i][0]+input[2]*x[j][0]+input[i+3];}}}
  13. void qiudao(float input[L],float s[L][L],float x[L][L],float a,float b,float *z)
  14. {a=input[0]*s[0][0]*s[0][0]+input[1]*s[1][0]*s[1][0]+input[2]*s[0][0]*s[1][0];
  15. b=2*input[0]*x[0][0]*s[0][0]+2*input[1]*x[1][0]*s[1][0]+input[2]*(x[0][0]*s[1][0]
  16.          +x[1][0]*s[0][0])+input[3]*s[0][0]+input[4]*s[1][0];
  17.         a=2*a;*z=-1*b/a;}
  18. void gongetidu(float input[L],float x[L][L])
  19. {int i,j,k=0,stop=0;float s[L][L]={0};float g0[L][L]={0},g1[L][L]={0},a=0,b=0,z=0,w=0;
  20.     while(stop==0)
  21.         {        grad(2,input,x,g0);
  22.                 if((g0[0][0]*g0[0][0]+g0[1][0]*g0[1][0])<0.000001)
  23.                 {stop=1;break;}
  24.                 for(i=0;i<2;i++)
  25.                 {for(j=0;j<1;j++) {s[i][j]=-1*g0[i][j];}}
  26.                 qiudao(input,s,x,a,b,&z);
  27.            FX(2,1,x,z,s,x);
  28.        grad(2,input,x,g1);
  29.           w=(g1[0][0]*g1[0][0]+g1[1][0]*g1[1][0])/(g0[0][0]*g0[0][0]+g0[1][0]*g0[1][0]);
  30.          for(i=0;i<2;i++)
  31.                  {for(j=0;j<1;j++){s[i][j]=-1*g1[i][j]+w*s[i][j];}}
  32.                 qiudao(input,s,x,a,b,&z);
  33.                 FX(2,1,x,z,s,x);
  34.                 k++;
  35.         }
  36. }
  37. void main()
  38. {float f[L]={0},x[L][L]={0};
  39. float r=0.25,a=5,u=0,c=2,e=0.000001,h,h1,h2;
  40. int i,j,k=0,stop=1;
  41. while(stop==1)
  42. { f[0]=0.5+c/2;f[1]=0.166667+c/2;f[2]=c;f[3]=u-c;
  43.    f[4]=u-c;f[5]=(u*u+u*c)/(2*c);h1=x[0][0]+x[1][0]-1;
  44.    gongetidu(f,x);
  45.    if(fabs(x[0][0]+x[1][0]-1)<e)stop=0;          
  46.    h2=x[0][0]+x[1][0]-1;
  47.    h=h1/h2;
  48.    if(h>r) c=a*c;
  49.    u=u+c*h2;
  50.    k++;}
  51. k--;
  52. printf("k=%d\n",k);
  53. printf("x*=");
  54. {for(i=0;i<2;i++)
  55.     {for(j=0;j<1;j++)
  56.         printf("%f  ",x[i][j]);}
  57. printf(" \n");}

  58. }
复制代码

评分

1

查看全部评分

发表于 2007-5-16 21:38 | 显示全部楼层

求助!

楼主能不能把求解约束最优化问题的算法罚函数法程序发我邮箱啊,谢谢!
急用!我的邮箱是:donglei06@163.com
能运行出结果的,小弟将不胜感激!!
发表于 2007-5-17 15:13 | 显示全部楼层
原帖由 donglei06 于 2007-5-16 21:38 发表
楼主能不能把求解约束最优化问题的算法罚函数法程序发我邮箱啊,谢谢!
急用!我的邮箱是:donglei06@163.com
能运行出结果的,小弟将不胜感激!!


搜索本版
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

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

GMT+8, 2024-5-10 18:55 , Processed in 0.057545 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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