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

📄 gga.cc

📁 linux下的一个分组遗传算法
💻 CC
字号:
//// gga.cc - Grouping Genetic Algorithm//// author: J.I.v.Hemert// last updated : 07-11-1997//// This file implements the class GGAC.//#include "gga.h"extern MLCG rnd;			// Used for random numbersextern IniFileC inifile;	// Contains all adjustable parametersvoid GGAC::Initialize (int numberofobjects, int maxevaluations, int seed)// Set all genetic algorithm specific parameters and// initialize the population.{	TimerC timer;	// This random-stuff is used in the mutation, inversion, crossover	// and the 2-tournament.	if (inifile.ReadBool ("randomize"))		randomseed = timer.GetTime ();	else		randomseed = seed;	rnd.seed1 (5);	rnd.seed2 (randomseed);	nrofobjects = numberofobjects;	maxevals = maxevaluations;	gaparams.PopulationSize = inifile.ReadInt ("populationsize");	gaparams.N_Crossover = inifile.ReadInt ("crossover");	gaparams.N_Mutation = inifile.ReadInt ("mutation");	gaparams.N_Inversion = inifile.ReadInt ("inversion");	gaparams.AllelMutationProb = inifile.ReadDouble ("allelemutationprob");	if (gaparams.PopulationSize < gaparams.N_Crossover * 2)	{		cerr << "Error: the population size (" << gaparams.PopulationSize << ") is smaller than twice the amount of crossovers (" << gaparams.N_Crossover << ") per generation" << endl;		exit(2);	}	if (gaparams.PopulationSize < gaparams.N_Mutation)	{		cerr << "Error: the population size (" << gaparams.PopulationSize << ") is smaller than the amount of mutations (" << gaparams.N_Mutation << ") per generation" << endl;		exit(2);	}	if (gaparams.PopulationSize < gaparams.N_Inversion)	{		cerr << "Error: the population size (" << gaparams.PopulationSize << ") is smaller than the amount of inversions (" << gaparams.N_Inversion << ") per generation" << endl;		exit(2);	}	if ((gaparams.AllelMutationProb < 0) || (gaparams.AllelMutationProb > 1))	{		cerr << "Error: the parameter AllelMutationProb (" << gaparams.AllelMutationProb << ") should lie between 0 and 1" << endl;		exit(2);	}	debug = inifile.ReadBool ("ggadebug");	plotdata = inifile.ReadBool ("plotdata");	printsolutions = inifile.ReadBool ("printsolutions");	if (printsolutions)	{		solutionsfile.open (inifile.ReadString("solutionsfile"));		if (!solutionsfile.is_open ())		{			printsolutions = false;			cerr << "Warning: could not open " 					<< inifile.ReadString("solutionsfile") << endl;		}	}	if (plotdata)	{		datafile.open (inifile.ReadString("datafile"));		if (!datafile.is_open ())		{			plotdata = false;			cerr << "Warning: could not open " 					<< inifile.ReadString("datafile") << endl;		}	}} // Initialize ()void GGAC::InitializePopulation (){	randomseed++;	rnd.seed2 (randomseed);	ColoringT coloringalgorithm = undefined;	if (strcmp (inifile.ReadString ("coloringalgorithm"), "firstfit") == 0)			coloringalgorithm = firstfit;	else		if (strcmp (inifile.ReadString ("coloringalgorithm"), "smallfirst") == 0)				coloringalgorithm = smallfirst;		else			if (strcmp (inifile.ReadString ("coloringalgorithm"), "largefirst") == 0)					coloringalgorithm = largefirst;	population.Initialize (gaparams, nrofobjects, inifile.ReadBool ("populationdebug"), inifile.ReadInt ("k-coloring"), coloringalgorithm);} // InintializePupulation ()bool GGAC::Run ()// Run the genetic algorithm until it solves the problem or until// it has reached _maxevals_ evaluations. Return true if a solution// is found.{	int gen;	population.Evaluate ();	gen = 0;	if (debug)		cerr << "\b\b\b\b\b\b\b\b\b\b" 				<< setw(5) 				<< population.GetBestFitness ()				<< setw(5)				<< GetColorsUsed ();		while ((population.GetBestFitness () > 0) && (population.GetTotalEvaluations ()) < maxevals)	{		population.Reproduce ();		gen++;		population.Evaluate ();		if (debug)			cerr << "\b\b\b\b\b\b\b\b\b\b" 					<< setw(5) 					<< population.GetBestFitness ()					<< setw(5)					<< GetColorsUsed ();		if (plotdata)			datafile << population.GetTotalEvaluations ()  					 << " " 					 << population.GetBestFitness () << endl;	}	if (plotdata)		datafile << endl;	// Print some stats	if (debug) 		cerr << "\b\b\b\b\b" 			 << "total evals = " 			 << population.GetTotalEvaluations () 			 << "    bestfitness = " 			 << population.GetBestFitness () << endl;	if (printsolutions)		population.PrintBest (solutionsfile);	if (population.GetBestFitness () == 0)		return (true);	else		return (false);} // Run ()void GGAC::Close (){	if (printsolutions)		solutionsfile.close ();	if (plotdata)		datafile.close ();} // Close ()double GGAC::GetColorsUsed (){	return (population.GetColorsUsed ());} // GetColorsUsed ()double GGAC::GetTotalEvaluations ()// Return the totalevaluations done so far by the// genetic algorithm, no calculations are done.{	return (population.GetTotalEvaluations ());} // GetTotalEvaluation ()// eof gga.cc

⌨️ 快捷键说明

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