Esempio n. 1
0
int HyperNEAT_main(int argc,char **argv) {
    CommandLineParser commandLineParser(argc,argv);

    // Quit if we don't have I/O/G
    if (!commandLineParser.HasSwitch("-I") ||
        !commandLineParser.HasSwitch("-O") ||
        !commandLineParser.HasSwitch("-G")) {
        cout << "./atari_generate [-R (seed)] -I (datafile) -O (outputfile) -G (ROMFile) [-P (populationfile) -F (fitnessprefix) [-E (evaluationFile)] ]\n";
        cout << "\t(datafile) experiment data file - typically data/AtariExperiment.dat\n";
        cout << "\t(outputfile) the next generation file to be created - typically generationXX.xml\n";
        cout << "\t(populationfile) the current generation file (required when outputfile is > generation0) - typically generationXX(-1).xml.gz\n";
        cout << "\t(fitnessprefix) used to locate the fitness files for individuals in the current generation (required for generation > 0) - typically fitness.XX.\n";
        cout << "\t(evaluationfile) populationfile + fitness + speciation (output only - not required for next cycle) - typicall generationXX(-1).eval.xml\n";
        return 0;
    }

    Globals *globals = Globals::init(commandLineParser.GetArgument("-I",0));

    // Setup the experiment
    int experimentType = int(globals->getParameterValue("ExperimentType") + 0.001);
    HCUBE::ExperimentRun experimentRun;
    string out_file = commandLineParser.GetArgument("-O",0);
    experimentRun.setupExperiment(experimentType, out_file);

    string rom_file = commandLineParser.GetArgument("-G",0);

    // Is this an experiment in progress? If so we should load the current experiment
    if (commandLineParser.HasSwitch("-P") &&
        commandLineParser.HasSwitch("-F")) {
        string populationFile = commandLineParser.GetArgument("-P",0);
        string fitnessFunctionPrefix = commandLineParser.GetArgument("-F",0);
        string evaluationFile = commandLineParser.GetSafeArgument("-E",0,"");
        cout << "[HyperNEAT core] Population for existing generation created from: " << populationFile << endl;
        experimentRun.createPopulationFromCondorRun(populationFile, fitnessFunctionPrefix, evaluationFile, rom_file);
    } else {
        cout << "[HyperNEAT core] Population for first generation created" << endl;
        shared_ptr<Experiment> e = experimentRun.getExperiment();        
        if (experimentType == 30 || experimentType == 35 || experimentType == 36) {
            shared_ptr<AtariExperiment> exp = static_pointer_cast<AtariExperiment>(e);
            exp->initializeExperiment(rom_file.c_str());
        } else if (experimentType == 31 || experimentType == 39 || experimentType == 40) {
            shared_ptr<AtariNoGeomExperiment> exp = static_pointer_cast<AtariNoGeomExperiment>(e);
            exp->initializeExperiment(rom_file.c_str());
        } else if (experimentType == 32 || experimentType == 37 || experimentType == 38) {
            shared_ptr<AtariFTNeatExperiment> exp = static_pointer_cast<AtariFTNeatExperiment>(e);
            exp->initializeExperiment(rom_file.c_str());
        } else if (experimentType == 33) {
            // This is the Hybrid experiment but always starts as HyperNEAT
            shared_ptr<AtariExperiment> exp = static_pointer_cast<AtariExperiment>(e);
            exp->initializeExperiment(rom_file.c_str());
        } else if (experimentType == 34) {
            shared_ptr<AtariIntrinsicExperiment> exp = static_pointer_cast<AtariIntrinsicExperiment>(e);
            exp->initializeExperiment(rom_file.c_str());
        } else if (experimentType == 41) {
            shared_ptr<AtariCMAExperiment> exp = static_pointer_cast<AtariCMAExperiment>(e);
            exp->setResultsPath(out_file);
            exp->initializeExperiment(rom_file.c_str());
        }
        experimentRun.createPopulation();
    }
    if (commandLineParser.HasSwitch("-R")) {
      double seed = stringTo<double>(commandLineParser.GetArgument("-R",0));
      globals->setParameterValue("RandomSeed",seed);
      globals->initRandom();
    }
    experimentRun.startCondor();

    NEAT::Globals::deinit();
}
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;
}