求大虾帮改一个小程序
求大虾帮帮小弟的忙把程序中的goto 改成标准的C语句。先谢谢了。#include<math.h>
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 2
#define K 4
int n;
/*目标函数*/
double Obj_Func_fx (double x[])
{
double fun_f;
fun_f = (x[0]-5)*(x[0]-5)+4*(x[1]-6)*(x[1]-6);
return fun_f;
}
/*限制条件*/
int constraint (double x[],double g[])/*判断产生的点是否满足约束条件若满足返回1否则返回0*/
{
g[0] = 64-(x[0] * x[0])-(x[1] * x[1]);
g[1]=x[1] - x[0] - 10;
g[2]=x[0] - 10;
if(g[0] <= 0 && g[1] <= 0 && g[2] <= 0)
return 1;
else
return 0;
}
void xcent (int n,int k,int ll,int lh,double x0[],double xcom[][100])
{
int i,l;
double xs;
for(i = 0;i < n;i++)
{
xs = 0;
for( l = 0;l < ll;l++)
{
if(l != lh)
xs = xs + xcom[i][l];
}
if(lh > -1)
x0[i] = xs / (ll-1);
else
x0[i] =xs / ll;
}
}
void fxse (int n,int k,double x[],double xcom[][100],double fxk[])
{
int l,lp,lp1,i;
double temp;
for(l = 0;l < k-1;l++)
for(lp = 0;lp < k-l;lp++)
{
lp1 = lp+1;
if(fxk[lp] <= fxk[lp1])
{
temp = fxk[lp];
fxk[lp] = fxk[lp1];
fxk[lp1] = temp;
for(i = 0;i < n;i++)
{
x[i] = xcom[i][lp];
xcom[i][lp] = xcom[i][lp1];
xcom[i][lp1] = x[i];
}
}
}
}
int comple(int n,int k,int kg,double ep,double x[],double bl[],double bu[], double xcom[][100],double *f)
{
int i,j,i_s,ll,lh,it;
double fx,fx0,sdx,fxh,fxr,alp;
double *x0 = (double*)calloc(n,sizeof(double));/*开辟一个double字节的空间赋给x0所指向的单位*/
double *xh = (double*)calloc(n,sizeof(double));
double *xr = (double*)calloc(n,sizeof(double));
double *fxk = (double*)calloc(k,sizeof(double));
double *g = (double*)calloc(kg,sizeof(double));
s5: for(i = 0;i < n;i++)
x[i] = bl[i] + rand() / 32767.0 * (bu[i] - bl[i]);/*产生一组随机数*/
i_s = constraint(x,g);
if(i_s==0)
goto s5;
for(i = 0;i < n;i++)
xcom[i][0] = x[i]; /*将第一个点存入数组*/
for(j = 1;j < k;j++)
for(i = 0;i < n;i++)
xcom[i][j] = bl[i] + rand() / 50000.0 * (bu[i]-bl[i]);/*产生其余的两个点*/
lh = -1;
for(ll = 1;ll < k;ll++)
{
xcent(n,k,ll,lh,x0,xcom);
i_s = constraint (x0,g);
if(i_s==0)
goto s5;
for(i = 0;i < n;i++)
x[i] = xcom[i][ll+1];
s24: i_s = constraint (x,g); /*判断x1,x2是否满足约束条件*/
if(i_s==0)/*如果不满足则重新产生点直到满足为止*/
{
for(i = 0;i < n;i++)
x[i] = x0[i] + 0.5 * (x[i]-x0[i]);
goto s24;
}
else
{
for(i = 0;i < n;i++)
xcom[i][ll+1] = x[i];
}
}
for(j = 0;j < k;j++)
{
for(i = 0;i < n;i++)
x[i] = xcom[i][j];
fx = Obj_Func_fx(x);
fxk[j] = fx;
}
it = 0;
s14: it = it+1;/*输出年代次数(每满10次输出一次),x1,x2及相应的目标函数值*/
if(it%10==0)
printf("\n\n迭代次数= %2d\n",it);
lh = -1;
xcent(n,k,k,lh,x0,xcom);
fx0 = Obj_Func_fx(x0);
i_s = constraint (x0,g);
for(i = 0;i < n;i++)
if(it % 10==0)
printf("X(%d) = %.5f ",i+1,x0[i]);
if(it%10==0)
printf( " 目标函数值 f(x) =%.5f",fx0);
sdx = 0;
for(j = 0;j < k;j++)
sdx = sdx + (fx0-fxk[j])*(fx0-fxk[j]);
sdx = sqrt(sdx/(double)(k-1));
if(sdx < ep) /*判断是否满足精度*/
goto s38;
fxse(n,k,x,xcom,fxk);
lh = 0;
s22: fxh = fxk[lh];
for(i = 0;i < n;i++)
xh[i] = xcom[i][lh];
xcent(n,k,k,lh,x0,xcom);
i_s = constraint(x0,g);
if(i_s==0)
goto s36;
alp = 1.3;
s12: for(i = 0;i < n;i++)
xr[i] = x0[i] + alp * (x0[i] - xh[i]);
i_s = constraint(xr,g);
if(i_s==0)
{
alp = alp * 0.5;
goto s12;
}
fxr = Obj_Func_fx(xr);
if(fxr >= fxh)
{
if(alp>1.0e-4)
{
alp=alp*0.5;
goto s12;
}
lh = lh+1;
if(lh < 3)
goto s22;
}
for(i = 0;i < n;i++)
xcom[i][lh] = xr[i];
fxk[lh] = fxr;
goto s14;
s36: for(i = 0;i < n;i++)
{
bl[i] = xcom[i][k];
bu[i] = x0[i];
}
goto s5;
s38: for(i = 0;i < n;i++)/*计算目标函数值*/
x[i] = x0[i];
*f = Obj_Func_fx(x);
return it;
free(x0);/*释放内存*/
free(xh);
free(xr);
free(g);
free(fxk);
}
/* n-优化模型维数 k-复合形顶点数 kg-约束函数个数 ep-收敛精度 bl-初始点下限 bu-初始点上限 *f进程中的优化结果 */
void main ()
{
int comple(int n,int k,int kg,double ep,double x[],double bl[],double bu[], double xcom[][100],double *f) ;
double a[]={0,0},b[]={10,20};/*初始点上限和下限*/
double f;
double x[2],xcom[2][100];
int n;/*总迭代次数*/
n = comple(N,K,3,0.00001,x,a,b,xcom,&f);
printf("\n\n\n 总迭代次数 输出最优解 目标函数值:\n");
printf("\n %d x*=[%.5f %.5f] f(x*)=%.5f\n\n ",n,x[0],x[1],f);
}
[ 本帖最后由 roseknife 于 2011-5-11 22:20 编辑 ]