Doc and tests added
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1706 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
parent
6cb6070df4
commit
baf31d0c30
25 changed files with 159 additions and 26 deletions
|
|
@ -35,6 +35,8 @@
|
|||
#ifndef _moMetropolisHastingExplorer_h
|
||||
#define _moMetropolisHastingExplorer_h
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include <explorer/moNeighborhoodExplorer.h>
|
||||
#include <comparator/moNeighborComparator.h>
|
||||
#include <comparator/moSolNeighborComparator.h>
|
||||
|
|
@ -67,6 +69,9 @@ public:
|
|||
moMetropolisHastingExplorer(Neighborhood& _neighborhood, moEval<Neighbor>& _eval, moNeighborComparator<Neighbor>& _neighborComparator, moSolNeighborComparator<Neighbor>& _solNeighborComparator, unsigned int _nbStep) : moNeighborhoodExplorer<Neighborhood>(_neighborhood, _eval), neighborComparator(_neighborComparator), solNeighborComparator(_solNeighborComparator), nbStep(_nbStep) {
|
||||
isAccept = false;
|
||||
current=new Neighbor();
|
||||
if(!neighborhood.isRandom()){
|
||||
std::cout << "moMetropolisHastingExplorer::Warning -> the neighborhood used is not random" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -69,6 +69,9 @@ public:
|
|||
nbStep(_nbStep) {
|
||||
isAccept = false;
|
||||
current=new Neighbor();
|
||||
if(!neighborhood.isRandom()){
|
||||
std::cout << "moRandomNeutralWalkExplorer::Warning -> the neighborhood used is not random" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -63,6 +63,9 @@ public:
|
|||
current=new Neighbor();
|
||||
// number of step done
|
||||
step = 0;
|
||||
if(!neighborhood.isRandom()){
|
||||
std::cout << "moRandomWalkExplorer::Warning -> the neighborhood used is not random" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -110,8 +110,7 @@ public:
|
|||
*/
|
||||
virtual void updateParam(EOT& _solution)
|
||||
{
|
||||
if ((*this).moveApplied())
|
||||
{
|
||||
if ((*this).moveApplied()){
|
||||
tabuList.add(_solution, *best);
|
||||
intensification.add(_solution, *best);
|
||||
diversification.add(_solution, *best);
|
||||
|
|
@ -126,10 +125,9 @@ public:
|
|||
|
||||
|
||||
/**
|
||||
* terminate : NOTHING TO DO
|
||||
* terminate : _solution becomes the best so far
|
||||
*/
|
||||
virtual void terminate(EOT & _solution)
|
||||
{
|
||||
virtual void terminate(EOT & _solution){
|
||||
_solution= bestSoFar;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@ public:
|
|||
/**
|
||||
* Constructor
|
||||
* @param _maxSize maximum size of the tabu list
|
||||
* @param _howlong how many iteration a move is tabu
|
||||
*/
|
||||
moSolVectorTabuList(unsigned int _maxSize) : maxSize(_maxSize){
|
||||
moSolVectorTabuList(unsigned int _maxSize, unsigned int _howlong) : maxSize(_maxSize), howlong(_howlong){
|
||||
tabuList.reserve(_maxSize);
|
||||
tabuList.resize(0);
|
||||
}
|
||||
|
|
@ -38,12 +39,18 @@ public:
|
|||
* @param _neighbor the current neighbor (unused)
|
||||
*/
|
||||
virtual void add(EOT & _sol, Neighbor & _neighbor){
|
||||
if(tabuList.size() < maxSize)
|
||||
tabuList.push_back(_sol);
|
||||
else{
|
||||
tabuList[index%maxSize] = _sol;
|
||||
index++;
|
||||
}
|
||||
|
||||
if(tabuList.size() < maxSize){
|
||||
std::pair<EOT, unsigned int> tmp;
|
||||
tmp.first=_sol;
|
||||
tmp.second=howlong;
|
||||
tabuList.push_back(tmp);
|
||||
}
|
||||
else{
|
||||
tabuList[index%maxSize].first = _sol;
|
||||
tabuList[index%maxSize].second = howlong;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -51,7 +58,10 @@ public:
|
|||
* @param _sol the current solution
|
||||
* @param _neighbor the current neighbor (unused)
|
||||
*/
|
||||
virtual void update(EOT & _sol, Neighbor & _neighbor){}
|
||||
virtual void update(EOT & _sol, Neighbor & _neighbor){
|
||||
for(unsigned int i=0; i<tabuList.size(); i++)
|
||||
tabuList[i].second--;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if the solution is tabu
|
||||
|
|
@ -63,7 +73,7 @@ public:
|
|||
EOT tmp=_sol;
|
||||
_neighbor.move(tmp);
|
||||
for(unsigned int i=0; i<tabuList.size(); i++){
|
||||
if (tabuList[i] == tmp)
|
||||
if ((howlong > 0 && tabuList[i].second > 0 && tabuList[i].first == tmp) || (howlong==0 && tabuList[i].first==tmp))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
@ -79,9 +89,13 @@ public:
|
|||
|
||||
|
||||
private:
|
||||
|
||||
std::vector<EOT> tabuList;
|
||||
//tabu list
|
||||
std::vector< std::pair<EOT, unsigned int> > tabuList;
|
||||
//maximum size of the tabu list
|
||||
unsigned int maxSize;
|
||||
//how many iteration a move is tabu
|
||||
unsigned int howlong;
|
||||
//index on the tabulist
|
||||
unsigned long index;
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -54,6 +54,13 @@ public:
|
|||
*/
|
||||
moNeighborhood(){}
|
||||
|
||||
/**
|
||||
* @return if the neighborhood is random
|
||||
*/
|
||||
virtual bool isRandom(){
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a solution has (again) a Neighbor
|
||||
* @param _solution the related solution
|
||||
|
|
|
|||
|
|
@ -33,6 +33,13 @@ Contact: paradiseo-help@lists.gforge.inria.fr
|
|||
#include <neighborhood/moNeighborhood.h>
|
||||
|
||||
template< class Neighbor >
|
||||
class moRndNeighborhood : virtual public moNeighborhood<Neighbor>{};
|
||||
class moRndNeighborhood : virtual public moNeighborhood<Neighbor>{
|
||||
|
||||
public:
|
||||
|
||||
bool isRandom(){
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -51,12 +51,10 @@
|
|||
#include <utils/eoMonitor.h>
|
||||
#include <utils/eoUpdater.h>
|
||||
|
||||
|
||||
|
||||
typedef eoBit<eoMinimizingFitness> bitVector;
|
||||
typedef moBitNeighbor<eoMinimizingFitness> bitNeighbor ;
|
||||
|
||||
class moDummyRndNeighborhood: public moOrderNeighborhood<bitNeighbor>, public moRndNeighborhood<bitNeighbor> {
|
||||
class moDummyRndNeighborhood: public moOrderNeighborhood<bitNeighbor>/*, public moRndNeighborhood<bitNeighbor>*/{
|
||||
public:
|
||||
moDummyRndNeighborhood(unsigned int a): moOrderNeighborhood<bitNeighbor>(a){}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -59,6 +59,8 @@ int main(){
|
|||
n3.fitness(3);
|
||||
n4.fitness(3);
|
||||
|
||||
|
||||
//verification qu'on update bien le best so far quand il faut
|
||||
test.init(sol1);
|
||||
assert(test.getBest()==sol1);
|
||||
assert(!test(sol2,n2));
|
||||
|
|
|
|||
|
|
@ -52,6 +52,8 @@ int main(){
|
|||
|
||||
s.fitness(17);
|
||||
|
||||
|
||||
//verification que toutes les operateurs sont bien apellés
|
||||
moSolutionStat< eoBit< eoMinimizingFitness > > stat;
|
||||
updater1 up1(a);
|
||||
updater1 up2(b);
|
||||
|
|
|
|||
|
|
@ -48,6 +48,8 @@ int main(){
|
|||
moCounterMonitorSaver test(3, mon1);
|
||||
test.add(mon2);
|
||||
|
||||
//on verifie qu'on apelle les moniteurs seulement tout les 3 itération
|
||||
|
||||
test();
|
||||
assert(a==2 && b==11);
|
||||
test();
|
||||
|
|
@ -56,6 +58,8 @@ int main(){
|
|||
assert(a==2 && b==11);
|
||||
test();
|
||||
assert(a==3 && b==12);
|
||||
|
||||
//test du lastCall
|
||||
test.lastCall();
|
||||
assert(a==4 && b==13);
|
||||
|
||||
|
|
|
|||
|
|
@ -54,6 +54,8 @@ int main(){
|
|||
sol3.push_back(true);
|
||||
sol3.push_back(true);
|
||||
|
||||
//verification de la stat avec une distance de Hamming
|
||||
|
||||
eoHammingDistance< eoBit<int> > dist;
|
||||
|
||||
moDistanceStat< eoBit<int> > test(dist, sol1);
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ int main(){
|
|||
|
||||
std::cout << "[t-moFirstImprExplorer] => START" << std::endl;
|
||||
|
||||
//Instanciation
|
||||
eoBit<eoMinimizingFitness> sol(4, true);
|
||||
sol.fitness(4);
|
||||
bitNeighborhood nh(4);
|
||||
|
|
@ -49,6 +50,8 @@ int main(){
|
|||
|
||||
moFirstImprExplorer<bitNeighborhood> test(nh, eval, ncomp, sncomp);
|
||||
|
||||
//on verifie qu'on améliore peut continuer à explorer tant qu'on améliore la solution
|
||||
|
||||
test(sol);
|
||||
assert(test.accept(sol));
|
||||
test.move(sol);
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ int main(){
|
|||
|
||||
std::cout << "[t-moHCneutralExplorer] => START" << std::endl;
|
||||
|
||||
//Instanciation
|
||||
eoBit<eoMinimizingFitness> sol(4, true);
|
||||
sol.fitness(4);
|
||||
bitNeighborhood nh(4);
|
||||
|
|
@ -49,6 +50,8 @@ int main(){
|
|||
|
||||
moHCneutralExplorer<bitNeighborhood> test(nh, eval, ncomp, sncomp,3);
|
||||
|
||||
//on verifie qu'on ameliore bien la solution et que l'exploration dure 3 itérations
|
||||
|
||||
test.initParam(sol);
|
||||
test(sol);
|
||||
assert(test.accept(sol));
|
||||
|
|
@ -57,7 +60,7 @@ int main(){
|
|||
test.updateParam(sol);
|
||||
assert(test.isContinue(sol));
|
||||
|
||||
|
||||
//les affichages permettent de voir qu'on prend pas toujours les mm voisins(lancer plusieurs fois l'exe)
|
||||
std::cout << sol << std::endl;
|
||||
|
||||
test(sol);
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ int main(){
|
|||
|
||||
std::cout << "[t-moMetropolisHastingExplorer] => START" << std::endl;
|
||||
|
||||
//Instanciation
|
||||
eoBit<eoMinimizingFitness> sol(4, true);
|
||||
sol.fitness(4);
|
||||
bitNeighborhood nh(4);
|
||||
|
|
@ -47,6 +48,7 @@ int main(){
|
|||
|
||||
moMetropolisHastingExplorer<bitNeighborhood> test(nh, eval, ncomp, sncomp, 3);
|
||||
|
||||
//test de l'acceptation d'un voisin améliorant
|
||||
test.initParam(sol);
|
||||
test(sol);
|
||||
assert(test.accept(sol));
|
||||
|
|
@ -57,6 +59,7 @@ int main(){
|
|||
|
||||
unsigned int oui=0, non=0;
|
||||
|
||||
//test de l'acceptation d'un voisin non améliorant
|
||||
for(unsigned int i=0; i<1000; i++){
|
||||
test(sol);
|
||||
if(test.accept(sol))
|
||||
|
|
@ -70,11 +73,13 @@ int main(){
|
|||
|
||||
assert(oui > 700 && oui < 800); //verification grossiere
|
||||
|
||||
//test du critere d'arret
|
||||
test.updateParam(sol);
|
||||
assert(test.isContinue(sol));
|
||||
test.updateParam(sol);
|
||||
assert(!test.isContinue(sol));
|
||||
|
||||
//test de l'acceptation d'un voisin
|
||||
sol[0]=false;
|
||||
sol[1]=false;
|
||||
sol[2]=false;
|
||||
|
|
|
|||
|
|
@ -41,15 +41,16 @@ int main(){
|
|||
|
||||
std::cout << "[t-moNeighbor] => START" << std::endl;
|
||||
|
||||
//test constructor
|
||||
moDummyNeighbor test1, test2;
|
||||
|
||||
test1.fitness(3);
|
||||
//test operateur d'affectation
|
||||
test2=test1;
|
||||
|
||||
assert(test1.fitness()==test2.fitness());
|
||||
|
||||
//test operateur de copy
|
||||
moDummyNeighbor test3(test1);
|
||||
|
||||
assert(test1.fitness()==test3.fitness());
|
||||
|
||||
test1.printOn(std::cout);
|
||||
|
|
|
|||
|
|
@ -45,6 +45,8 @@ int main(){
|
|||
dummyEvalOneMax eval2(4);
|
||||
moSolNeighborComparator<bitNeighbor> sncomp;
|
||||
|
||||
//test avec la fonction d'eval classique
|
||||
//on verifie qu'on ne trouve pas de voisin de mm fitness
|
||||
moRandomNeutralWalkExplorer<bitNeighborhood> test(nh, eval, sncomp, 3);
|
||||
|
||||
test.initParam(sol);
|
||||
|
|
@ -52,6 +54,8 @@ int main(){
|
|||
assert(!test.accept(sol));
|
||||
assert(!test.isContinue(sol));
|
||||
|
||||
//test avec une fonction d'eval bidon qui renvoie toujours la mm fitness
|
||||
//on peut donc verifier qu'on s'arette au bout des 3 itérations.
|
||||
moRandomNeutralWalkExplorer<bitNeighborhood> test2(nh, eval2, sncomp, 3);
|
||||
|
||||
sol.fitness(2);
|
||||
|
|
|
|||
|
|
@ -43,6 +43,8 @@ int main(){
|
|||
bitNeighborhood nh(4);
|
||||
evalOneMax eval(4);
|
||||
|
||||
//test avec un neighbordhood ordonné
|
||||
//Du coup on verifie juste qu'on a bien une evolution de la solution et qu'on fait 3 pas avant d'arreter l'exploration
|
||||
moRandomWalkExplorer<bitNeighborhood> test(nh, eval, 3);
|
||||
|
||||
test.initParam(sol);
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ int main(){
|
|||
|
||||
test.init(sol,n);
|
||||
|
||||
//on s'assure qu'on a bien toujours bien l'index 0, 1 ou 2 qui est renvoyé
|
||||
for(unsigned int i=0; i<100; i++){
|
||||
|
||||
a=n.index();
|
||||
|
|
|
|||
|
|
@ -43,12 +43,15 @@ int main(){
|
|||
eoBit<int> sol;
|
||||
moBitNeighbor<int> n;
|
||||
|
||||
//instanciation
|
||||
moRndWithoutReplNeighborhood< moBitNeighbor<int> > test(3);
|
||||
moRndWithoutReplNeighborhood< moBitNeighbor<int> > test2(0);
|
||||
|
||||
//on verifie que test a bien des voisins et que test2 n'en a pas
|
||||
assert(test.hasNeighbor(sol));
|
||||
assert(!test2.hasNeighbor(sol));
|
||||
|
||||
//on recupere successivement les index
|
||||
test.init(sol, n);
|
||||
assert(test.cont(sol));
|
||||
a=test.position();
|
||||
|
|
@ -59,6 +62,7 @@ int main(){
|
|||
assert(!test.cont(sol));
|
||||
c=test.position();
|
||||
|
||||
//on s'assure qu'on a bien 0, 1 et 2 (dans un ordre aléatoire)
|
||||
assert(a==0 || b==0 || c==0);
|
||||
assert(a==1 || b==1 || c==1);
|
||||
assert(a==2 || b==2 || c==2);
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ int main(){
|
|||
|
||||
std::cout << "[t-moSimpleHCneutralExplorer] => START" << std::endl;
|
||||
|
||||
//instanciation
|
||||
eoBit<eoMinimizingFitness> sol(4, true);
|
||||
sol.fitness(4);
|
||||
bitNeighborhood nh(4);
|
||||
|
|
@ -49,6 +50,7 @@ int main(){
|
|||
|
||||
moSimpleHCneutralExplorer<bitNeighborhood> test(nh, eval, ncomp, sncomp);
|
||||
|
||||
//test qu'on ameliore bien a chaque itération
|
||||
test.initParam(sol);
|
||||
test(sol);
|
||||
assert(test.accept(sol));
|
||||
|
|
@ -57,6 +59,7 @@ int main(){
|
|||
assert(test.isContinue(sol));
|
||||
test.updateParam(sol);
|
||||
|
||||
//les affichages permettent de voir qu'on choisit pas toujours les mm voisins améliorant (lancer plusieurs fois l'exe)
|
||||
std::cout << sol << std::endl;
|
||||
|
||||
test(sol);
|
||||
|
|
|
|||
|
|
@ -38,7 +38,8 @@ int main(){
|
|||
|
||||
std::cout << "[t-moSolVectorTabuList] => START" << std::endl;
|
||||
|
||||
moSolVectorTabuList<bitNeighbor> test(2);
|
||||
//test without countdown
|
||||
moSolVectorTabuList<bitNeighbor> test(2,0);
|
||||
bitNeighbor n1;
|
||||
bitNeighbor n2;
|
||||
bitNeighbor n3;
|
||||
|
|
@ -58,8 +59,13 @@ int main(){
|
|||
sol4[0]=false;
|
||||
sol4[1]=false;
|
||||
|
||||
//init
|
||||
test.init(sol1);
|
||||
|
||||
//ajout d'une sol tabu
|
||||
test.add(sol1,n1);
|
||||
|
||||
//verification des voisins de chaques sol
|
||||
assert(test.check(sol2,n1));
|
||||
assert(!test.check(sol2,n2));
|
||||
assert(!test.check(sol2,n3));
|
||||
|
|
@ -88,6 +94,59 @@ int main(){
|
|||
assert(!test.check(sol2,n1));
|
||||
assert(test.check(sol2,n2));
|
||||
|
||||
//test with a countdown at 3
|
||||
moSolVectorTabuList<bitNeighbor> test2(2,2);
|
||||
test2.init(sol1);
|
||||
test2.add(sol1,n1);
|
||||
assert(test2.check(sol2,n1));
|
||||
assert(!test2.check(sol2,n2));
|
||||
assert(!test2.check(sol2,n3));
|
||||
assert(!test2.check(sol2,n4));
|
||||
|
||||
assert(!test2.check(sol3,n1));
|
||||
assert(test2.check(sol3,n2));
|
||||
assert(!test2.check(sol3,n3));
|
||||
assert(!test2.check(sol3,n4));
|
||||
|
||||
assert(!test2.check(sol4,n1));
|
||||
assert(!test2.check(sol4,n2));
|
||||
assert(!test2.check(sol4,n3));
|
||||
assert(!test2.check(sol4,n4));
|
||||
|
||||
//coutdown sol1 -> 1
|
||||
test2.update(sol1,n1);
|
||||
assert(test2.check(sol2,n1));
|
||||
assert(!test2.check(sol2,n2));
|
||||
assert(!test2.check(sol2,n3));
|
||||
assert(!test2.check(sol2,n4));
|
||||
|
||||
assert(!test2.check(sol3,n1));
|
||||
assert(test2.check(sol3,n2));
|
||||
assert(!test2.check(sol3,n3));
|
||||
assert(!test2.check(sol3,n4));
|
||||
|
||||
assert(!test2.check(sol4,n1));
|
||||
assert(!test2.check(sol4,n2));
|
||||
assert(!test2.check(sol4,n3));
|
||||
assert(!test2.check(sol4,n4));
|
||||
|
||||
//coutdown sol1 -> 0 : sol1 is no longer tabu
|
||||
test2.update(sol1,n1);
|
||||
assert(!test2.check(sol2,n1));
|
||||
assert(!test2.check(sol2,n2));
|
||||
assert(!test2.check(sol2,n3));
|
||||
assert(!test2.check(sol2,n4));
|
||||
|
||||
assert(!test2.check(sol3,n1));
|
||||
assert(!test2.check(sol3,n2));
|
||||
assert(!test2.check(sol3,n3));
|
||||
assert(!test2.check(sol3,n4));
|
||||
|
||||
assert(!test2.check(sol4,n1));
|
||||
assert(!test2.check(sol4,n2));
|
||||
assert(!test2.check(sol4,n3));
|
||||
assert(!test2.check(sol4,n4));
|
||||
|
||||
std::cout << "[t-moSolVectorTabuList] => OK" << std::endl;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ int main(){
|
|||
moSolutionStat< eoBit<unsigned int > > test;
|
||||
|
||||
test(s);
|
||||
//on verifie que la solution est bien enregistré
|
||||
|
||||
assert(test.value()[0]);
|
||||
assert(test.value()[1]);
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ int main(){
|
|||
|
||||
std::cout << "[t-moTSExplorer] => START" << std::endl;
|
||||
|
||||
//instansiation
|
||||
eoBit<eoMinimizingFitness> sol(4, true);
|
||||
sol.fitness(4);
|
||||
bitNeighborhood nh(4);
|
||||
|
|
@ -51,7 +52,7 @@ int main(){
|
|||
moSolNeighborComparator<bitNeighbor> sncomp;
|
||||
moDummyIntensification<bitNeighbor> intens;
|
||||
moDummyDiversification<bitNeighbor> diver;
|
||||
moSolVectorTabuList<bitNeighbor> tabuList(4);
|
||||
moSolVectorTabuList<bitNeighbor> tabuList(4,0);
|
||||
moBestImprAspiration<bitNeighbor> aspir;
|
||||
|
||||
moTSExplorer<bitNeighborhood> test(nh, eval, ncomp, sncomp, tabuList, intens, diver, aspir);
|
||||
|
|
@ -152,6 +153,7 @@ int main(){
|
|||
test3.moveApplied(true);
|
||||
test3.updateParam(sol2);
|
||||
|
||||
//on a rempli la liste tabu pour que tout les voisins soit tabu
|
||||
test3(sol2);
|
||||
assert(!test3.accept(sol2));
|
||||
|
||||
|
|
|
|||
|
|
@ -151,7 +151,7 @@ void main_function(int argc, char **argv)
|
|||
*
|
||||
* ========================================================= */
|
||||
|
||||
moSolVectorTabuList<Neighbor> tl(10);
|
||||
moSolVectorTabuList<Neighbor> tl(10,10);
|
||||
moDummyIntensification<Neighbor> inten;
|
||||
moDummyDiversification<Neighbor> div;
|
||||
moBestImprAspiration<Neighbor> asp;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue