偶贴一下自己的程序,网格法。
/*
Filename : 97A.c
Author dcyu
Date 2002.9.13
*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.141519265359
float f(float x1, float x2, float x3, float x4, float x5, float x6, float x7)
{
return ((
174.42*x1*pow(x3/(-x1 + x2),0.85)*
sqrt((1 - 2.62*pow(1 - 0.36*pow(x2/x4,0.56),1.5)*
pow(x4/x2,1.16))/(x6*x7)))/x5
);
}
float ff(float x1,float x2,float x3,float x4,float x5,float x6,float x7)
{
return((
174.42*x1*pow(x3/(-x1 + x2),0.85)*
sqrt((1 - 2.62*pow(1 - 0.36*pow(x2/x4,0.56),1.5)*
pow(x4/x2,1.16))/(x6*x7)))/x5
);
}
float g1(float x1, float x2, float x3, float x4, float x5, float x6, float x7)
{
return 148.257*x1*x3*sqrt((1 - 2.62*pow(1 - 0.36*pow(x2/x4,0.56),1.5)*
pow(x4/x2,1.16))/(x6*x7))
/pow((x2-x1),2)/pow(x3/(x2-x1),0.15)/x5 +
174.42/x5*pow(x3/(x2-x1),0.85)*sqrt((1 - 2.62*pow(1 - 0.36*pow(x2/x4,0.56),1.5)*
pow(x4/x2,1.16))/(x6*x7));
}
float g2(float x1, float x2, float x3, float x4, float x5, float x6, float x7)
{
return -148.257*x1*x3*sqrt((1 - 2.62*pow(1 - 0.36*pow(x2/x4,0.56),1.5)*
pow(x4/x2,1.16))/(x6*x7))
/pow((x2-x1),2)/pow(x3/(x2-x1),0.15)/x5 +
87.21*x1*pow(x3/(x2-x1),0.85)*
(0.792288*x4*pow( (1-0.36*pow(x4/x2,-0.56)) , 0.5)
/pow(x2,2)/pow(x4/x2,0.4) + 3.0392*x4*pow(x4/x2,0.16)*
pow( (1-0.36*pow(x4/x2,-0.56)) , 1.5)/pow(x2,2) )/x5/x6/x7/
sqrt((1 - 2.62*pow(1 - 0.36*pow(x2/x4,0.56),1.5)*
pow(x4/x2,1.16))/(x6*x7));
}
float g3(float x1, float x2, float x3, float x4, float x5, float x6, float x7)
{
return 148.257*x1*sqrt((1 - 2.62*pow(1 - 0.36*pow(x2/x4,0.56),1.5)*
pow(x4/x2,1.16))/(x6*x7))
/(x2-x1)/pow(x3/(x2-x1),0.15)/x5 ;
}
float g4(float x1, float x2, float x3, float x4, float x5, float x6, float x7)
{
return -87.21*x1*pow(x3/(x2-x1),0.85)*
(0.792288*pow( (1-0.36*pow(x4/x2,-0.56)) , 0.5)
/x2/pow(x4/x2,0.4) + 3.0392*pow(x4/x2,0.16)*
pow( (1-0.36*pow(x4/x2,-0.56)) , 1.5)/x2 )/(x5*x6*x7*
sqrt((1 - 2.62*pow(1 - 0.36*pow(x2/x4,0.56),1.5)*
pow(x4/x2,1.16))/(x6*x7)));
}
float g5(float x1, float x2, float x3, float x4, float x5, float x6, float x7)
{
return ((
-174.42*x1*pow(x3/(-x1 + x2),0.85)*
sqrt((1 - 2.62*pow(1 - 0.36*pow(x2/x4,0.56),1.5)*
pow(x4/x2,1.16))/(x6*x7)))/pow(x5,2)
);
}
float g6(float x1, float x2, float x3, float x4, float x5, float x6, float x7)
{
return -87.21*x1*pow(x3/(x2-x1),0.85)*
(1 - 2.62*pow(1 - 0.36*pow(x2/x4,0.56),1.5)*
pow(x4/x2,1.16))/x5/pow(x6,2)/x7/
sqrt((1 - 2.62*pow(1 - 0.36*pow(x2/x4,0.56),1.5)*
pow(x4/x2,1.16))/(x6*x7));
}
float g7(float x1, float x2, float x3, float x4, float x5, float x6, float x7)
{
return -87.21*x1*pow(x3/(x2-x1),0.85)*
(1 - 2.62*pow(1 - 0.36*pow(x2/x4,0.56),1.5)*
pow(x4/x2,1.16))/x5/x6/pow(x7,2)/
sqrt((1 - 2.62*pow(1 - 0.36*pow(x2/x4,0.56),1.5)*
pow(x4/x2,1.16))/(x6*x7));
}
float fai(float x,int l)
{
float rm,rn,rp,rt,pq,rrt,x1,x2,y,s,h;
float p1,p2,q1,q2;
if(x==0) return 0.5;
rp=1;
if(x*l<0) rp=-1;
x1=fabs(x);
x2=x1*x1;
y=1/sqrt(2*PI)*exp(-0.5*x2);
rn=y/x1;
if(rp>=0&&fabs(rn)<1e-12) return 1.0;
if(rp<=0&&rn==0) return 0.0;
rrt=3.5;
if(rp==-1) rrt=2.32;
if(x1<=rrt)
{
x1*=y;
s=x1;
rn=1.0;
rt=0;
do
{
rn+=2;
rt=s;
x1*=x2/rn;
s+=x1;
}
while(s!=rt);
pq=0.5+s;
if(rp==-1) pq=0.5-s;
return pq;
}
q1=x1;
p2=y*x1;
rn=1;
p1=y;
q2=x2+1;
if(rp!=-1)
{
rm=1-p1/q1;
s=rm;
rt=1-p2/q2;
}
else
{
rm=p1/q1;
s=rm;
rt=p2/q2;
}
while(1)
{
rn++;
if(rm==rt||s==rt) return rt;
s=x*p2+rn*p1;
p1=p2; p2=s;
s=x*q2+rn*q1;
q1=q2; q2=s;
s=rm;
rm=rt;
rt=1-p2/q2;
if(rp==-1) rt=p2/q2;
}
return rt;
}
char rongcha(int n)
{
switch(n)
{
case 0: return 'A';
case 1: return 'B';
case 2: return 'C';
default: exit(1);
}
}
void main()
{
FILE *fp;
int i[7],cha[7],j,k,m;
float s[3][7] = {{25, 50, 200, 500, 50, 100, 100}, {25, 50, 50, 100, 50, 25, 25}, {25, 20,
20, 50, 50, 10, 25}};
float a[7] = {0.075, 0.225, 0.075, 0.075, 1.125, 12, 0.5625};
float b[7] = {0.125, 0.375, 0.125, 0.125, 1.875, 20, 0.935};
float tc[7] = {0.005, 0.03, 0.01, 0.01, 0.15, 1.6, 0.0375};
float rong[3] = {0.01, 0.05, 0.1};
float c[7],x[7],tx[7],ct[7],tcha[7];
float y,mincost,w,t1,t2,cy,tcy,ty,cost,tst,lt,tlt,temp;
mincost=100000000;
m=5;
if((fp=fopen("output.txt","w"))==NULL) exit(1);
for(cha[0] = 1; cha[0] <= 1; cha[0]++)
for(cha[1] = 1; cha[1] <= 2; cha[1]++)
for(cha[2] = 0; cha[2] <= 2; cha[2]++)
for(cha[3] = 0; cha[3] <= 2; cha[3]++)
for(cha[4] = 2; cha[4] <= 2; cha[4]++)
for(cha[5] = 0; cha[5] <= 2; cha[5]++)
for(cha[6] = 0; cha[6] <= 1; cha[6]++)
for(i[0] = 0; i[0] <= m; i[0]++ )
for(i[1] = 0; i[1] <= m; i[1]++ )
for(i[2] = 0; i[2] <= m; i[2]++ )
for(i[3] = 0; i[3] <= m; i[3]++ )
for(i[4] = 0; i[4] <= m; i[4]++ )
for(i[5] = 0; i[5] <= m; i[5]++ )
for(i[6] = 0; i[6] <= m; i[6]++ )
{
cost=0.0;
for(j = 0; j < 7; j++)
{
x[j]=a[j]+(b[j]-a[j])*i[j]/m;
ct[j]=s[cha[j]][j];
cost=cost+ct[j];
}
c[0]=g1(x[0],x[1],x[2],x[3],x[4],x[5],x[6]);
c[1]=g2(x[0],x[1],x[2],x[3],x[4],x[5],x[6]);
c[2]=g3(x[0],x[1],x[2],x[3],x[4],x[5],x[6]);
c[3]=g4(x[0],x[1],x[2],x[3],x[4],x[5],x[6]);
c[4]=g5(x[0],x[1],x[2],x[3],x[4],x[5],x[6]);
c[5]=g6(x[0],x[1],x[2],x[3],x[4],x[5],x[6]);
c[6]=g7(x[0],x[1],x[2],x[3],x[4],x[5],x[6]);
cy=0.0;
for(j = 0; j < 7; j++)
{
temp=c[j]*x[j]*rong[cha[j]];
cy=cy+temp*temp;
}
cy=0.333333333*sqrt(cy);
y=f(x[0],x[1],x[2],x[3],x[4],x[5],x[6]);
t1 = fai((1.6-y)/cy,1)-fai((1.4-y)/cy,1);
t2 = fai((1.8-y)/cy,1)-fai((1.2-y)/cy,1);
lt = (9000-1000*t1-8000*t2);
w=1000*(cost+lt);
if(w<mincost)
{
mincost = w; tcy=cy; tlt=lt;
for(j=0;j<7;j++)
{
tcha[j]=cha[j];
tx[j]=x[j];
}
tcy = cy; tst = cost; ty = y;
printf("mincost: %f y=%f\n",mincost, y);
fprintf(fp,"mincost: %f y=%f\n",mincost, y);
}
}
printf("\n\nMin cost: %f , x:");
fprintf(fp,"\n\nMin cost: %f , x:");
for(j=0;j<7;j++)
{
printf("x[%d]=%f ",j,x[j]);
fprintf(fp,"x[%d]=%f ",j,x[j]);
}
printf("\nRongchadengji: ");
fprintf(fp,"\nRongchadengji: ");
for(j=0;j<7;j++)
{
printf("%c ",rongcha(cha[j]));
fprintf(fp,"%c ",rongcha(cha[j]));
}
printf("\ny = %f, cy = %f, cost = %f, lt = %f\n",ty, tcy, tst, tlt);
fprintf(fp,"\ny = %f, cy = %f, cost = %f, lt = %f\n",ty, tcy, tst, tlt);
getch();
getch();
} |