Add the number of neighbor evaluations in moAdaptiveWalk

This commit is contained in:
verel 2014-10-18 16:15:49 +02:00
commit a3288caf6d
5 changed files with 151 additions and 78 deletions

View file

@ -45,12 +45,15 @@ template <class EOT, class ValueType>
class moValueStat : public moStat<EOT, double>
{
public :
using moStat< EOT, double>::value;
using moStat<EOT, double>::value;
/**
* Default Constructor
*
* @param _valueParam the value to report in the statistic
* @param _restart if true, the value is initialized at 0 for each init (restart)
*/
moValueStat(eoValueParam<ValueType> & _valueParam): moStat<EOT, double>(0, "value"), valueParam(_valueParam) {
moValueStat(eoValueParam<ValueType> & _valueParam, bool _restart = false): moStat<EOT, double>(0, "value"), valueParam(_valueParam), restart(_restart) {
}
/**
@ -58,7 +61,12 @@ public :
* @param _sol a solution
*/
virtual void init(EOT & _sol) {
value() = (double) valueParam.value();
if (restart)
value_start = valueParam.value();
else
value_start = 0;
value() = (double) (valueParam.value() - value_start);
}
/**
@ -66,7 +74,7 @@ public :
* @param _sol a solution
*/
virtual void operator()(EOT & _sol) {
value() = (double) valueParam.value() ;
value() = (double) (valueParam.value() - value_start);
}
/**
@ -79,6 +87,9 @@ public :
private:
eoValueParam<ValueType> & valueParam;
bool restart;
ValueType value_start ;
};
#endif

View file

@ -34,8 +34,8 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#include <utils/eoParam.h>
/**
Counts the number of neighbor evaluations actually performed, thus checks first
if it has to evaluate.. etc.
Counts the number of neighbor evaluations actually performed,
thus checks first if it has to be evaluated.. etc.
*/
template<class Neighbor>
class moEvalCounter : public moEval<Neighbor>, public eoValueParam<unsigned long>

View file

@ -46,12 +46,19 @@
#include <continuator/moMinusOneCounterStat.h>
#include <continuator/moStatFromStat.h>
#include <sampling/moSampling.h>
#include <eval/moEvalCounter.h>
#include <eoEvalFuncCounter.h>
#include <continuator/moValueStat.h>
/**
* To compute the length and final solution of an adaptive walk:
* Perform a first improvement Hill-climber based on the neighborhood (adaptive walk),
* The lengths of HC are collected and the final solution which are local optima
* The adaptive walk is repeated several times
* The adaptive walk is repeated several times (defined by a parameter)
*
* Statistics are:
* - the length of the adaptive walk
* - the number of neighbor evaluaitons
* - the final solution which are local optimum
*
*/
template <class Neighbor>
@ -64,11 +71,12 @@ public:
/**
* Constructor
*
* @param _init initialisation method of the solution
* @param _neighborhood neighborhood giving neighbor in random order
* @param _fullEval a full evaluation function
* @param _eval an incremental evaluation of neighbors
* @param _nbAdaptWalk Number of adaptive walks
* @param _nbAdaptWalk Number of adaptive walks (minimum value = 2)
*/
moAdaptiveWalkSampling(eoInit<EOT> & _init,
moNeighborhood<Neighbor> & _neighborhood,
@ -76,15 +84,22 @@ public:
moEval<Neighbor>& _eval,
unsigned int _nbAdaptWalk) :
moSampling<Neighbor>(initHC, * new moRandomSearch<Neighbor>(initHC, _fullEval, _nbAdaptWalk), copyStat),
copyStat(lengthStat),
neighborEvalCount(_eval),
nEvalStat(neighborEvalCount, true),
copyStat(lengthStat), // copy is used to report the statistic of the first walk
copyEvalStat(nEvalStat),
checkpoint(trueCont),
hc(_neighborhood, _fullEval, _eval, checkpoint),
hc(_neighborhood, _fullEval, neighborEvalCount, checkpoint),
initHC(_init, hc)
{
// to count the number of step in the HC
checkpoint.add(lengthStat);
// to count the number of evaluations
checkpoint.add(nEvalStat);
// add the solution into statistics
this->add(copyEvalStat);
this->add(solStat);
}
@ -97,6 +112,11 @@ public:
}
protected:
/* count the number of evaluations */
moEvalCounter<Neighbor> neighborEvalCount;
moValueStat<EOT, unsigned long> nEvalStat;
moStatFromStat<EOT, double> copyEvalStat;
moSolutionStat<EOT> solStat;
moMinusOneCounterStat<EOT> lengthStat;
moTrueContinuator<Neighbor> trueCont;
@ -104,6 +124,7 @@ protected:
moCheckpoint<Neighbor> checkpoint;
moFirstImprHC<Neighbor> hc;
moLocalSearchInit<Neighbor> initHC;
};

View file

@ -76,71 +76,112 @@ template <class Neighbor>
class moNeutralWalkSampling : public moSampling<Neighbor>
{
public:
typedef typename Neighbor::EOT EOT ;
typedef typename Neighbor::EOT EOT ;
using moSampling<Neighbor>::localSearch;
using moSampling<Neighbor>::localSearch;
/**
* Constructor
* @param _initSol the first solution of the walk
* @param _neighborhood neighborhood giving neighbor in random order
* @param _fullEval Fitness function, full evaluation function
* @param _eval neighbor evaluation, incremental evaluation function
* @param _distance component to measure the distance from the initial solution
* @param _nbStep Number of steps of the random walk
*/
moNeutralWalkSampling(EOT & _initSol,
moNeighborhood<Neighbor> & _neighborhood,
eoEvalFunc<EOT>& _fullEval,
moEval<Neighbor>& _eval,
eoDistance<EOT> & _distance,
unsigned int _nbStep) :
moSampling<Neighbor>(init, * new moRandomNeutralWalk<Neighbor>(_neighborhood, _fullEval, _eval, _nbStep), solutionStat),
init(_initSol),
distStat(_distance, _initSol),
neighborhoodStat(_neighborhood, _eval),
minStat(neighborhoodStat),
averageStat(neighborhoodStat),
stdStat(neighborhoodStat),
maxStat(neighborhoodStat),
nbSupStat(neighborhoodStat),
nbInfStat(neighborhoodStat),
sizeStat(neighborhoodStat),
ndStat(neighborhoodStat)
{
this->add(neighborhoodStat, false);
this->add(distStat);
this->add(minStat);
this->add(averageStat);
this->add(stdStat);
this->add(maxStat);
this->add(sizeStat);
this->add(nbInfStat);
this->add(ndStat);
this->add(nbSupStat);
}
/**
* Constructor
* @param _initSol the first solution of the walk
* @param _neighborhood neighborhood giving neighbor in random order
* @param _fullEval Fitness function, full evaluation function
* @param _eval neighbor evaluation, incremental evaluation function
* @param _distance component to measure the distance from the initial solution
* @param _nbStep Number of steps of the random walk
*/
moNeutralWalkSampling(EOT & _initSol,
moNeighborhood<Neighbor> & _neighborhood,
eoEvalFunc<EOT>& _fullEval,
moEval<Neighbor>& _eval,
eoDistance<EOT> & _distance,
unsigned int _nbStep) :
moSampling<Neighbor>(init, * new moRandomNeutralWalk<Neighbor>(_neighborhood, _fullEval, _eval, _nbStep), solutionStat),
init(_initSol),
distStat(_distance, _initSol),
neighborhoodStat(_neighborhood, _eval),
minStat(neighborhoodStat),
averageStat(neighborhoodStat),
stdStat(neighborhoodStat),
maxStat(neighborhoodStat),
nbSupStat(neighborhoodStat),
nbInfStat(neighborhoodStat),
sizeStat(neighborhoodStat),
ndStat(neighborhoodStat)
{
this->add(neighborhoodStat, false);
this->add(distStat);
this->add(minStat);
this->add(averageStat);
this->add(stdStat);
this->add(maxStat);
this->add(sizeStat);
this->add(nbInfStat);
this->add(ndStat);
this->add(nbSupStat);
}
/**
* default destructor
*/
~moNeutralWalkSampling() {
// delete the pointer on the local search which has been constructed in the constructor
delete localSearch;
}
/**
* Constructor
* @param _initSol the first solution of the walk
* @param _neighborhood neighborhood giving neighbor in random order
* @param _fullEval Fitness function, full evaluation function
* @param _eval neighbor evaluation, incremental evaluation function
* @param _distance component to measure the distance from the initial solution
* @param _nbStep Number of steps of the random walk
*/
moNeutralWalkSampling(eoInit<EOT> & _init,
moNeighborhood<Neighbor> & _neighborhood,
eoEvalFunc<EOT>& _fullEval,
moEval<Neighbor>& _eval,
unsigned int _nbStep) :
moSampling<Neighbor>(_init, * new moRandomNeutralWalk<Neighbor>(_neighborhood, _fullEval, _eval, _nbStep), solutionStat),
init(initialSol),
distStat(dummyDistance, initialSol),
neighborhoodStat(_neighborhood, _eval),
minStat(neighborhoodStat),
averageStat(neighborhoodStat),
stdStat(neighborhoodStat),
maxStat(neighborhoodStat),
nbSupStat(neighborhoodStat),
nbInfStat(neighborhoodStat),
sizeStat(neighborhoodStat),
ndStat(neighborhoodStat)
{
this->add(neighborhoodStat, false);
// this->add(distStat);
this->add(minStat);
this->add(averageStat);
this->add(stdStat);
this->add(maxStat);
this->add(sizeStat);
this->add(nbInfStat);
this->add(ndStat);
this->add(nbSupStat);
}
/**
* default destructor
*/
~moNeutralWalkSampling() {
// delete the pointer on the local search which has been constructed in the constructor
delete localSearch;
}
protected:
moSolInit<EOT> init;
moSolutionStat<EOT> solutionStat;
moDistanceStat<EOT> distStat;
moNeighborhoodStat< Neighbor > neighborhoodStat;
moMinNeighborStat< Neighbor > minStat;
moAverageFitnessNeighborStat< Neighbor > averageStat;
moStdFitnessNeighborStat< Neighbor > stdStat;
moMaxNeighborStat< Neighbor > maxStat;
moNbSupNeighborStat< Neighbor > nbSupStat;
moNbInfNeighborStat< Neighbor > nbInfStat;
moSizeNeighborStat< Neighbor > sizeStat;
moNeutralDegreeNeighborStat< Neighbor > ndStat;
EOT initialSol;
eoHammingDistance<EOT> dummyDistance;
moSolInit<EOT> init;
moSolutionStat<EOT> solutionStat;
moDistanceStat<EOT> distStat;
moNeighborhoodStat< Neighbor > neighborhoodStat;
moMinNeighborStat< Neighbor > minStat;
moAverageFitnessNeighborStat< Neighbor > averageStat;
moStdFitnessNeighborStat< Neighbor > stdStat;
moMaxNeighborStat< Neighbor > maxStat;
moNbSupNeighborStat< Neighbor > nbSupStat;
moNbInfNeighborStat< Neighbor > nbInfStat;
moSizeNeighborStat< Neighbor > sizeStat;
moNeutralDegreeNeighborStat< Neighbor > ndStat;
};

View file

@ -45,12 +45,12 @@ public:
*
* @param _fileData the file name which contains the instance of QAP from QAPlib
*/
QAPeval(string & _fileData) {
fstream file(_fileData.c_str(), ios::in);
QAPeval(std::string _fileData) {
std::fstream file(_fileData.c_str(), std::ios::in);
if (!file) {
string str = "QAPeval: Could not open file [" + _fileData + "]." ;
throw runtime_error(str);
std::string str = "QAPeval: Could not open file [" + _fileData + "]." ;
throw std::runtime_error(str);
}
unsigned i, j;