2011-12-12 101 views
-1

這是一個TSP問題,我試圖編譯它。C++分段錯誤

我似乎無法弄清楚爲什麼我得到這個分段錯誤。

該錯誤does not給我一個行號.. 我只是想讓這個徹底運行,任何幫助將不勝感激!

#include<iostream> 
#include <cstdlib> 
#include <ctime> 
#include <cmath> 
#include<cstdio> 
#define N 1000 

using namespace std; 

//Travelling sales man problem , travelling between 100 cities. 
//initializing the path lengths between cities and the paths to be included 
//in population 



void initialize(int pathlen[][100],int path[][100]) 
{ 
    int i,j,k; 


    //obtaining pathlengths 
    for(i=0;i<100;i++) 
    { 
     for(j=0;j<100;j++) 
     { 
      if(j<i)   //path length from a to b will be same as b to a 
      { 
       pathlen[i][j]=pathlen[j][i]; 
      } 

      if(j==i)   // path length from a to a will be 0 
      { 
       pathlen[i][j]=0; 

      } 

      if(j>i)   // rest initialized 
      { 
       do{ 
       pathlen[i][j]= random(); 
       }while(!pathlen[i][j]); 
      } 
     } 

    } 

    // display the path lengths 

    printf("\n\tThe PATH LENGTHS ARE: \n"); 

    for(i=0;i<100;i++) 
    { 
     for(j=0;j<100;j++) 
     { 
      printf(" %5d ",pathlen[i][j]); 
     } 
     printf("\n\n"); 
    } 


    // generating the population 

    for(i=0;i<50;i++) 
    { 
     for(j=0;j<100;j++) 
     { 
      path[i][j]=random(); 

      for(k=j-1;k>=0;k--) 
      { 
       if(path[i][j]==path[i][k]) //checking to avoid repeatition 
       { 
        path[i][j] = random(); 
        k=j; 
       } 
      } 
     } 
    } 



} 

// evaluating the fitness function or total distance 

void evaluate(int pathlen[100][100],int path[50][100],int fx[50]) 
{ 
    int sum =0,i,j,a,b; 

    //obtaing the sum of the path taken 
    for(i=0;i<50;i++) 
    { 
     sum=0; 
     for(j=0;j<5;j++) 
     { 
      a=path[i][j]; 
      b=path[i][j+1]; 
      sum=sum+pathlen[a][b]; 
     } 
     fx[i]=sum; 

    } 

    //display the paths generated 
    printf("\n"); 
    printf("\n\tPATH \t\tf(x) \n\n"); 
    for(i=0;i<50;i++) 
    { 
     printf("\t"); 
     for(j=0;j<100;j++) 
     { 
      printf(" %d",path[i][j]); 
     } 
     printf("\t%d",fx[i]); 
     printf("\n"); 
    } 

} 

//selecting the two points for cross over and then performing partial Crossover 

void selection(int fx[50],int pos[2],int posmax[2]) 
{ 
    int min1=fx[0],min2=fx[0],i,max1=fx[0],max2=fx[0]; 
    pos[0]=0; 
    pos[1]=0; 
    posmax[0]=0; 
    posmax[1]=0; 

    //calculating the minimum postion 
    for(i=1;i<50;i++) 
    { 
     if(fx[i]<min1) 
     { 
      min1=fx[i]; 
      pos[0]=i; 

     } 
    } 
    //calaculating the second minimum position 

    for(i=1;i<50;i++) 
    { 
     if(fx[i]<min2&&i!=pos[0]) 
     { 
      min2=fx[i]; 
      pos[1]=i; 

     } 
    } 

    //calculating the max position 

    for(i=1;i<50;i++) 
    { 
     if(fx[i]>max1) 
     { 
      max1=fx[i]; 
      posmax[0]=i; 

     } 
    } 
    //calculating the second max position 

    for(i=1;i<50;i++) 
    { 
     if(fx[i]>max2&&i!=posmax[0]) 
     { 
      max2=fx[i]; 
      posmax[1]=i; 

     } 

    } 
    printf("\n\tFIRST MINIMUM=%4d \tPOSITION=%4d\n\tSECOND MINIMUN=%4d \tPOSITION=%4d\n\tFIRST MAXIMUM=%4d \tPOSITION=%4d\n\tSECOND MAXIMUM=%4d \tPOSITION=%4d\n",min1,pos[0],min2,pos[1],max1,posmax[0],max2,posmax[1]); 

} 

//PERFORMING PARTIAL CROSSOVER 

void crossover(int pos[2],int path[][100],int child[2][100]) 
{ 
    int crosspt1,crosspt2,j,i,temp,temp1[2][100],temp2; 
    //TAKING 2 CROSS POINTS 
    do 
    { 
     crosspt1=random(); 
    }while(crosspt1>2) ; 
    do 
    { 
     crosspt2=random(); 
    }while(crosspt2<=3); 
    //clrscr(); 
    printf("\n\n\t The CROSSOVER POINTS ARE : %d , %d ",crosspt1,crosspt2); 
    printf("\n\n\tTHE PATHS FOR CROSSOVER ARE"); 
    printf("\n\n\t\t"); 

    for(j=0;j<100;j++) 
    { 
     child[0][j]=path[pos[0]][j]; 
     printf(" %d",child[0][j]); 
    } 
    printf("\n\t\t"); 
    for(j=0;j<100;j++) 
    { 
     child[1][j]=path[pos[1]][j]; 
     printf(" %d",child[1][j]); 
    } 

    int cnt=0; 
    //swapping the paths between two crosspoints 

    for(j=crosspt1+1;j<=crosspt2;j++) 
    { 
     temp1[1][cnt]=child[0][j]; 
     temp1[0][cnt]=child[1][j]; 
     temp=child[0][j]; 
     child[0][j]=child[1][j]; 
     child[1][j]=temp; 
     cnt++; 

    } 
    //performing partial crossover 

    int k,m; 
    for(m=0;m<2;m++) 
    { 
     for(i=0;i<crosspt1+1;i++) //taking the path before crosspoint 
     { 
      for(j=0;j<cnt;j++) //comparing the path within crossover point 
      { 
       if(child[m][i]==temp1[m][j]) //if found then 
       { 
        if(m==0) //for child 1 
        { 
         temp2=temp1[1][j]; //take the path from child2 crossover 

         for(k=0;k<100;k++) 
         { 
          if(child[m][k]==temp2) //if still the path repeats then repeat the process again 
          { temp2=child[1][k]; 
           k=0; 
          } 
         } 

         child[m][i]=temp2; //finally putting the value in child 

        } 
        else //for child 2 
        { 
         temp2=temp1[0][j]; 
         for(k=0;k<100;k++) 
         { 
          if(child[m][k]==temp2) 
          {temp2=child[0][k]; 
          k=0; 

          } 
         } 
         child[m][i]=temp2; 
        } 


       } 


      } 
     } 
    } 

    for(m=0;m<2;m++) 
    { 
     for(i=crosspt2+1;i<100;i++) //now chehcking the path after the second cross point 
     { 
      for(j=0;j<cnt;j++) //comparing the path within crossover point 
      { 
       if(child[m][i]==temp1[m][j]) //if found then 
       { 
        if(m==0) //for child 1 
        { 
         temp2=temp1[1][j]; //take the path from child2 crossove 
         for(k=0;k<100;k++) 
         { 
          if(child[m][k]==temp2) //if still the path repeats then repeat the process again 
          {temp2=child[1][k]; 
          k=0; 
          } 
         } 
         child[m][i]=temp2; //finally assigning the value 
        } 
        else //for child 2 
        { 

         temp2=temp1[0][j]; 
         for(k=0;k<cnt;k++) 
         { 
          if(child[m][k]==temp2) 
          {temp2=child[0][k]; 
          k=0; 
          } 
         } 
         child[m][i]=temp2; 
        } 

       } 


      } 
     } 
    } 
    //display AfTER CROSSOVER 
    printf("\n\tAFTER CROSSOVER\n\t\t"); 

    for(j=0;j<100;j++) 
    { 
     printf(" %d",child[0][j]); 
    } 
    printf("\n\t\t"); 
    for(j=0;j<100;j++) 
    { 
     printf(" %d",child[1][j]); 
    } 


} 

//insering the paths in population removing those having maximum populaiton 

void insert(int child[2][100],int posmax[2],int path[50][100]) 
{ 
    for(int j=0;j<100;j++) 
    { 
     path[posmax[0]][j]=child[0][j]; 
     path[posmax[1]][j]=child[1][j]; 
    } 


} 

// performing mutation 

void mutation(int child[2][100]) 
{ 
    int sel=random(); 
    int pos1=random(); 
    int pos2=random(); 
    int temp=child[sel][pos1]; 
    child[sel][pos1]=child[sel][pos2]; 
    child[sel][pos2]=temp; 
} 

void main() 
{ 
    //clrscr(); 
    //randomize(); 


    int pathlen[100][100],path[50][100],fx[50],pos[2],posmax[2],child[2][100]; 


    printf("\n\t\t\t TRAVELLING SALESMAN PROBLEM "); 
    printf("\n\t\t\t_____________________________"); 
    printf("\n\n\n\t\tTHE TRAVELLING SALES MAN PROBLEM DEALS WITH THE FACT"); 
    printf("\n\n\t\tTHAT A SALESMAN TRAVELS BETWEEN CITIES TAKING THE PATH"); 
    printf("\n\n\t\tTHAT IS OF MINIMUN DISTANCE."); 

    //getch(); 
    //clrscr(); 
    initialize(pathlen,path); 
    evaluate(pathlen,path,fx); 
    //getch(); 
    selection(fx,pos,posmax); 
    crossover(pos,path,child); 
    mutation(child); 
    //getch(); 
    insert(child,posmax,path); 

    for(int i=1;i<N;i++) 
    { 
     evaluate(pathlen,path,fx); 
     selection(fx,pos,posmax); 
     crossover(pos,path,child); 
     mutation(child); 
     insert(child,posmax,path); 

    } 
    evaluate(pathlen,path,fx); 
    selection(fx,pos,posmax); 
    crossover(pos,path,child); 
    insert(child,posmax,path); 

    evaluate(pathlen,path,fx); 
    //getch(); 


} 
+8

您是否嘗試過在調試器中運行呢?它會遇到分段錯誤,並應指定它發生的位置,並且可以在事件發生時獲取堆棧跟蹤。 – birryree

+0

不,我使用膩子來編譯..原因是我必須並行化它,我只是無法弄清楚如何修復分段錯誤 – user1093001

+0

膩子只是一個終端模擬器,你正在使用'g ++'。您可以使用帶'-g'標誌的調試符號來編譯程序,並且您可能希望在使用'-O0'的情況下進行零優化編譯。然後你可以用'gdb'加載程序,並按照如下教程進行操作:http://www.cs.cmu.edu/~gilpin/tutorial/ – birryree

回答

1

在gdb運行它,然後當段錯誤發生時,運行輸入BT獲得的行號。只要確保你用-g標誌編譯你的代碼。

1

除了在gdb下運行它....

  1. 如果Linux的

    1. 運行讓它崩潰並創建一個核心轉儲。現在您可以使用gdb研究核心轉儲,並查看問題發生的位置。
    2. 使用--tool = memcheck通過valgrind運行它。對於任何可能導致分段錯誤的內存相關問題,它生成的報告將幫助您解決問題。
  2. 如果您在Windows XP下運行

    1. 用你的博士沃森捕獲轉儲。然後使用WinDBG調試生成的轉儲。
    2. 如果您使用Win 2003或更高版本運行,則必須編寫代碼以生成dump
    3. 如果您有使用Visual Studio的奢侈品,您可以使用它進行調試並讓它崩潰。這將最有可能帶你到問題代碼,但記住並不總是這樣的作品。
    4. 您可以安裝WinDBG中的JIT甚至Visual Studio並用它來捕捉死機狀態,