?? yunchou.txt
字號(hào):
scanf("%d",&flag);
printf("number of the actions ");
scanf("%d",&m);
printf("number of the nature states ");
scanf("%d",&n);
for(j=0;j<n;j++)
printf(" t%d",j+1);
printf(" ");
for(i=0;i<m;i++)
{printf("A%d ",i+1);
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
}
printf("probability of the natural states=? ");
for(j=0;j<n;j++)
scanf("%f",&p[j]);
if(flag)
{printf("given data=? ");
scanf("%f",&xx);
printf("states of nature=? ");
for(j=0;j<n;j++)
scanf("%d",&state[j]);
sum=0;
for(j=0;j<n;j++)
sum+=p[j]*p(xx,state[j]);
for(j=0;j<n;j++)
p[j]=p[j]*p(xx,state[j])/sum;
}
for(i=0;i<m;i++)
{sum=0;
for(j=0;j<n;j++)
sum+=p[j]*a[i][j];
e[i]=sum;
}
decision=e[0];
index=1;
if(type==0)
for(i=1;i<m;i++)
{ if(decision<e[i])
{decision=e[i];
index=i+1;
}
}
else
for(i=0;i<m;i++)
if(decision>e[i])
{decision=e[i];
index=i+1;
}
printf(" ********** ");
printf("Results:");
printf(" ********** ");
printf("expected loss for each course of action based on prior distribution ");
for(i=0;i<m;i++)
printf("%f ",e[i]);
printf(" The optimum expected loss is %f ",decision);
printf(" choose action A( %d )",index);
return;
}
void main(){
clrscr();
decision();
getch();
}
(4)dp_invest:動(dòng)態(tài)規(guī)劃的投資問(wèn)題
#include <stdio.h>
int istar[10];
float dp_invest(int N,int K){
int i,j,sum,z,d[10][50];
float g[10][50],f[10][50];
printf(" The return function values as follows! ");
for(j=0;j<K+1;j++)
printf(" %d",j);
for(i=0;i<N;i++)
{printf(" task%d: ",i+1);
for(j=0;j<K+1;j++)
scanf("%f",&g[i][j]);
}
for(i=0;i<N;i++)
for(j=0;j<K+1;j++)
{f[i][j]=0;d[i][j]=0;}
for(j=0;j<K+1;j++)
{f[N-1][j]=g[N-1][j];
d[N-1][j]=j;
}
for(i=N-2;i>=0;i--)
for(j=1;j<K+1;j++)
{f[i][j]=g[i][0]+f[i+1][j];
d[i][j]=0;
for(z=1;z<=j;z++)
if((g[i][z]+f[i+1][j-z])>f[i][j])
{f[i][j]=g[i][z]+f[i+1][j-z];
d[i][j]=z;
}
}
istar[0]=d[0][K];
for(i=1;i<N;i++)
{sum=0;
for(j=0;j<=i-1;j++)
sum=sum+istar[j];
istar[i]=d[i][K-sum];
}
return f[0][K];
}
void main(){
int i,N,K;
clrscr();
printf("WELCOME TO THE DYNAMIC_INVEST SYSTEM! ");
printf("How many tasks ? ");
scanf("%d",&N);
printf(" How many units of materials ? ");
scanf("%d",&K);
printf(" The optimal return is %f",dp_invest(N,K));
for(i=0;i<N;i++)
printf(" The optimal amout to invest in task %d is %d",i+1,istar[i]);
getch();
}
(5)dp_plan:生產(chǎn)計(jì)劃算法
#include <stdio.h>
#define pc(j) 20+5*j
#define e(j) j
void dp_plan(){
int i,j,k,sum,limit,n,io,max_stock,max_production,d[10],x[10][100],z,z_maxlimit,z_minlimit,xstar[10];
float f[10][100],temp;
printf("WELCOME TO THE DYNIMIC SYSTEM! periods of the inventory =? ");
scanf("%d",&n);
printf(" the maximum stocks of each period=? ");
scanf("%d",&max_stock);
printf(" the maximum production of each period=? ");
scanf("%d",&max_production);
printf(" The stocks of the first period=? ");
scanf("%d",&io);
for(i=0;i<n;i++)
{printf("Demand for period %d is ",i+1);
scanf("%d",&d[i]);
}
for(k=0;k<d[n-1];k++)
{x[n-1][k]=d[n-1]-k;
f[n-1][k]=pc(x[n-1][k]);
}
f[n-1][d[n-1]]=0;
x[n-1][d[n-1]]=0;
for(i=n-2;i>=0;i--)
{sum=0;
for(j=i;j<n;j++)
sum+=d[j];
if(sum<max_stock)
limit=sum;
else
limit=max_stock;
for(k=0;k<=limit;k++)
{if(d[i]-k>0)
{z_minlimit=d[i]-k;
f[i][k]=pc(z_minlimit)+e(0)+f[i+1][0];
x[i][k]=z_minlimit;
}
else
{z_minlimit=0;
f[i][k]=e(k-d[i])+f[i+1][k-d[i]];
x[i][k]=0;
}
if(sum-k>max_stock+d[i]-k)
if(max_stock+d[i]-k>max_production)
z_maxlimit=max_production;
else
z_maxlimit=max_stock+d[i]-k;
else
if(sum-k>max_production)
z_maxlimit=max_production;
else
z_maxlimit=sum-k;
for(z=z_minlimit;z<=z_maxlimit;z++)
{temp=pc(z)+e(k+z-d[i])+f[i+1][k+z-d[i]];
if(f[i][k]>temp)
{f[i][k]=temp;
x[i][k]=z;
}
}
}
}
/* for(i=0;i<n;i++)
{printf(" the period %d ",i+1);
for(j=0;j<=5;j++)
printf(" %f--->%d ",f[i][j],x[i][j]);
getch();
}*/
printf(" The minimum policy cost for the %d periods is %f",n,f[0][io]);
xstar[0]=x[0][io];
j=io;
printf(" The optimal amount to produce in period 1 is %d",xstar[0]);
for(i=1;i<n;i++)
{xstar[i]=x[i][xstar[i-1]-d[i-1]+j];
printf(" The optimal amount to produce in period %d is %d",i+1,xstar[i]);
j=xstar[i-1]-d[i-1]+j;
}
}
void main(){
clrscr();
dp_plan();
getch();
}
(6)linear:線性規(guī)劃單純形算法
#include <stdio.h>
int K,M,N,Q=100,Type,Get,Let,Et,Code[50],XB[50],IA,IAA[50],Indexg,Indexl,Indexe;
float Sum,A[50][50],B[50],C[50];
void initiate();
void solve();
void main(){
int i,j;
clrscr();
/****** input data ******/
printf(" Welcome to the linear programming solution! ******** Notice 1! ******** If the type of objective equation is ?max? ,please enter ?1?! Else please enter ?0?! ");
printf("******** Notice 2! ******** Define the type of subjective equation as following: ?<=?is equal to ?0?! ?>=? is equal to ?1?! ?=?is equal to ?2?! ");
printf(" Please input the coefficients or the constants: ");
printf("THe number of subjective equations ? ");
scanf("%d",&M);
printf("THe number of variables ? ");
scanf("%d",&K);
printf("THe number of ? <= ?subjective equations ? ");
scanf("%d",&Let);
printf("THe number of ? >= ?subjective equations ? ");
scanf("%d",&Get);
printf("THe number of ? = ?subjective equations ? ");
scanf("%d",&Et);
printf("THe type of objective equation ? ");
scanf("%d",&Type);
N=K+Let+Et+2*Get;
for(i=0;i<M;i++)
{printf("Please input %d?s equation: ",i+1);
printf("type ? ");
scanf("%d",&Code[i]);
printf("constant ? ");
scanf("%f",&B[i]);
for(j=0;j<K;j++)
{printf("coefficient ? ");
scanf("%f",&A[i][j]);
}
}
printf("Plese input the constants of the object equation: ");
for(j=0;j<K;j++)
scanf("%f",&C[j]);
printf(" ************************************* Please check the data you just input! ************************************* ");
getch();
if(Type)
printf("The type of the object equation is ?max? ");
else
printf("The type of the object equation is ?min? ");
printf(" The object equation is : ");
for(j=0;j<K;j++)
{printf("(%f)X%d ",C[j],j+1);
if(j!=K-1)
printf("+");
}
printf(" The suject equation is :");
for(i=0;i<M;i++)
{printf(" Number %d suject equation is: %d %f ",i+1,Code[i],B[i]);
for(j=0;j<K;j++)
{printf("(%f)X%d ",A[i][j],j+1);
if(j!=K-1)
printf("+");
}
}
/****** initiate data ******/
initiate();
solve();
if(!Type)
A[M][N]=-A[M][N];
printf(" The optimal value of the original objective function is: %f ",A[M][N]);
getch();
}
/****** initiate variables function ******/
void initiate(){
int i,j;
Indexg=K;
Indexl=Indexg+Get;
Indexe=Indexl+Let;;
for(i=0;i<M+1;i++)
for(j=K;j<N+1;j++)
A[i][j]=0;
for(i=0;i<M;i++)
A[i][N]=B[i];
for(i=0;i<M;i++)
switch(Code[i])
{ case 0: {XB[i]=Indexl;
A[i][Indexl++]=1;
break;
};
case 1: {XB[i]=Indexe;
IAA[IA++]=i;
A[i][Indexe++]=1;
A[i][Indexg++]=-1;
break;
};
case 2: {XB[i]=Indexe;
IAA[IA++]=i;
A[i][Indexe++]=1;
break;
};
}
for(j=0;j<K;j++)
if(Type)
A[M][j]=-C[j];
else
A[M][j]=C[j];
for(j=K;j<=N;j++)
A[M][j]=0;
for(j=K+Get+Let;j<N;j++)
A[M][j]=Q;
Sum=0;
for(j=0;j<=N;j++)
{Sum=0;
for(i=0;i<IA;i++)
Sum=Sum+A[IAA[i]][j];
A[M][j]=A[M][j]-Sum*Q;
}
return;
}
/****** process data function ******/
void solve(){
int i,j,mark=1,minus,minusmark,basic=0,divide,dividemark;
float H,P;
while(1)
{mark=0;
minusmark=0;
minus=0;divide=1000;
dividemark=0;
printf(" Basic solution %d is ",++basic);
for(i=0;i<M;i++)
printf("Basic variable %d = X( %d )= %f ",i+1,XB[i]+1,A[i][N]);
printf(" Current value of the object equation is: %f ",A[M][N]);
getch();
for(j=0;j<N;j++)
{
if(A[M][j]<-6e-8)
mark++;
if(A[M][j]<minus)
{minus=A[M][j];
minusmark=j;
}
}
if(mark==0)
break;
for(i=0;i<M;i++)
{if(A[i][minusmark]==0)
continue;
if(A[i][N]/A[i][minusmark]<=0)
continue;
if(A[i][N]/A[i][minusmark]<divide)
{divide=A[i][N]/A[i][minusmark];
dividemark=i;
}
}
XB[dividemark]=minusmark;
if(divide<0)
printf("There is no solution because of no boundary!");
P=A[dividemark][minusmark];
for(j=0;j<N+1;j++)
A[dividemark][j]=A[dividemark][j]/P;
for(i=0;i<M+1;i++)
{H=A[i][minusmark];
if(i==dividemark)
continue;
for(j=0;j<N+1;j++)
A[i][j]=A[i][j]-H*A[dividemark][j];
}
}
printf(" ************************************* The last basic solution is optimal! ************************************* ");
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -