📄 ant.cc
字号:
// ant.cc File to demonstrate ant problem, build with GPQUICK-2.1// (for version intergrated with gp.cc)// W.B.Langdon cs.bham.ac.uk 7 Feb 1997 $Revision: 1.5 $//Modifications (in reverse order)//WBL 19 Apr 97 For Easter 1997 release//WBL 7 Feb 97 New file #include <assert.h>#include "pch.h"#include "chrome.h"#include "primitiv.h"#include "prob.h"#ifdef TRACE_RUN#include "trace.h"#endif#ifdef TIMINGS//#include "time.h"#endif#include "gp.h"#include "test.h"//not used but keeps loader happyint num_sol_found = 0;//used to report solution to each test phase found//**************************************************************************float display_run(Chrome* chrome) { cout << "\nExecuting "<< chrome << flush; chrome->write(PRETTY_NONE); cout<<endl; chrome->SetupEval(); // Display the best// ofstream of("RESULT.TXT",ios::out | ios::app);// cout << "\n\nFINAL RESULTS...";// cout << "\nAfter " << pop->gencount << " generates, #food found = " << pop->BestFitness->fvalue << "\n";// pop->pop[pop->BestMember]->write(PRETTY_NONE,cout);// cout.flush();// cout<<"****";pop->pop[pop->BestMember]->SetupEval(); int x,y,stop; float f = 0; // Get the track of the best ant CopyGrid(); Reset(); retval answer=0;#ifdef TIMINGS int evals = 0; const int start_energy = energy;#endif while ( energy > 0 ) {#ifdef TIMINGS evals++;#endif#ifdef MULTREE answer+= chrome->evalAll(0);#else answer+= chrome->evalAll();#endif // Show the track f=answer; cout << '\n'; cout << "Food eaten "<<answer <<" energy left "<<energy<<endl;// of << '\n'; for (y=0; y<Grid_Vertical; y++) { for (x=0; x<Grid_Horizontal; x++) { cout<<Grid[x][y];// of<<Grid[x][y]; } cout<<"\n";// of<<"\n"; } } cout<< "\nLegend: (8)=Uneaten Food (+)=Eaten food (-)=Tracks (*)=Multiple Tracks";// cout<<"\nFitness="<<f << " After " << pop->gencount << " generates";// of<<"\nFitness="<<f << " After " << pop->gencount << " generates";// cout<<"\n";pop->pop[pop->BestMember]->write(PRETTY_NONE,cout);// of<<"\n";pop->pop[pop->BestMember]->write(PRETTY_NONE,of);// cout << "\nResult in RESULT.TXT";// of.close(); chrome->nfitness->fvalue = f;#ifdef TIMINGS ptrmyfitnessvalue(chrome->nfitness)->cpu_time = float(start_energy - energy) / float(evals);#endif cout << "\nReturns " << chrome->nfitness->fvalue;#ifdef TIMINGS cout << " energy per evaluation " << ptrmyfitnessvalue(chrome->nfitness)->cpu_time;#endif cout << endl; return chrome->nfitness->fvalue;}int current_phase = 0; int tests_run = 0;int tests_apparently_run = 0;int total_cpu = 0;test::test(Problem* probl, istream& istr) {}void test::write_stats(ostream& out) const{out<<"Ran " << tests_run << " trees. Should have run " << tests_apparently_run <<" ratio "<< float(tests_apparently_run)/float(tests_run) <<" Instructions " << total_cpu << endl;}//end write_statsvoid write_problem_params(){//cout<<store_limit<<"["<<Array_bot<<".."<<Array_top<<"] ";//cout<<mem_penalty_bot<<" " cout << "max fitness "<<max_fitness;#ifdef TIMINGS// cout<<" "<<cpu_penalty_bot<<;#endif cout<<endl;}// Define your FitnessValue classvoid myfitnessvalue::write(ostream& fout){ fout << fvalue;#ifdef MINC_ADF fout << " adf1ver " << adf1;#endif#ifdef TIMINGS fout << " cpu " << cpu_time;#endif#ifdef TRACE_RUN fout << " ("; int notrun[NUM_TREES]; missed(this, notrun); for (i=0; i < NUM_TREES; i++) { char buf[80]; sprintf(buf,"%4d", ok(i)); fout << buf; {if(Tree_OK(this,i)) fout<<"K"; else fout<<"-";} sprintf(buf,"%-4d",nak(i)); fout << buf; sprintf(buf,"%-4d",notrun[i]); fout << buf; } fout << ")";#endif}//end myfitnessvalue::writevoid cache_init(){}void cache_stats_init(){}void cache_stats(){}//**************************************************************************///////////////////////// PROBLEM definitionvoid gp::LoadTests(istream& in){ }void gp::WriteTreeName(int tree, ostream& ostr){ostr<< "ant";}//end queue::WriteTreeName()BOOL gp::TreeNameMatch(int tree, char* s){return 0; //any string will do}//end gp::TreeNameMatch()int static_check_loop_check = 0;int gp::static_check(Chrome* chrome, int tree)// Perform static analysis of new chromsome. Just changed in tree{return 0; //Uncomment to disable static_check//ie ok}//end queue::static_checkvoid gp::write_stats(ostream& out){}retval random_value(int& seed) //seed must be > 0{//return retval(F_tan_spread*tan(float(intrnd(seed) % 3141)/1000.0));return retval(intrnd(seed)); //default -- not used}void myfitnessvalue::clear(){//NB clearing whole of myfitnessclass cause gcc produced code to crash//In theory dont need to do this most of the time since on copy or//crossover will soon be overwritten with data from parent//cout<<"Sizeof myfitnessvalue "<<sizeof(*this)<<endl;}int mincpu_found; //lowest cpu used by any solution reported so farint end_gens(){ //we never move on to next test phase return 0;}//end_gensint Chrome::ChooseCrossTree(Chrome* second_parent) {//not actually in usereturn rnd(NUM_TREES);} //end Chrome::ChooseCrossTree#ifdef MAXTREEDEPTHvoid gp::ProbLPStats(int f[][NUM_TREES][MAXTREEDEPTH+1], int pcount[pcount_size] ) #elsevoid gp::ProbLPStats(int f[][NUM_TREES], int pcount[pcount_size] ) #endif /*MAXTREEDEPTH*/{pcount[0] += 1;//not usedpcount[1] += 1;//not usedpcount[2] += 1;//not usedpcount[3] += 1;//not used}//end ProbLPStatsint gp::ProbLTStat(int tree, int start, int length, node* expr ){return -1; //ignore}//end ProbLTStat
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -