设为首页 - 加入收藏 焦点技术网
热搜:java
当前位置:首页 >

单纯形法求最优解

2006-03-29 22:26:00.0 人工智能 matrix math.h input c im  
导读: scanf("%d",&a); printf("N="); scanf("%d",&b); printf("P="); scanf("%d",&c); M=a; N=b; P=c; printf("INPUT MATRIX A/n"); for(i=2;i<=M+1;i++){  for(j=1;j<=N+2;j++){   printf("A[%d][%d]=",i,j);      scan...。。。
 scanf("%d",&a);
 printf("N=");
 scanf("%d",&b);
 printf("P=");
 scanf("%d",&c);
 M=a;
 N=b;
 P=c;

 printf("INPUT MATRIX A/n");
 for(i=2;i<=M+1;i++){
  for(j=1;j<=N+2;j++){
   printf("A[%d][%d]=",i,j);
      scanf("%d",&d);
      A[i][j]=d;   
  }    
 }
 print(A);
   
    for(i=2;j<=M+1;i++){
  for(j=1;j<=N+2;j++){
   if(A[i][N+2]>0)
    A[i][j]=A[i][j];
   if(A[i][N+2]<0)
    A[i][j]=-A[i][j];
   else
    A[i][j]=0;
  }
 }
 printf("/nINPUT OBJECTIVE FUNCTION/n");
 for(i=1;i<=N;i++){
  printf("A[1][%d]=",i);
  scanf("%d",&e); 
  A[1][i]=e;
 }
 printf("/n/tOBJECTIVE FUNCTION/n");
    for(i=1;i<=N;i++){
  printf("/t%d/t",A[1][i]);
 }
 printf("/n=======================/n");

 E=0.0001;                    //Line54
 for(i=1;i<=N;i++){
  A[1][i]=-P*A[1][i];
 }
 
 if(M==1){                 //Line62
  int R=1;
  for(i=2;i<=M+1;i++){
   L[i]=N+i-1;
   if(A[i][N+i-1]==1){
    continue;   
   }
   L[i]=M+N+1;
   for(j=1;j<=M+N;j++){
    A[M+2][j]=A[M+2][j]-A[i][j];
   }
   R=M+2;
  }
Label: T=1;                        //98
  for(i=2;i<=M+N;i++){        //Line100
   if( (A[R][i]-A[R][T])>E )
    continue;
   if( (A[R][i]-A[R][T])<-E ){
    T=i;
    continue;
   }
   if(R==1){
    continue;
   }
   if( (A[1][i]-A[1][T])>-E )
    continue;
  }
  if( A[R][T]<-E ){          //Line114
   S=1;
   for(i=2;i<=M+1;i++){
    if( A[i][T]<=E )
     continue;
    Y=A[i][M+N+1]/A[i][T];
    if(S==1){
     S=i;
     continue;
    }
                if( Y>=A[S][M+N+1]/A[S][T] )
     continue;
   }
   if(S==1){                      //154
    printf("UNBOUNDED");
    printf("==========================");   //214
    exit(1);
   }
   L[S]=T;                      //156
   Y=A[S][T];
   for(i=1;i<=M+N+1;i++){
    A[S][i]=A[S][i]/Y;
   }
   for(i=1;i<=M+2;i++){
    if(i==S)                      //168
     continue;
    Y=A[i][T];
    for(j=1;j<=M+N+1;j++){
     A[i][j]=A[i][j]-Y*A[S][j];
    }
   }
   goto Label;                        //180
   printf("INFEASLBLE");
   printf("==========================");    //214
   exit(1);
  }
  if(R==1){                                  //116
   for(i=2;i<=M+1;i++){                  //190
    if(L[i]>N)
     continue;
    S=L[i];
    X[S]=A[i][M+N+1];
   }
   Y=P*A[i][M+N+1];
   printf("OBJ. FUNC. Z=%f/n",Y);
   printf("OPTIMAL SOLUTION X[i]:/n");
   for(i=1;i<=N;i++){
    printf("X[%d]=%d/t",X[i]);
   }
   printf("==========================");
   exit(1);
  }
  for(i=1;i<=M+N;i++){        //118
   if(A[R][i]>E){          //120
    for(i=2;i     if( L[i]<=(M+N) )
      continue;
     if( A[i][M+N+1]>E ){        //132
      printf("INFEASLBLE");              //182
      printf("==========================");    //214
      exit(1);
     }
    }
    R=1;          
    goto Label;      //126
   }
  }
  for(i=2;i<=M+1;i++){                  //190
   if(L[i]>N)
    continue;
   S=L[i];
   X[S]=A[i][M+N+1];
  }
  Y=P*A[i][M+N+1];
  printf("OBJ. FUNC. Z=%d/n",Y);
  printf("OPTIMAL SOLUTION X[i]:/n");
  for(i=1;i<=N;i++){
   printf("X[%d]=%d/t",X[i]);
  }
  printf("==========================");
  exit(1);
 }     //end of commence 62 

 for(i=2;i<=M+N+1;i++){      //64
  A[i][M+N+1]=A[i][N+2];
  A[i][N+1]=0;
  if(i==1)
   continue;
  A[i][N+i-1]=A[i][N+1];
  A[i][N+1]=0;
 } 
}
        
/*888888888888888888888888888888888888888888888888888*/
print(int A[][10]){                                       
 int i,j;
 printf("/n/tNOW the MATRIX A is:/n");
 for(i=2;i<=M+1;i++){
  printf("/n");
  for(j=1;j<=N+2;j++){
   printf("%6d/t",A[i][j]);
  }
 }
 printf("/n");
 getch();
}

(编辑: jiazhen)

网友评论
相关文章