int main(int ac, char** av) { eoParser parser(ac, av); unsigned int popSize = parser.getORcreateParam((unsigned int)100, "popSize", "Population Size", 'P', "Evolution Engine").value(); unsigned int dimSize = parser.getORcreateParam((unsigned int)10, "dimSize", "Dimension Size", 'd', "Evolution Engine").value(); uint32_t seedParam = parser.getORcreateParam((uint32_t)0, "seed", "Random number seed", 0).value(); if (seedParam == 0) { seedParam = time(0); } make_parallel(parser); make_help(parser); rng.reseed( seedParam ); eoUniformGenerator< double > gen(-5, 5); eoInitFixedLength< EOT > init( dimSize, gen ); eoEvalFuncPtr< EOT, double, const std::vector< double >& > mainEval( real_value ); eoEvalFuncCounter< EOT > eval( mainEval ); eoPop< EOT > pop( popSize, init ); //apply< EOT >( eval, pop ); eoPopLoopEval< EOT > popEval( eval ); popEval( pop, pop ); eo::log << eo::quiet << "DONE!" << std::endl; #ifdef ENABLE_OPENMP #pragma omp parallel { if ( 0 == omp_get_thread_num() ) //Warning: omp_get_thread_num doestn't work with pragma and required openMP { eo::log << "num of threads: " << omp_get_num_threads() << std::endl; //Warning: omp_get_num_threads doestn't work with pragma and required openMP } } return 0; #endif }
int main ( int argc, char* argv[] ) { // WALLOCK TIME COUNTER eo::mpi::Node::init( argc, argv ); time_t time_start = std::time(NULL); /************** * PARAMETERS * **************/ // EO eoParser parser(argc, argv); make_verbose(parser); make_parallel(parser); eoState state; // log some EO parameters eo::log << eo::logging << "Parameters:" << std::endl; eo::log << eo::logging << FORMAT_LEFT_FILL_W_PARAM << "verbose" << eo::log.getLevelSelected() << std::endl; eo::log << eo::logging << FORMAT_LEFT_FILL_W_PARAM << "parallelize-loop" << eo::parallel.isEnabled() << std::endl; eo::log << eo::logging << FORMAT_LEFT_FILL_W_PARAM << "parallelize-dynamic" << eo::parallel.isDynamic() << std::endl; eo::log << eo::logging << FORMAT_LEFT_FILL_W_PARAM << "parallelize-prefix" << eo::parallel.prefix() << std::endl; eo::log << eo::logging << FORMAT_LEFT_FILL_W_PARAM << "parallelize-nthreads" << eo::parallel.nthreads() << std::endl; // GENERAL PARAMETERS // createParam (ValueType _defaultValue, std::string _longName, std::string _description, char _shortHand=0, std::string _section="", bool _required=false) std::string domain = parser.createParam( (std::string)"domain-zeno-time.pddl", "domain", "PDDL domain file", 'D', "Problem", true ).value(); eo::log << eo::logging << FORMAT_LEFT_FILL_W_PARAM << "domain" << domain << std::endl; std::string instance = parser.createParam( (std::string)"zeno10.pddl", "instance", "PDDL instance file", 'I', "Problem", true ).value(); eo::log << eo::logging << FORMAT_LEFT_FILL_W_PARAM << "instance" << instance << std::endl; std::string plan_file = parser.createParam( (std::string)"plan.soln", "plan-file", "Plan file backup", 'F', "Misc" ).value(); eo::log << eo::logging << FORMAT_LEFT_FILL_W_PARAM << "plan-file" << plan_file << std::endl; // pop size unsigned int pop_size = parser.createParam( (unsigned int)100, "popSize", "Population Size", 'P', "Evolution Engine").value(); eo::log << eo::logging << FORMAT_LEFT_FILL_W_PARAM << "pop_size" << pop_size << std::endl; // multi-start unsigned int maxruns = parser.createParam( (unsigned int)0, "runs-max", "Maximum number of runs, if x==0: unlimited multi-starts, if x>1: will do <x> multi-start", 'r', "Stopping criterions" ).value(); eo::log << eo::logging << FORMAT_LEFT_FILL_W_PARAM << "maxruns" << maxruns << std::endl; // b_max estimation bool insemination = parser.createParam(true, "insemination", "Use the insemination heuristic to estimate b_max at init", '\0', "Initialization").value(); // seed eoValueParam<unsigned int> & param_seed = parser.createParam( (unsigned int)0, "seed", "Random number seed", 'S' ); // if one want to initialize on current time if ( param_seed.value() == 0) { // change the parameter itself, that will be dumped in the status file // param_seed.value( time(0) ); param_seed.value()=time(0); // EO compatibility fixed by CC on 2010.12.24 } unsigned int seed = param_seed.value(); rng.reseed( seed ); eo::log << eo::logging << FORMAT_LEFT_FILL_W_PARAM << "seed" << seed << std::endl; // Parameters makers daex::do_make_eval_param( parser ); daex::do_make_init_param( parser ); daex::do_make_variation_param( parser, pop_size); daex::do_make_checkpoint_param( parser ); daex::do_make_replace_param( parser ); // special case of stopping criteria parameters daex::do_make_continue_param( parser ) ; // Those parameters are needed during restarts (see below) unsigned int mingen = parser.valueOf<unsigned int>("gen-min"); unsigned int steadygen = parser.valueOf<unsigned int>("gen-steady"); unsigned int maxgens = parser.valueOf<unsigned int>("gen-max"); make_help( parser ); /*********** * PARSING * ***********/ // PDDL #ifndef NDEBUG eo::log << eo::progress << "Load the instance..." << std::endl; eo::log.flush(); #endif daex::pddlLoad pddl( domain, instance, SOLVER_YAHSP, HEURISTIC_H1, eo::parallel.nthreads(), std::vector<std::string>()); #ifndef NDEBUG eo::log << eo::progress << "Load the instance...OK" << std::endl; eo::log << eo::progress << "Initialization..."; eo::log.flush(); #endif /****************** * INITIALIZATION * ******************/ daex::Init<daex::Decomposition>& init = daex::do_make_init_op<daex::Decomposition>( parser, state, pddl ); daex::Goal::atoms( & pddl.atoms() ); // randomly generate the population with the init operator eoPop<daex::Decomposition> pop = eoPop<daex::Decomposition>( pop_size, init ); // eoPop<daex::Decomposition> pop = eoPop<daex::Decomposition>( 10, init ); // used to pass the eval count through the several eoEvalFuncCounter evaluators unsigned int eval_count = 0; TimeVal best_makespan = MAXTIME; #ifndef SINGLE_EVAL_ITER_DUMP std::string dump_sep = "."; unsigned int dump_file_count = 1; std::string metadata = "domain " + domain + "\n" + IPC_PLAN_COMMENT + "instance " + instance; #endif unsigned int b_max_fixed = parser.valueOf<unsigned int>("bmax-fixed"); if( b_max_fixed == 0 ) { // b_max_fixed = 2048; // 128 b_max_fixed = daex::estimate_bmax_insemination( parser, pddl, pop, init.l_max() ); } unsigned b_max_in = b_max_fixed; double b_max_last_weight = parser.valueOf<double>("bmax-last-weight"); unsigned int b_max_last = static_cast<unsigned int>( std::floor( b_max_in * b_max_last_weight ) ); #ifndef NDEBUG eo::log << eo::logging << std::endl << "\tb_max for intermediate goals, b_max_in: " << b_max_in << std::endl; eo::log << eo::logging << "\tb_max for final goal, b_max_last: " << b_max_last << std::endl; #endif /************** * EVALUATION * **************/ #ifndef NDEBUG eo::log << eo::progress << "Creating evaluators..."; eo::log.flush(); #endif // do_make_eval returns a pair: the evaluator instance // and a pointer on a func counter that may be null of we are in release mode std::pair< eoEvalFunc<daex::Decomposition>&, eoEvalFuncCounter<daex::Decomposition>* > eval_pair = daex::do_make_eval_op<daex::Decomposition>( parser, state, init.l_max(), eval_count, b_max_in, b_max_last, plan_file, best_makespan, dump_sep, dump_file_count, metadata ); eoEvalFunc<daex::Decomposition>& eval = eval_pair.first; #ifndef NDEBUG // in debug mode, we should have a func counter assert( eval_pair.second != NULL ); eoEvalFuncCounter<daex::Decomposition>& eval_counter = * eval_pair.second; eo::log << eo::progress << "OK" << std::endl; eo::log << eo::progress << "Evaluating the first population..."; eo::log.flush(); #endif // a first evaluation of generated pop eoPopLoopEval<daex::Decomposition> pop_eval( eval ); pop_eval( pop, pop ); #ifndef NDEBUG eo::log << eo::progress << "OK" << std::endl; #endif /******************** * EVOLUTION ENGINE * ********************/ #ifndef NDEBUG eo::log << eo::progress << "Algorithm instanciation..."; eo::log.flush(); #endif // STOPPING CRITERIA eoCombinedContinue<daex::Decomposition> continuator = daex::do_make_continue_op<daex::Decomposition>( parser, state ); // Direct access to continuators are needed during restarts (see below) eoSteadyFitContinue<daex::Decomposition> & steadyfit = *( dynamic_cast<eoSteadyFitContinue<daex::Decomposition>* >( continuator[0] ) ); eoGenContinue<daex::Decomposition> & maxgen = *( dynamic_cast< eoGenContinue<daex::Decomposition>* >( continuator[1] ) ); // CHECKPOINTING eoCheckPoint<daex::Decomposition> & checkpoint = daex::do_make_checkpoint_op( continuator, parser, state, pop #ifndef NDEBUG , eval_counter #endif ); // SELECTION AND VARIATION // daex::MutationDelGoal<daex::Decomposition> delgoal; // FIXME Erreur de segmentation ici : delgoal devrait être un pointeur alloué. daex::MutationDelGoal<daex::Decomposition>* delgoal = new daex::MutationDelGoal<daex::Decomposition>; eoGeneralBreeder<daex::Decomposition> & breed = daex::do_make_variation_op<daex::Decomposition>( parser, state, pddl, delgoal ); // REPLACEMENT eoReplacement<daex::Decomposition> & replacor = daex::do_make_replace_op<daex::Decomposition>( parser, state ); unsigned int offsprings = parser.valueOf<unsigned int>("offsprings"); // ALGORITHM eoEasyEA<daex::Decomposition> dae( checkpoint, eval, breed, replacor, offsprings ); #ifndef NDEBUG eo::log << eo::progress << "OK" << std::endl; //eo::log << eo::progress << "Note: dual fitness is printed as two numbers: a value followed by a boolean (0=unfeasible, 1=feasible)" << std::endl; eo::log.flush(); eo::log << eo::debug << "Legend: \n\t- already valid, no eval\n\tx plan not found\n\t* plan found\n\ta add atom\n\tA add goal\n\td delete atom\n\tD delete goal\n\tC crossover" << std::endl; #endif /******************** * MULTI-START RUNS * ********************/ // best decomposition of all the runs, in case of multi-start // start at the best element of the init daex::Decomposition best = pop.best_element(); std::cout << "[Bench] Beginning serialization..." << std::endl; daex::Decomposition read; // Serializes in json int benchNb = 10; std::stringstream ss; eoserial::Object* jsonBest; time_t before_serialize = time(0); for (int i = 0; i < benchNb; ++i) { jsonBest = best.pack(); ss.clear(); ss.str(std::string()); jsonBest->print( ss ); delete jsonBest; } // std::cout<< "\n\n" << jsonBest << std::endl; time_t after_serialize = time(0); std::cout << "[Bench] Serialization took " << after_serialize - before_serialize << " seconds." << std::endl; std::cout<< "\n\nParsing..." << std::endl; time_t before_deserialize = time(0); for (int i = 0; i < benchNb; ++i) { eoserial::Object* received = eoserial::Parser::parse( ss.str() ); // Deserializes from json read.unpack( received ); delete received; } time_t after_deserialize = time(0); // std::cout<<"\n\nParsing finished, this is what I read :" << std::endl; // std::cout << received << std::endl; std::cout << "[Bench] Deserialization took " << after_deserialize - before_deserialize << " seconds." << std::endl; std::cout << "\n\nEnd of main, test is finished.\n" << std::endl; }
int main (int argc, char *argv[]) { /************************* * Initialisation de MPI * *************************/ boost::mpi::environment env(argc, argv, MPI_THREAD_MULTIPLE, true); boost::mpi::communicator world; /**************************** * Il faut au moins 4 nœuds * ****************************/ const size_t ALL = world.size(); const size_t RANK = world.rank(); /************************ * Initialisation de EO * ************************/ eoParser parser(argc, argv); eoState state; // keeps all things allocated dim::core::State state_dim; // keeps all things allocated /***************************** * Definition des paramètres * *****************************/ bool sync = parser.createParam(bool(true), "sync", "sync", 0, "Islands Model").value(); bool smp = parser.createParam(bool(true), "smp", "smp", 0, "Islands Model").value(); unsigned nislands = parser.createParam(unsigned(4), "nislands", "Number of islands (see --smp)", 0, "Islands Model").value(); // a double alphaP = parser.createParam(double(0.2), "alpha", "Alpha Probability", 'a', "Islands Model").value(); double alphaF = parser.createParam(double(0.01), "alphaF", "Alpha Fitness", 'A', "Islands Model").value(); // b double betaP = parser.createParam(double(0.01), "beta", "Beta Probability", 'b', "Islands Model").value(); // d double probaSame = parser.createParam(double(100./(smp ? nislands : ALL)), "probaSame", "Probability for an individual to stay in the same island", 'd', "Islands Model").value(); // I bool initG = parser.createParam(bool(true), "initG", "initG", 'I', "Islands Model").value(); bool update = parser.createParam(bool(true), "update", "update", 'U', "Islands Model").value(); bool feedback = parser.createParam(bool(true), "feedback", "feedback", 'F', "Islands Model").value(); bool migrate = parser.createParam(bool(true), "migrate", "migrate", 'M', "Islands Model").value(); unsigned nmigrations = parser.createParam(unsigned(1), "nmigrations", "Number of migrations to do at each generation (0=all individuals are migrated)", 0, "Islands Model").value(); unsigned stepTimer = parser.createParam(unsigned(1000), "stepTimer", "stepTimer", 0, "Islands Model").value(); bool deltaUpdate = parser.createParam(bool(true), "deltaUpdate", "deltaUpdate", 0, "Islands Model").value(); bool deltaFeedback = parser.createParam(bool(true), "deltaFeedback", "deltaFeedback", 0, "Islands Model").value(); double sensitivity = 1 / parser.createParam(double(1.), "sensitivity", "sensitivity of delta{t} (1/sensitivity)", 0, "Islands Model").value(); std::string rewardStrategy = parser.createParam(std::string("avg"), "rewardStrategy", "Strategy of rewarding: best or avg", 0, "Islands Model").value(); std::vector<double> rewards(smp ? nislands : ALL, 1.); std::vector<double> timeouts(smp ? nislands : ALL, 1.); for (size_t i = 0; i < (smp ? nislands : ALL); ++i) { std::ostringstream ss; ss << "reward" << i; rewards[i] = parser.createParam(double(1.), ss.str(), ss.str(), 0, "Islands Model").value(); ss.str(""); ss << "timeout" << i; timeouts[i] = parser.createParam(double(1.), ss.str(), ss.str(), 0, "Islands Model").value(); } /********************************* * Déclaration des composants EO * *********************************/ unsigned chromSize = parser.getORcreateParam(unsigned(0), "chromSize", "The length of the bitstrings", 'n',"Problem").value(); eoInit<EOT>& init = dim::do_make::genotype(parser, state, EOT(), 0); eoEvalFunc<EOT>* ptEval = NULL; ptEval = new SimulatedEval( rewards[RANK] ); state.storeFunctor(ptEval); eoEvalFuncCounter<EOT> eval(*ptEval); unsigned popSize = parser.getORcreateParam(unsigned(100), "popSize", "Population Size", 'P', "Evolution Engine").value(); dim::core::Pop<EOT>& pop = dim::do_make::detail::pop(parser, state, init); double targetFitness = parser.getORcreateParam(double(1000), "targetFitness", "Stop when fitness reaches",'T', "Stopping criterion").value(); unsigned maxGen = parser.getORcreateParam(unsigned(0), "maxGen", "Maximum number of generations () = none)",'G',"Stopping criterion").value(); dim::continuator::Base<EOT>& continuator = dim::do_make::continuator<EOT>(parser, state, eval); dim::core::IslandData<EOT> data(smp ? nislands : -1); std::string monitorPrefix = parser.getORcreateParam(std::string("result"), "monitorPrefix", "Monitor prefix filenames", '\0', "Output").value(); dim::utils::CheckPoint<EOT>& checkpoint = dim::do_make::checkpoint<EOT>(parser, state, continuator, data, 1, stepTimer); /************** * EO routine * **************/ make_parallel(parser); make_verbose(parser); make_help(parser); if (!smp) // no smp enabled use mpi instead { /**************************************** * Distribution des opérateurs aux iles * ****************************************/ eoMonOp<EOT>* ptMon = NULL; if (sync) { ptMon = new DummyOp; } else { ptMon = new SimulatedOp( timeouts[RANK] ); } state.storeFunctor(ptMon); /********************************** * Déclaration des composants DIM * **********************************/ dim::core::ThreadsRunner< EOT > tr; dim::evolver::Easy<EOT> evolver( /*eval*/*ptEval, *ptMon, false ); dim::feedbacker::Base<EOT>* ptFeedbacker = NULL; if (feedback) { if (sync) { ptFeedbacker = new dim::feedbacker::sync::Easy<EOT>(alphaF); } else { ptFeedbacker = new dim::feedbacker::async::Easy<EOT>(alphaF, sensitivity, deltaFeedback); } } else { ptFeedbacker = new dim::algo::Easy<EOT>::DummyFeedbacker(); } state_dim.storeFunctor(ptFeedbacker); dim::vectorupdater::Base<EOT>* ptUpdater = NULL; if (update) { dim::vectorupdater::Reward<EOT>* ptReward = NULL; if (rewardStrategy == "best") { ptReward = new dim::vectorupdater::Best<EOT>(alphaP, betaP); } else { ptReward = new dim::vectorupdater::Average<EOT>(alphaP, betaP, sensitivity, sync ? false : deltaUpdate); } state_dim.storeFunctor(ptReward); ptUpdater = new dim::vectorupdater::Easy<EOT>(*ptReward); } else { ptUpdater = new dim::algo::Easy<EOT>::DummyVectorUpdater(); } state_dim.storeFunctor(ptUpdater); dim::memorizer::Easy<EOT> memorizer; dim::migrator::Base<EOT>* ptMigrator = NULL; if (migrate) { if (sync) { ptMigrator = new dim::migrator::sync::Easy<EOT>(); } else { ptMigrator = new dim::migrator::async::Easy<EOT>(nmigrations); } } else { ptMigrator = new dim::algo::Easy<EOT>::DummyMigrator(); } state_dim.storeFunctor(ptMigrator); dim::algo::Easy<EOT> island( evolver, *ptFeedbacker, *ptUpdater, memorizer, *ptMigrator, checkpoint, monitorPrefix ); if (!sync) { tr.addHandler(*ptFeedbacker).addHandler(*ptMigrator).add(island); } /*************** * Rock & Roll * ***************/ /****************************************************************************** * Création de la matrice de transition et distribution aux iles des vecteurs * ******************************************************************************/ dim::core::MigrationMatrix probabilities( ALL ); dim::core::InitMatrix initmatrix( initG, probaSame ); if ( 0 == RANK ) { initmatrix( probabilities ); std::cout << probabilities; data.proba = probabilities(RANK); for (size_t i = 1; i < ALL; ++i) { world.send( i, 100, probabilities(i) ); } std::cout << "Island Model Parameters:" << std::endl << "alphaP: " << alphaP << std::endl << "alphaF: " << alphaF << std::endl << "betaP: " << betaP << std::endl << "probaSame: " << probaSame << std::endl << "initG: " << initG << std::endl << "update: " << update << std::endl << "feedback: " << feedback << std::endl << "migrate: " << migrate << std::endl << "sync: " << sync << std::endl << "stepTimer: " << stepTimer << std::endl << "deltaUpdate: " << deltaUpdate << std::endl << "deltaFeedback: " << deltaFeedback << std::endl << "sensitivity: " << sensitivity << std::endl << "chromSize: " << chromSize << std::endl << "popSize: " << popSize << std::endl << "targetFitness: " << targetFitness << std::endl << "maxGen: " << maxGen << std::endl ; } else { world.recv( 0, 100, data.proba ); } /****************************************** * Get the population size of all islands * ******************************************/ world.barrier(); dim::utils::print_sum(pop); FitnessInit fitInit; apply<EOT>(fitInit, pop); if (sync) { island( pop, data ); } else { tr( pop, data ); } world.abort(0); return 0 ; } // smp /********************************** * Déclaration des composants DIM * **********************************/ dim::core::ThreadsRunner< EOT > tr; std::vector< dim::core::Pop<EOT> > islandPop(nislands); std::vector< dim::core::IslandData<EOT> > islandData(nislands); dim::core::MigrationMatrix probabilities( nislands ); dim::core::InitMatrix initmatrix( initG, probaSame ); initmatrix( probabilities ); std::cout << probabilities; FitnessInit fitInit; for (size_t i = 0; i < nislands; ++i) { std::cout << "island " << i << std::endl; islandPop[i].append(popSize, init); apply<EOT>(fitInit, islandPop[i]); islandData[i] = dim::core::IslandData<EOT>(nislands, i); std::cout << islandData[i].size() << " " << islandData[i].rank() << std::endl; islandData[i].proba = probabilities(i); apply<EOT>(eval, islandPop[i]); /**************************************** * Distribution des opérateurs aux iles * ****************************************/ eoMonOp<EOT>* ptMon = NULL; ptMon = new SimulatedOp( timeouts[islandData[i].rank()] ); state.storeFunctor(ptMon); eoEvalFunc<EOT>* __ptEval = NULL; __ptEval = new SimulatedEval( rewards[islandData[i].rank()] ); state.storeFunctor(__ptEval); dim::evolver::Base<EOT>* ptEvolver = new dim::evolver::Easy<EOT>( /*eval*/*__ptEval, *ptMon, false ); state_dim.storeFunctor(ptEvolver); dim::feedbacker::Base<EOT>* ptFeedbacker = new dim::feedbacker::smp::Easy<EOT>(islandPop, islandData, alphaF); state_dim.storeFunctor(ptFeedbacker); dim::vectorupdater::Reward<EOT>* ptReward = NULL; if (rewardStrategy == "best") { ptReward = new dim::vectorupdater::Best<EOT>(alphaP, betaP); } else { ptReward = new dim::vectorupdater::Average<EOT>(alphaP, betaP, sensitivity, sync ? false : deltaUpdate); } state_dim.storeFunctor(ptReward); dim::vectorupdater::Base<EOT>* ptUpdater = new dim::vectorupdater::Easy<EOT>(*ptReward); state_dim.storeFunctor(ptUpdater); dim::memorizer::Base<EOT>* ptMemorizer = new dim::memorizer::Easy<EOT>(); state_dim.storeFunctor(ptMemorizer); dim::migrator::Base<EOT>* ptMigrator = new dim::migrator::smp::Easy<EOT>(islandPop, islandData, monitorPrefix); state_dim.storeFunctor(ptMigrator); dim::utils::CheckPoint<EOT>& checkpoint = dim::do_make::checkpoint<EOT>(parser, state, continuator, islandData[i], 1, stepTimer); dim::algo::Base<EOT>* ptIsland = new dim::algo::smp::Easy<EOT>( *ptEvolver, *ptFeedbacker, *ptUpdater, *ptMemorizer, *ptMigrator, checkpoint, islandPop, islandData, monitorPrefix ); state_dim.storeFunctor(ptIsland); ptEvolver->size(nislands); ptFeedbacker->size(nislands); ptReward->size(nislands); ptUpdater->size(nislands); ptMemorizer->size(nislands); ptMigrator->size(nislands); ptIsland->size(nislands); ptEvolver->rank(i); ptFeedbacker->rank(i); ptReward->rank(i); ptUpdater->rank(i); ptMemorizer->rank(i); ptMigrator->rank(i); ptIsland->rank(i); tr.add(*ptIsland); } tr(pop, data); return 0 ; }