⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 initial.bak

📁 genetic algorithm c code
💻 BAK
📖 第 1 页 / 共 2 页
字号:
	current_size = (*last_member) + n;	for (i= (*last_member); i < current_size; i++) {		/*  store oldpop and newpop  */		storepop(&(oldpop[i]),order);		storepop(&(newpop[i]),order);	}	/*   assign values  */	for (i = 0; i < order; i++)		genenumber_list[i] = i;	for (count=(*last_member); count < current_size; ) {		for (i = 0; i < order; i++)			allele_list[i] = 0;		for (j = 0; j < allele_combo; j++) {			if (j != 0)				/*   get the next allele combination  */				next_allele_comb(allele_list, order, order);			for (m = 0; m < copies[order]; m++)			{				tailold = oldpop[count].firstgene;				tailold->genenumber = genenumber_list[0];				if (r_initpop_flag)					tailold->allele = mut( \					pickallele(genenumber_list[0], template));				else 					tailold->allele = allele_list[0];				for (i = 1; tailold->nextgene != NIL; i++) {					tailold = tailold->nextgene;					tailold->genenumber = genenumber_list[i];					if (r_initpop_flag)						tailold->allele = mut( \						pickallele(genenumber_list[i],\								template));					else						tailold->allele = allele_list[i];				}				/*  fill up the under-specified genes  */				fill_chrom(&(oldpop[count]));				/*  evaluate objective function value  */				oldpop[count].fitness = \					objfunc(oldpop[count].fullchrom);				count++;			}		}		/*  get the next gene combination  */		next_genic_comb(genenumber_list, order);	}	*last_member = current_size;	TRACE("   Initpop exited\n");}int mut(bit)int bit;/* returns a 1 if the bit is 0 and vice versa */{	return((bit == 1) ? 0 : 1);}void objfunc_info()/*  reads objective function information  */{	register int i, j, k;	ALLELES allele_max = 1;	int sum, maxallele, bit, string_length, curr_len;	int numtable, numstrings, tablenum;	GENE_ID dummygene[MAX_ORDER];	double perfm;	char str[PAGEWIDTH], dummy[PAGEWIDTH], msg[PAGEWIDTH];	FILE *fp, *fopen();	fp = fopen(Objfilename,"r");	/*  read subfunction table information, skip a line  */	get_string(fp,dummy,PAGEWIDTH);	maxallele = allele_max + 1;	fscanf(fp,"%d\n",&numtable);	if(!(taborfunc = (BOOLEAN *)malloc(numtable * sizeof(BOOLEAN))))	{		sprintf(msg,"Insufficient memory for taborfunc.\n");		print_error(msg);	}	if(!(str_length = (int *)malloc(numtable * sizeof(int))))	{		sprintf(msg,"Insufficient memory for str_length.\n");		print_error(msg);	}	if(!(chromfitness = (double **)malloc(numtable * sizeof(double *))))	{		sprintf(msg,"Insufficient memory for chromfitness.\n");		print_error(msg);	}	get_string(fp,dummy,PAGEWIDTH);	for (k = 0; k < numtable; k++)	{		/* read size of the subfunction */		fscanf(fp,"%d\n",&tablenum);		fscanf(fp,"%s\n",&str[0]);		fscanf(fp,"%d\n",&string_length);		str_length[tablenum] = string_length;		taborfunc[tablenum] = 0;		if ((strcmp(str,"Table") == 0) || (strcmp(str,"table") == 0))		/* read a table, if the subfunction is a table  */		{			taborfunc[tablenum] = 1;			numstrings = power(maxallele,string_length);			if(!(chromfitness[tablenum] = (double *) \					malloc(numstrings * sizeof(double))))			{				sprintf(msg,"Insufficient memory for chromfitness.\n");				print_error(msg);			}			for (i = 0; i < numstrings; i++)			{				for (j = 0,sum = 0; j < string_length; j++)				{					/*   read each bit  */					fscanf(fp,"%d",&bit);					sum += power(maxallele,\						string_length-j-1) * bit;				}				/*  read the corresponding objective function value  */				fscanf(fp,"%lf\n",&perfm);				chromfitness[tablenum][sum] = perfm;			}		}		get_string(fp,dummy,PAGEWIDTH);	}	/*  read subfunction information, skip a line  */	get_string(fp,dummy,PAGEWIDTH);	/*  read number of subfunctions */	fscanf(fp,"%d",&numsubfunc);	if(!(table_id = (int *)malloc(numsubfunc * sizeof(int))))	{		sprintf(msg,"Insufficient memory for table_id.\n");		print_error(msg);	}	if(!(scale = (float *)malloc(numsubfunc * sizeof(float))))	{		sprintf(msg,"Insufficient memory for scale.\n");		print_error(msg);	}	if(!(genesets = (GENE_ID **)malloc(numsubfunc * sizeof(GENE_ID *))))	{		sprintf(msg,"Insufficient memory for variable, genesets.\n");		print_error(msg);	}	/*  for each subfunction  */	for (i = 0; i < numsubfunc; i++)	{		/*  read  the table number  */		fscanf(fp,"\n%d",&table_id[i]);		fscanf(fp,"%f",&scale[i]);		curr_len = str_length[table_id[i]];		read_subfunction(fp,dummygene,curr_len);		if(!(genesets[i] = (GENE_ID *)malloc(curr_len * sizeof(GENE_ID))))		{			sprintf(msg,"Insufficient memory for genes.\n");			print_error(msg);		}		for (j = 0; j < curr_len; j++)			genesets[i][j] = dummygene[j];	}	fclose(fp);}void read_subfunction(fp,dummygene,nstruc)FILE *fp;GENE_ID dummygene[];int nstruc;/*  reads subfunction data  */{	register int i;	int count = 0, sets = 0, prev = 0;	do {		prev = sets;		fscanf(fp,"%d",&sets);		if (sets >= 0) {			dummygene[count] = sets;			count++;		}		else {			sets *= -1;			for (i = 0; i <= sets - prev; i++) {				dummygene[count+i] = prev + i + 1;			}			count += sets - prev;		}	}   while (count < nstruc);}void partition_info()/*  reads partition information for analysis  */{	register int i, j;	FILE *fp, *fopen();	GENE_ID dummygene[MAX_PARTITIONSIZE];	char dummy[PAGEWIDTH], msg[PAGEWIDTH];	fp = fopen(Partinfilename,"r");	/*  read a comment  */	get_string(fp, dummy, PAGEWIDTH);	/* read the number of partitions */	fscanf(fp,"%d",&numpartition);	if(!(partition_len = (int *)malloc(numpartition * sizeof(int))))	{		sprintf(msg,"Insufficient memory for variable partition_len.\n");		print_error(msg);	}	if(!(partition_genes = (GENE_ID **)malloc(numpartition * \							sizeof(GENE_ID *))))	{		sprintf(msg,"Insufficient memory for partition genes.\n");		print_error(msg);	}		/*  for each structure  */	for (i = 0; i < numpartition; i++)	{		fscanf(fp,"\n%d",&partition_len[i]);		read_subfunction(fp,dummygene,partition_len[i]);		if(!(partition_genes[i] = (GENE_ID *)malloc(partition_len[i] * \							sizeof(GENE_ID))))		{			sprintf(msg,"Insufficient memory for partition genes.\n");			print_error(msg);		}		for (j = 0; j < partition_len[i]; j++)			partition_genes[i][j] = dummygene[j];	}	fclose(fp);}void poprec_info()/* reads generations for population record */{	register int i;        int numpopstatgen;	FILE *fp, *fopen();	char msg[PAGEWIDTH];	fp = fopen(Poprinfilename,"r");	get_string(fp, msg, PAGEWIDTH);	/*  read the generation numbers for population dump  */	fscanf(fp,"%d\n",&numpopstatgen);	if (!(sortpopstatgen = (int *)malloc(numpopstatgen*sizeof(int))))	{		sprintf(msg,"Insufficient memory for poprecfile.\n");		print_error(msg);	}	for (i = 0; i < numpopstatgen; i++)	{		fscanf(fp,"%d",&(sortpopstatgen[i]));	}	/*  sort the numbers in ascending order  */	sortnum(numpopstatgen, sortpopstatgen);	fclose(fp);}void setup_popsize()/*  sets up the popsize pattern in primordial phase  */{	int cal_cut, noofcut;	double init_prop, init_prop_1, init_prop_2;	init_select_gen = 1;	/*  initial proportion of the best individual  */	init_prop_1 = problem_length / era;	init_prop_1 /= (popsize / copies[era]);	init_prop_2 = prop_bestindv;	/* choose the greater of two */	init_prop = (init_prop_1 > init_prop_2) ? init_prop_1 : init_prop_2;	/* duration of primordial phase: (under tournament selection) is	   calculated to have the proportion of best individual equals to 0.5 */	prim_gen = 0;	if (init_prop < 0.5)		prim_gen = floor(log((1.0 - init_prop) / init_prop) / log(2.0));	cal_cut = prim_gen - (init_select_gen - 1);	if (popsize < juxtpopsize) juxtpopsize = popsize;	/*  theoretical number of cut from juxtpopsize and initial popsize */	noofcut = round((log(popsize) - log(juxtpopsize))/log(2.0));	if (cal_cut <= 0 || noofcut <= 0) {		/* if no cut is required */		cutpop_gen = 0;		juxtpopsize = popsize;		init_select_gen = 0;	}	else if (noofcut > cal_cut) {		/*  too many cut is requested  */		printf("Given juxtapositional popsize is too small %d, ",\						juxtpopsize);		juxtpopsize = popsize/power(2,cal_cut);		printf("using %d instead.\n",juxtpopsize);		cut_every_other_gen = 0;		cutpop_gen = prim_gen;	}	else if (noofcut >= cal_cut / 2) {		/*  some cut needs to be at every generation  */		cut_every_other_gen = init_select_gen - 1 + 2 * \					(cal_cut - noofcut);		cutpop_gen = prim_gen;	}	else {		/*  only cut at every other generation  */		cut_every_other_gen = init_select_gen - 1 + 2 * noofcut;		cutpop_gen = cut_every_other_gen;	}}void get_template()/*  get template information and create a template  */{	register int i, j;	unsigned mask = 1;	int id, sum; 	ALLELES dummytemplate[MAX_SIZE];	char msg[PAGEWIDTH];	FILE *fp, *fopen();	/*  set bytesize  */	id  = problem_length/UNSINTSIZE;	bytesize = (problem_length % UNSINTSIZE) ? id+1 : id;	/*  set up bytelimit array  */	if(!(bytelimit = (int *)malloc(bytesize*sizeof(int)))) {		sprintf(msg, "Insufficient memory for bytelimit\n");		print_error(msg);	}		for (i = 0; i < bytesize; i++)	{		if (i == bytesize-1)			bytelimit[i] = problem_length - (i*UNSINTSIZE);		else			bytelimit[i] = UNSINTSIZE;	}	/*  memory for template is allocated  */	if(!(template = (unsigned *)malloc(bytesize*sizeof(unsigned)))) {		sprintf(msg, "Insufficient memory for template\n");		print_error(msg);	}	if (levelmgaflag)	{		era = 1;		/*  get a random template  */		randomtemplate(template);	}	else	{		era = max_era;		/*  read template  */		check_input_file(Templatefilename);		fp = fopen(Templatefilename,"r");		for (i = 0; i < problem_length; i++)			fscanf(fp,"%d",&dummytemplate[i]);		fclose(fp);		/*  make template  */		for (i = 0,sum = 0; i < bytesize; i++)		{			sum += bytelimit[i];			template[i] = 0;			for (j = 0; j < bytelimit[i]; j++)			{				if (dummytemplate[sum - 1 - j] == 1)					template[i] |= mask;				if (j < bytelimit[i] - 1)					template[i] <<= 1;			}		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -