git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1344 331e1502-861f-0410-8da2-ba01fb791d7f

This commit is contained in:
jhumeau 2009-01-16 13:24:23 +00:00
commit 95ae2ce2a8
4 changed files with 550 additions and 0 deletions

View file

@ -0,0 +1,213 @@
/*
* <moeoNSGA.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008
* (C) OPAC Team, LIFL, 2002-2008
*
* 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 use,
* 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".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited liability.
*
* 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 MOEOMOGA_H_
#define MOEOMOGA_H_
#include <eoBreed.h>
#include <eoContinue.h>
#include <eoEvalFunc.h>
#include <eoGenContinue.h>
#include <eoGeneralBreeder.h>
#include <eoGenOp.h>
#include <eoPopEvalFunc.h>
#include <eoSGAGenOp.h>
#include <algo/moeoEA.h>
#include <diversity/moeoFrontByFrontSharingDiversityAssignment.h>
#include <fitness/moeoDominanceRankFitnessAssignment.h>
#include <replacement/moeoElitistReplacement.h>
#include <selection/moeoDetTournamentSelect.h>
/**
* MOGA.
*/
template < class MOEOT >
class moeoMOGA: public moeoEA < MOEOT >
{
public:
/**
* Ctor with a crossover, a mutation and their corresponding rates.
* @param _maxGen maximum number of generations before stopping
* @param _eval evaluation function
* @param _crossover crossover
* @param _pCross crossover probability
* @param _mutation mutation
* @param _pMut mutation probability
* @param _nicheSize niche size
*/
moeoMOGA (unsigned int _maxGen, eoEvalFunc < MOEOT > & _eval, eoQuadOp < MOEOT > & _crossover, double _pCross, eoMonOp < MOEOT > & _mutation, double _pMut, double _nicheSize = 0.5) :
defaultGenContinuator(_maxGen), continuator(defaultGenContinuator), eval(_eval), defaultPopEval(_eval), popEval(defaultPopEval), select (2), selectMany(select,0.0), selectTransform(defaultSelect, defaultTransform), defaultSGAGenOp(_crossover, _pCross, _mutation, _pMut), genBreed (select, defaultSGAGenOp), breed (genBreed), diversityAssignment(_nicheSize), replace (fitnessAssignment, diversityAssignment)
{}
/**
* Ctor with a eoContinue and a eoGenOp.
* @param _continuator stopping criteria
* @param _eval evaluation function
* @param _op variation operators
* @param _nicheSize niche size
*/
moeoMOGA (eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoGenOp < MOEOT > & _op, double _nicheSize = 0.5) :
defaultGenContinuator(0), continuator(_continuator), eval(_eval), defaultPopEval(_eval), popEval(defaultPopEval), select(2),
selectMany(select,0.0), selectTransform(defaultSelect, defaultTransform), defaultSGAGenOp(defaultQuadOp, 1.0, defaultMonOp, 1.0), genBreed(select, _op), breed(genBreed), diversityAssignment(_nicheSize), replace (fitnessAssignment, diversityAssignment)
{}
/**
* Ctor with a eoContinue, a eoPopEval and a eoGenOp.
* @param _continuator stopping criteria
* @param _popEval population evaluation function
* @param _op variation operators
* @param _nicheSize niche size
*/
moeoMOGA (eoContinue < MOEOT > & _continuator, eoPopEvalFunc < MOEOT > & _popEval, eoGenOp < MOEOT > & _op, double _nicheSize = 0.5) :
defaultGenContinuator(0), continuator(_continuator), eval(defaultEval), defaultPopEval(eval), popEval(_popEval), select(2),
selectMany(select,0.0), selectTransform(defaultSelect, defaultTransform), defaultSGAGenOp(defaultQuadOp, 1.0, defaultMonOp, 1.0), genBreed(select, _op), breed(genBreed), diversityAssignment(_nicheSize), replace (fitnessAssignment, diversityAssignment)
{}
/**
* Ctor with a eoContinue and a eoTransform.
* @param _continuator stopping criteria
* @param _eval evaluation function
* @param _transform variation operator
* @param _nicheSize niche size
*/
moeoMOGA (eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoTransform < MOEOT > & _transform, double _nicheSize = 0.5) :
defaultGenContinuator(0), continuator(_continuator), eval(_eval), defaultPopEval(_eval), popEval(defaultPopEval),
select(2), selectMany(select, 1.0), selectTransform(selectMany, _transform), defaultSGAGenOp(defaultQuadOp, 0.0, defaultMonOp, 0.0), genBreed(select, defaultSGAGenOp), breed(selectTransform), diversityAssignment(_nicheSize), replace(fitnessAssignment, diversityAssignment)
{}
/**
* Ctor with a eoContinue, a eoPopEval and a eoTransform.
* @param _continuator stopping criteria
* @param _popEval population evaluation function
* @param _transform variation operator
* @param _nicheSize niche size
*/
moeoMOGA (eoContinue < MOEOT > & _continuator, eoPopEvalFunc < MOEOT > & _popEval, eoTransform < MOEOT > & _transform, double _nicheSize = 0.5) :
defaultGenContinuator(0), continuator(_continuator), eval(defaultEval), defaultPopEval(eval), popEval(_popEval),
select(2), selectMany(select, 1.0), selectTransform(selectMany, _transform), defaultSGAGenOp(defaultQuadOp, 0.0, defaultMonOp, 0.0), genBreed(select, defaultSGAGenOp), breed(selectTransform), diversityAssignment(_nicheSize), replace(fitnessAssignment, diversityAssignment)
{}
/**
* Apply the algorithm to the population _pop until the stopping criteria is satified.
* @param _pop the population
*/
virtual void operator () (eoPop < MOEOT > &_pop)
{
eoPop < MOEOT > offspring, empty_pop;
popEval (empty_pop, _pop); // a first eval of _pop
// evaluate fitness and diversity
fitnessAssignment(_pop);
diversityAssignment(_pop);
do
{
// generate offspring, worths are recalculated if necessary
breed (_pop, offspring);
// eval of offspring
popEval (_pop, offspring);
// after replace, the new pop is in _pop. Worths are recalculated if necessary
replace (_pop, offspring);
}
while (continuator (_pop));
}
protected:
/** a continuator based on the number of generations (used as default) */
eoGenContinue < MOEOT > defaultGenContinuator;
/** stopping criteria */
eoContinue < MOEOT > & continuator;
/** default eval */
class DummyEval : public eoEvalFunc < MOEOT >
{
public:
void operator()(MOEOT &) {}
}
defaultEval;
/** evaluation function */
eoEvalFunc < MOEOT > & eval;
/** default popEval */
eoPopLoopEval < MOEOT > defaultPopEval;
/** evaluation function used to evaluate the whole population */
eoPopEvalFunc < MOEOT > & popEval;
/** default select */
class DummySelect : public eoSelect < MOEOT >
{
public :
void operator()(const eoPop<MOEOT>&, eoPop<MOEOT>&) {}
}
defaultSelect;
/** binary tournament selection */
moeoDetTournamentSelect < MOEOT > select;
/** default select many */
eoSelectMany < MOEOT > selectMany;
/** select transform */
eoSelectTransform < MOEOT > selectTransform;
/** a default crossover */
eoQuadCloneOp < MOEOT > defaultQuadOp;
/** a default mutation */
eoMonCloneOp < MOEOT > defaultMonOp;
/** an object for genetic operators (used as default) */
eoSGAGenOp < MOEOT > defaultSGAGenOp;
/** default transform */
class DummyTransform : public eoTransform < MOEOT >
{
public :
void operator()(eoPop<MOEOT>&) {}
}
defaultTransform;
/** general breeder */
eoGeneralBreeder < MOEOT > genBreed;
/** breeder */
eoBreed < MOEOT > & breed;
/** fitness assignment used in NSGA-II */
moeoDominanceRankFitnessAssignment < MOEOT > fitnessAssignment;
/** diversity assignment used in NSGA-II */
moeoFrontByFrontSharingDiversityAssignment < MOEOT > diversityAssignment;
/** elitist replacement */
moeoElitistReplacement < MOEOT > replace;
};
#endif /*MOEOMOGA_H_*/

View file

@ -0,0 +1,102 @@
/*
* <moeoAggregativeFitnessAssignment.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Lille-Nord Europe, 2006-2008
* (C) OPAC Team, LIFL, 2002-2008
*
* Arnaud Liefooghe
* Jeremie Humeau
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* 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".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited liability.
*
* 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
*
*/
//-----------------------------------------------------------------------------
// moeoAggregativeFitnessAssignment.h
//-----------------------------------------------------------------------------
#ifndef MOEOAGGREGATIVEFITNESSASSIGNMENT_H_
#define MOEOAGGREGATIVEFITNESSASSIGNMENT_H_
#include <vector>
#include <eoPop.h>
/*
* Fitness assignment scheme which used weight foreach objectives
*/
template < class MOEOT >
class moeoAggregativeFitnessAssignment : public moeoFitnessAssignment < MOEOT >
{
public:
/** the objective vector type of the solutions */
typedef typename MOEOT::ObjectiveVector ObjectiveVector;
/**
* Default ctor
* @param _weight vectors contains all weights.
*/
moeoAggregativeFitnessAssignment(std::vector<double> & _weight) : weight(_weight)
{}
/**
* Sets the fitness values for every solution contained in the population _pop (and in the archive)
* @param _pop the population
*/
void operator()(eoPop < MOEOT > & _pop)
{
unsigned int i= _pop.size();
unsigned int nb_obj= MOEOT::ObjectiveVector::nObjectives();
double res;
for (unsigned int k=0; k<i; k++){
res=0;
for(unsigned int l=0; l<nb_obj; l++)
res+=_pop[k].objectiveVector()[l] * weight[l];
_pop[k].fitness(res);
}
}
/**
* Warning: no yet implemented: Updates the fitness values of the whole population _pop by taking the deletion of the objective vector _objVec into account.
* @param _pop the population
* @param _objVec the objective vector
*/
void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec)
{
std::cout << "WARNING : updateByDeleting not implemented in moeoAssignmentFitnessAssignment" << std::endl;
}
private:
//the vector of weight
std::vector<double> weight;
};
#endif /*MOEOAGGREGATIVEFITNESSASSIGNMENT_H_*/

View file

@ -0,0 +1,115 @@
/*
* <t-moeoAggregativeFitnessAssignment.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Lille-Nord Europe, 2006-2008
* (C) OPAC Team, LIFL, 2002-2008
*
* Arnaud Liefooghe
* Jeremie Humeau
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* 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".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited liability.
*
* 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
*
*/
//-----------------------------------------------------------------------------
// t-moeoAggregativeFitnessAssignment.cpp
//-----------------------------------------------------------------------------
#include <eo>
#include <moeo>
#include <cassert>
//-----------------------------------------------------------------------------
class ObjectiveVectorTraits : public moeoObjectiveVectorTraits
{
public:
static bool minimizing (int i)
{
return true;
}
static bool maximizing (int i)
{
return false;
}
static unsigned int nObjectives ()
{
return 2;
}
};
typedef moeoRealObjectiveVector < ObjectiveVectorTraits > ObjectiveVector;
typedef MOEO < ObjectiveVector, double, double > Solution;
//-----------------------------------------------------------------------------
int main()
{
int res=EXIT_SUCCESS;
std::cout << "[moeoAggregativeFitnessAssignment] => ";
// objective vectors
ObjectiveVector obj0, obj1, obj2, obj3, obj4, obj5, obj6;
obj0[0] = 2;
obj0[1] = 5;
obj1[0] = 3;
obj1[1] = 3;
obj2[0] = 4;
obj2[1] = 1;
obj3[0] = 5;
obj3[1] = 5;
std::vector<double> poids;
poids.resize(2);
poids[0]=2;
poids[1]=3;
// population
eoPop < Solution > pop;
pop.resize(4);
pop[0].objectiveVector(obj0);
pop[1].objectiveVector(obj1);
pop[2].objectiveVector(obj2);
pop[3].objectiveVector(obj3);
moeoAggregativeFitnessAssignment < Solution > fitness(poids);
fitness(pop);
assert(pop[0].fitness() == 19.0);
assert(pop[1].fitness() == 15.0);
assert(pop[2].fitness() == 11.0);
assert(pop[3].fitness() == 25.0);
std::cout << "Ok" << std::endl;
return EXIT_SUCCESS;
}
//-----------------------------------------------------------------------------

View file

@ -0,0 +1,120 @@
/*
* <t-moeoMOGA.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
*
* Arnaud Liefooghe
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* 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".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited liability.
*
* 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
*
*/
//-----------------------------------------------------------------------------
// t-moeoMOGA.cpp
//-----------------------------------------------------------------------------
#include <eo>
#include <es/eoRealInitBounded.h>
#include <es/eoRealOp.h>
#include <moeo>
//-----------------------------------------------------------------------------
class ObjectiveVectorTraits : public moeoObjectiveVectorTraits
{
public:
static bool minimizing (int i)
{
return true;
}
static bool maximizing (int i)
{
return false;
}
static unsigned int nObjectives ()
{
return 2;
}
};
typedef moeoRealObjectiveVector < ObjectiveVectorTraits > ObjectiveVector;
class Solution : public moeoRealVector < ObjectiveVector, double, double >
{
public:
Solution() : moeoRealVector < ObjectiveVector, double, double > (1) {}
};
class TestEval : public moeoEvalFunc < Solution >
{
public:
void operator () (Solution & _sol)
{
ObjectiveVector objVec;
objVec[0] = _sol[0];
objVec[1] = _sol[0] * _sol[0];
_sol.objectiveVector(objVec);
}
};
//-----------------------------------------------------------------------------
int main()
{
std::cout << "[moeoMOGA]" << std::endl;
TestEval eval;
eoPopLoopEval <Solution> popEval(eval);
eoQuadCloneOp < Solution > xover;
eoUniformMutation < Solution > mutation(0.05);
eoRealVectorBounds bounds(1, 1.0, 2.0);
eoRealInitBounded < Solution > init(bounds);
eoPop < Solution > pop(20, init);
eoQuadGenOp <Solution> genOp(xover);
eoSGATransform < Solution > transform(xover, 0.1, mutation, 0.1);
eoGenContinue <Solution > continuator(10);
// build MOGA
moeoMOGA < Solution > algo(20, eval, xover, 1.0, mutation, 1.0);
moeoMOGA < Solution > algo2(continuator, eval, genOp);
moeoMOGA < Solution > algo3(continuator, popEval, genOp);
moeoMOGA < Solution > algo4(continuator, eval, transform);
moeoMOGA < Solution > algo5(continuator, popEval, transform);
// run the algo
algo(pop);
// final pop
std::cout << "Final population" << std::endl;
std::cout << pop << std::endl;
std::cout << "[moeoMOGA] OK" << std::endl;
return EXIT_SUCCESS;
}
//-----------------------------------------------------------------------------