Originally posted by: QiuFeng
here is the function in my code printing tableau, hope it help.
int OptModel::printTableau(string tableauFileName)
{
int num_rows = CPXgetnumrows(cpxenv, cpxlp);
int num_cols = CPXgetnumcols(cpxenv, cpxlp) ; // need to know why
int surplus;
vector<string> varNames;
int status = CPXgetcolname (cpxenv, cpxlp, NULL, NULL, 0, &surplus, 0,num_cols-1);
int cur_colnamespace = - surplus;
if ( cur_colnamespace > 0 ) {
char **cur_colname = (char **) malloc (sizeof(char *)*(num_cols));
char *cur_colnamestore = (char *) malloc (cur_colnamespace);
if ( cur_colname == NULL ||
cur_colnamestore == NULL ) {
fprintf (stderr, "Failed to get memory for column names.\n");
status = -1;
cout<< endl <<" error in printTableau"<<endl;
return 0;
}
status = CPXgetcolname (cpxenv, cpxlp, cur_colname, cur_colnamestore,
cur_colnamespace, &surplus, 0, num_cols-1);
// map name with column index
for (int i = 0; i <num_cols ; i ++)
{
string varName = cur_colname[i];
varNames.push_back(varName);
}
free_and_null(cur_colname);
// free_and_null((char **)&cur_colnamestore);
}
/***************get head ****************************/
int* head = new int[num_rows]; // head is the 0-th column of the tableau
double *head_val = new double[num_rows];
status = CPXgetbhead (cpxenv, cpxlp, head, head_val);
ofstream log(tableauFileName.c_str() ,ios_base::app);
streambuf * old_buf = std::cout.rdbuf(log.rdbuf());
double *objval_p = new double;
status = CPXgetobjval(cpxenv, cpxlp, objval_p);
cout<<"objective value, " << *objval_p<<endl;
double* tabealu;
tabealu = (double*)malloc(num_rows*num_cols*sizeof(double));
for (int i = 0; i < num_rows; i++)
{
CPXbinvarow(cpxenv, cpxlp, i, tabealu+i*num_cols);
}
cout << endl;
cout << "Tableau, x, " ;
for ( int j = 0; j < num_cols ; j ++)
{
cout << varNames[j] << ", ";
}
cout << endl;
for (int i = 0; i < num_rows; i++)
{
if (head[i] < 0)
{
cout << head[i] <<", " << head_val[i]<< ", ";
}
else{
cout << varNames[head[i]] <<", " << head_val[i]<< ", ";
}
for (int j = 0; j < num_cols ; j ++)
{
cout << tabealu[i*num_cols +j] << ", " ;
}
cout<<endl;
}
cout << endl;
cout.rdbuf( old_buf);
delete []head;
delete []head_val;
delete objval_p;
free_and_null((char**) &tabealu);
return 1;
}
#CPLEXOptimizers#DecisionOptimization