Im programming the simplex method for a course and i'm having problems with referencing an array of arrays (matrix) or perhaps its an array of pointers (not sure of the lingo). What I want to do in my main is while the standard input file has something to read, i solve the simplex method. I read from a txt file of the form:
我正在为一个课程编写单纯形法,我在引用一个数组(矩阵)或者它的一个指针数组(不确定术语)时遇到了问题。我想要做的主要是在标准输入文件有东西要读的时候,我解决了单纯形法。我从表格的txt文件中读取:
m n
b1 a1 a2 ... am
b1 a1 a2 ... am
:
bn an a2 ... an
bn a a2 ... an
In the function read_problem, I read this text file into a matrix A. This function returns true as long as there is something more to read in the text file. I can read the values into the matrix, but when I return to the main the matrix A is null. What this function is not doing (and what I want it to do) is reference the matrix defined in my main and be able to edit the values inside read_problem. HOW DO I DO THIS?
在函数read_problem中,我将此文本文件读入矩阵A中。只要在文本文件中有更多要读取的内容,此函数将返回true。我可以将值读入矩阵,但是当我返回main时,矩阵A为空。这个函数没有做什么(以及我想要它做什么)是引用我的main中定义的矩阵,并能够编辑read_problem中的值。我该怎么做呢?
Any help would be great!
任何帮助都会很棒!
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;
}
1 个解决方案
#1
0
Change read_problem()
signature to
将read_problem()签名更改为
bool read_problem(int* n, int* m, double **&A, int *basis);
^^^^
#1
0
Change read_problem()
signature to
将read_problem()签名更改为
bool read_problem(int* n, int* m, double **&A, int *basis);
^^^^