void apply(Ea& ea) { size_t k = Params::nslc::k; // merge the population and the archive in a single archive pop_t archive = _archive; archive.insert(archive.end(), ea.pop().begin(), ea.pop().end()); // compute the sparseness of each individual of the population // and potentially add some of them to the archive int added = 0; for (size_t i = 0; i < ea.pop().size(); ++i) { size_t nb_objs = ea.pop()[i]->fit().objs().size(); assert(nb_objs >= 2); // sort parallel::sort(archive.begin(), archive.end(), nslc::_compare_dist_p<phen_t>(ea.pop()[i])); // local competition score int rank = k; for (size_t j = 0; j < k; ++j) if (ea.pop()[i]->fit().value() < archive[j]->fit().value()) rank--; ea.pop()[i]->fit().set_obj(0, rank); // novelty double n = 0.0; for (size_t j = 0; j < k; ++j) n += ea.pop()[i]->fit().dist(*archive[j]); ea.pop()[i]->fit().set_obj(1, n); // add to the archive if (n > _rho_min || misc::rand<float>() < Params::nslc::add_to_archive_prob) { _archive.push_back(ea.pop()[i]); _not_added = 0; ++added; } else { ++_not_added; } } // end for all individuals // update rho_min if (_not_added > Params::nslc::stalled_tresh) { //2500 _rho_min *= 0.95; _not_added = 0; } if (_archive.size() > Params::nslc::k && added > Params::nslc::adding_tresh) {//4 _rho_min *= 1.05f; } std::cout<<"archive size:"<<_archive.size()<<std::endl; }
static void run_ea(int argc, char **argv, Ea& ea, const typename Ea::phen_t::fit_t& fit_proto, const boost::program_options::options_description& add_opts = boost::program_options::options_description(), bool init_rand = true) { ea.set_fit_proto(fit_proto); namespace po = boost::program_options; std::cout<<"sferes2 version: "<<VERSION<<std::endl; if (init_rand) { time_t t = time(0) + ::getpid(); std::cout<<"seed: " << t << std::endl; srand(t); } po::options_description desc("Allowed sferes2 options"); desc.add(add_opts); desc.add_options() ("help,h", "produce help message") ("stat,s", po::value<int>(), "statistic number") ("out,o", po::value<std::string>(), "output file") ("number,n", po::value<int>(), "number in stat") ("load,l", po::value<std::string>(), "load a result file") ("verbose,v", po::value<std::vector<std::string> >()->multitoken(), "verbose output, available default streams : all, ea, fit, phen, trace") ; po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); if (vm.count("help")) { std::cout << desc << std::endl; return; } if (vm.count("verbose")) { dbg::init(); std::vector<std::string> streams = vm["verbose"].as<std::vector<std::string> >(); attach_ostream(dbg::warning, std::cout); attach_ostream(dbg::error, std::cerr); attach_ostream(dbg::info, std::cout); bool all = std::find(streams.begin(), streams.end(), "all") != streams.end(); bool trace = std::find(streams.begin(), streams.end(), "trace") != streams.end(); if (all) { streams.push_back("ea"); streams.push_back("fit"); streams.push_back("phen"); streams.push_back("eval"); } BOOST_FOREACH(const std::string& s, streams) { dbg::enable(dbg::all, s.c_str(), true); dbg::attach_ostream(dbg::info, s.c_str(), std::cout); if (trace) dbg::attach_ostream(dbg::tracing, s.c_str(), std::cout); }
void apply(Ea& ea) { //std::cout<<"insidenov"<<std::endl; // parallel compute parallel::init(); parallel::p_for(parallel::range_t(0, ea.pop().size()), novelty::_parallel_behavior_nov<typename Ea::phen_t, archive_t, Params>(ea.pop(),_archive)); //std::cout<<"paralleldone"<<std::endl; //Update archive int obj_num = 1; int bestindiv = -1; float max_sparse = -1.0; for(size_t i = 0; i < ea.pop().size(); ++i) { float sparse = ea.pop()[i]->fit().obj(obj_num); if(max_sparse < sparse) { max_sparse = sparse; bestindiv = i; } } if(bestindiv!=-1) _archive.push_back(ea.pop()[bestindiv]->fit().pos_bd); }
void apply(Ea& ea) { // boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::max> > acc_max; // boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::min> > acc_min; SFERES_CONST size_t k = Params::novelty::k; // merge the population and the archive in a single archive pop_t archive = _archive; archive.insert(archive.end(), ea.pop().begin(), ea.pop().end()); // we compute all the distances from pop(i) to archive(j) and store them Eigen::MatrixXf distances(ea.pop().size(), archive.size()); novelty::_distance_f<Phen> f(ea.pop(), archive, distances); parallel::init(); parallel::p_for(parallel::range_t(0, ea.pop().size()), f); // compute the sparseness of each individual of the population // and potentially add to the archive int added = 0; for (size_t i = 0; i < ea.pop().size(); ++i) { size_t nb_objs = ea.pop()[i]->fit().objs().size(); Eigen::VectorXf vd = distances.row(i); // Get the k-nearest neighbors double n = 0.0; std::partial_sort(vd.data(), vd.data() + k, vd.data() + vd.size()); // Sum up the total of distances from k-nearest neighbors // This is the novelty score n = vd.head<k>().sum() / k; // Set the novelty score to the last objective in the array of objectives ea.pop()[i]->fit().set_obj(nb_objs - 1, n); // std::cout << "rho = " << _rho_min << "; n = " << n << "\n"; // acc_max(n); // acc_min(n); // Check if this individual is already in the archive if (!in_archive_already(ea.pop()[i]->id()) && (n > _rho_min // Check if the novelty score passes the threshold || misc::rand<float>() < Params::novelty::add_to_archive_prob)) { // Add this individual to the permanent archive _archive.push_back(ea.pop()[i]); // Print this indiv just added // std::cout << "----> n = " << n << "; label = " << ea.pop()[i]->fit().label() << "; score = " << ea.pop()[i]->fit().score() << "; id = " << ea.pop()[i]->id() <<"\n"; // std::cout << "----> n = " << n << "; label = " << "; id = " << ea.pop()[i]->id() <<"\n"; _not_added = 0; ++added; } else { // Do not add this individual to the archive ++_not_added; } } // end for all individuals // update rho_min if (_not_added > Params::novelty::stalled_tresh)//2500 { _rho_min *= 0.95; // Lower rho_min _not_added = 0; // After lowering the stalled threshold, we need to reset the _not_added count } if (_archive.size() > Params::novelty::k && added > Params::novelty::adding_tresh)//4 { _rho_min *= 1.05f; // Increase rho_min } std::cout<<"a size:"<<_archive.size()<<std::endl; // DEBUG // std::cout << "a size:"<<_archive.size() // << "; max: " << boost::accumulators::max(acc_max) // << "; min: " << boost::accumulators::min(acc_min) // << std::endl; }