Small modif in moIndexedSwapNeighbor

This commit is contained in:
verel 2014-06-25 17:02:38 +02:00
commit edefae4b28
17 changed files with 141 additions and 54 deletions

View file

@ -183,9 +183,9 @@ Easy, isn't it ?
By performing tests, you can check your installation. By performing tests, you can check your installation.
Testing is disable by default, except if you build with the full install type. Testing is disable by default, except if you build with the full install type.
To enable testing, define -DENABLE_TESTING when you launch cmake. To enable testing, define -DENABLE_CMAKE_TESTING=true when you launch cmake.
To perform tests simply type ctest ou make test. To perform tests simply type ctest or make test.
------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------
5.2 REPORTING 5.2 REPORTING

View file

@ -47,7 +47,7 @@ install(DIRECTORY do es ga gp other utils
add_subdirectory(es) add_subdirectory(es)
add_subdirectory(ga) add_subdirectory(ga)
add_subdirectory(utils) add_subdirectory(utils)
add_subdirectory(serial) #add_subdirectory(serial)
if(ENABLE_PYEO) if(ENABLE_PYEO)
add_subdirectory(pyeo) add_subdirectory(pyeo)

View file

@ -62,12 +62,35 @@ public:
}; };
/** /**
SV: from eoHammingDistance, it is in fact the L1 distance
This is a generic class for L1 distance computation: This is a generic class for L1 distance computation:
assumes the 2 things are std::vectors of something assumes the 2 things are std::vectors of something
that is double-castable that is double-castable
For bitstrings, this is the Hamming distance For bitstrings, this is the Hamming distance
*/ */
template< class EOT > template< class EOT >
class eoL1Distance : public eoDistance<EOT>
{
public:
double operator()(const EOT & _v1, const EOT & _v2)
{
double sum=0.0;
for (unsigned i=0; i<_v1.size(); i++)
{
double r = static_cast<double> (_v1[i]) - static_cast<double> (_v2[i]);
sum += fabs(r);
}
return sum;
}
};
/**
SV: change to have the Hamming (number of differences)
For bitstrings, this is the Hamming distance
*/
template< class EOT >
class eoHammingDistance : public eoDistance<EOT> class eoHammingDistance : public eoDistance<EOT>
{ {
public: public:
@ -76,8 +99,8 @@ public:
double sum=0.0; double sum=0.0;
for (unsigned i=0; i<_v1.size(); i++) for (unsigned i=0; i<_v1.size(); i++)
{ {
double r = static_cast<double> (_v1[i]) - static_cast<double> (_v2[i]); if (_v1[i] != _v2[i])
sum += fabs(r); sum++;
} }
return sum; return sum;
} }

View file

@ -69,6 +69,7 @@ set (TEST_LIST
#t-openmp # does not work anymore since functions used in this test were removed from EO #t-openmp # does not work anymore since functions used in this test were removed from EO
#t-eoDualFitness #t-eoDualFitness
t-eoParser t-eoParser
t-eoPartiallyMappedXover
) )

View file

@ -60,7 +60,7 @@ public:
*/ */
moRandomWalk(Neighborhood& _neighborhood, eoEvalFunc<EOT>& _fullEval, moEval<Neighbor>& _eval, unsigned _nbStepMax): moRandomWalk(Neighborhood& _neighborhood, eoEvalFunc<EOT>& _fullEval, moEval<Neighbor>& _eval, unsigned _nbStepMax):
moLocalSearch<Neighbor>(explorer, iterCont, _fullEval), moLocalSearch<Neighbor>(explorer, iterCont, _fullEval),
iterCont(_nbStepMax), iterCont(_nbStepMax, false),
explorer(_neighborhood, _eval) explorer(_neighborhood, _eval)
{} {}

View file

@ -53,7 +53,7 @@ public:
* Constructor * Constructor
* @param _param the parameter of type double to save in the vector * @param _param the parameter of type double to save in the vector
*/ */
moVectorMonitor(eoValueParam<double> & _param) : doubleParam(&_param), intParam(NULL), eotParam(NULL) moVectorMonitor(eoValueParam<double> & _param) : doubleParam(&_param), intParam(NULL), intLongParam(NULL), eotParam(NULL)
{ {
// precision of the output by default // precision of the output by default
precisionOutput = std::cout.precision(); precisionOutput = std::cout.precision();
@ -63,7 +63,17 @@ public:
* Default Constructor * Default Constructor
* @param _param the parameter of type unsigned int to save in the vector * @param _param the parameter of type unsigned int to save in the vector
*/ */
moVectorMonitor(eoValueParam<unsigned int> & _param) : doubleParam(NULL), intParam(&_param), eotParam(NULL) moVectorMonitor(eoValueParam<unsigned int> & _param) : doubleParam(NULL), intParam(&_param), intLongParam(NULL), eotParam(NULL)
{
// precision of the output by default
precisionOutput = std::cout.precision();
}
/**
* Default Constructor
* @param _param the parameter of type unsigned int to save in the vector
*/
moVectorMonitor(eoValueParam<unsigned long> & _param) : doubleParam(NULL), intParam(NULL), intLongParam(&_param), eotParam(NULL)
{ {
// precision of the output by default // precision of the output by default
precisionOutput = std::cout.precision(); precisionOutput = std::cout.precision();
@ -73,7 +83,7 @@ public:
* Default Constructor * Default Constructor
* @param _param the parameter of type EOT to save in the vector * @param _param the parameter of type EOT to save in the vector
*/ */
moVectorMonitor(eoValueParam<EOT> & _param) : doubleParam(NULL), intParam(NULL), eotParam(&_param) moVectorMonitor(eoValueParam<EOT> & _param) : doubleParam(NULL), intParam(NULL), intLongParam(NULL), eotParam(&_param)
{ {
// precision of the output by default // precision of the output by default
precisionOutput = std::cout.precision(); precisionOutput = std::cout.precision();
@ -84,7 +94,7 @@ public:
* @param _param the parameter of type eoScalarFitness to save in the vector * @param _param the parameter of type eoScalarFitness to save in the vector
*/ */
template <class ScalarType, class Compare> template <class ScalarType, class Compare>
moVectorMonitor(eoValueParam<eoScalarFitness<ScalarType, Compare> > & _param) : doubleParam( & (eoValueParam<double>&)_param), intParam(NULL), eotParam(NULL) moVectorMonitor(eoValueParam<eoScalarFitness<ScalarType, Compare> > & _param) : doubleParam( & (eoValueParam<double>&)_param), intParam(NULL), intLongParam(NULL), eotParam(NULL)
{ {
// precision of the output by default // precision of the output by default
precisionOutput = std::cout.precision(); precisionOutput = std::cout.precision();
@ -95,7 +105,7 @@ public:
* @param _param unvalid Parameter * @param _param unvalid Parameter
*/ */
template <class T> template <class T>
moVectorMonitor(eoValueParam<T> & _param) : doubleParam(NULL), intParam(NULL), eotParam(NULL) moVectorMonitor(eoValueParam<T> & _param) : doubleParam(NULL), intParam(NULL), intLongParam(NULL), eotParam(NULL)
{ {
std::cerr << "Sorry the type can not be in a vector of moVectorMonitor" << std::endl; std::cerr << "Sorry the type can not be in a vector of moVectorMonitor" << std::endl;
} }
@ -120,6 +130,9 @@ public:
else else
if (intParam != NULL) if (intParam != NULL)
valueVec.push_back((double) intParam->value()); valueVec.push_back((double) intParam->value());
else
if (intLongParam != NULL)
valueVec.push_back((double) intLongParam->value());
else else
eotVec.push_back(eotParam->value()); eotVec.push_back(eotParam->value());
return *this ; return *this ;
@ -227,6 +240,7 @@ public:
protected: protected:
eoValueParam<double> * doubleParam ; eoValueParam<double> * doubleParam ;
eoValueParam<unsigned int> * intParam ; eoValueParam<unsigned int> * intParam ;
eoValueParam<unsigned long> * intLongParam ;
eoValueParam<EOT> * eotParam ; eoValueParam<EOT> * eotParam ;
std::vector<double> valueVec; std::vector<double> valueVec;

View file

@ -190,6 +190,7 @@
//#include <problems/eval/moUBQPSimpleIncrEval.h> //#include <problems/eval/moUBQPSimpleIncrEval.h>
//#include <problems/eval/moUBQPdoubleIncrEvaluation.h> //#include <problems/eval/moUBQPdoubleIncrEvaluation.h>
//#include <problems/eval/moUBQPBitsIncrEval.h> //#include <problems/eval/moUBQPBitsIncrEval.h>
#include <problems/eval/moNKlandscapesIncrEval.h>
#include <sampling/moAdaptiveWalkSampling.h> #include <sampling/moAdaptiveWalkSampling.h>

View file

@ -121,6 +121,22 @@ public:
return (key == _neighbor.index()); return (key == _neighbor.index());
} }
/**
* Write object with its index
* @param _os A std::ostream.
*/
virtual void printOn(std::ostream& _os) const {
if (this->invalid()) {
_os << "INVALID ";
}
else
{
_os << this->fitness() << ' ';
}
_os << key ;
}
protected: protected:
// key allowing to describe the neighbor // key allowing to describe the neighbor
unsigned int key; unsigned int key;

View file

@ -130,7 +130,7 @@ public:
return "moRndWithoutReplNeighborhood"; return "moRndWithoutReplNeighborhood";
} }
private: protected:
unsigned int maxIndex; unsigned int maxIndex;
std::vector<unsigned int> indexVector; std::vector<unsigned int> indexVector;
}; };

View file

@ -45,6 +45,33 @@ public:
using moIndexNeighbor<EOT>::key; using moIndexNeighbor<EOT>::key;
using moIndexNeighbor<EOT>::index; using moIndexNeighbor<EOT>::index;
/**
* Default Constructor
*/
moIndexedSwapNeighbor() : moIndexNeighbor<EOT, Fitness>() {
}
/**
* Copy Constructor
* @param _n the neighbor to copy
*/
moIndexedSwapNeighbor(const moIndexedSwapNeighbor<EOT, Fitness> & _n) : moIndexNeighbor<EOT, Fitness>(_n)
{
indices.first = _n.first();
indices.second = _n.second();
}
/**
* Assignment operator
* @param _source the source neighbor
*/
moIndexedSwapNeighbor<EOT, Fitness> & operator=(const moIndexedSwapNeighbor<EOT, Fitness> & _source) {
moIndexNeighbor<EOT, Fitness>::operator=(_source);
indices.first = _source.first();
indices.second = _source.second();
return *this;
}
/** /**
* Apply the swap * Apply the swap
* @param _solution the solution to move * @param _solution the solution to move
@ -111,7 +138,7 @@ public:
* Getter of the firt location * Getter of the firt location
* @return first indice * @return first indice
*/ */
unsigned int first() { unsigned int first() const {
return indices.first; return indices.first;
} }
@ -119,11 +146,11 @@ public:
* Getter of the second location * Getter of the second location
* @return second indice * @return second indice
*/ */
unsigned int second() { unsigned int second() const {
return indices.second; return indices.second;
} }
private: protected:
std::pair<unsigned int, unsigned int> indices; std::pair<unsigned int, unsigned int> indices;
}; };

View file

@ -18,6 +18,7 @@ set (TEST_LIST
t-moOrderNeighborhood t-moOrderNeighborhood
t-moFullEvalByCopy t-moFullEvalByCopy
t-moFullEvalByModif t-moFullEvalByModif
t-moNKlandscapesIncrEval
t-moNeighborComparator t-moNeighborComparator
t-moSolNeighborComparator t-moSolNeighborComparator
t-moTrueContinuator t-moTrueContinuator

View file

@ -85,6 +85,7 @@ void main_function(int argc, char **argv)
string str_out = "out.dat"; // default value string str_out = "out.dat"; // default value
eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o'); eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o');
parser.processParam(outParam, "Persistence" ); parser.processParam(outParam, "Persistence" );
str_out = outParam.value();
// the name of the "status" file where all actual parameter values will be saved // the name of the "status" file where all actual parameter values will be saved
string str_status = parser.ProgramName() + ".status"; // default value string str_status = parser.ProgramName() + ".status"; // default value

View file

@ -89,6 +89,7 @@ void main_function(int argc, char **argv)
string str_out = "out.dat"; // default value string str_out = "out.dat"; // default value
eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o'); eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o');
parser.processParam(outParam, "Persistence" ); parser.processParam(outParam, "Persistence" );
str_out = outParam.value();
// the name of the "status" file where all actual parameter values will be saved // the name of the "status" file where all actual parameter values will be saved
string str_status = parser.ProgramName() + ".status"; // default value string str_status = parser.ProgramName() + ".status"; // default value

View file

@ -83,6 +83,7 @@ void main_function(int argc, char **argv)
string str_out = "out.dat"; // default value string str_out = "out.dat"; // default value
eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o'); eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o');
parser.processParam(outParam, "Persistence" ); parser.processParam(outParam, "Persistence" );
str_out = outParam.value();
// the name of the "status" file where all actual parameter values will be saved // the name of the "status" file where all actual parameter values will be saved
string str_status = parser.ProgramName() + ".status"; // default value string str_status = parser.ProgramName() + ".status"; // default value

View file

@ -83,7 +83,7 @@ class moeoHyperVolumeMetric : public moeoVectorUnaryMetric < ObjectiveVector , d
* @param _ref_point the reference point * @param _ref_point the reference point
* @param _bounds bounds value * @param _bounds bounds value
*/ */
moeoHyperVolumeMetric(ObjectiveVector& _ref_point=NULL, std::vector < eoRealInterval >& _bounds=NULL): normalize(false), rho(0.0), ref_point(_ref_point), bounds(_bounds){} moeoHyperVolumeMetric(ObjectiveVector& _ref_point, std::vector < eoRealInterval >& _bounds): normalize(false), rho(0.0), ref_point(_ref_point), bounds(_bounds){}
/** /**
* calculates and returns the HyperVolume value of a pareto front * calculates and returns the HyperVolume value of a pareto front

View file

@ -80,7 +80,7 @@ class moeoRouletteSelect:public moeoSelectOne < MOEOT >
protected: protected:
/** size */ /** size */
double & tSize; unsigned int & tSize;
}; };

View file

@ -31,6 +31,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#define __nkLandscapesEval_H #define __nkLandscapesEval_H
#include <eoEvalFunc.h> #include <eoEvalFunc.h>
#include <fstream>
template< class EOT > template< class EOT >
class nkLandscapesEval : public eoEvalFunc<EOT> { class nkLandscapesEval : public eoEvalFunc<EOT> {
@ -79,7 +80,7 @@ public:
*/ */
nkLandscapesEval(const char * _fileName) nkLandscapesEval(const char * _fileName)
{ {
string fname(_fileName); std::string fname(_fileName);
load(fname); load(fname);
}; };
@ -132,16 +133,16 @@ public:
* *
* @param _fileName file name of the instance * @param _fileName file name of the instance
*/ */
virtual void load(const string _fileName) virtual void load(const std::string _fileName)
{ {
fstream file; std::fstream file;
file.open(_fileName.c_str(), ios::in); file.open(_fileName.c_str(), std::fstream::in);
if (file.is_open()) { if (file.is_open()) {
string s; std::string s;
// Read the commentairies // Read the commentairies
string line; std::string line;
file >> s; file >> s;
while (s[0] == 'c') { while (s[0] == 'c') {
getline(file,line,'\n'); getline(file,line,'\n');
@ -150,14 +151,14 @@ public:
// Read the parameters // Read the parameters
if (s[0] != 'p') { if (s[0] != 'p') {
string str = "nkLandscapesEval.load: -- p -- expected in [" + _fileName + "] at the begining." ; std::string str = "nkLandscapesEval.load: -- p -- expected in [" + _fileName + "] at the begining." ;
throw runtime_error(str); throw std::runtime_error(str);
} }
file >> s; file >> s;
if (s != "NK") { if (s != "NK") {
string str = "nkLandscapesEval.load: -- NK -- expected in [" + _fileName + "] at the begining." ; std::string str = "nkLandscapesEval.load: -- NK -- expected in [" + _fileName + "] at the begining." ;
throw runtime_error(str); throw std::runtime_error(str);
} }
// read parameters N and K // read parameters N and K
@ -166,22 +167,22 @@ public:
// read the links // read the links
if (s[0] != 'p') { if (s[0] != 'p') {
string str = "nkLandscapesEval.load: -- p -- expected in [" + _fileName + "] after the parameters N and K." ; std::string str = "nkLandscapesEval.load: -- p -- expected in [" + _fileName + "] after the parameters N and K." ;
throw runtime_error(str); throw std::runtime_error(str);
} }
file >> s; file >> s;
if (s == "links") { if (s == "links") {
loadLinks(file); loadLinks(file);
} else { } else {
string str = "nkLandscapesEval.load: -- links -- expected in [" + _fileName + "] after the parameters N and K." ; std::string str = "nkLandscapesEval.load: -- links -- expected in [" + _fileName + "] after the parameters N and K." ;
throw runtime_error(str); throw std::runtime_error(str);
} }
// lecture des tables // lecture des tables
if (s[0] != 'p') { if (s[0] != 'p') {
string str = "nkLandscapesEval.load: -- p -- expected in [" + _fileName + "] after the links." ; std::string str = "nkLandscapesEval.load: -- p -- expected in [" + _fileName + "] after the links." ;
throw runtime_error(str); throw std::runtime_error(str);
} }
file >> s; file >> s;
@ -189,14 +190,14 @@ public:
if (s == "tables") { if (s == "tables") {
loadTables(file); loadTables(file);
} else { } else {
string str = "nkLandscapesEval.load: -- tables -- expected in [" + _fileName + "] after the links." ; std::string str = "nkLandscapesEval.load: -- tables -- expected in [" + _fileName + "] after the links." ;
throw runtime_error(str); throw std::runtime_error(str);
} }
file.close(); file.close();
} else { } else {
string str = "nkLandscapesEval.load: Could not open file [" + _fileName + "]." ; std::string str = "nkLandscapesEval.load: Could not open file [" + _fileName + "]." ;
throw runtime_error(str); throw std::runtime_error(str);
} }
}; };
@ -206,7 +207,7 @@ public:
* *
* @param file the file to read * @param file the file to read
*/ */
void loadLinks(fstream & file) { void loadLinks(std::fstream & file) {
for(int j = 0; j < K+1; j++) for(int j = 0; j < K+1; j++)
for(int i = 0; i < N; i++) { for(int i = 0; i < N; i++) {
file >> links[i][j]; file >> links[i][j];
@ -218,7 +219,7 @@ public:
* *
* @param file the file to read * @param file the file to read
*/ */
void loadTables(fstream & file) { void loadTables(std::fstream & file) {
for(int j = 0; j < (1<<(K+1)); j++) for(int j = 0; j < (1<<(K+1)); j++)
for(int i = 0; i < N; i++) for(int i = 0; i < N; i++)
file >> tables[i][j]; file >> tables[i][j];
@ -230,29 +231,29 @@ public:
* @param _fileName the file name of instance * @param _fileName the file name of instance
*/ */
virtual void save(const char * _fileName) { virtual void save(const char * _fileName) {
fstream file; std::fstream file;
file.open(_fileName, ios::out); file.open(_fileName, std::fstream::out);
if (file.is_open()) { if (file.is_open()) {
file << "c name of the file : " << _fileName << endl; file << "c name of the file : " << _fileName << std::endl;
file << "p NK " << N << " " << K <<endl; file << "p NK " << N << " " << K <<std::endl;
file << "p links" << endl; file << "p links" << std::endl;
for(int j=0; j<K+1; j++) for(int j=0; j<K+1; j++)
for(int i=0; i<N; i++) for(int i=0; i<N; i++)
file << links[i][j] << endl; file << links[i][j] << std::endl;
file << "p tables" << endl; file << "p tables" << std::endl;
for(int j=0; j<(1<<(K+1)); j++) { for(int j=0; j<(1<<(K+1)); j++) {
for(int i=0; i<N; i++) for(int i=0; i<N; i++)
file << tables[i][j] << " "; file << tables[i][j] << " ";
file << endl; file << std::endl;
} }
file.close(); file.close();
} else { } else {
string fname(_fileName); std::string fname(_fileName);
string str = "nkLandscapesEval.save: Could not open file [" + fname + "]." ; std::string str = "nkLandscapesEval.save: Could not open file [" + fname + "]." ;
throw runtime_error(str); throw std::runtime_error(str);
} }
}; };