コード例 #1
0
void GA_BuildTower::Run()
{
  for (int i = 0; i < generation_cnt; ++i) {
    Select();
    Crossover();
    Mutate();
    CalFitness();
    Elitist();
  }
}
コード例 #2
0
ファイル: generate.c プロジェクト: CarlStevenson/schoolwork
Generate()
{
	static int rsflag = 1;	/* flag cleared after restart		*/

	STRUCTURE *temp;	/* for swapping population pointers	*/
	register int i;		/* for marking structures		*/

	if (Traceflag)
		printf("                    Gen %d\n",Gen);
	Trace("Generate entered");

	/* create a new population */

	if (Restartflag && rsflag)
	{
		/* this is a restart so read checkpoint file */
		Restart();
		rsflag = 0;	/* disable local restart flag. */
		Converge();
	}

	else if (Gen == 0)
		/* this is a fresh experiment */
	{
		Initialize();	/* form an initial population */
		Spin++;	
	}

	else
		/* form a new population from */
		/* the old one via genetic operators */
	{
		Select();
		Mutate();
		Crossover();
		if (Eliteflag)
			Elitist();
		if (Allflag)	/* mark structures for evaluation */
			for (i=0; i<Popsize; i++) New[i].Needs_evaluation = 1;
		Spin++;
	}

	/* evaluate the newly formed population */
	Evaluate();

	/* gather performance statistics */
	Measure();

	/* check termination condition for this experiment	*/
	Doneflag = Done();

	/* checkpoint if appropriate */
	if (Num_dumps && Dump_freq && Gen % Dump_freq == 0)
	{
		if (Num_dumps > 1)
		{
			sprintf(Dumpfile, "dump.%d", Curr_dump);
			Curr_dump = (Curr_dump + 1) % Num_dumps;
			Checkpoint(Dumpfile);
		}
		Checkpoint(Ckptfile);
	}
	else
	{
		if (Doneflag)
		{
			if (Lastflag)
				Checkpoint(Ckptfile);
			else
				if (Savesize)
					Printbest();
		}
	}


	/* swap pointers for next generation */
	temp = Old;
	Old = New;
	New = temp;

	/* update generation counter */
	Gen++;

	Trace("Generate completed");
}