edoRepairerDispatcher.h
00001 /*
00002 The Evolving Distribution Objects framework (EDO) is a template-based,
00003 ANSI-C++ evolutionary computation library which helps you to write your
00004 own estimation of distribution algorithms.
00005 
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 This library is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public
00017 License along with this library; if not, write to the Free Software
00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020 Copyright (C) 2011 Thales group
00021 */
00022 /*
00023 Authors:
00024     Johann Dréo <johann.dreo@thalesgroup.com>
00025     Pierre Savéant <pierre.saveant@thalesgroup.com>
00026 */
00027 
00028 #ifndef _edoRepairerDispatcher_h
00029 #define _edoRepairerDispatcher_h
00030 
00031 #include <vector>
00032 #include <utility>
00033 
00034 #include "edoRepairer.h"
00035 
00095 template < typename EOT, typename ICT = std::vector<unsigned int> >
00096 class edoRepairerDispatcher 
00097     : public edoRepairer<EOT>, 
00098              std::vector< 
00099                   std::pair< ICT, edoRepairer< EOT >* > 
00100              >
00101 {
00102 public:
00103 
00105     edoRepairerDispatcher() : 
00106         std::vector< 
00107             std::pair< std::vector< unsigned int >, edoRepairer< EOT >* > 
00108         >()
00109     {}
00110 
00112     edoRepairerDispatcher( ICT idx, edoRepairer<EOT>* op ) :
00113         std::vector< 
00114             std::pair< std::vector< unsigned int >, edoRepairer< EOT >* > 
00115         >() 
00116     {
00117         this->add( idx, op );
00118     }
00119 
00121     void add( ICT idx, edoRepairer<EOT>* op )
00122     {
00123         //assert( idx.size() > 0 );
00124 #ifndef NDEBUG
00125         eo::log << eo::warnings << "A repairer is added to the dispatcher while having an empty index list, nothing will be repaired" << std::endl;
00126 #endif
00127         assert( op != NULL );
00128 
00129         this->push_back( std::make_pair(idx, op) );
00130     }
00131 
00133     virtual void operator()( EOT& sol )
00134     {
00135 //        std::cout << "in dispatcher, sol = " << sol << std::endl;
00136 
00137         // iterate over { indexe, repairer }
00138         // ipair is an iterator that points on a pair of <indexes,repairer>
00139         for( typename edoRepairerDispatcher<EOT>::iterator ipair = this->begin(); ipair != this->end(); ++ipair ) {
00140 
00141             assert( ipair->first.size() <= sol.size() ); // assert there is less indexes than items in the whole solution
00142 
00143             // a partial copy of the sol
00144             EOT partsol;
00145 
00146 //            std::cout << "\tusing indexes = ";
00147 //
00148             // iterate over indexes
00149             // j is an iterator that points on an uint
00150             for( std::vector< unsigned int >::iterator j = ipair->first.begin(); j != ipair->first.end(); ++j ) {
00151 
00152 //                std::cout << *j << " ";
00153 //                std::cout.flush();
00154 
00155                 partsol.push_back( sol.at(*j) );
00156             } // for j
00157 //            std::cout << std::endl;
00158 //            std::cout << "\tpartial sol = " << partsol << std::endl;
00159 
00160             if( partsol.size() == 0 ) {
00161                 continue;
00162             }
00163             assert( partsol.size() > 0 );
00164 
00165             // apply the repairer on the partial copy
00166             // the repairer is a functor, thus second is callable
00167             (*(ipair->second))( partsol );
00168 
00169             { // copy back the repaired partial solution to sol
00170                 // browse partsol with uint k, and the idx set with an iterator (std::vector is an associative tab)
00171                 unsigned int k=0;
00172                 for( std::vector< unsigned int >::iterator j = ipair->first.begin(); j != ipair->first.end(); ++j ) {
00173                     sol[ *j ] = partsol[ k ];
00174                     k++;
00175                 } // for j
00176             } // context for k
00177         } // for ipair
00178 
00179         sol.invalidate();
00180     }
00181 };
00182 
00183 #endif // !_edoRepairerDispatcher_h
 All Classes Functions Variables Typedefs