edoRepairerApply.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 */
00026 
00027 #ifndef _edoRepairerApply_h
00028 #define _edoRepairerApply_h
00029 
00030 #include <algorithm>
00031 
00032 #include "edoRepairer.h"
00033 
00038 template < typename EOT, typename F = typename EOT::AtomType(typename EOT::AtomType) >
00039 class edoRepairerApply : public edoRepairer<EOT>
00040 {
00041 public:
00042     edoRepairerApply( F function ) : _function(function) {}
00043 
00044 protected:
00045     F * _function;
00046 };
00047 
00048 
00055 template < typename EOT, typename F = typename EOT::AtomType(typename EOT::AtomType)>
00056 class edoRepairerApplyUnary : public edoRepairerApply<EOT,F>
00057 {
00058 public:
00059     edoRepairerApplyUnary( F function ) : edoRepairerApply<EOT,F>(function) {}
00060 
00061     virtual void operator()( EOT& sol )
00062     {
00063         std::transform( sol.begin(), sol.end(), sol.begin(), *(this->_function) );
00064         sol.invalidate();
00065     }
00066 };
00067 
00068 
00076 template < typename EOT, typename F = typename EOT::AtomType(typename EOT::AtomType, typename EOT::AtomType)>
00077 class edoRepairerApplyBinary : public edoRepairerApply<EOT,F>
00078 {
00079 public:
00080     typedef typename EOT::AtomType ArgType;
00081 
00082     edoRepairerApplyBinary(   
00083             F function, 
00084             ArgType arg 
00085         ) : edoRepairerApply<EOT,F>(function), _arg(arg) {}
00086 
00087     virtual void operator()( EOT& sol )
00088     {
00089         // call the binary function on each item
00090         // TODO find a way to use std::transform here? Or would it be too bloated?
00091         for(typename EOT::iterator it = sol.begin(); it != sol.end(); ++it ) {
00092             *it = (*(this->_function))( *it, _arg );
00093         }
00094         sol.invalidate();
00095     }
00096 
00097 protected:
00098     ArgType _arg;
00099 };
00100 
00101 
00102 #endif // !_edoRepairerApply_h
00103 
 All Classes Functions Variables Typedefs