From 50850230eae4c96ae6d30f6969367af3f7a9c60d Mon Sep 17 00:00:00 2001 From: jhumeau Date: Fri, 11 Sep 2009 09:22:23 +0000 Subject: [PATCH] Work's start on Generic COSEARCH git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1609 331e1502-861f-0410-8da2-ba01fb791d7f --- contribution/branches/MOLS/COSEARCH/fspEA.cpp | 318 ++++++++++++++ .../branches/MOLS/COSEARCH/moeoTransfer.h | 388 ++++++++++++++++++ 2 files changed, 706 insertions(+) create mode 100755 contribution/branches/MOLS/COSEARCH/fspEA.cpp create mode 100644 contribution/branches/MOLS/COSEARCH/moeoTransfer.h diff --git a/contribution/branches/MOLS/COSEARCH/fspEA.cpp b/contribution/branches/MOLS/COSEARCH/fspEA.cpp new file mode 100755 index 000000000..48bbf6a3a --- /dev/null +++ b/contribution/branches/MOLS/COSEARCH/fspEA.cpp @@ -0,0 +1,318 @@ +//eo +#include +#include + +//general +#include +#include +#include +#include +#include +#include + +// moeo +#include +#include +#include + +// fsp +#include + +//peo +#include +#include +#include +#include +#include +#include + +//DMLS + + +#include + +void make_help(eoParser & _parser); + +using namespace std; + +int main(int argc, char* argv[]) +{ + try{ + + eoParser parser(argc, argv); // for user-parameter reading + eoState state; // to keep all things allocated + + /*** number of objectives ***/ + fspObjectiveVectorTraits::nObj = 2; + + /*** parameters ***/ + eoValueParam& _seedParam = parser.createParam(uint32_t(0), "seed", "Random number seed", 'S'); + std::string _file = parser.createParam(std::string(), "file", "", '\0', "Representation", true).value(); + double _crossRate = parser.createParam((double) 0.05, "crossRate", "Rate for 2-PT crossover", 0, "Variation Operators").value(); + double _mutRate = parser.createParam((double) 1.0, "mutRate", "Rate for shift mutation", 0, "Variation Operators").value(); + unsigned int _popSize = parser.createParam((unsigned int)(100), "popSize", "Population Size", 'P', "Evolution Engine").value(); + std::string _strategy = parser.createParam(std::string(), "explorer", "OneOne - OneAll - AllOne - AllAll - OneFirst - AllFirst - OneND - AllND", '\0', "Evolution Engine", true).value(); + unsigned int _nbKick = parser.createParam((unsigned int)(10), "nbKick", "Number of kick", 'K', "Evolution Engine").value(); + + // seed + if (_seedParam.value() == 0) + _seedParam.value() = time(0); + rng.reseed(_seedParam.value()); + + /*** the representation-dependent things ***/ + + // load data + fspData data(_file); + // size + unsigned int size = data.getN(); // nb jobs + // init + fspInit init(size, 0); + // eval + fspEval simpleEval(data.getM(), data.getN(), data.getP(), data.getD()); + eoEvalFuncCounter eval(simpleEval); + // cross + fspCross cross; + // mut + fspMut mut; + // op + eoSGAGenOp op(cross, _crossRate, mut, _mutRate); + + /*** the representation-independent things ***/ + + // move init + fspMoveInit moveInit; + // move next + fspMoveNext moveNext; + // move incr eval + fspMoveIncrEval moveIncrEval(data.getM(), data.getN(), data.getP(), data.getD()); + + + bool multiply=true; + moeoPopNeighborhoodExplorer * explorer; + moeoUnvisitedSelect * selector; + if (_strategy == std::string("OneOne")) + { + selector = new moeoNumberUnvisitedSelect (1); + explorer = new moeoSimpleSubNeighborhoodExplorer (moveInit, moveNext, moveIncrEval, 1); + } + else if (_strategy == std::string("OneAll")) + { + selector = new moeoNumberUnvisitedSelect (1); + explorer = new moeoExhaustiveNeighborhoodExplorer (moveInit, moveNext, moveIncrEval); + } + else if (_strategy == std::string("AllOne")) + { + selector = new moeoExhaustiveUnvisitedSelect; + explorer = new moeoSimpleSubNeighborhoodExplorer (moveInit, moveNext, moveIncrEval, 1); + multiply=false; + } + else if (_strategy == std::string("AllAll")) + { + selector = new moeoExhaustiveUnvisitedSelect; + explorer = new moeoExhaustiveNeighborhoodExplorer (moveInit, moveNext, moveIncrEval); + } + else if (_strategy == std::string("OneFirst")) + { + selector = new moeoNumberUnvisitedSelect (1); + explorer = new moeoFirstImprovingNeighborhoodExplorer (moveInit, moveNext, moveIncrEval); + } + else if (_strategy == std::string("AllFirst")) + { + selector = new moeoExhaustiveUnvisitedSelect; + explorer = new moeoFirstImprovingNeighborhoodExplorer (moveInit, moveNext, moveIncrEval); + } + else if (_strategy == std::string("OneND")) + { + selector = new moeoNumberUnvisitedSelect (1); + explorer = new moeoNoDesimprovingNeighborhoodExplorer (moveInit, moveNext, moveIncrEval); + } + else if (_strategy == std::string("AllND")) + { + selector = new moeoExhaustiveUnvisitedSelect; + explorer = new moeoNoDesimprovingNeighborhoodExplorer (moveInit, moveNext, moveIncrEval); + multiply=false; + } + else + { + std::string stmp = std::string("Invalid explorer strategy: ") + _strategy; + throw std::runtime_error(stmp.c_str()); + } + state.storeFunctor(selector); + state.storeFunctor(explorer); + + //peo :: init (argc, argv); + + eoPop popIBEA; + eoPop popLS; + eoPop popCentral; + eoPop popCentral2; + popCentral.resize(0); + popCentral2.resize(0); + // initialization of the population + popIBEA.append(_popSize, init); + popLS.append(1, init); + // definition of an unbounded archive + moeoNewBoundedArchive archCentral(100); + moeoNewBoundedArchive archCentral2(100); + moeoNewBoundedArchive archLS(100); + moeoUnboundedArchive archIBEA; + + // stopping criteria + eoContinue & stop = do_make_continue_moeo(parser, state, eval); + + eoGenContinue stop1(5000000); + eoGenContinue stop2(5000000); + eoGenContinue stop3(5000000); + + // checkpointing + eoCheckPoint & checkpoint1 = do_make_checkpoint_moeo(parser, state, eval, stop1, popIBEA, archIBEA); + eoCheckPoint & checkpoint2 = do_make_checkpoint_moeo(parser, state, eval, stop2, popLS, archLS); + eoCheckPoint & checkpoint3 = do_make_checkpoint_moeo(parser, state, eval, stop3, popCentral, archCentral); + + moeoArchiveObjectiveVectorSavingUpdater < FSP > * save_updater1 = new moeoArchiveObjectiveVectorSavingUpdater < FSP > (archIBEA, "archIBEA"); + state.storeFunctor(save_updater1); + checkpoint1.add(*save_updater1); + + moeoArchiveObjectiveVectorSavingUpdater < FSP > * save_updater2 = new moeoArchiveObjectiveVectorSavingUpdater < FSP > (archLS, "archLS"); + state.storeFunctor(save_updater2); + checkpoint2.add(*save_updater2); + + moeoArchiveObjectiveVectorSavingUpdater < FSP > * save_updater3 = new moeoArchiveObjectiveVectorSavingUpdater < FSP > (archCentral, "archCentral"); + state.storeFunctor(save_updater3); + checkpoint3.add(*save_updater3); + + // metric + moeoAdditiveEpsilonBinaryMetric metric; + // algorithms + moeoIBEA algo1 (checkpoint1, eval, op, metric); + //moeoSEEA algo1 (checkpoint1, eval, op, arch1); + moeoUnifiedDominanceBasedLS algo2(checkpoint2, eval, archLS, *explorer, *selector); + + //moeoUnifiedDominanceBasedLS algo3(checkpoint3, eval, arch3, *explorer, *selector); + //moeoSEEA algo3 (checkpoint3, eval, op, arch3); + //moeoSEEA algo4 (checkpoint4, eval, op, arch4); + //moeoIBEA algo3 (checkpoint3, eval, op, metric); + //moeoNSGAII algo3 (checkpoint3, eval, op); + + //PEO:initialisation + + + + //Topolgy + RingTopology ring1, ring2; + StarTopology star; + + + + + eoSwapMutation swap; + + + + CentralAggregation test; + IBEAAggregation test2; + LSAggregation test3(swap, eval, _nbKick); + ArchToArchAggregation test4; + + apply(eval, popIBEA); + popIBEA.sort(); + apply(eval, popLS); + popLS.sort(); + + + + /* peoSyncDataTransfer transfer1(archCentral, ring1, test); + centralArchive centre1(archCentral, popCentral, transfer1, checkpoint3); + + + peoSyncDataTransfer transfer2(archCentral, ring2, test); + centralArchive centre2(archCentral, popCentral, transfer2, checkpoint3); + + + + + peoSyncDataTransfer transfer3(popIBEA, ring1, test2); + eoGenContinue cont2(500); + + gestionTransfer gest2(cont2, transfer3, archIBEA); + //centralArchive centre1(arch1, pop1, transfer1); + checkpoint1.add(gest2); + + + + + peoSyncDataTransfer transfer4(archLS, ring2, test3); + eoAmeliorationContinue cont(archLS, 1, false); + //eoGenContinue cont(10000); + gestionTransfer2 gest(cont, transfer4, archLS); + checkpoint2.add(gest);*/ + + + // std::vector vect; + + + // initDebugging(); + //setDebugMode(true); + + testPEO hop(argc, argv, algo1, algo2, popIBEA, popLS, archCentral, test, test, test2, test3, checkpoint1, checkpoint2, checkpoint3); + + + hop(); + + + + // Start the parallel EA + /*eoPop dummyPop; + dummyPop.resize(0); + + eoPop dummyPop2; + dummyPop2.resize(0);*/ + + //Wrapp algorithms + + /* peoParallelAlgorithmWrapper parallelEA_1(centre1, dummyPop); + transfer1.setOwner( parallelEA_1 ); + + + peoParallelAlgorithmWrapper parallelEA_2(algo1, popIBEA); + transfer3.setOwner( parallelEA_2 ); + + + peoParallelAlgorithmWrapper parallelEA_3(centre2, dummyPop2); + transfer2.setOwner( parallelEA_3 ); + + + peoParallelAlgorithmWrapper parallelEA_4(algo2, popLS); + transfer4.setOwner( parallelEA_4 );*/ + + + /* if (getNodeRank()==1) + { + + cout << "Initial Population IBEA\n" << popIBEA << endl; + } + + if (getNodeRank()==2) + { + + cout << "Initial Population LS\n" << popLS << endl; + }*/ + + + + + + //run + //peo :: run( ); + //peo :: finalize( ); + //endDebugging(); + + + + } + + catch (exception& e){ + cout << e.what() << endl; + } + return EXIT_SUCCESS; +} diff --git a/contribution/branches/MOLS/COSEARCH/moeoTransfer.h b/contribution/branches/MOLS/COSEARCH/moeoTransfer.h new file mode 100644 index 000000000..f6fbc4ce5 --- /dev/null +++ b/contribution/branches/MOLS/COSEARCH/moeoTransfer.h @@ -0,0 +1,388 @@ +//eo +#include + +// moeo +#include + +//general +#include +#include +#include +#include +#include + +//peo +#include +#include +#include + +#include + + +template +class generalAggregation: public eoBF{}; + +template + class AbstractEntityToPopAggregation: public generalAggregation&, A, R>{}; + +template + class AbstractEntityToArchiveAggregation: public generalAggregation&, A, R>{}; + +template + class PopToPopAggregation: public AbstractEntityToPopAggregation&, void>{}; + +template + class ArchiveToPopAggregation: public AbstractEntityToPopAggregation&, void>{}; + +template + class PopToArchiveAggregation: public AbstractEntityToArchiveAggregation&, void>{}; + +template + class ArchiveToArchiveAggregation: public AbstractEntityToArchiveAggregation&, void>{}; + + +/* + * + */ +template +class gestionTransfer: public eoUpdater{ + +public: + gestionTransfer(eoGenContinue < EOT > & _continuator, + peoDataTransfer & _asyncDataTransfer, + moeoArchive& _archive):continuator(_continuator), asyncDataTransfer(_asyncDataTransfer), archive(_archive){} + + void operator()(){ + if(!continuator(archive)){ + asyncDataTransfer(); + continuator.totalGenerations(continuator.totalGenerations()); + } + } + +private: + eoGenContinue < EOT > & continuator; + peoDataTransfer& asyncDataTransfer; + moeoArchive& archive; +}; + + +/* + * + */ +template +class gestionTransfer2: public eoUpdater{ + +public: + gestionTransfer2(eoContinue < EOT > & _continuator, + peoSyncDataTransfer & _asyncDataTransfer, + moeoArchive& _archive):continuator(_continuator), asyncDataTransfer(_asyncDataTransfer), archive(_archive){} + + void operator()(){ + if(!continuator(archive)){ + asyncDataTransfer(); + } + } + +private: + eoContinue < EOT > & continuator; + peoSyncDataTransfer& asyncDataTransfer; + moeoArchive& archive; +}; + + +/* + * + */ +/*template +class IBEAAggregationRnd: public ArchiveToPopAggregation{ + +public: + void operator()(eoPop< EOT >& _pop, moeoArchive< EOT >& _archive) { + UF_random_generator rndGen; + std::vector tab; + unsigned int resizeValue; + unsigned int popCorrectSize= _pop.size(); + + if (_pop.size() - _archive.size()>0){ + resizeValue=_pop.size() - _archive.size(); + _pop.shuffle(); + } + else + resizeValue=0; + + for(unsigned int i=0; i<_archive.size(); i++) + tab.push_back(i); + std::random_shuffle(tab.begin(), tab.end(), rndGen); + + _pop.resize(resizeValue); + + for (unsigned int i=0; i<(popCorrectSize-resizeValue); i++) + { + _pop.push_back(_archive[tab[i]]); + } + for (unsigned int i=0; i<_pop.size(); i++) + { + _pop[i].fitness(0.0); + _pop[i].diversity(0.0); + } + } + };*/ + + + +/* + * + */ +template +class IBEAAggregation: public ArchiveToPopAggregation{ +public: + void operator()(eoPop< EOT >& _pop, moeoArchive< EOT >& _archive) { + unsigned int popSize= _pop.size(); + _pop.reserve(popSize + _archive.size()); + for (unsigned int i=0; i<_archive.size(); i++) + { + _pop.push_back(_archive[i]); + _pop[i+popSize].fitness(0.0); + //_pop[i].diversity(0.0); + } + diversityAssignment(_pop); + std::sort(_pop.begin(), _pop.end(), cmp); + _pop.resize(popSize); + } + +private: + moeoCrowdingDiversityAssignment diversityAssignment; + moeoDiversityThenFitnessComparator cmp; +}; + + +/* + * + */ +/*template +class IBEAAggregationQuiMarchePas{ +public: + void operator()(eoPop< EOT >& _pop, moeoArchive< EOT >& _archive) { + UF_random_generator rndGen; + std::vector tab; + unsigned int resizeValue; + unsigned int popCorrectSize= _pop.size(); + + + if (_pop.size() <= _archive.size()) + _pop.resize(0); + + for (unsigned int i=0; i<_archive.size(); i++) + { + _pop.push_back(_archive[i]); + _pop[i].fitness(0.0); + _pop[i].diversity(0.0); + } + + } + };*/ + + +/* + * + */ +template +class LSAggregation: public ArchiveToArchiveAggregation{ +public: + + LSAggregation(eoMonOp & _op, eoEvalFunc& _eval, unsigned int _nbKick): ArchiveToArchiveAggregation(),op(_op), eval(_eval), nbKick(_nbKick){} + + void operator()(moeoArchive< EOT >& _archive1, moeoArchive & _archive2) { + unsigned int archSize=_archive2.size(); + if(archSize>0){ + _archive1.resize(0); + _archive1.push_back(_archive2[rng.random(archSize)]); + // std::cout << "kick : " << nbKick << std::endl; + //si la solution n'a pas encore été visité + if(_archive1[0].flag()==1){ + std::cout << "kick pour du vrai" << std::endl; + //on la kick + for(unsigned int i=0; i & op; + eoEvalFunc & eval; + unsigned int nbKick; + +}; + + +//Aggregation pour archiver une pop +template +class CentralAggregation: public PopToArchiveAggregation{ +public: + void operator()(moeoArchive< EOT >& _archive, eoPop< EOT >& _pop) { + _archive(_pop); + } +}; + +//aggregation pour archiver une archive +template +class ArchToArchAggregation{ +public: + void operator()(moeoArchive< EOT >& _archive1, moeoArchive< EOT >& _archive2) { + _archive1(_archive2); + } +}; + +template +class gestArchive{ + + public: + gestArchive(moeoArchive & _archive, + eoPop & _pop, + peoSyncDataTransfer & _syncDataTransfer, + eoContinue & _cont): + archive(_archive), pop(_pop), syncDataTransfer(_syncDataTransfer), cont(_cont){} + + void operator()(eoPop & population) { + while(true){ + // pop.resize(0); + //for(unsigned int i=0; i& archive; + eoPop & pop; + peoSyncDataTransfer& syncDataTransfer; + eoContinue & cont; +}; + + +template +class testPEO{ + + public: + testPEO(int _argc, + char** _argv, + moeoPopAlgo & _algo1, + moeoPopAlgo & _algo2, + eoPop & _pop1, + eoPop & _pop2, + moeoNewBoundedArchive & _centralArchive, + AbstractEntityToArchiveAggregation &, void>& _algo1ToArchive, + AbstractEntityToArchiveAggregation &, void>& _algo2ToArchive, + generalAggregation &, moeoArchive &, void>& _archiveToAlgo1, + generalAggregation &, moeoArchive &, void>& _archiveToAlgo2, + eoCheckPoint & _checkAlgo1, + eoCheckPoint & _checkAlgo2, + eoCheckPoint & _checkArchive): + argc(_argc), + argv(_argv), + algo1(_algo1), + algo2(_algo2), + pop1(_pop1), + pop2(_pop2), + centralArchive(_centralArchive), + algo1ToArchive(_algo1ToArchive), + algo2ToArchive(_algo2ToArchive), + archiveToAlgo1(_archiveToAlgo1), + archiveToAlgo2(_archiveToAlgo2), + checkAlgo1(_checkAlgo1), + checkAlgo2(_checkAlgo2), + checkArchive(_checkArchive){} + + void operator()(){ + + //PEO Initialization + peo :: init (argc, argv); + + //Two RingTopolgy + RingTopology ring1, ring2; + + + //DataTransfer for the fisrt ring + peoSyncDataTransfer transfer1(centralArchive, ring1, algo1ToArchive); + peoSyncDataTransfer transfer2(pop1, ring1, archiveToAlgo1); + + //DataTransfer for the second ring + peoSyncDataTransfer transfer3(centralArchive, ring2, algo2ToArchive); + peoSyncDataTransfer transfer4(pop2, ring2, archiveToAlgo2); + + //Transfer Algo1 -> archiveCentral (Ring1) + gestArchive toCenter1(centralArchive, pop1, transfer1, checkArchive); + + //Transfer archiveCentral -> Algo1 (Ring1) + eoGenContinue genContinuator1(100); + gestionTransfer exitCenter1(genContinuator1, transfer2, centralArchive); + checkAlgo1.add(exitCenter1); + + //Transfer Algo2 -> archiveCentral (Ring2) + gestArchive toCenter2(centralArchive, pop2, transfer3, checkArchive); + + //Transfer archiveCentral -> Algo2 (Ring2) + eoGenContinue genContinuator2(200); + gestionTransfer exitCenter2(genContinuator2, transfer4, centralArchive); + checkAlgo2.add(exitCenter2); + + //dummyPop + eoPop dummyPop; + + //Wrapping of algotithm + peoParallelAlgorithmWrapper parallelEA_1(toCenter1, dummyPop); + transfer1.setOwner( parallelEA_1 ); + + + peoParallelAlgorithmWrapper parallelEA_2(algo1, pop1); + transfer2.setOwner( parallelEA_2 ); + + + peoParallelAlgorithmWrapper parallelEA_3(toCenter2, dummyPop); + transfer3.setOwner( parallelEA_3 ); + + + peoParallelAlgorithmWrapper parallelEA_4(algo2, pop2); + transfer4.setOwner( parallelEA_4 ); + + + //run + peo :: run( ); + peo :: finalize( ); + endDebugging(); + } + + private: + int argc; + char** argv; + moeoPopAlgo & algo1; + moeoPopAlgo & algo2; + eoPop & pop1; + eoPop & pop2; + moeoNewBoundedArchive & centralArchive; + AbstractEntityToArchiveAggregation &, void> & algo1ToArchive; + AbstractEntityToArchiveAggregation &, void> & algo2ToArchive; + generalAggregation &, moeoArchive &, void> & archiveToAlgo1; + generalAggregation &, moeoArchive &, void> & archiveToAlgo2; + eoCheckPoint & checkAlgo1; + eoCheckPoint & checkAlgo2; + eoCheckPoint & checkArchive; + + +};