From a3288caf6d1ffa71864d24217f603a1127ef0cd2 Mon Sep 17 00:00:00 2001 From: verel Date: Sat, 18 Oct 2014 16:15:49 +0200 Subject: [PATCH] Add the number of neighbor evaluations in moAdaptiveWalk --- mo/src/continuator/moValueStat.h | 19 ++- mo/src/eval/moEvalCounter.h | 4 +- mo/src/sampling/moAdaptiveWalkSampling.h | 31 ++++- mo/src/sampling/moNeutralWalkSampling.h | 161 ++++++++++++++--------- problems/eval/qapEval.h | 8 +- 5 files changed, 148 insertions(+), 75 deletions(-) diff --git a/mo/src/continuator/moValueStat.h b/mo/src/continuator/moValueStat.h index 6c8806e87..0d8d8877d 100644 --- a/mo/src/continuator/moValueStat.h +++ b/mo/src/continuator/moValueStat.h @@ -45,12 +45,15 @@ template class moValueStat : public moStat { public : - using moStat< EOT, double>::value; + using moStat::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 & _valueParam): moStat(0, "value"), valueParam(_valueParam) { + moValueStat(eoValueParam & _valueParam, bool _restart = false): moStat(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 & valueParam; + bool restart; + ValueType value_start ; + }; #endif diff --git a/mo/src/eval/moEvalCounter.h b/mo/src/eval/moEvalCounter.h index e795f4384..e12949b1f 100644 --- a/mo/src/eval/moEvalCounter.h +++ b/mo/src/eval/moEvalCounter.h @@ -34,8 +34,8 @@ Contact: paradiseo-help@lists.gforge.inria.fr #include /** - 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 moEvalCounter : public moEval, public eoValueParam diff --git a/mo/src/sampling/moAdaptiveWalkSampling.h b/mo/src/sampling/moAdaptiveWalkSampling.h index 14d99ac44..0dae8633c 100644 --- a/mo/src/sampling/moAdaptiveWalkSampling.h +++ b/mo/src/sampling/moAdaptiveWalkSampling.h @@ -46,12 +46,19 @@ #include #include #include +#include +#include +#include /** * 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 @@ -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 & _init, moNeighborhood & _neighborhood, @@ -76,15 +84,22 @@ public: moEval& _eval, unsigned int _nbAdaptWalk) : moSampling(initHC, * new moRandomSearch(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 neighborEvalCount; + moValueStat nEvalStat; + moStatFromStat copyEvalStat; + moSolutionStat solStat; moMinusOneCounterStat lengthStat; moTrueContinuator trueCont; @@ -104,6 +124,7 @@ protected: moCheckpoint checkpoint; moFirstImprHC hc; moLocalSearchInit initHC; + }; diff --git a/mo/src/sampling/moNeutralWalkSampling.h b/mo/src/sampling/moNeutralWalkSampling.h index 789dd8fed..708f0ac14 100644 --- a/mo/src/sampling/moNeutralWalkSampling.h +++ b/mo/src/sampling/moNeutralWalkSampling.h @@ -76,71 +76,112 @@ template class moNeutralWalkSampling : public moSampling { public: - typedef typename Neighbor::EOT EOT ; + typedef typename Neighbor::EOT EOT ; - using moSampling::localSearch; + using moSampling::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 & _neighborhood, - eoEvalFunc& _fullEval, - moEval& _eval, - eoDistance & _distance, - unsigned int _nbStep) : - moSampling(init, * new moRandomNeutralWalk(_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 & _neighborhood, + eoEvalFunc& _fullEval, + moEval& _eval, + eoDistance & _distance, + unsigned int _nbStep) : + moSampling(init, * new moRandomNeutralWalk(_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 & _init, + moNeighborhood & _neighborhood, + eoEvalFunc& _fullEval, + moEval& _eval, + unsigned int _nbStep) : + moSampling(_init, * new moRandomNeutralWalk(_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 init; - moSolutionStat solutionStat; - moDistanceStat 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 dummyDistance; + moSolInit init; + moSolutionStat solutionStat; + moDistanceStat 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; }; diff --git a/problems/eval/qapEval.h b/problems/eval/qapEval.h index 0915a8886..3236dae60 100644 --- a/problems/eval/qapEval.h +++ b/problems/eval/qapEval.h @@ -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;