//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 */
/* we are done now */
if (row[j]==1)
{
tempselect[tempselcount].push_back(eliset[j][0]);
tempselect[tempselcount].push_back(eliset[j][1]);
tempselcount++;
}
}
}
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.