void ExperimentRun::experimentRunPrintToGenChampFile() { try { population->printToGenChampFile(); } catch (const std::exception &ex) { cout << "EXCEPTION DURING experimentRunPrintToGenChampFile: " << endl; CREATE_PAUSE(ex.what()); } catch (...) { cout << "Unhandled Exception\n"; } }
void ExperimentRun::produceNextGeneration() { cout << "Producing next generation.\n"; try { population->produceNextGeneration(); } catch (const std::exception &ex) { cout << "EXCEPTION DURING POPULATION REPRODUCTION: " << endl; CREATE_PAUSE(ex.what()); } catch (...) { cout << "Unhandled Exception\n"; } }
void EvaluationSet::run() { #ifndef _DEBUG try #endif { //Process individuals sequentially running=true; vector<shared_ptr<NEAT::GeneticIndividual> >::iterator tmpIterator; tmpIterator = individualIterator; for (int a=0;a<individualCount;a++,tmpIterator++) { while (!running) { boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC_); xt.sec += 1; boost::thread::sleep(xt); // Sleep for 1 second } experiment->addIndividualToGroup(*tmpIterator); if (experiment->getGroupSize()==experiment->getGroupCapacity()) { //cout << "Processing group...\n"; experiment->processGroup(generation); //cout << "Done Processing group\n"; experiment->clearGroup(); } } if (experiment->getGroupSize()>0) { //Oops, maybe you specified a bad # of threads? throw CREATE_LOCATEDEXCEPTION_INFO("Error, individuals were left over after run finished!"); } finished=true; } #ifndef _DEBUG catch (string s) { cout << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE(s); } catch (const char *s) { cout << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE(s); } catch (const std::exception &ex) { cout << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE(ex.what()); } catch (...) { cout << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE("An unknown exception has occured!"); } #endif }
void ExperimentRun::start() { if(true) {//NEAT::Globals::getSingleton()->getParameterValue("MultiObjective") > 0.5) { boost::filesystem::remove(outputFileName); boost::filesystem::remove(outputFileName+string(".gz")); } cout << "Experiment started\n"; #ifndef _DEBUG try { #endif int maxGenerations = int(NEAT::Globals::getSingleton()->getParameterValue("MaxGenerations")); started=running=true; clock_t start,current,reference; start = clock(); reference = clock(); //resuming an old experiment, want to produce next gen right away because last one already evaluated if ((population->getGenerationCount()-1) > 0) { mutex::scoped_lock scoped_lock(*populationMutex); cout << "PRODUCING NEXT GENERATION\n"; produceNextGeneration(); cout << "DONE PRODUCING\n"; } int startGen = population->getGenerationCount()-1; for (int generations=(population->getGenerationCount()-1);generations<maxGenerations;generations++) { current = clock(); double time = (double(current)-double(reference))/CLOCKS_PER_SEC; if(time >= 3600.0 ) { //if at least an hour elapsed double elapsed = (double(current)-double(start))/CLOCKS_PER_SEC; printf("TIME_STATS: best fitness = %5.5f, generation = %d, elapsed time = %5.5f s\n", population->getBestAllTimeIndividual()->getFitness(), generations - 1,elapsed); reference = clock(); } if (generations>startGen) { mutex::scoped_lock scoped_lock(*populationMutex); //set IDs before reproducing so maintain ancestry -- NO LONGER NEEDED SINCE IDs will have been set in finishEvaluations //population->getGeneration()->setIDs(); cout << "PRODUCING NEXT GENERATION\n"; produceNextGeneration(); cout << "DONE PRODUCING\n"; // cout << "DUMPING REPRODUCED FROM PREVIOUS GENERATION\n"; // population->dumpReproducedFromPreviousGeneration(outputFileName/*+string(".backup.xml")*/,true,true); // cout << "DONE DUMPING\n"; } if (experiments[0]->performUserEvaluations()) { #ifdef HCUBE_NOGUI throw CREATE_LOCATEDEXCEPTION_INFO("ERROR: TRIED TO USE INTERACTIVE EVOLUTION WITH NO GUI!"); #else frame->getUserEvaluationFrame()->updateEvaluationPanels(); running=false; while (!running) { boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); xt.sec += 1; boost::thread::sleep(xt); // Sleep for 1/2 second //cout << "Sleeping while user evaluates!\n"; } #endif } else { while (!running) { boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); xt.sec += 1; boost::thread::sleep(xt); // Sleep for 1/2 second } evaluatePopulation(); } cout << "Finishing evaluations\n"; finishEvaluations(); cout << "Evaluations Finished\n"; } //if want to dump all will be taken care of from finishEvaluations now for all gens //cout << "DUMPING ALL INDIVIDUALS FROM FINAL GENERATION\n"; //population->getGeneration()->setIDs(); //population->dump(outputFileName/*+string(".backup.xml")*/,true,true); //cout << "DONE DUMPING\n"; cout << "Experiment finished\n"; //cout << "Saving Dump..."; //population->dump(outputFileName,true,false); //cout << "Done!\n"; if(true) {//NEAT::Globals::getSingleton()->getParameterValue("MultiObjective") > 0.5) { //need to combine files into one //TiXmlDocument doc(); TiXmlElement *root = new TiXmlElement("Genetics"); NEAT::Globals::getSingleton()->dump(root); /* stringstream ss; //root->Print(ss,0); ss << root; string s = ss.str(); */ cout << "Merging Output Files..."; TiXmlPrinter rootPrinter; root->Accept( &rootPrinter ); string s = rootPrinter.CStr(); ofstream out( outputFileName.c_str() ); string lastGenFileName = outputFileName + string(".lastgen.xml"); ofstream out2( lastGenFileName.c_str() ); out << s.substr(0, s.length() - 3) << ">" <<endl; out2 << s.substr(0, s.length() - 3) << ">" <<endl; int maxGenerations = int(NEAT::Globals::getSingleton()->getParameterValue("MaxGenerations")); for(int i=0; i<maxGenerations; i++) { stringstream ss; ss << i; TiXmlDocument doc( outputFileName + string("-") + ss.str() + string(".backup.xml.gz") ); doc.LoadFileGZ(); TiXmlPrinter printer; doc.Accept(&printer); out << printer.CStr() << endl; if( i == (maxGenerations - 1)) out2 << printer.CStr() << endl; } out << "</Genetics>" << endl; out2 << "</Genetics>" << endl; out.close(); out2.close(); cout << "Done!\n"; cout << "Compressing Merged File..."; stringstream ss; ss << "gzip " << outputFileName; std::system(ss.str().c_str()); stringstream ssLastGen; ssLastGen << "gzip " << lastGenFileName; std::system(ssLastGen.str().c_str()); cout << "Done!\n"; cout << "Deleting backup files..."; for(int i=0; i<maxGenerations; i++) { stringstream ss2; ss2 << outputFileName << "-" << i << ".backup.xml.gz"; boost::filesystem::remove(ss2.str()); } boost::filesystem::remove(outputFileName + string("-root.backup.xml")); cout << "Done!\n"; } else { cout << "Saving best individuals..."; string bestFileName = outputFileName.substr(0,outputFileName.length()-4)+string("_best.xml"); population->dumpBest(bestFileName,true,true); cout << "Done!\n"; cout << "Deleting backup file..."; boost::filesystem::remove(outputFileName+string(".backup.xml")); cout << "Done!\n"; } #ifndef _DEBUG } catch (const std::exception &ex) { cout << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE(ex.what()); } catch (...) { cout << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE("AN UNKNOWN EXCEPTION HAS BEEN THROWN!"); } #endif }
void EvaluationSet::run() { #ifndef _DEBUG try #endif { //Process individuals sequentially running=true; vector<shared_ptr<NEAT::GeneticIndividual> >::iterator tmpIterator; tmpIterator = individualIterator; for (int a=0;a<individualCount;a++,tmpIterator++) { while (!running) { boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); //(nac: changed for latest version of boost) xt.sec += 1; cout << "jmc: used1!\n"; exit(3); boost::thread::sleep(xt); // Sleep for 1 second } experiment->addIndividualToGroup(*tmpIterator); if (experiment->getGroupSize()==experiment->getGroupCapacity()) { //cout << "Processing group...\n"; experiment->processGroup(generation); //cout << "Done Processing group\n"; experiment->clearGroup(); } } if (experiment->getGroupSize()>0) { //Oops, maybe you specified a bad # of threads? throw CREATE_LOCATEDEXCEPTION_INFO("Error, individuals were left over after run finished! Do you have the c++ flag -DINTERACTIVELYEVOLVINGSHAPES set? Did you set getGroupCapacity to return the pop size in HCUBE_Experiment.h? or there may have been a bad (or negative, or zero) fitness value for an organism?"); } finished=true; } #ifndef _DEBUG catch (string s) { cout << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE(s); } catch (const char *s) { cout << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE(s); } catch (const std::exception &ex) { cout << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE(ex.what()); } catch (...) { cout << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE("An unknown exception has occured!"); } #endif }
void ExperimentRun::start() { cout << "Experiment started\n"; #ifndef DEBUG_EXPRUN try { #endif int maxGenerations = int(NEAT::Globals::getSingleton()->getParameterValue("MaxGenerations")); started=running=true; for (int generations=(population->getGenerationCount()-1);generations<GET_PARAMETER("MaxGenerations");generations++) { cout << "CURRENT SUBEXPERIMENT: " << currentSubExperiment << " Generation:" << generations << endl; if (generations>0) { // TODO: replace OR with experiments[currentSubExperiment].getExperimentName() == "HYBRID" if(experiments[currentSubExperiment]->getExperimentName() == "HYBRID" && switchSubExperiment(generations)) // if((experimentType == EXPERIMENT_LEGSWING_HYBRID || experimentType == EXPERIMENT_BITMIRRORING_HYBRID || experimentType == EXPERIMENT_TARGETWEIGHTS_HYBRID) && switchSubExperiment(generations)) { cout << "\n\n" << "************************\n" << "Switching SubExperiment: Hyper -> FT\n" << "************************\n\n"; // copy population from (currentSubExperiment-1)%totalSubExperiments to currentSubExperiment using HyperNEAT to P-NEAT converter population = shared_ptr<NEAT::GeneticPopulation>(experiments[currentSubExperiment]->createInitialPopulation(population, experiments[(currentSubExperiment + 1) % totalSubExperiments])); //TODO: check that this copying works //THESE CAN COME OUT when Hyper -> NEAT works cout << "changing the following three parameter settings from the HyperNEAT settings of:" << endl; cout << "MutateAddNodeProbability: " << NEAT::Globals::getSingleton()->getParameterValue("MutateAddNodeProbability") << endl; cout << "MutateAddLinkProbability: " << NEAT::Globals::getSingleton()->getParameterValue("MutateAddLinkProbability") << endl; cout << "MutateDemolishLinkProbability: " << NEAT::Globals::getSingleton()->getParameterValue("MutateDemolishLinkProbability") << endl; NEAT::Globals::getSingleton()->setParameterValue("MutateAddNodeProbability",0.0); NEAT::Globals::getSingleton()->setParameterValue("MutateAddLinkProbability",0.0); NEAT::Globals::getSingleton()->setParameterValue("MutateDemolishLinkProbability",0.0); cout << endl << "to the FT-NEAT values of: " << endl; cout << "MutateAddNodeProbability: " << NEAT::Globals::getSingleton()->getParameterValue("MutateAddNodeProbability") << endl; cout << "MutateAddLinkProbability: " << NEAT::Globals::getSingleton()->getParameterValue("MutateAddLinkProbability") << endl; cout << "MutateDemolishLinkProbability: " << NEAT::Globals::getSingleton()->getParameterValue("MutateDemolishLinkProbability") << endl << endl; if(experiments[currentSubExperiment]->getHybrid_FTMutateOnlyProbability() != -1.0) NEAT::Globals::getSingleton()->setParameterValue("MutateOnlyProbability", experiments[currentSubExperiment]->getHybrid_FTMutateOnlyProbability()); if(experiments[currentSubExperiment]->getHybrid_FTMutateLinkProbability() != -1.0) NEAT::Globals::getSingleton()->setParameterValue("MutateLinkProbability", experiments[currentSubExperiment]->getHybrid_FTMutateLinkProbability()); //THESE CAN COME OUT ONCE THIS WORKS cout << "MutateOnlyProbability" << NEAT::Globals::getSingleton()->getParameterValue("MutateOnlyProbability") << endl; cout << "MutateLinkProbability" << NEAT::Globals::getSingleton()->getParameterValue("MutateLinkProbability") << endl; } else { mutex::scoped_lock scoped_lock(*populationMutex); cout << "\nPRODUCING NEXT GENERATION\n"; produceNextGeneration(); } } if (experiments[currentSubExperiment]->performUserEvaluations()) { throw CREATE_LOCATEDEXCEPTION_INFO("ERROR: TRIED TO USE INTERACTIVE EVOLUTION WITH NO GUI!"); } else { while (!running) { boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC_); xt.sec += 1; // boost::thread::sleep(xt); // Sleep for 1/2 second usleep(500000); } #ifdef INTERACTIVELYEVOLVINGSHAPES stringstream genNum; genNum << setw(5) << std::setfill('0') << generations; population->dumpLast(outputFileName +"_"+ genNum.str()+".xml",true,false); //print out xml file each generation #endif cout << "about to evaluatePopulation\n"; evaluatePopulation(); } #ifdef DEBUG_EXPRUN cout << "Finishing evaluations\n"; #endif finishEvaluations(); experimentRunPrintToGenChampFile(); #ifdef DEBUG_EXPRUN cout << "Evaluations Finished\n"; #endif } cout << "Experiment finished\n"; cout << "Saving Dump..."; population->dump(outputFileName+string("_pop.xml"),true,false); cout << "Done!\n"; cout << "Saving best individuals..."; string bestFileName = outputFileName.substr(0,outputFileName.length()-4)+string("_best.xml"); population->dumpBest(bestFileName,true,true); cout << "Done!\n"; cout << "Skippped deleting backup files because of problem with boost!"; //cout << "Deleting backup file..."; //boost::filesystem::remove(outputFileName+string(".backup.xml.gz")); //cout << "Done!\n"; #ifndef DEBUG_EXPRUN } catch (const std::exception &ex) { cout << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE(ex.what()); } catch (...) { cout << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE("AN UNKNOWN EXCEPTION HAS BEEN THROWN!"); } #endif }
void MainFrame::loadExperiment(wxCommandEvent& WXUNUSED(event)) { try { if (experimentRun.isStarted()) { throw CREATE_LOCATEDEXCEPTION_INFO("Loading a new experiment while one is running is not yet supported!\n"); } else { wxFileDialog loadParametersDialog( this, _T("Choose a Parameter File"), wxGetCwd(), _T(""), _T("*.dat"), wxOPEN|wxFILE_MUST_EXIST ); int retVal = loadParametersDialog.ShowModal(); string fileName = WXSTRING_TO_STRING(loadParametersDialog.GetFilename()); fileName = fileName.substr(0,fileName.length()-3) + string("xml"); if (retVal==wxID_OK) { wxFileDialog outputFileDialog( this, _T("Choose an Output File"), loadParametersDialog.GetDirectory(), STRING_TO_WXSTRING(fileName), _T("*.xml;*.gz"), wxSAVE ); retVal = outputFileDialog.ShowModal(); if (retVal==wxID_OK) { NEAT::Globals::init(WXSTRING_TO_STRING(loadParametersDialog.GetPath())); int experimentType = int(NEAT::Globals::getSingleton()->getParameterValue("ExperimentType")+0.001); cout_ << "Loading Experiment: " << experimentType << endl; experimentRun.setupExperiment(experimentType,WXSTRING_TO_STRING(outputFileDialog.GetPath())); cout_ << "Experiment set up\n"; experimentRun.createPopulation(); cout_ << "Population Created\n"; fileMenu->FindItem(wxID_RUNEXPERIMENT_MENUITEM)->Enable(true); setPopulationSize(int(NEAT::Globals::getSingleton()->getParameterValue("PopulationSize"))); if (experimentRun.getExperiment()->performUserEvaluations()) { cout_ << "Creating User Evaluation Window..."; //Create a Frame for User Evaluations userEvaluationFrame = new UserEvaluationFrame(&experimentRun,(wxWindow*)this); cout_ << "...Done!\n"; } StackMap<string,double,4096>::iterator parameterIterator = NEAT::Globals::getSingleton()->getMapBegin(); for (; parameterIterator != NEAT::Globals::getSingleton()->getMapEnd(); parameterIterator++) { ostringstream ostr; ostr << parameterIterator->first << ": " << parameterIterator->second; parameterListBox->AppendString(STRING_TO_WXSTRING(ostr.str())); } return; } } } } catch (const std::exception &ex) { cout_ << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE(ex.what()); } catch (...) { cout_ << "CAUGHT ERROR AT " << __FILE__ << " : " << __LINE__ << endl; CREATE_PAUSE("AN UNKNOWN EXCEPTION HAS OCCURED!"); } }
pair<double,double> CoCheckersExperiment::playGame( shared_ptr<NEAT::GeneticIndividual> ind1, shared_ptr<NEAT::GeneticIndividual> ind2 ) { //You get 1 point just for entering the game, wahooo! pair<double,double> rewards(1.0,1.0); #if DEBUG_GAME_ANNOUNCER cout << "Playing game\n"; #endif populateSubstrate(ind1,0); populateSubstrate(ind2,1); uchar b[8][8]; //cout << "Playing games with HyperNEAT as black\n"; //for (handCodedType=0;handCodedType<5;handCodedType++) for (testCases=0;testCases<2;testCases++) { if (testCases==0) { individualBlack = ind1; individualWhite = ind2; } else //testCases==1 { individualBlack = ind2; individualWhite = ind1; } resetBoard(b); int retval=-1; int rounds=0; for (rounds=0;rounds<CHECKERS_MAX_ROUNDS&&retval==-1;rounds++) { //cout << "Round: " << rounds << endl; moveToMake = CheckersMove(); if (testCases==0) { currentSubstrateIndex=0; } else //testCases==1 { currentSubstrateIndex=1; } //cout << "Black is thinking...\n"; evaluatemax(b,CheckersNEATDatatype(INT_MAX/2),0,2); #if CHECKERS_EXPERIMENT_DEBUG cout << "BLACK MAKING MOVE\n"; printBoard(b); #endif if (moveToMake.from.x==255) { //black loses cout << "BLACK LOSES!\n"; retval = WHITE; } else { makeMove(moveToMake,b); retval = getWinner(b,WHITE); } #if CHECKERS_EXPERIMENT_LOG_EVALUATIONS memcpy(gameLog[rounds*2],b,sizeof(uchar)*8*8); #endif #if COCHECKERS_EXPERIMENT_DEBUG printBoard(b); CREATE_PAUSE(""); #endif if (retval==-1) { //printBoard(b); moveToMake = CheckersMove(); { //progress_timer t; if (testCases==0) { currentSubstrateIndex=1; } else //testCases==1 { currentSubstrateIndex=0; } //cout << "White is thinking...\n"; evaluatemin(b,CheckersNEATDatatype(INT_MAX/2),0,3); //cout << "SimpleCheckers time: "; } #if COCHECKERS_EXPERIMENT_DEBUG cout << "WHITE MAKING MOVE\n"; printBoard(b); #endif if (moveToMake.from.x==255) { //white loses cout << "WHITE LOSES BECAUSE THERE'S NO MOVES LEFT!\n"; retval = BLACK; #if COCHECKERS_EXPERIMENT_DEBUG printBoard(b); CREATE_PAUSE(""); #endif } else { makeMove(moveToMake,b); retval = getWinner(b,BLACK); } #if COCHECKERS_EXPERIMENT_DEBUG printBoard(b); CREATE_PAUSE(""); #endif } #if CHECKERS_EXPERIMENT_LOG_EVALUATIONS memcpy(gameLog[rounds*2+1],b,sizeof(uchar)*8*8); #endif } if (retval==BLACK) { #if DEBUG_GAME_ANNOUNCER cout << "BLACK WON!\n"; #endif if (ind1==individualBlack) { rewards.first += 800; rewards.first += (CHECKERS_MAX_ROUNDS-rounds); } else { rewards.second += 800; rewards.second += (CHECKERS_MAX_ROUNDS-rounds); } } else if (retval==-1) //draw { #if DEBUG_GAME_ANNOUNCER cout << "WE TIED!\n"; #endif //rewards.first += 200; //rewards.second += 200; } else //White wins { #if DEBUG_GAME_ANNOUNCER cout << "WHITE WON\n"; #endif if (ind1==individualWhite) { rewards.first += 800; rewards.first += (CHECKERS_MAX_ROUNDS-rounds); } else { rewards.second += 800; rewards.second += (CHECKERS_MAX_ROUNDS-rounds); } } int whiteMen,blackMen,whiteKings,blackKings; //countPieces(gi.board,whiteMen,blackMen,whiteKings,blackKings); countPieces(b,whiteMen,blackMen,whiteKings,blackKings); if (ind1==individualWhite) { rewards.first += (2 * (whiteMen) ); rewards.first += (3 * (whiteKings) ); rewards.second += (2 * (blackMen) ); rewards.second += (3 * (blackKings) ); } else { rewards.first += (2 * (blackMen) ); rewards.first += (3 * (blackKings) ); rewards.second += (2 * (whiteMen) ); rewards.second += (3 * (whiteKings) ); } } #if DEBUG_GAME_ANNOUNCER cout << "Fitness earned: " << rewards.first << " & " << rewards.second << endl; CREATE_PAUSE(""); #endif return rewards; }