Beispiel #1
0
extern void swGPUParamSearch(Chain* rowChainL, Chain* columnChainL, 
    SWPrefs* swPrefsL) {

    SWResult* swResult = 
        swDataGetResult(swSolveGPU(rowChainL, columnChainL, swPrefsL), 0);

    rowChain = rowChainL;
    columnChain = columnChainL;
    swPrefs = swPrefsL;
    columns = chainGetLength(columnChainL);
    expectedResult = swResultGetScore(swResult);

    srand(time(NULL));

    Cromosome child;

    initPopulation();
    evaluatePopulation();

    int generationIdx;
    int childIdx;

    for (generationIdx = 0; generationIdx < GENERATION_NMR; ++generationIdx) {

        sortCromosomes(population, 0, POPULATION_NMR - 1);

        printf(".............................................\n");
        printf("Generation: %d\n", generationIdx);

        for (childIdx = 0; childIdx < CHILD_NMR; ++childIdx) {

            child = crossover(selection());
            mutation(&child);

            children[childIdx] = child;
        }

        evaluateChildren();

        for (childIdx = 0; childIdx < CHILD_NMR; ++childIdx) {
            population[POPULATION_NMR - childIdx - 1] = children[childIdx];
        }
    }
}
    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 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
  }
Beispiel #4
0
void run(int iRun)
{
	double t_ini, t_fin;
	double secs;

	char evalsal[50];//={"results/Evaluations_"};
	char timesal[50];//={"results/Time_"};
	char sizesal[50];//={"results/Size_"};
	char funsal[50];//={"results/FUN_"};
	char varsal[50];//={"results/VAR_"};
	int evaluations;

	numRest = 0;
	cycle = 0;
	gen = 0;

	sprintf(evalsal,"results/Evaluations_%s_%d",problemInstance,D);
	sprintf(timesal,"results/Time_%s_%d",problemInstance,D);
	sprintf(sizesal,"results/Size_%s_%d",problemInstance,D);
	sprintf(funsal,"results/FUN_%s_%d_%d",problemInstance,D,iRun);
	sprintf(varsal,"results/VAR_%s_%d_%d",problemInstance,D,iRun);

	if(mpi_rank==0)
		t_ini=clock();

	randomize();    
	initializeProblem(problemInstance);    
	initializePopulation();
	synchronize_x_sub_all();
	evaluateSpeciesInitial();

	int i;
	while(cycle<Cycles){
		cycle++;
// 		if(mpi_rank==0)
// 			printf("Cycle:\t%d\n",cycle);

		gen = 0;
		while(gen<Gmax){
			gen++;
			ED_rand_1_bin();
			//species parallel, results differ from original
			evaluatePopulation();
			addSolution();
			selection();
			synchronize_x_sub_all();
		}
	}

	evaluations = cycle * Gmax * numSpecies * NP;

	generateSolutions();	

	if(mpi_rank==0)
	{
		fptx = fopen(funsal,"w");     

		exportNonDominatedPopulationObjetivesValues(finalFitness,sizeSol,fptx);    
		fptv = fopen(varsal,"w");
		exportNonDominatedPopulationSolutionValues(finalSolutions,sizeSol,fptv);

		fptu = fopen(evalsal,"a");
		fprintf(fptu, "%d\n",evaluations);

		fpts = fopen(sizesal,"a");
		fprintf(fpts, "%d\n",sizeSolND);

		if(mpi_rank==0)
			t_fin=clock();

		secs = (t_fin-t_ini)/CLOCKS_PER_SEC;

		fptt = fopen(timesal,"a");
		fprintf(fptt,"%.16g \n", secs);

		fclose(fptx);
		fclose(fptu);
		fclose(fptv);
		fclose(fptt);
		fclose(fpts);
	}
	MPI_Barrier(MPI_COMM_WORLD);
	return;
}