解TSP问题的遗传算法C语言程序
#include#include#include#include#include#include#include#include#include#define maxpop 100#define maxstring 100struct pp{unsigned char chrom[maxstring];? ?float x,fitness;? ?unsigned int parent1,parent2,xsite;? ?};struct pp *oldpop,*newpop,*p1;unsigned int popsize,lchrom,gem,maxgen,co_min,jrand;unsigned int nmutation,ncross,jcross,maxpp,minpp,maxxy;float pcross,pmutation,sumfitness,avg,max,min,seed,maxold,oldrand[maxstring];unsigned char x[maxstring],y[maxstring];float *dd,ff,maxdd,refpd,fm[201];FILE *fp,*fp1;float objfunc(float);void statistics();int select();int flip(float);int crossover();void generation();void initialize();void report();float decode();void crtinit();void inversion();float random1();void randomize1();main(){unsigned int gen,k,j,tt;char fname[10];float ttt;clrscr();co_min=0;if((oldpop=(struct pp *)farmalloc(maxpop*sizeof(struct pp)))==NULL){printf("memory requst fail!\n");exit(0);}if((dd=(float *)farmalloc(maxstring*maxstring*sizeof(float)))==NULL){printf("memory requst fail!\n");exit(0);}if((newpop=(struct pp *)farmalloc(maxpop*sizeof(struct pp)))==NULL){printf("memory requst fail!\n");exit(0);}if((p1=(struct pp *)farmalloc(sizeof(struct pp)))==NULL){printf("memory requst fail!\n");exit(0);}for(k=0;k