?? initial.bak
字號:
/*======================================================== file : initial.c purpose : create initial population and initialize parameters developed : 1991 author : Kalyanmoy Deb==========================================================*/#include "mga.ext"void get_input()/* read and set input parameters */{ register int i; FILE *fp, *finput, *fopen(); long clock, time(); char *ctime(), msg[PAGEWIDTH]; gen_file("Parameters", Inputfilename, "parameters"); gen_file("Era", Erafilename, "era"); gen_file("Objective function", Objfilename, "subfunc"); gen_file("Template", Templatefilename, "template"); gen_file("Partition input", Partinfilename, "partitions"); gen_file("Pop record input", Poprinfilename, "poprecin"); gen_file("Extra members", Extrafilename, "extra"); gen_file("Output", Outputfilename, "output"); gen_file("Pop record output", Poproutfilename, "poprecout"); gen_file("Partition output", Partoutfilename, "partout"); gen_file("Plotting", Plotfilename, "plot"); /* check input files */ check_input_file(Inputfilename); /* read input parameters */ finput = fopen(Inputfilename, "r"); if (fscanf(finput, FORMAT_1, VARS_1) != 17) { sprintf(msg,"Error in the input file: parameters.\n"); print_error(msg); } check_input_file(Erafilename); check_input_file(Objfilename); if (extrapopflag) check_input_file(Extrafilename); /* open output files */ scratch_file(Outputfilename); if (plotstatflag) scratch_file(Plotfilename); if (popprintflag) { check_input_file(Poprinfilename); scratch_file(Poproutfilename); } if (partitionflag) { check_input_file(Partinfilename); scratch_file(Partoutfilename); } /* set up the clock */ fp = fopen(Outputfilename, "a"); time(&clock); fprintf(fp,"Start-up time is : %s\n",ctime(&clock)); fclose(fp); /* generate random numbers */ randomize(); /* get the template */ get_template(); fscanf(finput,"Copies (10 1 1) ="); if(!(copies = (int *)malloc((max_era+1) * sizeof(int)))) { sprintf(msg,"Insufficient memory for variable, copies.\n"); print_error(msg); } for (i = era; i <= max_era; i++) { copies[i] = 1; /* default */ fscanf(finput,"%d",&copies[i]); } fclose(finput); /* get the objective function information */ function_evaluations = 0.0; objfunc_info(); /* get the auxiliary information for report */ if (partitionflag) partition_info(); if (popprintflag) poprec_info(); /* writes the problem information */ problem_rep();}void initialize()/* initialize parameters */{ char msg[PAGEWIDTH]; if (thresholdingflag) shuffle_num = problem_length; if (popprintflag) { /* set the counter to the first generation */ nextpopstatgen = sortpopstatgen[0]; } /* get other input parameters */ if (fscanf(fin, FORMAT_2, VARS_2) != 2) { sprintf(msg,"Error in input file era.\n"); print_error(msg); } /* generate initial population, get stats, and write report */ initialize_pop(); statistics(oldpop); /* set up the population sizing in primordial phase */ setup_popsize(); initreport();}void initialize_pop()/* setup parameters for initial population */{ register int i; long member_id = 0; int temp_order, allele_combo; long max_size, total_size = 0, population_size[MAX_ORDER]; char msg[PAGEWIDTH]; gen = 0; stopmgaflag = 0; /* reset the population size array */ for (i=0; i <= era; i++) population_size[i] = 0; /* only order era strings are created */ order = temp_order = era; /* but if tiebreaking is on, all strings of order less than era are created */ if (tiebreakingflag) { order = temp_order = 1; } while (order <= era) { /* if reduction in initpop is desired */ if (r_initpop_flag) allele_combo = 1; else allele_combo = power(2,order); max_size = copies[order] * allele_combo * \ choose(problem_length,order); population_size[order] = max_size; total_size += max_size; order++; } /* storage allocation for */ /* population array */ if(!(newpop=(struct INDIVIDUAL *)malloc(total_size * \ sizeof(struct INDIVIDUAL)))) { sprintf(msg,"Insufficient memory for newpop.\n"); print_error(msg); } if(!(oldpop=(struct INDIVIDUAL *)malloc(total_size * \ sizeof(struct INDIVIDUAL)))) { sprintf(msg,"Insufficient memory for oldpop.\n"); print_error(msg); } /* storage for shuffle array */ if (!(shuffle = (int *)malloc(total_size*sizeof(int *)))) { sprintf(msg,"Insufficient memory for shuffle array.\n"); print_error(msg); } order = temp_order; while (order <= era) { initpop(&member_id, population_size[order]); order++; } if (extrapopflag) extra_pop(&member_id); order--; storepop(&best_indv,era); copy_chrom(oldpop[0],&best_indv); popsize = member_id; templatefitness = objfunc(template);}void extra_pop(last_member)long *last_member;/* creates extra population members */{ register int i,j,k,m; int flag=0, count=0, extra_size=0; long size, cnt; int patsize[MAX_PARTITIONS], extra_copies[MAX_PARTITIONS]; GENE_ID pattern[MAX_PARTITIONS][MAX_ORDER]; ALLELES allele_list[MAX_ORDER]; struct GENE *tailold; char dummy[PAGEWIDTH]; FILE *fp, *fopen(); fp = fopen(Extrafilename,"r"); get_string(fp, dummy, PAGEWIDTH); /* read gene information */ while (fscanf(fp,"%d",&patsize[count]) == 1) { for (i=0; i<patsize[count]; i++) fscanf(fp,"%d",&pattern[count][i]); fscanf(fp,"%d\n",&extra_copies[count]); extra_size += extra_copies[count] * power(2,patsize[count]); count++; } fclose(fp); size = (*last_member) + extra_size; /* reallocate memory size and allocate memory for extra individuals */ reallocate_memory(size); cnt = (*last_member); for (i=0; i<count; i++) { for (j=0; j<patsize[i]; j++) allele_list[j] = 0; for (j=0; j<power(2,patsize[i]); j++) { if (j != 0) next_allele_comb(allele_list,patsize[i],patsize[i]); for (k=0; k<extra_copies[i]; k++) { storepop(&(oldpop[cnt]),patsize[i]); storepop(&(newpop[cnt]),patsize[i]); tailold = oldpop[cnt].firstgene; tailold->genenumber = pattern[i][0]; tailold->allele = allele_list[0]; for (m=1; m<patsize[i]; m++) { tailold = tailold->nextgene; tailold->genenumber = pattern[i][m]; tailold->allele = allele_list[m]; } fill_chrom(&(oldpop[cnt])); oldpop[cnt].fitness = \ objfunc(oldpop[cnt].fullchrom); cnt++; } } } *last_member = cnt;}void randomtemplate(temp)unsigned *temp;/* create a random template */{ register int i,j; unsigned mask = 1; for (i = 0; i < bytesize; i++) { temp[i] = 0; for (j = 0; j < bytelimit[i]; j++) { if (flip(0.5)) temp[i] |= mask; if (j < bytelimit[i]-1) temp[i] <<= 1; } }}void next_genic_comb(list, n)GENE_ID *list;int n;/* calculates the next gene combination */{ register int j, s, k; BOOLEAN exitflag = 0; int pos; pos = n-1; if (list[pos] < problem_length - 1) (list[pos])++; else { for (j = pos; j > 0 && !exitflag; j--) { if (list[j-1] < problem_length - pos + j - 2) { (list[j-1])++; for (s = j; s <= pos; s++) { list[s] = list[j-1] + s - j + 1; exitflag = 1; } } } }}void next_allele_comb(list,size,len)ALLELES *list;int size, len;/* calculates the next bit combination */{ register int i; int total=0, pos; ALLELES allele_max = 1; pos = size-1; for (i = 0; i < pos+1; i++) { if (list[i]) total++; } if (list[pos] < allele_max) (list[pos])++; else if (total == pos+1) reset_list(list,size); else if ((list[pos-1] >= allele_max) && (list[pos] >= allele_max)) next_allele_comb(list, pos, len); else if (list[pos] >= allele_max) { for (i = pos; i < len; i++) list[i] = 0; (list[pos-1])++; }}void storepop(pop, n)struct INDIVIDUAL *pop;int n;/* allocates memory for an individual */{ register int j; struct GENE *tail; char msg[PAGEWIDTH]; /* storage allocation for */ /* genes */ if(!((*pop).firstgene = (struct GENE *)malloc(sizeof(struct GENE)))) { sprintf(msg,"Insufficient memory for firstgene.\n"); print_error(msg); } /* initialize genes */ tail = (*pop).firstgene; /* for each gene */ for (j = 1; j < n; j++) { if (!(tail->nextgene = (struct GENE *)malloc(sizeof(struct GENE)))) { sprintf(msg,"Insufficient memory for making a gene.\n"); print_error(msg); } tail = tail->nextgene; } /* assign last gene pointer and chromosome length */ (*pop).lastgene = tail; tail->nextgene = NIL; (*pop).chromlen = n; (*pop).genelen = n; /* allocate storage for fullchrom */ if(!((*pop).fullchrom = (unsigned *)malloc(bytesize*sizeof(unsigned)))) { sprintf(msg,"Insufficient memory for fullchrom.\n"); print_error(msg); } /* allocate storage for fullgene */ if(!((*pop).fullgene = (unsigned *)malloc(bytesize*sizeof(unsigned)))) { sprintf(msg,"Insufficient memory for fullgene.\n"); print_error(msg); }}void initpop(last_member, n)long *last_member;long n;/* generates initial population */{ register int i, j, m, count; long current_size; int allele_combo; GENE_ID genenumber_list[MAX_ORDER]; ALLELES allele_list[MAX_ORDER]; struct GENE *tailold; char msg[PAGEWIDTH]; sprintf(msg," Initpop entered for order %d",order); TRACE(msg); /* check if reduced population is desired */ if (r_initpop_flag) allele_combo = 1; else allele_combo = power(2, order);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -