Today, I encounter a strange problem. My c++ code can work under debug mode.
using g++ -g to compile the code. However, when I use g++ -O to optimize the code. It will stuck somewhere. It seems there is dead looping. Does anybody know how to find this kind of error? When I debug the code with DDD debuger, it works fine. Thanks!
The part of code (I find the stuck came from this part) is pasted below:
void Solver::reduceRoutes()
{
int V=pinst->get_V(); //get the given number of vehicles
if(int(curSol_.size())<=V) // return when solution has no extra routes
return;
int routeNum1,routeNum2; // the two routes modified
listSize=int(0.2*pinst->get_N());
short TheNode,anode; // the second node
float totalInc; //the obj increase of a candidate position
Route::iterator it;
vector<short> candidateList;
vector<short> validCandidateList; //nodes can be moved to
vector<float> totalImpList;
int solSize=int(curSol_.size());
while(solSize>V)
{
// cout <<"debug6.0 ";
routeNum1=psol->findRouteWithMinC(curSol_);
cout <<" debug6.1 "<<curSol_.size()<<" "<<routeNum1;
while(curSol_[routeNum1].size()>2)
{
it=curSol_[routeNum1].begin();
it++;
TheNode=*it;
candidateList=pinst->get_PNL(TheNode,listSize);
// evaluate the effect of moving the node to each possible position
for(unsigned int i=1;i<candidateList.size();i++) //the first node is itself
{
anode=candidateList[i];
routeNum2=RouteNumList[anode]; //find the route of second node
if(routeNum2!=routeNum1) //inter route move
{
totalInc=evaluateAreduceRouteMove(curSol_,routeNum1,routeNum2,TheNode,anode);
totalImpList.push_back(totalInc);
validCandidateList.push_back(anode);
}
}
//find the best position to insert the
int ii=(min_element(totalImpList.begin(),totalImpList.end())-totalImpList.begin());
anode=validCandidateList[ii];
it=find(curSol_[routeNum1].begin(),curSol_[routeNum1].end(),TheNode);
curSol_[routeNum1].erase(it); //remove from route1
routeNum2=RouteNumList[anode];
it=find(curSol_[routeNum2].begin(),curSol_[routeNum2].end(),anode);
++it;
curSol_[routeNum2].insert(it,TheNode); //insert to the second route
RouteNumList[TheNode]=routeNum2; //update route number
//improve the modified routes
psol->doTwoOpt(curSol_[routeNum2]);
totalImpList.clear();
validCandidateList.clear();
}
//update route number list
for(unsigned int i=routeNum1+1;i<curSol_.size();i++)
{
for(it=curSol_[i].begin();it!=curSol_[i].end();it++)
RouteNumList[*it]-=1;
}
RouteNumList[0]=0;
// eliminate the empty route
curSol_.erase(curSol_.begin()+routeNum1);
solSize=curSol_.size();
cout <<" debug6.3 "<<solSize<< " \n";
}
return;
}
Thanks you for answering my questions. Now my code works without optimizing. I just wonder what the optimizer does thus the code does work. If the optimize mode does not improve the speed much, then it is ok not using it. Can anyone comment on the power of optimize mode?
Thanks again.
Why is it that at the beginning of the loop you typecasted the solSize to int
int solSize=int(curSol_.size());but didn’t do that in the while loopsolSize=curSol_.size();. You might want to investigate the values in the debug and the optimized version.Also, there seem to be places in the code where there are floats / doubles being compared with (may be) integers. May be that is the cause of the infinite loop.