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

📄 statistics.cc

📁 linux下的一个分组遗传算法
💻 CC
字号:
//// statistics.cc - Used for the Genetic Grouping Algorithm//// author: J.I.v.Hemert// last updated : 07-11-1997//// This file implements the class StatisticsC. It is used to// calculate the Succes Rate (SR), Average Evaluations to // Solution (AES), and the number of colors used in the final// chromosome. It records these values in a file together// with all the data for the experiment. On user request it// builds two more files, these can be used to plot the SR and// AES against a variable that is being tested in a range.//#include "statistics.h"extern IniFileC inifile;//------------------------------------------------ Public functionsvoid StatisticsC::Initialize (bool & rescue)// Open statistics file and if needed the graph-data-files. Then// enter some descriptions into the statistics file.{	totaltimer.Reset ();	Reset ();	char * hostname;	ifstream rescuefile;	hostname = new char [30];	if (gethostname (hostname, 20) != 0)		strcpy (hostname, "unknown");	printgraphs = inifile.ReadBool ("printgraphs");	// Test if we are recovering from a interruption of the program	rescuefile.open (inifile.ReadString ("rescuefile"));	if (rescuefile.is_open ())	{		// Found a rescue file read in values from it		rescuefile.close ();		cerr << "Warning: rescue initiated on " << hostname << endl;				statisticsfile.open (inifile.ReadString ("statisticsfile"), ios::app);		if (printgraphs)		{			graphAESfile.open (inifile.ReadString ("graphAESfile"), ios::app);			graphSRfile.open (inifile.ReadString ("graphSRfile"), ios::app);			graphCOLORSfile.open (inifile.ReadString ("graphCOLORSfile"), ios::app);		}		rescue = true;	}	else	{		// No rescue create necessary files		statisticsfile.open (inifile.ReadString ("statisticsfile"));		if (!statisticsfile.is_open ())		{			cerr << "Error: could not open " 					<< inifile.ReadString ("statisticsfile") << endl;			exit (1);		}		if (printgraphs)		{			graphAESfile.open (inifile.ReadString ("graphAESfile"));			if (!graphAESfile.is_open ())			{				printgraphs = false;				cerr << "Warning: could not open " 						<< inifile.ReadString ("graphAESfile") << endl;			}			graphSRfile.open (inifile.ReadString ("graphSRfile"));			if (!graphSRfile.is_open ())			{				printgraphs = false;				cerr << "Warning: could not open " 						<< inifile.ReadString ("graphSRfile") << endl;			}			graphCOLORSfile.open (inifile.ReadString ("graphCOLORSfile"));			if (!graphCOLORSfile.is_open ())			{				printgraphs = false;				cerr << "Warning: could not open " 						<< inifile.ReadString ("graphCOLORSfile") << endl;			}		}		// Write information about experiment in statistics file		statisticsfile << "# nodes = "						<< inifile.ReadInt ("nodes")						<< "   edgeprobability  = "						<< inifile.ReadDouble ("edgeprobability")						<<  "   seed = "						<< inifile.ReadInt ("seed") << endl						<< "# coloring the nodes using "						<< inifile.ReadString ("coloringalgorithm") << endl						<<  "# graphtype = "						<< inifile.ReadString ("graphtype")						<<  "   k-coloring = "						<< inifile.ReadInt ("k-coloring") << endl						<< "# runs = " << inifile.ReadInt ("runs")						<< "   Tmax = " << inifile.ReadInt ("Tmax")						<< "   randomize = " << inifile.ReadBool ("randomize") << endl						<< "# population = "						<< inifile.ReadInt ("populationsize")						<< "   crossover = "						<< inifile.ReadInt ("crossover")						<< "   mutation = "						<< inifile.ReadInt ("mutation")						<< "   inversion = "						<< inifile.ReadInt ("inversion") << endl						<< "# allelemutationprobability = "						<< inifile.ReadDouble ("allelemutationprob") << endl						<< "# experiment runs on " << hostname						<< " with version " << VERSION << endl;		if (inifile.ReadBool ("rangetest"))			statisticsfile << "# range test is being done on \""							<< inifile.ReadString ("rangevariable")							<< "\" from " 							<< inifile.ReadString ("rangefrom") << " to "							<< inifile.ReadString ("rangeto") << " step "							<< inifile.ReadString ("rangestepsize") << endl;		statisticsfile << endl;		rescue = false;	}	free (hostname);} // Initialize ()void StatisticsC::Reset ()// Reset all internal variables to zero.{	runs = 0;	SR = 0;	AES = 0;	addedSR = 0;	addedAES = 0;	colorsused = 0;	addedcolorsused = 0;	timer.Reset ();} // Reset ()void StatisticsC::UpdateSR (double add){	addedSR = add;	SR += add;} // UpdateSR ()void StatisticsC::UpdateAES (double add){	addedAES = add;	AES += add;} // UpdateAES ()void StatisticsC::UpdateColorsUsed (double add){	addedcolorsused = add;	colorsused += add;} // UpdateColorsUsed ()void StatisticsC::Run ()// Give the stats from one run.{	runs++;	statisticsfile << "run " << setw (2) << runs					<< "  SR = " << addedSR					<< "  AES = " << addedAES					<< "  COLORS = " << addedcolorsused << endl;} // Run ()void StatisticsC::Print (double rangevar)// Give stats over multiple runs.{	statisticsfile << "    SR/run  = " << SR / runs;	if (SR == 0)		statisticsfile << "   AES/SR = " << inifile.ReadInt ("Tmax")  << "  no solutions found";	else		statisticsfile 	<< "   AES/SR = " << AES / SR;	statisticsfile << "   COLORS/runs = " << colorsused /runs;	statisticsfile << "   step = " << rangevar;	statisticsfile << "    time : ";	timer.Print (statisticsfile);	statisticsfile << endl;	if (printgraphs)	{		graphSRfile << rangevar << " " << SR / runs << endl;		if (SR == 0)			graphAESfile << rangevar << " " << inifile.ReadInt ("Tmax") << endl;		else			graphAESfile << rangevar << " " << AES / SR << endl;		graphCOLORSfile << rangevar << " " << colorsused / runs << endl;	}	// It's "save" to remove the rescue file now	remove (inifile.ReadString ("rescuefile"));} // Print ()void StatisticsC::Close ()// End of the experiment, close all files.{	statisticsfile << "  Total runtime : ";	totaltimer.Print (statisticsfile);	statisticsfile << endl;	statisticsfile.close ();	if (printgraphs)	{		graphSRfile.close ();		graphAESfile.close ();		graphCOLORSfile.close ();	}} // Close ()void StatisticsC::LoadRescueFile (double & rangevar, int & run)// Load all the necessary parameters from the rescue file needed// to continue with the experiments.{	IniFileC rescuefile;	rescuefile.Open (inifile.ReadString ("rescuefile"));	AES = rescuefile.ReadDouble ("AES");	SR = rescuefile.ReadDouble ("SR");	rangevar = rescuefile.ReadDouble ("rangevar");	runs = rescuefile.ReadInt ("runs");	run =  runs;	colorsused = rescuefile.ReadDouble ("colorsused");	statisticsfile << "# program was interrupted, continueing with version " << VERSION << endl;} // LoadRescueFile ()void StatisticsC::SaveRescueFile (double rangevar)// Save all the necessary parameters to the rescue file needed// to continue with the experiments.{	ofstream rescuefile;	rescuefile.open (inifile.ReadString ("rescuefile"));	if (!rescuefile.is_open ())		cerr << "Warning: could not save to rescuefile \"" << inifile.ReadString ("rescuefile") << endl;	else	{		rescuefile << "runs = " << runs << endl					<< "rangevar = " << rangevar << endl					<< "AES = " << AES << endl					<< "SR = " << SR << endl					<< "colorsused = " << colorsused << endl;		rescuefile.close ();	}} // SaveRecoveryFile ()//------------------------------------------------ Private functions// eof statistics.cc

⌨️ 快捷键说明

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