hang problems

My program is now facing a strange problem. When I want to return a value from this function

maxInfoItemInIPSoln(estimate, item, strata[k], testLength, j, selected, loBound, upBound, count);

where the function is as follows

int maxInfoItemInIPSoln(double& t, vector<Item>& item, vector<int>& index,
int& length, int& q, vector<bool>& selected, vector<int>& loBound, vector<int>& upBound,
vector<int>& count){

TwoDVector<double> tempselect, eliset;
TwoDVector<int> prop;
vector<int> ctemp(loBound.size(), 0);
vector<double> c(index.size(), 0.);

int maxIndex, ind;
int adminNo,selectNo;
double maxInfo = -INF, info;

//1.Initialize lp_solve model
lprec *lp;

int Ncol, *colno, ret = 0;
REAL *row = NULL;
int r,j,elicount;

HINSTANCE lpsolve;
make_lp_func *_make_lp;
delete_lp_func *_delete_lp;

prop.resize(index.size()-q);
tempselect.resize(length-q);
eliset.resize(index.size()-q);
//1: initialize eligible set of items
elicount=0;
for(int i = 0; i < index.size(); i++){
if(!selected[index[i]])
{
c[index[i]] = fisherInformation(item[index[i]].a, item[index[i]].b,
item[index[i]].c, t);
eliset[elicount].push_back(item[index[i]].id);
eliset[elicount].push_back(c[index[i]]);
int m=0;
for (int k=0; k<upBound.size(); k++)
{
if (k==item[index[i]][m])
{
eliset[elicount].push_back(1);
m++;
}
else
eliset[elicount].push_back(0);
}
elicount++;
}
}

for(int j=0; j<loBound.size(); j++)
ctemp[j]=count[j];
for (int i=0; i<eliset.size(); i++)
{
for (int j=0; j<eliset[i].size(); j++)
cout << eliset[i][j] << "\t";
cout << endl;
}
//2: initialize model
lpsolve = LoadLibrary("lpsolve55.dll");

if (lpsolve == NULL) {
printf("Unable to load lpsolve shared library\n");
return(FALSE);
}
_make_lp = (make_lp_func *) GetProcAddress(lpsolve, "make_lp");
_delete_lp = (delete_lp_func *) GetProcAddress(lpsolve, "delete_lp");

//3:Set up the model
Ncol = index.size()-q; /* there are index.size()-q variables in the model */
lp = make_lp(0, Ncol);
if(lp == NULL)
ret = 1; /* couldn't construct a new model... */
if(ret == 0) {
for (int i=1; i<=Ncol; i++)
set_binary(lp,i,TRUE);
/* create space large enough for one row */
colno = (int *) malloc(Ncol * sizeof(*colno));
row = (REAL *) malloc(Ncol * sizeof(*row));
//cout << sizeof(*colno) << "\t" <<sizeof(*row) << endl;
if((colno == NULL) || (row == NULL))
ret = 2;
}

if(ret == 0) {

set_add_rowmode(lp, TRUE); /* makes building the model faster if it is done rows by row */
/* construct first row (sum(x)=testlength) */
j=1;
while (j<=Ncol)
{
colno[j] = j; /* first column */
row[j++]=1;
}
/* add the row to lpsolve */
if(!add_constraintex(lp, j, row, colno, EQ, length-q))
ret = 3;
}

if(ret == 0) {
/* set the objective function (sum(Ix)) */
j = 0;
while (j<=Ncol)
{
colno[j] = j; /* first column */
row[j] = eliset[j-1][1];
j++;
}

/* set the objective in lpsolve */
if(!add_constraintex(lp, j, row, colno, GE, 0.05))
ret = 3;
}

for (int i=0; i<upBound.size(); i++)
{
if(ret == 0) {

/* construct second row (sum(x)<=upBound) */
//cout << "Second constraint is setting up\n";
j=0;
while (j<=Ncol)
{
colno[j]=j;
row[j++]=eliset[j-1][i+2];
}
/* add the row to lpsolve */
if(!add_constraintex(lp, j, row, colno, LE, upBound[i]-count[i]))
ret = 3;
//cout << "Second constraint is set up\n";
}
}
if(ret == 0) {
set_add_rowmode(lp, FALSE); /* rowmode should be turned off again when done building the model */

/* set the objective function (sum(Ix)) */
j = 1;
while (j<=Ncol)
{
colno[j] = j; /* first column */
row[j] = eliset[j-1][1];
j++;
}

/* set the objective in lpsolve */
if(!set_obj_fnex(lp, j, row, colno))
ret = 4;
}

if(ret == 0) {
/* set the object direction to maximize */
set_maxim(lp);

/* just out of curioucity, now show the model in lp format on screen */
/* this only works if this is a console application. If not, use write_lp and a filename */
write_LP(lp, stdout);
/* write_lp(lp, "model.lp"); */

/* I only want to see important messages on screen while solving */
set_verbose(lp, IMPORTANT);

/* Now let lpsolve calculate a solution */
ret = solve(lp);
cout << "The solution is " << ret << endl;
if(ret == OPTIMAL)
ret = 0;
else
ret = 5;
}

if(ret == 0) {
/* a solution is calculated, now lets get some results */

/* objective value */
cout << "Objective value: " << get_objective(lp) << endl;
//printf("Objective value: %f\n", get_objective(lp));

/* variable values */
get_variables(lp, row);
int tempselcount=0;
for(j = 0; j <Ncol; j++)
{
cout << get_col_name(lp, j+1) << ": " << row[j] << endl;
//printf("%s: %f\n", get_col_name(lp, j + 1), row[j]);

/* we are done now */
if (row[j]==1)
{
tempselect[tempselcount].push_back(eliset[j][0]);
tempselect[tempselcount].push_back(eliset[j][1]);
tempselcount++;
}
}
}

// free allocated memory
/*if(row != NULL)
free(row);
if(colno != NULL)
free(colno);*/

if(lp != NULL) {
//clean up such that all used memory by lpsolve is freed
delete_lp(lp);
}
for(int i = 0; i < tempselect.size(); i++)
cout << tempselect[i][0] << "\t" << tempselect[i][1] << "\n";
for(int i = 0; i < tempselect.size(); i++)
{
if(tempselect[i][1] > maxInfo)
{
maxInfo = tempselect[i][1];
maxIndex = (int)tempselect[i][0];
}
}
for (int i=0; i<index.size();i++)
if (item[index[i]].id==maxIndex)
maxIndex=index[i];

return maxIndex; //index in item list
}

sometimes it cannot return maxIndex, and sometimes it can, but then when the program returns to this function, it also hangs in this part
if(ret == 0) {
cout << "Second constraint: information of test\n";
j = 0;
while (j<=Ncol)
{
colno[j] = j; /* first column */
row[j] = eliset[j-1][1];
j++;
}
if(!add_constraintex(lp, j, row, colno, GE, 0.05))
ret = 3;
}
I don't know what happens. So can anyone tell me? Thanks~
Is that really one function? You have to learn to split your code into smaller logical units. That would be easier to debug. Also, use [code] tags and proper indentation when you post code.
Topic archived. No new replies allowed.