// Compare solution with a randomly generated opponent, return whichever // has the higher fitness void hill_climb::binary_tournament(Random & rand, vector<bool> & solution, float & fitness, Evaluator& evaluator) { auto guess = rand_vector(rand, solution.size()); float guess_fitness = evaluator.evaluate(guess); if (fitness < guess_fitness) { solution = guess; fitness = guess_fitness; } }
bool TestDocument::evaluate(Evaluator &evaluator) { bool rval = true; XMLSize_t numpass = 0; XMLSize_t numfail = 0; std::map<char *, bool, ltcstr> test_state; XMLCh *TEST = xercesc::XMLString::transcode("test"); XMLCh *PREREQ = xercesc::XMLString::transcode("prereq"); xercesc::DOMElement *root = doc->dom->getDocument()->getDocumentElement(); xercesc::DOMNodeList *tests = root->getElementsByTagName(TEST); XMLSize_t numtests = tests->getLength(); std::cout << numtests << " tests found in " << doc->path << std::endl; for (int i = 0; i < numtests; i++) { xercesc::DOMElement *e = static_cast<xercesc::DOMElement*>(tests->item(i)); Test test(*this, e); bool testval = true; std::cout << "test " << (i+1) << " of " << numtests << ": " << test.id << "... "; if (e->hasAttribute(PREREQ)) { char *prereqs = xercesc::XMLString::transcode(e->getAttribute(PREREQ)); char *scan = prereqs; std::stringstream prereq; while (char c = *scan++) { if (c == ' ' || c == ',') { testval = __checkPrereq(test, test_state, prereq) && testval; prereq.str(""); } else { prereq << c; } } testval = __checkPrereq(test, test_state, prereq) && testval; xercesc::XMLString::release(&prereqs); } if (testval) { testval = evaluator.evaluate(*this, test) && testval; testval = js_evaluator.evaluate(*this, test) && testval; } if (testval) { std::cout << "[OK]"; numpass++; } else { std::cout << "[FAIL] (" << test.messages.str() << ")"; numfail++; } std::cout << std::endl; test_state[xercesc::XMLString::replicate(test.id)] = testval; rval = testval && rval; } for (std::map<char *, bool, ltcstr>::iterator i = test_state.begin(); i != test_state.end(); i++) { xercesc::XMLString::release((char**)&i->first); } xercesc::XMLString::release(&TEST); xercesc::XMLString::release(&PREREQ); std::cout << numpass << " of " << numtests << " tests passed, " << numfail << " failed (" << ( ((double)numpass) / ((double)numtests) * 100.0 ) << "%)" << std::endl; return rval; }
double backtracking_linear_search( Evaluator& evaluator, // class of objective function value evaluation double *xp, // backup place for x double *p, // negative of search direction double *fx, // current function value at x double c, // sufficient decrease condition threshold double init_step, // initial step length double r, // scale factor in backtracking int *evaluateCnt // counter ){ double *x=0; //current points int n=evaluator.get_current_parameter(x); double *g=evaluator.get_gradient_vec(); double dec=vec_dot(g,p,n); //cout<<"#IN_LINEAR_SEARCH unit decrease of g'p="<<dec<<endl; if(dec<0){ // non suitable step,p is not a descent search direction return -1; } // for(int i=0;i<5;i++) cout<<"x["<<i<<"]="<<x[i]<<" p["<<i<<"]="<<p[i]<<endl; double alpha=init_step; //vec_add(xp,x,p,n,1,-alpha); // p is the negative of search of direction //TODO: do it better for(int i=0;i<n;++i) xp[i]=(x[i]-alpha*p[i]<1e-6)?0:(x[i]-alpha*p[i]); double old_fx=*fx; *fx=evaluator.evaluate(xp,g); ++(*evaluateCnt); int trials=0; while( *fx > old_fx-alpha*c*dec ){ //cout<<"-----try step length "<<alpha<<" get obj="<<*fx<<" dec="<<old_fx-*fx<<" require min dec="<<alpha*c*dec<<endl; alpha*=r; //vec_add(xp,x,p,n,1,-alpha); //TODO: do it better for(int i=0;i<n;++i) xp[i]=(x[i]-alpha*p[i]<1e-6)?0:(x[i]-alpha*p[i]); *fx=evaluator.evaluate(xp,g); ++(*evaluateCnt); ++trials; } //cout<<"#IN_LINEAR_SEARCH success linear search, get alpha="<<alpha<<" obj="<<*fx<<" dec="<<old_fx-*fx<<" required min dec="<<alpha*c*dec<<" trails="<<trials<<endl; return alpha; }
// Attempt to flip each bit in a random order, accepting improvements // as they are found. Test each bit exactly once for improvement void hill_climb::once_each(Random & rand, vector<bool> & solution, float & fitness, Evaluator& evaluator) { vector<int> options(solution.size()); iota(options.begin(), options.end(), 0); float new_fitness; std::shuffle(options.begin(), options.end(), rand); for (const auto& index : options) { // flip the bit and evaluate new fitness solution[index] = not solution[index]; new_fitness = evaluator.evaluate(solution); if (fitness < new_fitness) { fitness = new_fitness; } else { solution[index] = not solution[index]; } } }
BitMoveOrderingIterator::BitMoveOrderingIterator( const Board& board, Evaluator<Board>& eval) : board_(board), sorted_next_() { std::vector<eval_t> sort_key; BitBoard::data_type moves = board.get_move_bit(); while (moves.any()) { BitBoard::data_type next = moves.get_next_bit(); unsigned pos = next.get_next(); BitBoard next_board(board); next_board.try_move(pos); eval_t v = eval.evaluate(next_board, BLACK); sort_insert(sort_key, sorted_next_, v, pos); moves ^= next; } current_ = sorted_next_.begin(); }
int main() { Evaluator eval; string input; cout<<"'exit' to quit"<<endl<<endl; while (true) { cout<<"Enter expression: "; std::getline(cin, input, '\n'); if (input == "exit") break; try { Object obj = eval.evaluate(input); cout<<obj.symbol()<<" = "<<obj.value().value()<<endl; } catch (Error error) { cout<<error.message<<endl; } } return 0; }
// Only modify the solution once you know which single bit change will // result in the best fitness improvement void hill_climb::steepest_ascent(Random & rand, vector<bool> & solution, float & fitness, Evaluator& evaluator) { float new_fitness; bool improved; // keep a list of which locations are tied for the most improvement vector<size_t> bests; // Stores which bit was flipped to create the current solution size_t previous = -1; // Keep looping until there is no single bit flip improvement do { improved = false; for (size_t working = 0; working < solution.size(); working++) { if (working != previous) { // flip the bit and determine the new fitness solution[working] = not solution[working]; new_fitness = evaluator.evaluate(solution); if (fitness <= new_fitness) { // strict improvements clear out the old list if (fitness < new_fitness) { fitness = new_fitness; improved = true; bests.clear(); } // Record the index of each solution that obtained the best fitness bests.push_back(working); } // revert the change solution[working] = not solution[working]; } } if (improved) { // choose a random gene from those resulting in an equally large improvement int index = std::uniform_int_distribution<int>(0, bests.size() - 1)(rand); previous = bests[index]; solution[previous] = not solution[previous]; bests.clear(); } } while (improved); }
// Iteratively tests bits in a random order, accepting improvements as they // are found. Tracks bits that have been tested since the last modification // to prevent waste. void hill_climb::first_improvement(Random & rand, vector<bool> & solution, float & fitness, Evaluator& evaluator) { // Set up data structure for random bit selection vector<int> options(solution.size()); iota(options.begin(), options.end(), 0); float new_fitness; bool improvement; // keep track of locations already tried since last improvement std::unordered_set<int> tried; // Keep looping until there is no single bit flip improvement do { improvement = false; // Test the bits in a random order std::shuffle(options.begin(), options.end(), rand); for (const auto& index : options) { // If this location has already been tried, skip to the next one if (tried.count(index) != 0) { continue; } // flip and evaluate the modification solution[index] = not solution[index]; new_fitness = evaluator.evaluate(solution); if (fitness < new_fitness) { // Keep change, update variables fitness = new_fitness; improvement = true; tried.clear(); } else { // Revert the change solution[index] = not solution[index]; } tried.insert(index); } } while (improvement); }
virtual int evaluate(char* buf, int maxbuf, Range range) const { assert(e != 0); return e->evaluate(buf, maxbuf); }
int main(int argc, char *args[]) { int n = 8; cerr << "argc:"<<argc<<endl; if(argc!=n){ hyperdatasetUsage(args[0]); exit(1); } int rands = 0; rands = atoi(args[1]); if(rands!=0) srand(rands); else{ rands = time(0); srand(rands); cout << "ny seed: " << rands << endl; } //1. load the example file NEATsettings * s = new NEATsettings(); ifstream ifs(args[2],ios::in); ifs>>s; ifs.close(); //2. generate the pop TransferFunctions * tfs = new TransferFunctions(s); Population * pop = makePopulation(args[3],s,tfs); //3. then the selector Selector * sel = makeSelector(args[4]); int g = atoi(args[5]); int iter = atoi(args[6]); string dfile = args[7]; DataSet * set = new DataSet(false,dfile,0.0); FitnessEvaluator * de = new DatasetHyperNEAT(s,tfs,set); Evaluator * ev = new Evaluator(de); LocalReproducer * rp = new LocalReproducer(); int gc = 0; double sum=0; double sum2=0; double sum3=0; Phenotype * cbest = NULL; Phenotype * sbest = NULL; Phenotype * best = NULL; int osize=pop->getMembers()->size(); int ss=0; double ocomp=0; time_t startt; double timesum = 0; for(int i2=0;i2<iter;i2++){ gc = 0; best = NULL; startt = time(0); for(int i=0;i<g;i++){ gc++; // cout << "before eval.." << endl; ev->evaluate(pop->getMembers(),pop->getMembers()->size()); // cout << "after eval.." << endl; if((unsigned int)pop->getOriginalSize()!=pop->getMembers()->size()) cout << "size not right after eval.." << endl; ss=pop->getSpecies()->size(); // cout << "before pop juggling.." << endl; pop->updateSpeciesStats(); pop->sortmembers(); pop->sortspecies(); cbest = pop->getCopyOfCurrentBest(); de->f(cbest); // cout << "before best juggling.." << endl; if(best==NULL){ best = cbest; }else if(cbest->getFitness()>best->getFitness()){ delete best; best = cbest; }else{ delete cbest; } // cout << "after best juggling.." << endl; cout << "gen: "<<i<<" best: " << best->getFitness() << "worst: " << pop->getMembers()->at(pop->getMembers()->size()-1)->getFitness() << endl; if(((DatasetHyperNEAT*)de)->done(best)){ i = g; // cout << best; ((DatasetHyperNEAT*)de)->runTest(best); } // cout << "after xor testing.." << endl; sel->select(pop,0); rp->reproduce(pop); // cout << "after select and reproduce.." << endl; } if(sbest==NULL) sbest = best; else if(best->getFitness()>sbest->getFitness()) sbest = best; time_t ft = time(0)-startt; timesum += ft; cout << "run: "<<i2+1<<" gc: "<<gc<<" maxfitness: " << pop->getHighestFitness() << " sbest fitness: " << sbest->getFitness() << " species: " << ss << " time: " << ft << " time/gen: " << (double)ft/(double)gc << endl; sum += gc; sum2 += best->getGenome()->extrons(); sum3 += best->getGenome()->countHidden(); if(i2!=iter){ if(pop->getOriginalSeed()!=NULL){ Genome * oseed = pop->getOriginalSeed(); int oelitism = pop->getOriginalInitialElitism(); ocomp = pop->getOcomp(); delete pop; pop = new Population(s,ocomp,tfs); // oseed->setTfs(pop->getTfs()); pop->genesis(oseed,osize,oelitism); }else{ pop->resetSpawn(); } } } ((DatasetHyperNEAT*)de)->runTest(sbest); cout << ((DatasetHyperNEAT*)de)->output(sbest); }