-2
Im은 코스의 심플 렉스 메서드를 프로그래밍하고 배열 배열 (행렬) 또는 포인터 배열을 참조하는 데 문제가 있습니다 (뜻 모를 말). 필자가 주로하고 싶은 것은 표준 입력 파일이 읽을만한 것을 가지고있는 반면에, 나는 simplex 메서드를 해결합니다. I는 폼의 TXT 파일로부터 읽어C++ 배열의 배열 참조 (포인터에 대한 포인터)
MN
B1 A1 A2 ...
이니
BN A2 ... 함수 read_problem에서
이 텍스트 파일을 행렬 A로 읽습니다.이 함수는 텍스트 파일에서 읽을 내용이 더있는 한 true를 반환합니다. 행렬에 값을 읽을 수는 있지만 행렬 A로 돌아갈 때 행렬 A는 null입니다. 이 함수가 수행하지 않는 것은 (그리고 내가하고 싶은 일은) 내 메인에 정의 된 행렬을 참조하며 read_problem 내부의 값을 편집 할 수 있어야합니다. 어떻게해야합니까?
도움이 될 것입니다.
int solve_simplex_phase1(int n, int m, double **Aij,int * basis, int pivot_rule);
bool read_problem(int* n, int* m, double **A, int *basis);
int main(int argc, char *argv[]){
double **A; // global variable
int *basis; // global variable
int pivot_rule; //pivot rule
// Pivot Rule:
// 1 - smallest subscript.
// 2 - largest coefficient
// 3 - maximum increase
if(argc == 2){
if (argv[1][0] == '1'){
pivot_rule =1;
}else if (argv[1][0] == '2'){
pivot_rule = 2;
}else if (argv[1][0] == '3'){
pivot_rule = 3;
}else{
cout<< "Error - Incorrect Input Pivot Rule \n";
return 0;
}
}else{
cout<< "Error - No Input Pivot Rule \n";
return 0;
}
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n";
int m,n;
bool read_problem_tf;
do{
read_problem_tf=read_problem(&n , &m , A, basis);
if(A==NULL) { cout << "WHY!!!!!x\n" ;}
if(basis==NULL) { cout << "WHY22222!!!!!x\n" ; return 0;}
// call simplex function to solve this problem
solve_simplex_phase1(n , m , A, basis, pivot_rule);
}while(read_problem_tf);
// while(read_problem(&n , &m , A, basis)) {
// // cout << n << "\n";
// // cout << m << "\n";
// if(A==NULL) { cout << "WHY!!!!!x\n" ;}
// if(basis==NULL) { cout << "WHY22222!!!!!x\n" ; return 0;}
// // call simplex function to solve this problem
// solve_simplex_phase1(n , m , A, basis, pivot_rule);
// }
}
bool read_problem(int *nn, int *mm, double **A, int *basis) {
// PHASE 1 ARCHITECTURE
// 0 1 2 ............. n n+1 ......... n+m n+m+1
// ------------------------------------------------
// | 0 | objective function | 0 ........... 0 | 0 | 0
// ------------------------------------------------
// | | | | 0 | 1
// | | | | |
// | | | | : | :
// | b | A | I | : | :
// | | | | : | :
// | | | | : | :
// | | | | |
// | | | | 0 | m
// ------------------------------------------------
// | 0 | 0 .............. 0 | 0 ........... 0 | 0 | m+1
// ------------------------------------------------
//
bool result = true;
int n;
int m;
if(scanf("%d%d", nn, mm) != EOF){ // find size of problem
n=*nn;
m=*mm;
basis = new int [n+m+2]; //create basis vector 0 if not in basis, 1 if in basis, 2 for constant value.
// define matrix A
A = new double *[m+2]; // m+2 to take into account phase 2 problems
for (int i = 0; i <= m+2 ; i++) {
A[i] = new double [n+m+2];// n+m+2 to take into account phase 2 problems
}
// scan matrix for remaining entries
for (int row = 0 ; row <= m ; row++) { // ROWS
for (int col = 0 ; col <= n+m ; ++col) { // COLUMNS
if (row == 0) {
if (col > 0 && col <= n) {
scanf("%lf" , &A[row][col]);
// std:: cout << "row" << row << ", col" << col << ", input" << A[row][col] << '\n';
basis[col]=0; // define basis
} else {
A[row][col] =0.00000;
// std:: cout << "row" << row << ", col" << col << ", input" << A[row][col] << '\n';
basis[col]=1; // define basis
}
}else if (row > 0) {
if (col <= n) {
scanf("%lf" , &A[row][col]);
if(col !=0){
A[row][col]*=(-1);
}
// std:: cout << "row" << row << ", col" << col << ", input" << A[row][col] << '\n';
} else if (row == col - n) {
A[row][col]=1.00000;
// std:: cout << "row" << row << ", col" << col << ", input" << A[row][col] << '\n';
} else {
A[row][col]=0.00000;
// std:: cout << "row" << row << ", col" << col << ", input" << A[row][col] << '\n';
}
}
}
}
// sneak dimension into basis (unused 0,0 spot)
basis[0]=2;//m+n; // fix (0,0) of basis
}else{
result = false;
}
return result;
}
int solve_simplex_phase1(int n, int m, double **Aij, int * basis, int pivot_rule) {
// function called to solve the simplex method
cout << "//=========================================================//\n";
cout << "// LP Solver - Simplex Method //\n";
// Pivot Rule:
// 1 - smallest subscript.
// 2 - largest coefficient
// 3 - maximum increase
// Take the input and transform the problem into standard form
// - assumed to be in standard form
if(pivot_rule == 1){
cout << "// USING: Smallest Subscript //\n\n" ;
}else if(pivot_rule == 2){
cout << "// USING: Largest Coefficient //\n\n" ;
}else if(pivot_rule == 3){
cout << "// USING: Maximum Increase //\n\n" ;
}
// Define Initial Dictionary
cout << "\n Initial Dictionary: \n";
// Define Objective function location (row)
int objective_function_row = 0;
display_dictionary(0 , 0 , 0 , n+m , m , Aij , basis, objective_function_row);
// If the linear program does not have an initial feasible origin
// (that is , one of the bi's is strictly less than zero),
// terminate;
if (!check_positive_objective_function(n+m , m , Aij, objective_function_row)){
cout << "//=========================================================//\n\n";
return 0;
}
// check boundedness of LP problem
if (!check_boundedness(n+m , m , Aij , basis , objective_function_row)){
cout << "\n ERROR - LP is not bounded \n\n";
cout << "//=========================================================//\n";
return 0;
}
// check for phase 1 or phase 2 problem
if (!check_positive_bs(n+m , m , Aij)){
cout << "\n Initial Dictionary Infeasible - Auxiliary Problem \n";
solve_auxiliary(m+n+1, m+1, Aij, basis, pivot_rule);
// cout << "exit auxiliary?" << flush;
if(check_feasible_auxiliary(n+m+1,m+1,Aij,basis)){
cout << "\n Auxiliary problem feasible, continue to phase 2\n" <<flush;
} else if (!check_feasible_auxiliary(n+m+1,m+1,Aij,basis)){
if(check_degenerate_pivot(n+m+1 , m+1 , Aij, basis)){
take_degenerate_pivot(m+n+1 , m+1 , Aij , basis);
if(check_feasible_auxiliary(n+m+1 , m+1 , Aij, basis)){
cout << "\n Auxiliary problem feasible after degenerate pivot, continue to phase 2\n" <<flush;
}else{
cout << "\n ERROR in Auxiliary - Infeasible \n\n"<<flush;
cout << "//=========================================================//\n";
return 0;
}
} else {
cout << "\n ERROR in Auxiliary - Infeasible \n\n"<<flush;
cout << "//=========================================================//\n";
return 0;
}
}
}
int iteration_count=1;
do {
/* Iterate dictionaries
1) find in and out variables
2) pivot dictionary;
4) run checks;
5) go to 1) until z row has no positive c;
*/
// cout << "check z row: " << check_positive_objective_function(n+m , m , Aij)<<'\n';
// cout << "iteration" << iteration_count << '\n';
// Entering variable
int in_col=find_pivot_col(n+m , m , Aij , pivot_rule, basis, objective_function_row);
// cout << "why?"<<objective_function_row<<endl<<flush;
// leaving variable (this is the ROW of the leaving variable not the variable Label!)
int in_out_row=find_pivot_row(in_col , m , Aij);
// take the in_out_row and find the column of the output variable
int out_col=find_leaving_variable(in_out_row,n+m,Aij,basis, objective_function_row);
// perform pivot & updates basis
pivot(in_out_row , in_col , out_col , n+m , m , Aij, basis);
// display dictionary
display_dictionary(iteration_count , in_col , out_col , n+m , m , Aij , basis, objective_function_row);
// perform checks (b's are positive, bounding of columns)
// check for phase 1 or phase 2 problem
if (!check_positive_bs(n+m , m , Aij)){
cout << "\n ERROR - LP does not have a feasible origin. \n\n"<<flush;
cout << "//=========================================================//\n"<<flush;
return 0;
}
// check boundedness of LP problem
if (!check_boundedness(n+m , m , Aij , basis, objective_function_row)){
cout << "\n ERROR - LP is not bounded \n\n"<<flush;
cout << "//=========================================================//\n"<<flush;
return 0;
}
// check if you are in an infinite loop
if(iteration_count > 300){
cout << "\n ERROR - Infinite Loop? Try another pivot rule\n" <<flush;
cout << "//=========================================================//\n"<<flush;
return 0;
}
iteration_count++;
} while (check_positive_objective_function(n+m , m , Aij, objective_function_row));
cout << "\n";
cout << "//---------------------------------------------------------//\n\n";
cout << "Objective function has all negative terms:" << '\n';
// export solution
return_optimal_solution(n+m, m, Aij, basis);
cout << "//=========================================================//\n\n\n\n\n";
return 0;
}