Repairer operator more generic than Bounder, example with a Repairer that rounds values, a Dispatcher to call several Repairer on partial solutions
This commit is contained in:
parent
d6ccadd18e
commit
e4399683dd
6 changed files with 291 additions and 0 deletions
|
|
@ -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"
|
||||
|
|
|
|||
47
edo/src/edoRepairer.h
Normal file
47
edo/src/edoRepairer.h
Normal file
|
|
@ -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 <johann.dreo@thalesgroup.com>
|
||||
Pierre Savéant <pierre.saveant@thalesgroup.com>
|
||||
*/
|
||||
|
||||
#ifndef _edoRepairer_h
|
||||
#define _edoRepairer_h
|
||||
|
||||
#include <eoFunctor.h>
|
||||
|
||||
/** 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
|
||||
110
edo/src/edoRepairerDispatcher.h
Normal file
110
edo/src/edoRepairerDispatcher.h
Normal file
|
|
@ -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 <johann.dreo@thalesgroup.com>
|
||||
Pierre Savéant <pierre.saveant@thalesgroup.com>
|
||||
*/
|
||||
|
||||
#ifndef _edoRepairerDispatcher_h
|
||||
#define _edoRepairerDispatcher_h
|
||||
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
|
||||
#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<unsigned int> idx, edoRepairer<EOT>* 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<unsigned int> idx, edoRepairer<EOT>* 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<EOT>::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
|
||||
68
edo/src/edoRepairerRound.h
Normal file
68
edo/src/edoRepairerRound.h
Normal file
|
|
@ -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 <johann.dreo@thalesgroup.com>
|
||||
Pierre Savéant <pierre.saveant@thalesgroup.com>
|
||||
*/
|
||||
|
||||
#ifndef _edoRepairerRound_h
|
||||
#define _edoRepairerRound_h
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "edoRepairer.h"
|
||||
|
||||
/**
|
||||
*
|
||||
* @ingroup Repairers
|
||||
*/
|
||||
template < typename EOT >
|
||||
class edoRepairerFloor : public edoRepairer<EOT>
|
||||
{
|
||||
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<EOT>
|
||||
{
|
||||
public:
|
||||
virtual void operator()( EOT& sol )
|
||||
{
|
||||
for( unsigned int i=0; i < sol.size(); ++i ) {
|
||||
sol[i] = ceil( sol[i] );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif // !_edoRepairerRound_h
|
||||
|
|
@ -38,6 +38,7 @@ SET(SOURCES
|
|||
t-bounderno
|
||||
t-uniform
|
||||
t-continue
|
||||
t-dispatcher-round
|
||||
)
|
||||
|
||||
FOREACH(current ${SOURCES})
|
||||
|
|
|
|||
62
edo/test/t-dispatcher-round.cpp
Normal file
62
edo/test/t-dispatcher-round.cpp
Normal file
|
|
@ -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 <johann.dreo@thalesgroup.com>
|
||||
Pierre Savéant <pierre.saveant@thalesgroup.com>
|
||||
*/
|
||||
|
||||
#include <eo>
|
||||
#include <edo>
|
||||
#include <es.h>
|
||||
|
||||
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<EOT>* rep1 = new edoRepairerFloor<EOT>();
|
||||
edoRepairer<EOT>* rep2 = new edoRepairerCeil<EOT>();
|
||||
|
||||
std::set<unsigned int> indexes1;
|
||||
indexes1.insert(0);
|
||||
indexes1.insert(2);
|
||||
|
||||
std::set<unsigned int> indexes2;
|
||||
indexes2.insert(1);
|
||||
indexes2.insert(3);
|
||||
|
||||
edoRepairerDispatcher<EOT> repare( indexes1, rep1 );
|
||||
repare.add( indexes2, rep2 );
|
||||
|
||||
repare(sol);
|
||||
|
||||
std::cout << sol << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue