Esempio n. 1
0
int HyperNEAT_main(int argc,char **argv)
{
	
	CommandLineParser commandLineParser(argc,argv);
	//This is for memory debugging
	//_CrtSetBreakAlloc(137405);
	
	try
	{
		if(commandLineParser.HasSwitch("-I"))
		   {
			   string paramFileName = commandLineParser.GetSafeArgument("-I",0,"input.dat");
			   ifstream paramFile;
			   paramFile.open(paramFileName.c_str());
			   if(!paramFile)
			   {
				   cout << "The following parameter file does not exist: " << paramFileName << endl;
				   exit(3);
			   }
			   else
				   cout << "Using the following parameter file: " << paramFileName << endl;
		   }
		
		if (argc<=1)
		{
			cout << "You must pass the parameters and the output file as command "
			<< "parameters!\n";
		}
		else if (argc==2) //running post-hoc eval
		{
			
			//Run the post-hoc analysis on every generation
			HCUBE::ExperimentRun experimentRun;
			
			experimentRun.setupExperimentInProgress(string(argv[1]),"");
			
			int numGenerations = experimentRun.getPopulation()->getGenerationCount();
			
			HCUBE::Experiment *experiment = experimentRun.getExperiment()->clone();
			if(false) //this block is for printing post hoc info on every generation, but not necessary for evaluating an org
			{
#ifndef PRINT_GENCHAMPFITNESSVIAPOSTHOCEVAL
				string outString = (erase_tail_copy(string(argv[1]),3)+string("_fitness.out"));
				cout << "Creating file " << outString << endl;
				ofstream outfile( outString.c_str() );
				string previousSummary;                
				bool doOnce=false;
#endif 
				
				for (int generation=0;generation<numGenerations;generation++)
				{
					//CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
					
					//CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
					
					if ( generation &&
							(*(experimentRun.getIndividual(generation,0).get())) == (*(experimentRun.getIndividual(generation-1,0).get())) )
					{
#ifndef PRINT_GENCHAMPFITNESSVIAPOSTHOCEVAL
						outfile << (generation+1) << ' ' << previousSummary << endl;
#endif
						continue;
					}
					
					shared_ptr<NEAT::GeneticIndividual> indiv =
					shared_ptr<NEAT::GeneticIndividual>(
																							new NEAT::GeneticIndividual(
																																					*(experimentRun.getIndividual(generation,0).get())
																																					)
																							);
					
					//CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
					
					//cout << "Beginning post-hoc evaluation " << (generation+1) << "/" << numGenerations << "...";
					//experiment->processIndividualPostHoc(indiv);
					//cout << "done!\n";
					
					//CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
#ifndef PRINT_GENCHAMPFITNESSVIAPOSTHOCEVAL
					if (indiv->getUserData())
					{
						if (!doOnce)
						{
							doOnce=true;
							outfile
							<< "#Generation: "
							<< indiv->getUserData()->summaryHeaderToString()
							<< endl;
						}
						
						previousSummary = indiv->getUserData()->summaryToString();
						outfile << (generation+1) << ' ' << previousSummary << endl;
					}
					else
					{
						if (!doOnce)
						{
							doOnce=true;
							outfile
							<< "#Generation: "
							<< "Fitness:"
							<< endl;
						}
						
						previousSummary = toString(indiv->getFitness());
						outfile << (generation+1) << ' ' << previousSummary << endl;
					}
#endif
					
				}
			}
			int generation = numGenerations-1; // jmc: interested in last generation (I don't know why it is -1)
			//                    int generation = 0; // jmc: interested in first generation
			cout << "Beginning post-hoc evaluation " << (generation) << "/" << numGenerations-1 << "...\n";
			
			shared_ptr<NEAT::GeneticIndividual> indiv =
			shared_ptr<NEAT::GeneticIndividual>(new NEAT::GeneticIndividual(*(experimentRun.getIndividual(generation,0).get())));
			
			
			
			experiment->processIndividualPostHoc(indiv);
			cout << "done!\n";
#if 0
			
			//CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
			
			if (indiv->getUserData())
			{
				if (!doOnce)
				{
					doOnce=true;
					outfile
					<< "#Generation: "
					<< indiv->getUserData()->summaryHeaderToString()
					<< endl;
				}
				
				previousSummary = indiv->getUserData()->summaryToString();
				outfile << (generation+1) << ' ' << previousSummary << endl;
			}
			else
			{
				if (!doOnce)
				{
					doOnce=true;
					outfile
					<< "#Generation: "
					<< "Fitness:"
					<< endl;
				}
				
				previousSummary = toString(indiv->getFitness());
				outfile << (generation+1) << ' ' << previousSummary << endl;
			}
			
		}
	}
#endif
	
	/*
	 cout << "Done with run!  Running medium resolution tests...\n";
	 ((HCUBE::FindClusterExperiment*)experiment)->increaseResolution();
	 
	 {
	 string outString = (erase_tail_copy(string(argv[1]),4)+string("_midres.out"));
	 ofstream outfile( outString.c_str() );
	 cout << "Creating file " << outString << endl;
	 string previousSummary;
	 
	 bool doOnce=false;
	 
	 for (int generation=0;generation<numGenerations;generation++)
	 {
	 //CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
	 
	 //CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
	 
	 if ( generation &&
	 (*(experimentRun.getIndividual(generation,0).get())) == (*(experimentRun.getIndividual(generation-1,0).get())) )
	 {
	 outfile << (generation+1) << ' ' << previousSummary << endl;
	 continue;
	 }
	 
	 shared_ptr<NEAT::GeneticIndividual> indiv =
	 shared_ptr<NEAT::GeneticIndividual>(
	 new NEAT::GeneticIndividual(
	 *(experimentRun.getIndividual(generation,0).get())
	 )
	 );
	 
	 //CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
	 
	 cout << "Beginning post-hoc evaluation " << (generation+1) << "/" << numGenerations << "...";
	 experiment->processIndividualPostHoc(indiv);
	 cout << "done!\n";
	 
	 //CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
	 
	 if (indiv->getUserData())
	 {
	 if (!doOnce)
	 {
	 doOnce=true;
	 outfile
	 << "#Generation: "
	 << indiv->getUserData()->summaryHeaderToString()
	 << endl;
	 }
	 
	 previousSummary = indiv->getUserData()->summaryToString();
	 outfile << (generation+1) << ' ' << previousSummary << endl;
	 }
	 
	 }
	 }
	 */
	}
int HyperNEAT_main(int argc,char **argv)
{
    //This is for memory debugging
    //_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); 
    //_CrtSetBreakAlloc(1378);

    int retval=0;

    CommandLineParser commandLineParser(argc,argv);

#if 1

#if 0
    try
#endif
    {
        if (argc<=1)
        {
#ifndef HCUBE_NOGUI
            cout_ << "Starting wxEntry...\n";
            retval = wxEntry(argc, argv);
#else
            cout_ << "You must pass the parameters and the output file as command "
                 << "parameters!\n";
            cout_ << "(If you intended to run in GUI mode, please run cmake and set the USE_GUI flag to true)\n";
#endif
        }
        else if (argc==2)
        {
            //Run the post-hoc analysis on last generation
            cout_ << "Running post-hoc analysis on: " << argv[1] << endl;
            HCUBE::ExperimentRun experimentRun;
			
            experimentRun.setupExperimentInProgress(
													string(argv[1]),
													""
													);
			
            int numGenerations = experimentRun.getPopulation()->getGenerationCount();
			
            HCUBE::Experiment *experiment = experimentRun.getExperiment()->clone();
			
            /*
			 {
			 string outString = (erase_tail_copy(string(argv[1]),4)+string("_fitness.out"));
			 cout_ << "Creating file " << outString << endl;
			 ofstream outfile( outString.c_str() );
			 string previousSummary;
			 
			 bool doOnce=false;
			 
			 for (int generation=0;generation<numGenerations;generation++)
			 {
			 //CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
			 
			 //CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
			 
			 if ( generation &&
			 (*(experimentRun.getIndividual(generation,0).get())) == (*(experimentRun.getIndividual(generation-1,0).get())) )
			 {
			 outfile << (generation+1) << ' ' << previousSummary << endl;
			 continue;
			 }
			 
			 shared_ptr<NEAT::GeneticIndividual> indiv =
			 shared_ptr<NEAT::GeneticIndividual>(
			 new NEAT::GeneticIndividual(
			 *(experimentRun.getIndividual(generation,0).get())
			 )
			 );
			 
			 //CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
			 
			 cout_ << "Beginning fitness evaluation " << (generation+1) << "/" << numGenerations << "...";
			 cout_.flush();
			 experiment->addIndividualToGroup(indiv);
			 experiment->processGroup(experimentRun.getGeneration(generation));
			 experiment->clearGroup();
			 cout_ << "done!\n";
			 
			 //CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
			 
			 if (indiv->getUserData())
			 {
			 if (!doOnce)
			 {
			 doOnce=true;
			 outfile
			 << "#Generation: "
			 << indiv->getUserData()->summaryHeaderToString()
			 << endl;
			 }
			 
			 previousSummary = indiv->getUserData()->summaryToString();
			 outfile << (generation+1) << ' ' << previousSummary << endl;
			 }
			 else
			 {
			 throw CREATE_LOCATEDEXCEPTION_INFO("No user data!\n");
			 }
			 
			 }
			 }
			 */
			
            {
                string outString = (erase_tail_copy(string(argv[1]),3)+string("_fitness.out"));
                cout_ << "Creating file " << outString << endl;
                ofstream outfile( outString.c_str() );
                string previousSummary;
				
                bool doOnce=false;
				
				int firstGen = 0;
				//int firstGen = numGenerations-1;
                for (int generation=firstGen;generation<numGenerations;generation++)
                {
                    //CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
					
                    //CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
					
#if 0
                    if ( generation &&
						(*(experimentRun.getIndividual(generation,0).get())) == (*(experimentRun.getIndividual(generation-1,0).get())) )
                    {
                        outfile << (generation+1) << ' ' << previousSummary << endl;
                        continue;
                    }
#endif
					
                    shared_ptr<NEAT::GeneticIndividual> indiv =
					shared_ptr<NEAT::GeneticIndividual>(
														new NEAT::GeneticIndividual(
																					*(experimentRun.getIndividual(generation,0).get())
																					)
														);
					
					if(generation!=firstGen)
					{
						shared_ptr<NEAT::GeneticIndividual> lastindiv =
                        shared_ptr<NEAT::GeneticIndividual>(
															new NEAT::GeneticIndividual(
																						*(experimentRun.getIndividual(generation-1,0).get())
																						)
															);
						
						if(fabsf(indiv->getFitness()-lastindiv->getFitness())<1e-3)
						{
							//cout_ << "Skipping redundant individual\n";
							continue;
						}
					}
					
                    //CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
					
                    //cout_ << "Beginning post-hoc evaluation " << (generation+1) << "/" << numGenerations << "...";
                    //experiment->processIndividualPostHoc(indiv);
                    //cout_ << "done!\n";
					
                    //CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
					
#if 0
                    if (indiv->getUserData().length())
                    {
                        if (!doOnce)
                        {
                            doOnce=true;
                            outfile
							<< "#Generation: "
							<< indiv->getUserData()
							<< endl;
                        }
						
                        previousSummary = indiv->getUserData();
                        outfile << (generation+1) << ' ' << previousSummary << endl;
                    }
                    else
                    {
                        if (!doOnce)
                        {
                            doOnce=true;
                            outfile
							<< "#Generation: "
							<< "Fitness:"
							<< endl;
                        }
						
                        previousSummary = toString(indiv->getFitness());
                        outfile << (generation+1) << ' ' << previousSummary << endl;
                    }
#endif
					
					cout_ << "Beginning post-hoc evaluation " << (generation+1) << "/" << numGenerations << "...";
					experiment->processIndividualPostHoc(indiv);
					cout_ << indiv->getUserData() << endl;
					cout_ << "done!\n";
					
					//CREATE_PAUSE(string("Error!: ")+toString(__LINE__));
					
#if 0
					if (indiv->getUserData())
					{
						if (!doOnce)
						{
							doOnce=true;
							outfile
							<< "#Generation: "
							<< indiv->getUserData()->summaryHeaderToString()
							<< endl;
						}
						
						previousSummary = indiv->getUserData()->summaryToString();
						outfile << (generation+1) << ' ' << previousSummary << endl;
					}
					else
					{
						if (!doOnce)
						{
							doOnce=true;
							outfile
							<< "#Generation: "
							<< "Fitness:"
							<< endl;
						}
						
						previousSummary = toString(indiv->getFitness());
						outfile << (generation+1) << ' ' << previousSummary << endl;
					}
#endif
				}
			}
		}
        else if (
				 commandLineParser.HasSwitch("-P")
				 )
        {
			string inputFile = commandLineParser.GetSafeArgument("-P",0,"input.dat");
            //Run the post-hoc analysis
            cout_ << "Running post-hoc analysis on: " << inputFile << endl;
            HCUBE::ExperimentRun experimentRun;
			
            experimentRun.setupExperimentInProgress(
													inputFile,
													""
													);
			
            int numGenerations = experimentRun.getPopulation()->getGenerationCount();
			
            HCUBE::Experiment *experiment = experimentRun.getExperiment()->clone();
			
			{
				int firstGen = 0;
				//int firstGen = numGenerations-1;
				bool ignoreFirstIfElitism=false;
				
				if(commandLineParser.HasSwitch("-G"))
				{
					firstGen = atoi(commandLineParser.GetSafeArgument("-G",0,"0").c_str())-1;
					if(firstGen<numGenerations)
						numGenerations = firstGen+1;
				}
		
				if(firstGen && commandLineParser.HasSwitch("-S"))
					ignoreFirstIfElitism=true;
				
                for (int generation=firstGen;generation<numGenerations;generation++)
                {
                    shared_ptr<NEAT::GeneticIndividual> indiv =
					shared_ptr<NEAT::GeneticIndividual>(
														new NEAT::GeneticIndividual(
																					*(experimentRun.getIndividual(generation,0).get())
																					)
														);
					
					if(generation!=firstGen || ignoreFirstIfElitism)
					{
						shared_ptr<NEAT::GeneticIndividual> lastindiv =
                        shared_ptr<NEAT::GeneticIndividual>(
															new NEAT::GeneticIndividual(
																						*(experimentRun.getIndividual(generation-1,0).get())
																						)
															);
						
						if(fabsf(indiv->getFitness()-lastindiv->getFitness())<1e-3)
						{
							//cout_ << "Skipping redundant individual\n";
							continue;
						}
					}
					
					cout_ << "Beginning post-hoc evaluation " << (generation+1) << "/" << numGenerations << "..." << endl;
					experiment->processIndividualPostHoc(indiv);
					cout_ << indiv->getUserData() << endl;
					cout_ << "done!\n";
				}
			}
		}
		else if(
    commandLineParser.HasSwitch("-I") &&
    commandLineParser.HasSwitch("-O")
    )
{
    NEAT::Globals::init(commandLineParser.GetSafeArgument("-I",0,"input.dat"));

    if(commandLineParser.HasSwitch("-R"))
    {
        NEAT::Globals::getSingleton()->seedRandom(stringTo<unsigned int>(commandLineParser.GetSafeArgument("-R",0,"0")));
    }

    int experimentType = int(NEAT::Globals::getSingleton()->getParameterValue("ExperimentType")+0.001);

    cout_ << "Loading Experiment: " << experimentType << endl;

    HCUBE::ExperimentRun experimentRun;

    experimentRun.setupExperiment(experimentType,commandLineParser.GetSafeArgument("-O",0,"output.xml"));

    cout_ << "Experiment set up\n";

    experimentRun.createPopulation();

    experimentRun.setCleanup(true);

    cout_ << "Population Created\n";

    experimentRun.start();
}
else if(
    commandLineParser.HasSwitch("-C")
    )
{
    HCUBE::ExperimentRun experimentRun;

    string backupFile = commandLineParser.GetArgument("-C",0);
    string outputFile = backupFile.substr(0,backupFile.length()-14);
			
    //This also calls NEAT::Globals::init(...)
    experimentRun.setupExperimentInProgress(backupFile,outputFile);

    cout_ << "Experiment set up\n";

    experimentRun.setCleanup(true);
    experimentRun.start();
}
else
{
    cout_ << "Syntax for passing command-line options to HyperNEAT (do not actually type '(' or ')' ):\n";
    cout_ << "./HyperNEAT [-R (seed)] -I (datafile) -O (outputfile)\n";
}
}
#if 0
catch (const std::exception &ex)
{
    cout_ << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl;
    cout_ << "An exception has occured: " << ex.what() << endl;
}
catch (string s)
{
    cout_ << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl;
    cout_ << "An exception has occured: " << s << endl;
}
catch (...)
{
    cout_ << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl;
    cout_ << "An error has occured!\n";
}
#endif

NEAT::Globals::deinit();

#endif

return retval;
}