diff --git a/edo/src/edo b/edo/src/edo index 1a6a2c682..b19d19d97 100644 --- a/edo/src/edo +++ b/edo/src/edo @@ -56,6 +56,9 @@ Authors: #include "edoVectorBounds.h" +#include "edoRepairer.h" +#include "edoRepairerDispatcher.h" +#include "edoRepairerRound.h" #include "edoBounder.h" #include "edoBounderNo.h" #include "edoBounderBound.h" diff --git a/edo/src/edoRepairer.h b/edo/src/edoRepairer.h new file mode 100644 index 000000000..8ea2366ab --- /dev/null +++ b/edo/src/edoRepairer.h @@ -0,0 +1,47 @@ +/* +The Evolving Distribution Objects framework (EDO) is a template-based, +ANSI-C++ evolutionary computation library which helps you to write your +own estimation of distribution algorithms. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Copyright (C) 2011 Thales group +*/ +/* +Authors: + Johann Dréo + Pierre Savéant +*/ + +#ifndef _edoRepairer_h +#define _edoRepairer_h + +#include + +/** The interface of a set of classes that modifies an unfeasible candidate + * solution so as to respect a given set of constraints and thus make a feasible + * solution. + * + * @ingroup Repairers + */ +template < typename EOT > +class edoRepairer : public eoUF< EOT&, void > +{ +public: + // virtual void operator()( EOT& ) = 0 (provided by eoUF< A1, R >) + virtual void operator()( EOT& ) {} +}; + +#endif // !_edoRepairer_h diff --git a/edo/src/edoRepairerDispatcher.h b/edo/src/edoRepairerDispatcher.h new file mode 100644 index 000000000..6824d1159 --- /dev/null +++ b/edo/src/edoRepairerDispatcher.h @@ -0,0 +1,110 @@ +/* +The Evolving Distribution Objects framework (EDO) is a template-based, +ANSI-C++ evolutionary computation library which helps you to write your +own estimation of distribution algorithms. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Copyright (C) 2011 Thales group +*/ +/* +Authors: + Johann Dréo + Pierre Savéant +*/ + +#ifndef _edoRepairerDispatcher_h +#define _edoRepairerDispatcher_h + +#include +#include +#include + +#include "edoRepairer.h" + +/** Repair a candidate solution by sequentially applying several repairers on + * subparts of the solution (subparts being defined by the corresponding set + * of indexes). + * + * Only work on EOT that implements the "push_back( EOT::AtomType )" and + * "operator[](uint)" and "at(uint)" methods. + * + * Expects _addresses_ of the repairer operators. + * + * @example t-dispatcher-round.cpp + * + * @ingroup Repairers + */ + +template < typename EOT > +class edoRepairerDispatcher + : public eoUF< EOT&, void >, + std::vector< + std::pair< std::set< unsigned int >, edoRepairer< EOT >* > + > +{ +public: + //! Empty constructor + edoRepairerDispatcher() : + std::vector< + std::pair< std::set< unsigned int >, edoRepairer< EOT >* > + >() + {} + + //! Constructor with a single index set and repairer operator + edoRepairerDispatcher( std::set idx, edoRepairer* op ) : + std::vector< + std::pair< std::set< unsigned int >, edoRepairer< EOT >* > + >() + { + this->add( idx, op ); + } + + //! Add more indexes set and their corresponding repairer operator address to the list + void add( std::set idx, edoRepairer* op ) + { + this->push_back( std::make_pair(idx, op) ); + } + + //! Repair a solution by calling several repair operator on subset of indexes + virtual void operator()( EOT& sol ) + { + // i is an iterator that points on a pair + for( typename edoRepairerDispatcher::iterator i = this->begin(); i != this->end(); ++i ) { + // a partial copy of the sol + EOT partsol; + + // j is an iterator that points on an uint + for( std::set< unsigned int >::iterator j = i->first.begin(); j != i->first.end(); ++j ) { + partsol.push_back( sol.at(*j) ); + } // for j + + // apply the repairer on the partial copy + // the repairer is a functor, thus second is callable + (*(i->second))( partsol ); + + { // copy back the repaired partial solution to sol + // browse partsol with uint k, and the idx set with an iterator (std::set is an associative tab) + unsigned int k=0; + for( std::set< unsigned int >::iterator j = i->first.begin(); j != i->first.end(); ++j ) { + sol[ *j ] = partsol[ k ]; + k++; + } // for j + } // context for k + } // for i + } +}; + +#endif // !_edoRepairerDispatcher_h diff --git a/edo/src/edoRepairerRound.h b/edo/src/edoRepairerRound.h new file mode 100644 index 000000000..032169424 --- /dev/null +++ b/edo/src/edoRepairerRound.h @@ -0,0 +1,68 @@ +/* +The Evolving Distribution Objects framework (EDO) is a template-based, +ANSI-C++ evolutionary computation library which helps you to write your +own estimation of distribution algorithms. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Copyright (C) 2011 Thales group +*/ +/* +Authors: + Johann Dréo + Pierre Savéant +*/ + +#ifndef _edoRepairerRound_h +#define _edoRepairerRound_h + +#include + +#include "edoRepairer.h" + +/** + * + * @ingroup Repairers + */ +template < typename EOT > +class edoRepairerFloor : public edoRepairer +{ +public: + virtual void operator()( EOT& sol ) + { + for( unsigned int i=0; i < sol.size(); ++i ) { + sol[i] = floor( sol[i] ); + } + } +}; + +/** + * + * @ingroup Repairers + */ +template < typename EOT > +class edoRepairerCeil : public edoRepairer +{ +public: + virtual void operator()( EOT& sol ) + { + for( unsigned int i=0; i < sol.size(); ++i ) { + sol[i] = ceil( sol[i] ); + } + } +}; + + +#endif // !_edoRepairerRound_h diff --git a/edo/test/CMakeLists.txt b/edo/test/CMakeLists.txt index aea8eaee2..acdabc7b7 100644 --- a/edo/test/CMakeLists.txt +++ b/edo/test/CMakeLists.txt @@ -38,6 +38,7 @@ SET(SOURCES t-bounderno t-uniform t-continue + t-dispatcher-round ) FOREACH(current ${SOURCES}) diff --git a/edo/test/t-dispatcher-round.cpp b/edo/test/t-dispatcher-round.cpp new file mode 100644 index 000000000..245a2e2cb --- /dev/null +++ b/edo/test/t-dispatcher-round.cpp @@ -0,0 +1,62 @@ +/* +The Evolving Distribution Objects framework (EDO) is a template-based, +ANSI-C++ evolutionary computation library which helps you to write your +own estimation of distribution algorithms. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Copyright (C) 2010 Thales group +*/ +/* +Authors: + Johann Dréo + Pierre Savéant +*/ + +#include +#include +#include + +typedef eoReal< eoMinimizingFitness > EOT; + +int main(void) +{ + EOT sol; + sol.push_back(1.1); + sol.push_back(1.1); + sol.push_back(3.9); + sol.push_back(3.9); + // we expect {1,2,3,4} + + edoRepairer* rep1 = new edoRepairerFloor(); + edoRepairer* rep2 = new edoRepairerCeil(); + + std::set indexes1; + indexes1.insert(0); + indexes1.insert(2); + + std::set indexes2; + indexes2.insert(1); + indexes2.insert(3); + + edoRepairerDispatcher repare( indexes1, rep1 ); + repare.add( indexes2, rep2 ); + + repare(sol); + + std::cout << sol << std::endl; + + return 0; +}