一些C程序运筹学.docx
《一些C程序运筹学.docx》由会员分享,可在线阅读,更多相关《一些C程序运筹学.docx(6页珍藏版)》请在冰豆网上搜索。
![一些C程序运筹学.docx](https://file1.bdocx.com/fileroot1/2023-1/24/4e3d31fb-96ab-49e0-8b19-93c3c3999b81/4e3d31fb-96ab-49e0-8b19-93c3c3999b811.gif)
一些C程序运筹学
一些C程序(运筹学)
[作者:
佚名 | 来源:
本站原创 | 阅读:
453 | 更新:
2006-1-5 | 编辑:
]
2、运筹学
(1)BRANCH:
分枝定界算法#include <>
#define len sizeof(struct node)typedef struct node{ float bound; int staus[50]; struct node *next; }node;int item[50],wl,n,state[50];float value[50],weight[50],max_value,ratio[50];
void dele(node *father,node *current){ if(current->next==NULL) {father->next=NULL; return; } father->next=current->next;}
void init(node *father,node *son){ int i; father->next=son; for(i=0;istaus[i]=0; son->next=NULL;}
void branch(){ int i,t,j; float diff,sum=0,sum_value=0; node *head,*sonbrother,*father,*son,*prenode,*p,*q; head=prenode=(node *)malloc(len); father=(node *)malloc(len); init(prenode,father); father->bound=32768; while(head->next!
=NULL) { /*1*/ son=(node *)malloc(len); init(father,son); for(i=0;istaus[i]!
=0;i++) son->staus[i]=father->staus[i]; t=i; son->staus[t]=-(t+1); sum=0; sum_value=0; for(j=0;jstaus[j]!
=0;j++) if(son->staus[j]>0) {sum=sum+weight[item[j]]; sum_value=sum_value+value[item[j]]; } while(sum!
=wl&&son->staus[n-1]==0) {diff=wl-(sum+weight[item[j]]); if(diff>=0) {sum=sum+weight[item[j]]; sum_value=sum_value+value[item[j]]; } else {sum=wl; sum_value=sum_value+(1+diff/weight[item[j]])*value[item[j]]; } j++; } son->bound=sum_value; /*2*/ sonbrother=(node *)malloc(len); init(son,sonbrother); for(i=0;istaus[i]=father->staus[i]; sonbrother->staus[t]=t+1; sum=0; sum_value=0; for(j=0;jstaus[j]!
=0;j++) if(sonbrother->staus[j]>0) {sum=sum+weight[item[j]]; sum_value=sum_value+value[item[j]]; } if(sum>wl) {sonbrother->bound=-32768; dele(son,sonbrother); } else {while(sum!
=wl&&sonbrother->staus[n-1]==0) {diff=wl-(sum+weight[item[j]]); if(diff>=0) {sum=sum+weight[item[j]]; sum_value=sum_value+value[item[j]]; } else {sum=wl; sum_value=sum_value+(1+diff/weight[item[j]])*value[item[j]]; } j++; } sonbrother->bound=sum_value; } dele(prenode,father); father=prenode->next; if(son->staus[n-1]!
=0) {if(son->next!
=NULL) {max_value=sonbrother->bound; for(i=0;istaus[i]; dele(son,sonbrother); dele(prenode,father); father=prenode->next; } else {max_value=son->bound; for(i=0;istaus[i]; dele(prenode,father); } q=head; p=head->next; while((p!
=NULL)&&(p->bound<=max_value)) {dele(q,p); p=q->next; } if(p!
=NULL) {prenode=q; father=p; } else return; } else if(father->next!
=NULL) {prenode=prenode->next; father=father->next; } } return;}
int getmin(){ int i; float amin=weight[0]; for(i=1;iweight[i]) amin=weight[i]; return amin;}
void sort(){ int i,j,exchange=1; float temp1,temp2; for(i=0;i=0&&exchange==1;j--) {exchange=0; for(i=0;iratio[i]) {exchange=1; temp1=ratio[i+1];ratio[i+1]=ratio[i];ratio[i]=temp1; temp2=item[i+1];item[i+1]=item[i];item[i]=temp2; } }
}
void main(){ int i,j; float sum=0; clrscr(); printf("Welcome to the BRANCH_BOUND system!
"); printf("number of the materials=?
"); scanf("%d",&n); printf("maximun weigh of the problem=?
"); scanf("%d",&wl); for(i=0;i",i+1); printf("*******************"); printf("weight %d=?
",i+1); scanf("%f",&weight[i]); printf("value %d=?
",i+1); scanf("%f",&value[i]); } if((getmin())>wl) {printf("There is no solution of the problem!
"); exit(0); } for(i=0;i"); exit(0); } sort(); branch(); printf("The maximum value of the materials is %f ",max_value); printf("including the following materials"); sum=0; for(i=0;i0) {sum=sum+weight[item[i]]; printf("%d",item[i]+1); } printf("The weight of the materials is %f ",sum); getch();}
(2)CHAIN:
马尔可夫链算法#include <>#include <>
double a[10][10];
void Guass(int n){ int i,j,k; double t; for(k=0;k=k) {t=a[i][k]/a[k][k]; for(j=k;jvoid chain(){ static double p[10][10],pr[10],diff,table[100][10],pnew[10][10],ptemp[10][10],temp[10],exr[10][10]; int n,i,j,k,s,m,found,inr,inc; printf("Welcome to the MARKOV CHAIN ANALYSIS system!
"); printf("how many states =?
"); scanf("%d",&n); printf("the steady transmit possibility of step 1 ?
"); for(i=0;i"); for(i=0;i {found=0; break; } found=1; } if(diff> break; } if(diff> break; } if(found==0) {if(k%5==0) {printf(" step %d",k); for(i=0;i=100) {printf("steady_state probability have not been detained in 100"); return; } } else {printf("step %d",k); for(i=0;i for(s=0;s=s) exr[j][s]=a[i++][n-1]; else exr[j][s]=1/pnew[n-1][s]; } printf("Table of expected first passage times and recurrence times"); for(i=0;ivoid main(){ clrscr(); chain(); getch();}
(3)DECISION:
贝叶斯决策方法#include <>#include <>
#define pi #define p(x,t) exp(-(x-t)*(x-t)/20)/sqrt(20*pi)
void decision(){ int i,j,type,m,n,flag,state[5],index; float xx,a[5][5],p[5],e[5],sum,decision; printf("Welcome to the DECISION_STSTEM!
"); printf("type of the problem,max(key ?
0?
)or min(key ?
1?
)?
"); scanf("%d",&type); printf("type of the decision,without data(key?
0?
)or with data(key?
1?
)?
"); scanf("%d",&flag); printf("number of the actions "); scanf("%d",&m); printf("number of the nature states "); scanf("%d",&n); for(j=0;j"); for(j=0;j "); scanf("%f",&xx); printf("states of nature=?
"); for(j=0;je[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;ivoid main(){ clrscr(); decision(); getch();}
(4)dp_invest:
动态规划的投资问题#include <>
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",i+1); for(j=0;j=0;i--) for(j=1;jf[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;ivoid 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 (5)dp_plan:
生产计划算法#include <>
#define pc(j) 20+5*j#define e(j) j
void dp_plan(){ int i,j,k,sum,limit,n,io,max_st