From ab375d55ac9327598333a37386ad144b5298566b Mon Sep 17 00:00:00 2001 From: nojhan Date: Fri, 10 Feb 2023 09:51:56 +0100 Subject: [PATCH] refactor(mo): use clog instead of cout & use at accessors in Debug builds Should really use eo::log, but waiting for logger refactoring. --- .../continuator/moBestNoImproveContinuator.h | 2 +- mo/src/continuator/moIterContinuator.h | 2 +- mo/src/continuator/moNeighborFitnessStat.h | 2 +- mo/src/continuator/moTimeContinuator.h | 2 +- mo/src/eval/moFullEvalByModif.h | 4 ++- mo/src/explorer/moILSexplorer.h | 2 +- mo/src/explorer/moMetropolisHastingExplorer.h | 2 +- mo/src/explorer/moNeighborhoodExplorer.h | 6 ++--- mo/src/explorer/moRandomBestHCexplorer.h | 27 +++++++++++++++---- mo/src/explorer/moRandomNeutralWalkExplorer.h | 2 +- mo/src/explorer/moRandomWalkExplorer.h | 2 +- mo/src/explorer/moSAexplorer.h | 2 +- mo/src/sampling/moSampling.h | 2 +- 13 files changed, 38 insertions(+), 19 deletions(-) diff --git a/mo/src/continuator/moBestNoImproveContinuator.h b/mo/src/continuator/moBestNoImproveContinuator.h index d772180f3..c14c4b87d 100644 --- a/mo/src/continuator/moBestNoImproveContinuator.h +++ b/mo/src/continuator/moBestNoImproveContinuator.h @@ -84,7 +84,7 @@ public: bool res = (cpt < maxNoImprove); if (!res && verbose) - std::cout << "STOP in moBestNoImproveContinuator: Reached maximum number of iterations without improvement [" << cpt << "/" << maxNoImprove << "]" << std::endl; + std::clog << "STOP in moBestNoImproveContinuator: Reached maximum number of iterations without improvement [" << cpt << "/" << maxNoImprove << "]" << std::endl; return res; } diff --git a/mo/src/continuator/moIterContinuator.h b/mo/src/continuator/moIterContinuator.h index f9400254c..ff3056062 100644 --- a/mo/src/continuator/moIterContinuator.h +++ b/mo/src/continuator/moIterContinuator.h @@ -57,7 +57,7 @@ public: cpt++; res = (cpt < maxIter); if (!res && verbose) - std::cout << "STOP in moIterContinuator: Reached maximum number of iterations [" << cpt << "/" << maxIter << "]" << std::endl; + std::clog << "STOP in moIterContinuator: Reached maximum number of iterations [" << cpt << "/" << maxIter << "]" << std::endl; return res; } diff --git a/mo/src/continuator/moNeighborFitnessStat.h b/mo/src/continuator/moNeighborFitnessStat.h index ab0d3bcb9..5cbe3351d 100644 --- a/mo/src/continuator/moNeighborFitnessStat.h +++ b/mo/src/continuator/moNeighborFitnessStat.h @@ -62,7 +62,7 @@ public : neighborhood(_neighborhood), eval(_eval) { if (!neighborhood.isRandom()) { - std::cout << "moNeighborFitnessStat::Warning -> the neighborhood used is not random, the neighbor will not be random" << std::endl; + std::clog << "moNeighborFitnessStat::Warning -> the neighborhood used is not random, the neighbor will not be random" << std::endl; } } diff --git a/mo/src/continuator/moTimeContinuator.h b/mo/src/continuator/moTimeContinuator.h index 172fec933..67804dda9 100644 --- a/mo/src/continuator/moTimeContinuator.h +++ b/mo/src/continuator/moTimeContinuator.h @@ -94,7 +94,7 @@ public: time_t elapsed = (time_t) difftime(time(NULL), start); res = (elapsed < max); if (!res && verbose) - std::cout << "STOP in moTimeContinuator: Reached maximum time [" << elapsed << "/" << max << "]" << std::endl; + std::clog << "STOP in moTimeContinuator: Reached maximum time [" << elapsed << "/" << max << "]" << std::endl; return res; } diff --git a/mo/src/eval/moFullEvalByModif.h b/mo/src/eval/moFullEvalByModif.h index 80ee94d1c..a4cb9369a 100644 --- a/mo/src/eval/moFullEvalByModif.h +++ b/mo/src/eval/moFullEvalByModif.h @@ -40,7 +40,9 @@ /** * Full evaluation to use with a moBackableNeighbor - * !!!WARNING!!! Use only when your solution is composed by a fitness Value and a "genotype" + * + * @warning Use only when your solution is composed by a fitness Value and a "genotype", + * and no any other data structure. * */ template diff --git a/mo/src/explorer/moILSexplorer.h b/mo/src/explorer/moILSexplorer.h index 49d74fea5..89d7bfd47 100644 --- a/mo/src/explorer/moILSexplorer.h +++ b/mo/src/explorer/moILSexplorer.h @@ -126,7 +126,7 @@ public: //apply the local search on the copy ls(currentSol); -// std::cout << "(solution)\t" << current << std::endl; +// std::clog << "(solution)\t" << current << std::endl; }; diff --git a/mo/src/explorer/moMetropolisHastingExplorer.h b/mo/src/explorer/moMetropolisHastingExplorer.h index ea0913f1e..9716469b9 100644 --- a/mo/src/explorer/moMetropolisHastingExplorer.h +++ b/mo/src/explorer/moMetropolisHastingExplorer.h @@ -71,7 +71,7 @@ public: moMetropolisHastingExplorer(Neighborhood& _neighborhood, moEval& _eval, moNeighborComparator& _neighborComparator, moSolNeighborComparator& _solNeighborComparator, unsigned int _nbStep) : moNeighborhoodExplorer(_neighborhood, _eval), neighborComparator(_neighborComparator), solNeighborComparator(_solNeighborComparator), nbStep(_nbStep) { isAccept = false; if (!neighborhood.isRandom()) { - std::cout << "moMetropolisHastingExplorer::Warning -> the neighborhood used is not random" << std::endl; + std::clog << "moMetropolisHastingExplorer::Warning -> the neighborhood used is not random" << std::endl; } } diff --git a/mo/src/explorer/moNeighborhoodExplorer.h b/mo/src/explorer/moNeighborhoodExplorer.h index 8b4113b7a..2db8623ac 100644 --- a/mo/src/explorer/moNeighborhoodExplorer.h +++ b/mo/src/explorer/moNeighborhoodExplorer.h @@ -44,16 +44,16 @@ #include /** - * Explore the neighborhood according to the local search algorithm + * Explore the neighborhood according to the local search algorithm * * During this exploration, * the parameters are updated * one neighbor is selected * a comparason with the solution is made to acccept or not this new neighbor - * + * * The current neighbor (currentNeigbor) is the neighbor under consideration during the search (in operator()(EOT &)) * - * The selected neighbor (selectedNeighbor) is the neighbor selected in method operator()(EOT &). + * The selected neighbor (selectedNeighbor) is the neighbor selected in method operator()(EOT &). * If this neighbor is accepted, then the solution is moved on this neighbor (in move(EOT &)) * */ diff --git a/mo/src/explorer/moRandomBestHCexplorer.h b/mo/src/explorer/moRandomBestHCexplorer.h index 92e690e37..3e7218065 100644 --- a/mo/src/explorer/moRandomBestHCexplorer.h +++ b/mo/src/explorer/moRandomBestHCexplorer.h @@ -120,6 +120,7 @@ public: eval(_solution, currentNeighbor); //initialize the best neighbor + assert(not currentNeighbor.invalid()); bestVector.push_back(currentNeighbor); //test all others neighbors @@ -129,21 +130,37 @@ public: //eval eval(_solution, currentNeighbor); + assert(not currentNeighbor.invalid()); //if we found a better neighbor, update the best + #ifndef NDEBUG + assert(bestVector.size() > 0); + assert(not bestVector.at(0).invalid()); + if (neighborComparator(bestVector.at(0), currentNeighbor)) { + #else if (neighborComparator(bestVector[0], currentNeighbor)) { + #endif bestVector.clear(); + assert(not currentNeighbor.invalid()); bestVector.push_back(currentNeighbor); } - else if (neighborComparator.equals(currentNeighbor, bestVector[0])) //if the current is equals to previous best solutions then update vector of the best solution + //if the current is equals to previous best solutions + // then update vector of the best solution + #ifndef NDEBUG + else if (neighborComparator.equals(currentNeighbor, bestVector.at(0))) { + #else + else if (neighborComparator.equals(currentNeighbor, bestVector[0])) { + #endif + assert(not currentNeighbor.invalid()); bestVector.push_back(currentNeighbor); + } } - // choose randomly one of the best solutions - unsigned int i = rng.random(bestVector.size()); + // choose randomly one of the best solutions + unsigned int i = rng.random(bestVector.size()); - // the selected Neighbor - selectedNeighbor = bestVector[i]; + // the selected Neighbor + selectedNeighbor = bestVector[i]; } else { //if _solution hasn't neighbor, diff --git a/mo/src/explorer/moRandomNeutralWalkExplorer.h b/mo/src/explorer/moRandomNeutralWalkExplorer.h index e8f9713bf..521c357e6 100644 --- a/mo/src/explorer/moRandomNeutralWalkExplorer.h +++ b/mo/src/explorer/moRandomNeutralWalkExplorer.h @@ -73,7 +73,7 @@ public: nbStep(_nbStep) { isAccept = false; if (!neighborhood.isRandom()) { - std::cout << "moRandomNeutralWalkExplorer::Warning -> the neighborhood used is not random (" << neighborhood.className() << ")" << std::endl; + std::clog << "moRandomNeutralWalkExplorer::Warning -> the neighborhood used is not random (" << neighborhood.className() << ")" << std::endl; } } diff --git a/mo/src/explorer/moRandomWalkExplorer.h b/mo/src/explorer/moRandomWalkExplorer.h index 889382a2b..d0a84fe29 100644 --- a/mo/src/explorer/moRandomWalkExplorer.h +++ b/mo/src/explorer/moRandomWalkExplorer.h @@ -68,7 +68,7 @@ public: moRandomWalkExplorer(Neighborhood& _neighborhood, moEval& _eval) : moNeighborhoodExplorer(_neighborhood, _eval) { isAccept = false; if (!neighborhood.isRandom()) { - std::cout << "moRandomNeutralWalkExplorer::Warning -> the neighborhood used is not random (" << neighborhood.className() << ")" << std::endl; + std::clog << "moRandomNeutralWalkExplorer::Warning -> the neighborhood used is not random (" << neighborhood.className() << ")" << std::endl; } } diff --git a/mo/src/explorer/moSAexplorer.h b/mo/src/explorer/moSAexplorer.h index b774d5c57..132d584f9 100644 --- a/mo/src/explorer/moSAexplorer.h +++ b/mo/src/explorer/moSAexplorer.h @@ -72,7 +72,7 @@ public: isAccept = false; if (!neighborhood.isRandom()) { - std::cout << "moSAexplorer::Warning -> the neighborhood used is not random" << std::endl; + std::clog << "moSAexplorer::Warning -> the neighborhood used is not random" << std::endl; } } diff --git a/mo/src/sampling/moSampling.h b/mo/src/sampling/moSampling.h index 90dee4707..93ae19615 100644 --- a/mo/src/sampling/moSampling.h +++ b/mo/src/sampling/moSampling.h @@ -75,7 +75,7 @@ public: checkpoint = new moCheckpoint(*continuator); add(_stat, _monitoring); // precision of the output by default - precisionOutput = std::cout.precision(); + precisionOutput = std::clog.precision(); } /**