tests added

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1702 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
jhumeau 2010-03-19 14:16:47 +00:00
commit 3c08dd0157
11 changed files with 260 additions and 9 deletions

View file

@ -44,6 +44,7 @@
/**
* Explorer for the Metropolis-Hasting Sampling
* Only the symetric case is considered when Q(x,y) = Q(y,x)
* Fitness must be > 0
*/
template< class Neighborhood >
class moMetropolisHastingExplorer : public moNeighborhoodExplorer<Neighborhood>
@ -103,8 +104,6 @@ public:
* @param _solution
*/
virtual void operator()(EOT & _solution){
//est qu'on peut initializer
//Test if _solution has a Neighbor
if(neighborhood.hasNeighbor(_solution)){
//init the first neighbor
@ -151,11 +150,18 @@ public:
isAccept = true;
else{
if(_solution.fitness() != 0){
alpha = (double) current->fitness() / (double) _solution.fitness();
if( (double)current->fitness() < (double)_solution.fitness()) // maximizing
alpha = (double) current->fitness() / (double) _solution.fitness();
else //minimizing
alpha = (double) _solution.fitness() / (double) current->fitness();
isAccept = (rng.uniform() < alpha) ;
}
else
isAccept = false;
else{
if( (double)current->fitness() < (double)_solution.fitness()) // maximizing
isAccept = true;
else
isAccept = false;
}
}
}
return isAccept;

View file

@ -41,6 +41,7 @@
/**
* Explorer for a random neutral walk
* accept the movement when the neighbor has the same fitnes
* To sample the neutral networks by random walk, there is no memory
* neighborhood must be explored in random order
*/
template< class Neighborhood >

View file

@ -41,7 +41,7 @@
* A Indexed Neighborhood
*/
template< class Neighbor >
class moIndexNeighborhood : public moNeighborhood<Neighbor>
class moIndexNeighborhood : virtual public moNeighborhood<Neighbor>
{
public:
/**

View file

@ -0,0 +1,38 @@
/*
<moRndNeighborhood.h>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can ue,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef _moRndNeighborhood_h
#define _moRndNeighborhood_h
#include <neighborhood/moNeighborhood.h>
template< class Neighbor >
class moRndNeighborhood : virtual public moNeighborhood<Neighbor>{};
#endif

View file

@ -36,13 +36,14 @@
#define _moRndWithReplNeighborhood_h
#include <neighborhood/moIndexNeighborhood.h>
#include <neighborhood/moRndNeighborhood.h>
#include <utils/eoRNG.h>
/**
* A Random With replacement Neighborhood
*/
template< class N >
class moRndWithReplNeighborhood : public moIndexNeighborhood<N>
class moRndWithReplNeighborhood : public moIndexNeighborhood<N>, public moRndNeighborhood<N>
{
public:

View file

@ -36,13 +36,14 @@
#define _moRndWithoutReplNeighborhood_h
#include <neighborhood/moIndexNeighborhood.h>
#include <neighborhood/moRndNeighborhood.h>
#include <utils/eoRNG.h>
/**
* A Random without replacement Neighborhood
*/
template< class N >
class moRndWithoutReplNeighborhood : public moIndexNeighborhood<N>
class moRndWithoutReplNeighborhood : public moIndexNeighborhood<N>, public moRndNeighborhood<N>
{
public:

View file

@ -90,6 +90,7 @@
#include <neighborhood/moNeighbor.h>
#include <neighborhood/moNeighborhood.h>
#include <neighborhood/moOrderNeighborhood.h>
#include <neighborhood/moRndNeighborhood.h>
#include <neighborhood/moRndWithoutReplNeighborhood.h>
#include <neighborhood/moRndWithReplNeighborhood.h>

View file

@ -53,6 +53,8 @@ SET (TEST_LIST
t-moHCneutralExplorer
t-moFirstImprExplorer
t-moRandomWalkExplorer
t-moMetropolisHastingExplorer
t-moRandomNeutralWalkExplorer
)

View file

@ -40,6 +40,7 @@
#include <neighborhood/moNeighbor.h>
#include <neighborhood/moBackableNeighbor.h>
#include <neighborhood/moNeighborhood.h>
#include <neighborhood/moRndNeighborhood.h>
#include <eval/moEval.h>
#include <ga/eoBit.h>
@ -50,9 +51,17 @@
#include <utils/eoMonitor.h>
#include <utils/eoUpdater.h>
typedef eoBit<eoMinimizingFitness> bitVector;
typedef moBitNeighbor<eoMinimizingFitness> bitNeighbor ;
typedef moOrderNeighborhood<bitNeighbor> bitNeighborhood ;
class moDummyRndNeighborhood: public moOrderNeighborhood<bitNeighbor>, public moRndNeighborhood<bitNeighbor> {
public:
moDummyRndNeighborhood(unsigned int a): moOrderNeighborhood<bitNeighbor>(a){}
};
typedef moDummyRndNeighborhood bitNeighborhood ;
typedef EO<int> Solution;
@ -123,6 +132,22 @@ public:
}
};
class dummyEvalOneMax : public moEval< bitNeighbor >
{
private:
unsigned size;
public:
dummyEvalOneMax(unsigned _size) : size(_size) {};
~dummyEvalOneMax(void) {} ;
void operator() (bitVector& _sol, bitNeighbor& _n) {
unsigned int fit = _sol.fitness();
_n.fitness(fit);
}
};
class monitor1 : public eoMonitor
{
public:

View file

@ -0,0 +1,92 @@
/*
<t-moMetropolisHastingExplorer.cpp>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can ue,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <explorer/moMetropolisHastingExplorer.h>
#include "moTestClass.h"
#include <iostream>
#include <cstdlib>
#include <cassert>
int main(){
std::cout << "[t-moMetropolisHastingExplorer] => START" << std::endl;
eoBit<eoMinimizingFitness> sol(4, true);
sol.fitness(4);
bitNeighborhood nh(4);
evalOneMax eval(4);
moNeighborComparator<bitNeighbor> ncomp;
moSolNeighborComparator<bitNeighbor> sncomp;
moMetropolisHastingExplorer<bitNeighborhood> test(nh, eval, ncomp, sncomp, 3);
test.initParam(sol);
test(sol);
assert(test.accept(sol));
test.move(sol);
assert(sol.fitness()==3);
test.updateParam(sol);
assert(test.isContinue(sol));
unsigned int oui=0, non=0;
for(unsigned int i=0; i<1000; i++){
test(sol);
if(test.accept(sol))
oui++;
else
non++;
}
std::cout << "Attention test en fonction d'une proba \"p\" uniforme dans [0,1] , oui si p < 3/4, non sinon -> resultat sur 1000 essai" << std::endl;
std::cout << "oui: " << oui << std::endl;
std::cout << "non: " << non << std::endl;
assert(oui > 700 && oui < 800); //verification grossiere
test.updateParam(sol);
assert(test.isContinue(sol));
test.updateParam(sol);
assert(!test.isContinue(sol));
sol[0]=false;
sol[1]=false;
sol[2]=false;
sol[3]=false;
sol.fitness(0);
test.initParam(sol);
test(sol);
assert(!test.accept(sol));
std::cout << "[t-moMetropolisHastingExplorer] => OK" << std::endl;
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,84 @@
/*
<t-moRandomNeutralWalkExplorer.cpp>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010
Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can ue,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <explorer/moRandomNeutralWalkExplorer.h>
#include "moTestClass.h"
#include <iostream>
#include <cstdlib>
#include <cassert>
int main(){
std::cout << "[t-moRandomNeutralWalkExplorer] => START" << std::endl;
eoBit<eoMinimizingFitness> sol(4, true);
sol.fitness(4);
bitNeighborhood nh(4);
evalOneMax eval(4);
dummyEvalOneMax eval2(4);
moSolNeighborComparator<bitNeighbor> sncomp;
moRandomNeutralWalkExplorer<bitNeighborhood> test(nh, eval, sncomp, 3);
test.initParam(sol);
test(sol);
assert(!test.accept(sol));
assert(!test.isContinue(sol));
moRandomNeutralWalkExplorer<bitNeighborhood> test2(nh, eval2, sncomp, 3);
sol.fitness(2);
test2.initParam(sol);
test2(sol);
assert(test2.accept(sol));
test2.move(sol);
assert(sol.fitness()==2);
test2.updateParam(sol);
assert(test2.isContinue(sol));
test2(sol);
assert(test2.accept(sol));
test2.move(sol);
assert(sol.fitness()==2);
test2.updateParam(sol);
assert(test2.isContinue(sol));
test2(sol);
assert(test2.accept(sol));
test2.move(sol);
assert(sol.fitness()==2);
test2.updateParam(sol);
assert(!test2.isContinue(sol));
std::cout << "[t-moRandomNeutralWalkExplorer] => OK" << std::endl;
return EXIT_SUCCESS;
}