| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3186 人关注过本帖
标题:一段C语言程序,vc++6.0编译通过。运行时提示“出现问题需要关闭”。。
只看楼主 加入收藏
applepink
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-4-1
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
一段C语言程序,vc++6.0编译通过。运行时提示“出现问题需要关闭”。。
程序代码:
/***************************************************/
/*          The Simple Genetic Algoirithm          */
/*                      tspga8.c                   */
/*                Datafiles:tsp20.txt              */
/*        Writed by wang jinglun  (2005.6.10)      */
/***************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
/***************************************************/
/*          The Definition of Constant             */
/***************************************************/
#define MaxPOPSIZE 100        /*max population size*/
#define MaxGeneration 500     /*generation size*/
#define MaxPathlength 20       /*every path length*/
/****************************************************/
/*         The Definition of Data Structure         */
/****************************************************/
struct individual         /*data structure of individual*/
{   int  Path[MaxPathlength];      /*path of this individual*/
    int value;              /*object value of this individual*/
    double fitness;            /*fitness value of this individual*/
}population[MaxPOPSIZE];
struct  node
{   int Path[MaxPathlength];
    int gen;
    int value;
} bestone;
/****************************************************/
/*           The Definition of User Data            */
/*(For different problem,there are some difference.)*/
/****************************************************/
int l[MaxPathlength][MaxPathlength];
double Pc=0.8;                     /*probability of crossover*/
double Pm=0.05;                     /*probability of mutation*/
/****************************************************/
/*          The Definition of Global Variables      */
/****************************************************/
int POPSIZE;
int Pathlength;
int code[MaxPOPSIZE][MaxPathlength];    /*code of path*/
int chose[3];
long int rightvalue_sum; /*sum of rightvalue*/
/*long int value_max;*/
int max=0;
int min=0;
double best;        /*     fitness  of best individual */
int generation;     /*number of generation*/
GenerateInitialPopulation(void);
GenerateNextPopulation(void);
SelectionOperator(void);
CrossoverOperator(void);
MutationOperator(void);
Evaluaterate(void);
OutputTextReport(void);
FILE * fp,* fpw;
/*******************************************************************/
main(void)
{  int i,j,str[10];
   char c;
   int number;
   int array[MaxPathlength][MaxPathlength];
/*******************************************************************/
  if((fp=fopen("tsp20.txt","r"))==NULL)
  {  printf("cannot open file\n");   exit(0);
  }
  fread(str[10],3,1,fp);
  Pathlength=atoi(str[10]);
  for(i=0;i<Pathlength;i++)
  {
      for(j=0;j<Pathlength;j++)
      {  fread(str[10],3,1,fp);
         array[i][j]=atoi(str[10]);
         fread(c,1,1,fp);
      }
      fread(c,1,1,fp);
  }
  fclose(fp);
  Pathlength=20;
  for(i=0;i<Pathlength;i++)
  {   for(j=0;j<Pathlength;j++)
      {   l[i][j]=array[i][j];
          printf("%2d ",l[i][j]);
      }
      printf("\n\n");
  }
  printf("\n\n\n\n");
  getch();
  if((fpw=fopen("tsp20w.txt","w"))==NULL)
  {   printf("cannot open file\n");      exit(0);
  }
/****************************************************************/
  min_max();
  printf("%d _ %d",min,max);
  fwrite("Min=",5,1,fpw);  itoa(min,str,10);
  fwrite(str,3,1,fpw);     fwrite(",",1,1,fpw);
  fwrite("Max=",5,1,fpw);  itoa(max,str,10);
  fwrite(str,4,1,fpw);     fwrite("\n",1,1,fpw);
  getch();
  printf("\n\n");
  printf("******************************************\n\n");
  printf("** Choose: 1:best chose other crossover **\n\n");
  printf("**         2:best cross other crossover **\n\n");
  printf("******************************************\n\n");
  printf("Please chose the method of crossover!\n\n");
  scanf("%d",&chose[0]);
  printf("\n\n");
  printf("******************************************\n\n");
  printf("** Choose: 1:one_point_crossover        **\n\n");
  printf("**         2:partially_mapped_crossover **\n\n");
  printf("******************************************\n\n");
  printf("Please chose the method of crossover!\n\n");
  scanf("%d",&chose[1]);
  printf("\n\n");
  printf("******************************************\n\n");
  printf("** Choose: 1:simple_mutation            **\n\n");
  printf("**         2:inversion_mutation         **\n\n");
  printf("**         3:insert_mutation            **\n\n");
  printf("**         4:change_mutation            **\n\n");
  printf("******************************************\n\n");
  printf("\nPlease chose the method of mutation!\n\n");
  scanf("%d",&chose[2]);
  printf("\n\n") ;
  evaluate_popsize();
  generation=0;
  GenerateInitialPopulation();
  while(generation<MaxGeneration)
  {   generation++;
      GenerateNextPopulation();
      Evaluaterate();
      OutputTextReport();
  }
  fclose(fpw);
  getch();
}
/*******************************************************************/
GenerateInitialPopulation()     /*random*/                                   /*完全随机产生新种群*/
{  int x,y;
  /* Seed the random-number generator with current time so that
   * the numbers will be different every time we run.
   */
   srand( (unsigned)time( NULL ) );
   for(x=0;x<POPSIZE;x++)
   {   for(y=0;y<Pathlength;y++)
       {   if(y==0)
               code[x][y]=1;
           else
           code[x][y]=rand()%(Pathlength-y)+1;
       }
   }
   for(x=0;x<POPSIZE;x++)     code_path(x);
   for(x=0;x<POPSIZE;x++)
   {   printf("%3d   ",x+1);
       for(y=0;y<Pathlength;y++)    printf("%3d",code[x][y]);
       printf("\n");
   }
   printf("\n*****************************************\n\n");
   getch();
}
/*******************************************************************/
GenerateNextPopulation()
{   int x;
    SelectionOperator();/*     ppp();     */
    CrossoverOperator();/*     ppp();     */
    MutationOperator(); /*     ppp();     */
}
/*******************************************************************/
SelectionOperator()
{   int x;
    if(generation==1)
    {   rightvalue(POPSIZE);
    mode();
    for(x=0;x<Pathlength;x++)
        bestone.Path[x]=population[0].Path[x];
    bestone.value=population[0].value;
    }

 }
/*******************************************************************/
CrossoverOperator()
{   int x;
    switch(chose[0])
    {   case 1: crossover1();      break;
        case 2: crossover2();      break;
    }  /*    rightvalue(POPSIZE);     mode();     */
}
/**********************************************************************/
MutationOperator()
{   int x;
    for(x=0;x<POPSIZE;x++)    path_code(x);
    for(x=0;x<(int)(POPSIZE*Pm/1);x++)
     {   switch(chose[2])
     {    case 1: simple_mutation();        continue;
          case 2: inversion_mutation();     continue;
          case 3: insert_mutation();        continue;
          case 4: change_mutation();        continue;
     }
     }/*   rightvalue(POPSIZE);   mode();      */
}
/*******************************************************************/
Evaluaterate()
{   int i;
    rightvalue(POPSIZE);
    mode();
    for(i=0;i<POPSIZE;i++)        rate(i);
    best=(double)population[0].value/(double)rightvalue_sum;
    if(generation==1)
    {   bestone.gen=generation;
        bestone.value=population[0].value;
        for(i=0;i<Pathlength;i++)
        bestone.Path[i]=population[0].Path[i];
    }
    else
    {   if(population[0].value<bestone.value)
        {   bestone.gen=generation;
            bestone.value=population[0].value;
            for(i=0;i<Pathlength;i++)
            bestone.Path[i]=population[0].Path[i];
        }
    }
    mode();
}
/*******************************************************************/
OutputTextReport()
{   int i;
    char str[10],strp[10]="         ";
    printf("generation=%3d,  best=%f\n ",generation,best);
    fwrite("generation=",12,1,fpw);
    strcpy(str,strp);
    itoa(generation,str,10);    fwrite(str,3,1,fpw);
    fwrite(",",1,1,fpw);
    fwrite("bestone=",8,1,fpw);
    itoa(population[0].value,str,10);
    fwrite(str,4,1,fpw);    fwrite(",",1,1,fpw);
    fwrite("Path=",6,1,fpw);
    printf("        path=");
    for(i=0;i<Pathlength;i++)
    {   printf("%d ",population[0].Path[i]);
    itoa(population[0].Path[i],str,10);
    fwrite(str,3,1,fpw);    fwrite(",",1,1,fpw);
    }
    fwrite("\n",1,1,fpw);
    printf("\nvalue=%d,  bestone.value=%d",population[0].value,bestone.value);
    printf("\n\n");
    if(generation==MaxGeneration)
    {   printf("\n\nThe best one: generation=%3d,  ",bestone.gen);
       fwrite("bestval=",10,1,fpw);
       itoa(bestone.value,str,10);       fwrite(str,4,1,fpw);
       fwrite(",",1,1,fpw);
       fwrite("bestgen=",10,1,fpw);
       itoa(bestone.gen,str,10);       fwrite(str,4,1,fpw);
       fwrite(",",1,1,fpw);
       fwrite("Path=",6,1,fpw);    printf("path=");
    for(i=0;i<Pathlength;i++)
    {   printf("%d ",bestone.Path[i]);
        itoa(bestone.Path[i],str,10);
        fwrite(str,3,1,fpw);
        fwrite(",",1,1,fpw);
    }
    printf("  value=%d,",bestone.value);
        printf("\n");
    }
}
/*******************************************************************/
evaluate_popsize()
{   int i;    int sum=1;
    if(Pathlength<7)
    {   for(i=1;i<Pathlength;i++)    sum=sum*i;
        POPSIZE=(sum*5)/12 ;
    }
    else
    POPSIZE=MaxPOPSIZE;
}
/*******************************************************************/
code_path(int n)  /*code change into path */
{   int b[MaxPathlength];
    int i,j,k;
    for(i=0;i<MaxPathlength;i++)
    b[i]=0;
    for(i=0;i<Pathlength;i++)
    {   k=0;
    for(j=0;j<Pathlength;j++)
    {   if((b[j]==1))
          k++;
          else
         { if(code[n][i]+k==j+1)
              {   population[n].Path[i]=j+1;
              b[j]=1;  break;
              }
          }
    }
    }
}
/*******************************************************************/
path_code(int n)    /*path change into code */                                /*遗传基因编码方法*/
{   int b[MaxPathlength];
    int i,j,k;
    for(i=0;i<MaxPathlength;i++)    b[i]=0;
    for(i=0;i<Pathlength;i++)
    {   k=0;
    for(j=1;j<Pathlength+1;j++)
    {   if((b[j]==0))
        {   if(population[n].Path[i]==j)
            {   code[n][i]=j-k;
                 b[j]=1;            break;
            }
        }
        else
         k++;
    }
    }
}
/******************************************************************/
rightvalue(int n)
{   int i,j;    int length;    rightvalue_sum=0;
    for(i=0;i<n;i++)
    {   length=0;
        for(j=0;j<Pathlength-1;j++)
          length=l[population[i].Path[j]-1][population[i].Path[j+1]-1]+length;
    population[i].value=length+l[population[i].Path[Pathlength-1]-1][population[i].Path[0]-1];
    }
    for(i=0;i<POPSIZE;i++)
        rightvalue_sum=population[i].value+rightvalue_sum;
}
/******************************************************************/
rate(int m)
{    population[m].fitness=(double)population[m].value/(double)rightvalue_sum;
}
/*******************************************************************/
crossover1()
{   int coding[MaxPOPSIZE]={0};
    int c[MaxPathlength]={0};
    int i,j,x,y;

 /*   srand( (unsigned)time( NULL ) );*/
    for(i=0;i<POPSIZE*Pc;i++)
    {   coding[i]=rand()%(POPSIZE-1)+1;
    if(c[coding[i]]!=0)
        coding[i]=rand()%(POPSIZE-1)+1;
    else
        c[coding[i]]=1;
    }
    for(j=0;j<POPSIZE*Pc/2-1;j++)
    {   switch(chose[1])
    {
      case 1: one_point_crossover(coding[0+j*2],coding[1+j*2]);      continue;
      case 2: partially_mapped_crossover(coding[0+j*2],coding[1+j*2]); continue ;
    }
    }
}
/*******************************************************************/
crossover2()
{   int i,j;
    for(i=1;i<POPSIZE;i++)
    {   switch(chose[1])
    {
           case 1: for(j=0;j<Pathlength;j++)
               population[0].Path[j]=bestone.Path[j];
               path_code(0);
               one_point_crossover(0,i);
               continue;
           case 2: for(j=0;j<Pathlength;j++)
               population[0].Path[j]=bestone.Path[j];
               partially_mapped_crossover(0,i);
               continue;
    }
    }
}
/*******************************************************************/
one_point_crossover(int a,int b)
{   int i,j;    int t;
    i=rand()%(Pathlength-3)+2;
    for(i;i<Pathlength;i++)
    {   t=code[a][i];
        code[a][i]=code[b][i];
        code[b][i]=t;
    }
    code_path(a);    code_path(b);
    rightvalue(POPSIZE);
    if(a==0)
    {   if(population[a].value<population[b].value)
        {   for(i=0;i<Pathlength;i++)
                code[b][i]=code[a][i];
        }
    }
    code_path(a);    code_path(b);
}
/*******************************************************************/
partially_mapped_crossover(int a,int b)
{   int i,j,t;
    int p1,p2,temp;
    int c[MaxPathlength]={0};
    p1=rand()%(Pathlength-2)+1;
    p2=rand()%(Pathlength-2)+1;
    while(abs(p2-p1)<1)
    {  /* srand( (unsigned)time( NULL ) );*/
        p1=rand()%(Pathlength-2)+1;
    }
    if(p1>p2)
    {   temp=p1;   p1=p2;     p2=temp;
    }
    for(i=p1;i<p2;i++)
    c[i]=population[a].Path[i];
    for(i=p1;i<p2;i++)
    {   for(j=0;j<Pathlength;j++)
    {   if(population[a].Path[j]==population[b].Path[i])
        {    population[a].Path[j]=population[a].Path[i];
         population[a].Path[i]=population[b].Path[i];
         break;
        }
    }
    }
    for(i=p1;i<=p2;i++)
    {   for(j=0;j<Pathlength;j++)
    {   if(population[b].Path[j]==c[i])
        {      population[b].Path[j]=population[b].Path[i];
        population[b].Path[i]=c[i];
        break;
        }
    }
    }
    if(a==0)
    {   rightvalue(POPSIZE);
        if(population[a].value<population[b].value)
    {   for(i=0;i<Pathlength;i++)
        population[b].Path[i]=population[a].Path[i];
    }
    }
}
/*******************************************************************/
simple_mutation()                                                   /*点位变异*/
{   int i,j,p;
    i=rand()%(POPSIZE-1)+1;
    j=rand()%(Pathlength-2)+1;
    p=rand()%(Pathlength-j)+1;
    path_code(i);
    while(p==code[i][j])
    {   srand( (unsigned)time( NULL ) );
        p=rand()%(Pathlength-j)+1;
    }
   code[i][j]=p;
   code_path(i);
}
/******************************************************************/
inversion_mutation()                                               /*逆转变异*/
{   int i;   int x,temp,p1,p2;
    int insert[MaxPathlength]={0};
    int num;    int m;
    x=rand()%(POPSIZE-1)+1;
    p1=rand()%(Pathlength-2)+1;
    p2=rand()%(Pathlength-2)+1;
    while(abs(p2-p1)<1)
    {   srand( (unsigned)time( NULL ) );
          p2=rand()%(Pathlength-1)+1;
    }
    if(p1>p2)
    {   temp=p1;    p1=p2;    p2=temp;
    }
    num=0;
    for(i=p1;i<=p2;i++)
    {   insert[num]=population[x].Path[i];
    num++;
    }
    m=0;
    for(i=p2;i>=p1;i--)
    {   population[x].Path[i]=insert[m];
        if(m==num-1)
            break;
           else
            m++;
    }
}
/****************************************************************/
insert_mutation()                                                /*插入变异*/
{   int i;
    int x,temp,p1,p2;
    int  insert[MaxPathlength];
    x=rand()%(POPSIZE-1)+1;
    p1=rand()%(Pathlength-2);
    p2=rand()%(Pathlength-2);
    while(abs(p2-p1)<2)
    {  /* srand( (unsigned)time( NULL ) );*/
    p2=rand()%(Pathlength-1);
    }
    if(p1>p2)
    {   temp=p1;        p1=p2;        p2=temp;
    }
    temp=population[x].Path[p2];
    for(i=p2;i>p1;i--)
        population[x].Path[i]=population[x].Path[i-1];
    population[x].Path[p1+1]=temp;
}
/******************************************************************/
change_mutation()                                                  /*对换变异*/
{   int i;    int x,temp,p1,p2;
    int insert[MaxPathlength];
    x=rand()%(POPSIZE-1)+1;
    p1=rand()%(Pathlength-2)+1;
    p2=rand()%(Pathlength-2)+1;
    while(abs(p2-p1)<1)
    {   srand( (unsigned)time( NULL ) );
        p2=rand()%(Pathlength-1)+1;
    }
    if(p1>p2)
    {   temp=p1;    p1=p2;        p2=temp;
    }
    temp=population[x].Path[p1];
    population[x].Path[p1]=population[x].Path[p2];
    population[x].Path[p2]=temp;
}
/*******************************************************************/
mode()
{   int i,j;    int x;    int temp_value;
    double temp_fitness;
    int temp_Path[MaxPathlength];
    for(j=0;j<POPSIZE;j++)
    {   for(i=j+1;i<POPSIZE;i++)
    {   if(population[j].value>=population[i].value)
        {   temp_value=population[j].value;
        population[j].value=population[i].value ;
            population[i].value=temp_value;
        temp_fitness=population[j].fitness;
        population[j].fitness=population[i].fitness ;
        population[i].fitness=temp_fitness;
        for(x=0;x<Pathlength;x++)
             { temp_Path[x]=population[j].Path[x];
               population[j].Path[x]=population[i].Path[x] ;
               population[i].Path[x]=temp_Path[x];
             }
        }
    }
    }
    for(x=0;x<POPSIZE;x++)       path_code(x);
}
/*******************************************************************/
min_max()
{   int i,j,n;
    int m[MaxPathlength*MaxPathlength/2];
    int temp;
    for(i=0;i<Pathlength*Pathlength/2;i++)       m[i]=0;
    n=0;
    for(i=1;i<Pathlength;i++)
    {   for(j=0;j<i;j++)
        {   m[n]=l[i][j];   n++;
        }
    }
    for(i=0;i<n;i++)
    {   for(j=i+1;j<n;j++)
        {   if(m[i]>m[j])
            {   temp=m[i];    m[i]=m[j];    m[j]=temp;
            }
        }
    }
    for(i=0;i<Pathlength;i++)         min=min+m[i];
    for(i=n-1;i>=n-1-Pathlength;i--) max=max+m[i];
}
/*******************************************************************/
save(int x)
{   FILE * fp;
    int i;
    char str[11],strp[11]="          ";
/*------------------------------------------------------------------*/
    if((fp=fopen("tsp1111.txt","a"))==NULL)
    {    printf("cannot open file\n");    exit(0);
    }
    itoa(x,str,10);        fwrite(str,3,1,fp);
    fwrite(" ",1,1,fp);    fwrite(" ",1,1,fp);
    for(i=0;i<7;i++)
    {   strcpy(str,strp);
    itoa(population[x].Path[i],str,10);
        fwrite(str,3,1,fp);    fwrite(",",1,1,fp);
    }
    itoa(rightvalue(x),str,10);
    fwrite(str,3,1,fp);       fwrite(",",1,1,fp);
    fwrite("\n",1,1,fp);
    fclose(fp);
}
/*******************************************************************/
ppp()
{   int x,y;
    printf("Gen=%d \n", generation);
    for(x=0;x<POPSIZE;x++)
    {   printf("%3d ",x+1);
    for(y=0;y<Pathlength;y++)  printf(",%2d",code[x][y]);
    printf(" path=");
    for(y=0;y<Pathlength;y++)  printf(",%d",population[x].Path[y])     ;
    printf("v=%d \n",population[x].value);
    }
    getch();
}


求助各位大虾。。哪里出现问题??
搜索更多相关主题的帖子: 提示 运行 编译 C语言 
2010-04-23 21:19
‰ミ不乖ゅ
Rank: 2
等 级:论坛游民
帖 子:9
专家分:12
注 册:2010-4-2
收藏
得分:6 
好长啊。。。
2010-04-23 21:22
applepink
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-4-1
收藏
得分:0 
回复 2楼 ‰ミ不乖ゅ
确实有点长。。。
2010-04-23 21:29
LBY318902435
Rank: 2
来 自:江苏南京
等 级:论坛游民
帖 子:76
专家分:48
注 册:2009-3-31
收藏
得分:6 
编译好像通不过的……

走自己的路……
2010-04-23 22:15
jackface
Rank: 2
等 级:论坛游民
帖 子:63
专家分:17
注 册:2009-4-15
收藏
得分:6 
只能一段一段调试了。
2010-04-23 22:27
applepink
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-4-1
收藏
得分:0 
回复 4楼 LBY318902435
有一个TSP20.TXT的文档我没附上。。
我用VC++6.0是可以编译通过的。
2010-04-23 22:45
快速回复:一段C语言程序,vc++6.0编译通过。运行时提示“出现问题需要关闭”。。 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.074017 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved