コード例 #1
0
ファイル: EntityRecipe.cpp プロジェクト: angkorcn/ember
void EntityRecipe::associateBindings()
{
	S_LOG_VERBOSE("Associating bindings.");
	if (mEntitySpec) {
		// Iterate over all entity spec XML nodes
		EntityRecipe::SpecIterator iter(this);
		TiXmlElement *elem = mEntitySpec->FirstChildElement("atlas");
		if (elem) {
			elem->Accept(&iter);
		}
	}
}
コード例 #2
0
ファイル: XmlForLua.cpp プロジェクト: AllenWangxiao/winner
	/*** XmlForLua ***/
	int XmlForLua::_Encode(lua_State* L){
		// prepare name
		if((lua_gettop(L)<1) || (!lua_istable(L, 1))){
			lua_pushnil(L);
			lua_pushstring(L, "invalid arg");
			return 2;
		}
		// parse
		TiXmlPrinter printer;
		printer.SetIndent("\t");
		TiXmlElement* ele =lua_to_xml(L, "root");
		if(ele->Accept(&printer) && printer.CStr()){
			lua_pushstring(L, printer.CStr());
			delete ele;
			return 1;
		}
		else{
			lua_pushnil(L);
			lua_pushstring(L, "tinyxml unknown error");
			delete ele;
			return 2;
		}
	}
コード例 #3
0
    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
    }
コード例 #4
0
    void ExperimentRun::finishEvaluations()
    {
        cout << "Cleaning up...\n";
        //int generationDumpModulo = int(NEAT::Globals::getSingleton()->getParameterValue("GenerationDumpModulo"));
        if (cleanup)
            population->cleanupOld(INT_MAX/2);

        cout << "Adjusting fitness...\n";
        population->adjustFitness();

        population->getGeneration()->setIDs();
        if((NEAT::Globals::getSingleton()->getParameterValue("DumpAll",0.0) > 0.5) || (population->getGenerationCount() % 50 == 0) ) {
            cout << "Dumping individuals...\n";
            population->dump(outputFileName/*+string(".backup.xml")*/,true,true);
            
            TiXmlElement *root = new TiXmlElement("Genetics");
		    NEAT::Globals::getSingleton()->dump(root);
		    TiXmlPrinter rootPrinter;
			root->Accept( &rootPrinter );
		    string s = rootPrinter.CStr();
		    string rootFileName = outputFileName + string("-root.backup.xml");
		    ofstream out( rootFileName.c_str() );
		    out << s.substr(0, s.length() - 3) << ">" <<endl;
		    out.close();

        } else {
            cout << "Dumping best individuals...\n";
            //population->dumpBest(outputFileName+string(".backup.xml"),true,true);
            population->dumpBest(outputFileName/*+string(".backup.xml")*/,true,true);

            //population->cleanupOld(25);
            //population->dumpBest("out/dumpBestWithGenes(backup).xml",true);
        }
        cout << "Done Dumping\n";

#ifndef HCUBE_NOGUI
        if (frame)
        {
            frame->updateNumGenerations(population->getGenerationCount());
        }
#endif

        cout << "Resetting generation data...\n";
        shared_ptr<NEAT::GeneticGeneration> generation = population->getGeneration();
        experiments[0]->resetGenerationData(generation);

        for (int a=0;a<population->getIndividualCount();a++)
        {
            //cout << __FILE__ << ":" << __LINE__ << endl;
            experiments[0]->addGenerationData(generation,population->getIndividual(a));
        }

        /*
        if(experimentType == EXPERIMENT_TIC_TAC_TOE_GAME)
        {
        //Take the best individual and run him a lot

        ((TicTacToeGameExperiment*)experiment)->setNumGames(20000);
        ((TicTacToeGameExperiment*)experiment)->setNumGames(100);
        }
        */
        
    }
コード例 #5
0
ファイル: url_api.cpp プロジェクト: caicry/wxUrlRefresh
int CProxyParse::RunFromMem( wxString content )
{
	char *pBuffer;
	//http://www.51proxied.com/http_non_anonymous.html
	//wxString path = wxT("f:/work/windows/wxUrlRefresh/data/最新透明HTTP代理服务器.htm");
	//wxString path1 = wxT("f:/work/windows/wxUrlRefresh/data/result.xml");

	wxString data_path = wxGetCwd() + "/data/";
	wxString path1 = data_path + "_tmp.xml";

	if (!wxDirExists(data_path))
		wxMkdir(data_path);

	pBuffer = (char*)calloc(content.Length()+1, 1);
	wxStrncpy(pBuffer, content, content.Len()+1);


	wxLogMessage("Run Tidy!");
	TidyBuffer output;
	TidyBuffer errbuf;
	int rc = -1;
	Bool ok;
	TidyDoc tdoc = tidyCreate();                     // Initialize "document"
	tidyBufInit( &output );
	tidyBufInit( &errbuf );
	//printf( "Tidying:\t\%s\\n", input );
	tidySetCharEncoding(tdoc, "utf8");
	ok = tidyOptSetBool( tdoc, TidyXhtmlOut, yes );  // Convert to XHTML
	if ( ok )
		rc = tidySetErrorBuffer( tdoc, &errbuf );      // Capture diagnostics
	if ( rc >= 0 )
		rc = tidyParseString( tdoc, pBuffer );           // Parse the input
	if ( rc >= 0 )
		rc = tidyCleanAndRepair( tdoc );               // Tidy it up!
	if ( rc >= 0 )
		rc = tidyRunDiagnostics( tdoc );               // Kvetch
	if ( rc > 1 )                                    // If error, force output.
		rc = ( tidyOptSetBool(tdoc, TidyForceOutput, yes) ? rc : -1 );
	if ( rc >= 0 )
		rc = tidySaveBuffer( tdoc, &output );          // Pretty Print
	if ( rc >= 0 )
	{
#ifdef _DEBUG
		//if ( rc > 0 )
		//	WriteAllToFile("f:/work/windows/wxUrlRefresh/data/error.xml", (char*)errbuf.bp, errbuf.size);
		WriteAllToFile(path1, (char*)output.bp, output.size);
#endif

	}
	else
		wxLogError("tidyFail");

	tidyBufFree( &output );
	tidyBufFree( &errbuf );
	tidyRelease( tdoc );
	if (pBuffer) free(pBuffer);


	wxLogMessage("Fetch data!");
	// 解析数据
	TiXmlDocument doc(path1);
	if (doc.LoadFile()) 
	{
		// root
		CTiXmlProxyVistor vistor(&m_array);
		TiXmlElement *pRoot = doc.RootElement();
		pRoot->Accept(&vistor);
	}
	else
	{
		wxLogMessage("shit");
		return -2;
	}
	return 0;
}