Esempio n. 1
0
void main(void)
{int i,j;
 if((galog=fopen("galog.txt","w"))==NULL)
   {
     exit(1);
   }

for(i=0;i<SUMNUM;i++)
{
  generation=0;
  population[POPSIZE].fitness=1000;
  initialize();


 evaluate();
 keep_the_best();
  report();
while(generation<MAXGENS)
{

  generation++;
  if(generation%100==0)
 printf("%d\n",generation);

  select();

 crossover();
 mutate();

evaluate();
elitist();

 report();
}

for(j=0;j<NVARS;j++)
{fprintf(galog,"\n var(%d)=%3.4f",j,population[POPSIZE].gene[j]);
}
fprintf(galog,"\n\n Best fitness=%3.4f",population[POPSIZE].fitness);

}

}
Esempio n. 2
0
int main() {
//	srand(time(0));
//	generateInst(CASE);

	for(CASE = 1; CASE<=10; CASE++) {
		_mkdir("GAgrid");
		strcpy(strdir,"GAgrid//inst");
		itoa(CASE,ch,10);
		strcat(strdir,ch);
		_mkdir(strdir);
		strcat(strdir,"//");
	readInst(CASE);

	int i,j;
	int gen;
	clock_t start;			//////////////////
	avg_result_time = 0;	//////////////////
	for(trialNum = 1; trialNum <= TRIALNUM; trialNum++) {
		strcpy(str,strdir);
		strcat(str,"result.txt");
		ftotal = fopen(str,"a");

		strcpy(str,strdir);
		itoa(trialNum,ch,10);
		strcat(str,"resultMnum");
		strcat(str,ch);
		strcat(str,".txt");
		fresultgMnum = fopen(str,"w");

		strcpy(str,strdir);
		strcat(str,"runtime");
		strcat(str,ch);
		strcat(str,".txt");
		fruntime = fopen(str,"w");

		start = clock();	//////////////////
		gen = 0;
		initialize(CASE);
		evaluate(CASE);
		keep_the_best();
		while(gen < MAXGENS) {
			select();			//using roulette wheel selection
			crossover();
			mutate();
			evaluate(CASE);	
			elitist();						
			gen++;
		//	printf("%d\t%g\t%g\n",gen,pop[POPSIZE].fitness,pop[POPSIZE].cost);
			fprintf(fruntime,"%d\t%g\t%g\n",gen,pop[POPSIZE].fitness,pop[POPSIZE].cost);
		}
		avg_result_time += clock() - start; //////////////////
		printf("%d\t%g\t%g\n",trialNum,pop[POPSIZE].fitness,pop[POPSIZE].cost);
		fprintf(ftotal,"%d\t%g\t%g\t%g\n",trialNum,pop[POPSIZE].fitness,pop[POPSIZE].cost,(double)avg_result_time/trialNum);//////////////////
		for(i=0;i<24;i++) {
			for(j=0;j<MTYPE;j++) {
				fprintf(fresultgMnum,"%d\t",pop[POPSIZE].gMnum[i][j]);
			}
			fprintf(fresultgMnum,"\n");
		}
	//	printf("\n");
		fprintf(fresultgMnum,"\n");
		fclose(fresultgMnum);
		fclose(ftotal);
		fclose(fruntime);
	}
	}

	return 0;
}