From 816ea1553aa2a43d3508ef305ae4ba19f53794a8 Mon Sep 17 00:00:00 2001 From: LPTK Date: Tue, 9 Jul 2013 16:25:30 +0200 Subject: [PATCH] intermediate commit 5 --- eo/src/utils/eoFileMonitor.cpp | 18 +- eo/src/utils/eoMonitor.h | 1 + eo/src/utils/eoParam.h | 4 +- mo/src/algo/moLocalSearch.h | 24 +- mo/src/algo/moSA.h | 15 +- mo/src/continuator/moCheckpoint.h | 15 +- .../moDynSpanCoolingSchedule.h | 38 +- .../coolingSchedule/moTrikiCoolingSchedule.h | 393 +++++++++++++----- mo/src/mo.h | 2 +- mo/test/t-moSA.cpp | 4 +- problems/eval/queenEval.h | 20 +- 11 files changed, 377 insertions(+), 157 deletions(-) diff --git a/eo/src/utils/eoFileMonitor.cpp b/eo/src/utils/eoFileMonitor.cpp index 3cc96deb1..4927fe2ab 100644 --- a/eo/src/utils/eoFileMonitor.cpp +++ b/eo/src/utils/eoFileMonitor.cpp @@ -73,16 +73,18 @@ eoMonitor& eoFileMonitor::operator()(void) eoMonitor& eoFileMonitor::operator()(std::ostream& os) { - - iterator it = vec.begin(); - - os << (*it)->getValue(); - - for(++it; it != vec.end(); ++it) + if (vec.size() > 0) { - os << delim.c_str() << (*it)->getValue(); + iterator it = vec.begin(); + + os << (*it)->getValue(); + + for(++it; it != vec.end(); ++it) + { + os << delim.c_str() << (*it)->getValue(); + } } - + os << std::endl; return *this; diff --git a/eo/src/utils/eoMonitor.h b/eo/src/utils/eoMonitor.h index 1c92e9c00..d234921c4 100644 --- a/eo/src/utils/eoMonitor.h +++ b/eo/src/utils/eoMonitor.h @@ -78,3 +78,4 @@ protected : }; #endif + diff --git a/eo/src/utils/eoParam.h b/eo/src/utils/eoParam.h index 948092c93..1c0937528 100644 --- a/eo/src/utils/eoParam.h +++ b/eo/src/utils/eoParam.h @@ -224,7 +224,9 @@ public : void setValue(const std::string& _value) { std::istringstream is(_value); - is >> repValue; + bool read = (is >> repValue); + if (!read) + throw std::runtime_error("Could not read value passed in eoValueParam::setValue"); } protected: diff --git a/mo/src/algo/moLocalSearch.h b/mo/src/algo/moLocalSearch.h index 0c125d62a..ca2f04c6e 100644 --- a/mo/src/algo/moLocalSearch.h +++ b/mo/src/algo/moLocalSearch.h @@ -57,9 +57,8 @@ public: * @param _cont an external continuator (can be a checkpoint!) * @param _fullEval a full evaluation function */ - moLocalSearch(NeighborhoodExplorer& _searchExpl, - moContinuator & _cont, eoEvalFunc& _fullEval) - : searchExplorer(_searchExpl), cont(&_cont), fullEval(_fullEval) + moLocalSearch(NeighborhoodExplorer& _searchExpl, moContinuator & _cont, eoEvalFunc& _fullEval) + : searchExplorer(_searchExpl), cont(&_cont), fullEval(_fullEval), currentSolutionFitness(0) { } /** @@ -70,15 +69,19 @@ public: if (_solution.invalid()) fullEval(_solution); - + // initialization of the parameter of the search (for example fill empty the tabu list) searchExplorer.initParam(_solution); - + // initialization of the external continuator (for example the time, or the number of generations) cont->init(_solution); - + bool b; do { + currentSolutionFitness = _solution.fitness(); + //std::cout << currentSolutionFitness << std::endl; + //std::cin.get(); + // explore the neighborhood of the solution searchExplorer(_solution); // if a solution in the neighborhood can be accepted @@ -128,6 +131,11 @@ public: moNeighborhoodExplorer & getNeighborhoodExplorer() const { return searchExplorer; } + + // TODO doc + double getCurrentSolutionFitness() const { + return currentSolutionFitness; + } protected: // make the exploration of the neighborhood according to a local search heuristic @@ -138,6 +146,10 @@ protected: //full evaluation function eoEvalFunc& fullEval; + +private: + double currentSolutionFitness; + }; #endif diff --git a/mo/src/algo/moSA.h b/mo/src/algo/moSA.h index 0914f0db0..4cf1b3fba 100644 --- a/mo/src/algo/moSA.h +++ b/mo/src/algo/moSA.h @@ -68,7 +68,7 @@ public: { }*/ moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=0.01) : moLocalSearch ( - explorer_ptr = defaultExplorer = new moSAExplorer(_neighborhood, _eval, /*NULL,*/ *(defaultCool = new moSimpleCoolingSchedule(_initT, _alpha, _span, _finalT)), NULL), + *(explorer_ptr = defaultExplorer = new moSAExplorer(_neighborhood, _eval, /*NULL,*/ *(defaultCool = new moSimpleCoolingSchedule(_initT, _alpha, _span, _finalT)), NULL)), *(defaultContinuator = new moTrueContinuator()), _fullEval ), explorer(*explorer_ptr), @@ -150,7 +150,7 @@ public: _cool ) { }*/ : moLocalSearch ( - explorer_ptr = defaultExplorer = new moSAExplorer ( + *(explorer_ptr = defaultExplorer = new moSAExplorer ( _neighborhood, //_eval, //_eval.hasValue()? _eval.get(): *(defaultEval = new moFullEvalByCopy(_fullEval)), _eval.hasValue()? defaultEval = NULL, _eval.get(): *(defaultEval = new moFullEvalByCopy(_fullEval)), @@ -158,7 +158,7 @@ public: //_comp, //_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator()), //_cool ), _cool, - _comp ), + _comp )), // ), _cont.hasValue()? defaultContinuator = NULL, _cont.get(): *(defaultContinuator = new moTrueContinuator()), _fullEval ), @@ -177,12 +177,13 @@ public: ) : moLocalSearch ( *(explorer_ptr = &_explorer), - _cont.hasValue()? _cont.get(): *(defaultContinuator = new moTrueContinuator()), _fullEval ), + _cont.hasValue()? defaultContinuator = NULL, _cont.get(): *(defaultContinuator = new moTrueContinuator()), _fullEval ), + defaultEval(NULL), // removed in C++11 with unique_ptr defaultExplorer(NULL), // removed in C++11 with unique_ptr explorer(*explorer_ptr), - defaultCool(NULL), // removed in C++11 with unique_ptr - //defaultEval(NULL), // removed in C++11 with unique_ptr - defaultContinuator(NULL) // removed in C++11 with unique_ptr + defaultCool(NULL) // removed in C++11 with unique_ptr + //defaultEval(NULL) // removed in C++11 with unique_ptr + //defaultContinuator(NULL) // removed in C++11 with unique_ptr //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr { } diff --git a/mo/src/continuator/moCheckpoint.h b/mo/src/continuator/moCheckpoint.h index 37b5cd610..6cc3d0fb5 100644 --- a/mo/src/continuator/moCheckpoint.h +++ b/mo/src/continuator/moCheckpoint.h @@ -107,14 +107,21 @@ public : virtual void init(EOT& _sol) { for (unsigned i = 0; i < stats.size(); ++i) stats[i]->init(_sol); - counter=1; - + counter = 1; + + //for (unsigned i = 0; i < updaters.size(); ++i) + // updaters[i]->init(); + for (unsigned i = 0; i < moupdaters.size(); ++i) moupdaters[i]->init(); - + /* + * Removed because there was no reason for it to be done here. + * It caused premature monitoring of eoParams with undefined values + * for (unsigned int i = 0; i < monitors.size(); ++i) (*monitors[i])(); - + */ + for (unsigned i = 0; i < continuators.size(); ++i) continuators[i]->init(_sol); } diff --git a/mo/src/coolingSchedule/moDynSpanCoolingSchedule.h b/mo/src/coolingSchedule/moDynSpanCoolingSchedule.h index 4e6216ad5..5ec3a886c 100644 --- a/mo/src/coolingSchedule/moDynSpanCoolingSchedule.h +++ b/mo/src/coolingSchedule/moDynSpanCoolingSchedule.h @@ -82,25 +82,25 @@ public: * @param _acceptedMove true when the move is accepted, false otherwise * @param _currentSolution the current solution */ - virtual void update(double& _temp, bool _acceptedMove, EOT & _currentSolution) { - spanTries++; - - if (_acceptedMove) - spanMove++; - - if (spanTries >= spanTriesMax || spanMove >= spanMoveMax) { - _temp *= alpha; - - if (spanMove == 0) // no move during this span ? - nbSpan++; - else - nbSpan = 0; - - spanTries = 0; - spanMove = 0; - } - } - + virtual void update(double& _temp, bool _acceptedMove, EOT & _currentSolution) { + spanTries++; + + if (_acceptedMove) + spanMove++; + + if (spanTries >= spanTriesMax || spanMove >= spanMoveMax) { + _temp *= alpha; + + if (spanMove == 0) // no move during this span ? + nbSpan++; + else + nbSpan = 0; + + spanTries = 0; + spanMove = 0; + } + } + /** * compare the number of span with no move * @param _temp current temperature diff --git a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h index 900e3b189..4bb09d649 100644 --- a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h +++ b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h @@ -47,22 +47,28 @@ Lionel Parreaux #include using namespace std; +/* +static const char * stoppingReasons[] = {"no accepted solution", "null std dev" , "frozen >= theta"}; +static const char * chainEndingReasons[] = {"MAX GENerated solutions", "MAX ACCepted solutions"}; +*/ //! /*! */ -template< class EOT, class Neighbor > //, class Neighborhood > +//template< class Neighbor > //, class Neighborhood > +//class moTrikiCoolingSchedule: public moCoolingSchedule< typename Neighbor::EOT > +template< class EOT > class moTrikiCoolingSchedule: public moCoolingSchedule< EOT > { public: //typedef typename Neighbor::EOT EOT ; - typedef moNeighborhood Neighborhood ; + //typedef moNeighborhood Neighborhood ; //! Constructor /*! */ - - moTrikiCoolingSchedule (Neighborhood& _neighborhood, moEval& _eval, double _initTemp) + /* + moTrikiCoolingSchedule (Neighborhood& _neighborhood, moEval& _eval, double _initTemp) // FIXME rem useless params!! : initTemp(_initTemp), mu2(10), // mu2 typically belongs to [1; 20] K1(2), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature @@ -74,29 +80,59 @@ public: max_accepted(50), // depends on pb/neighborhood max_generated(100), // depends on pb/neighborhood theta(10), // theta is typically set to 10 - statIsInitialized(false), - outf("out.data") + statIsInitialized(false)//, + //outf("out.data") { } +*/ + +// moTrikiCoolingSchedule ( +// Neighborhood& _neighborhood, moEval& _eval, double _initTemp, +// double _max_accepted, +// double _max_generated +// ) +// : initTemp(_initTemp), +// mu2(10), // mu2 typically belongs to [1; 20] +// K1(2), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature +// K2(5), // ??? +// lambda1(2), // the increase in temperature, typically in [1.5; 4] +// lambda2(.7), // lambda2 in [0.5; 0.99] +// mu1(10), // target decrease in cost factor, in [2; 20] +// xi(1.05), // xi typically belongs to [1; 1.1] +// max_accepted(_max_accepted), // depends on pb/neighborhood +// max_generated(_max_generated), // depends on pb/neighborhood +// theta(10), // theta is typically set to 10 +// statIsInitialized(false)//, +// //outf("out.data") +// { } moTrikiCoolingSchedule ( - Neighborhood& _neighborhood, moEval& _eval, double _initTemp, - double _max_accepted, - double _max_generated - ) + double _initTemp, + int _max_accepted = 50, + int _max_generated = 100, + double _mu2 = 2, + double _mu1 = 10, + double _lambda1 = 2, + double _lambda2 = .7, + double _xi = 1.05, + int _theta = 10, + int _K1 = 10, + int _K2 = 5 + ) // TODO reorder inits : initTemp(_initTemp), - mu2(10), // mu2 typically belongs to [1; 20] - K1(2), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature - K2(5), // ??? - lambda1(2), // the increase in temperature, typically in [1.5; 4] - lambda2(.7), // lambda2 in [0.5; 0.99] - mu1(10), // target decrease in cost factor, in [2; 20] - xi(1.05), // xi typically belongs to [1; 1.1] + mu2(_mu2), // mu2 typically belongs to [1; 20] + K1(_K1), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature + K2(_K2), // ??? + lambda1(_lambda1), // the increase in temperature (reheating factor), typically in [1.5; 4] + lambda2(_lambda2), // lambda2 in [0.5; 0.99] + mu1(_mu1), // target decrease in cost factor, in [2; 20] + xi(_xi), // xi typically belongs to [1; 1.1] max_accepted(_max_accepted), // depends on pb/neighborhood max_generated(_max_generated), // depends on pb/neighborhood - theta(10), // theta is typically set to 10 - statIsInitialized(false), - outf("out.data") - { } + theta(_theta), // theta is typically set to 10 + statIsInitialized(false) + { + chainStat.temperature = initTemp; + } /** * Initial temperature @@ -104,16 +140,25 @@ public: */ double init(EOT & _solution) { - accepted = generated = costs_sum = 0; + accepted = generated = 0;// = costs_sum = 0; negative_temp = equilibrium_not_reached = frozen = 0; + //cout << "acc " << max_accepted << " " << max_generated << endl; + + /* reinitializing = false; terminated = false; statIsInitialized = false; + */ + reinitializing = false; + //cout << "INIT T=" << initTemp << endl; + //cout << "INIT T=" << chainStat.temperature << endl; + //chainStat.temperature = initTemp; + /// - cout << "INIT T=" << initTemp << endl; + //cout << "INIT T=" << initTemp << endl; /// //outf.open("out"); @@ -132,9 +177,18 @@ public: void update(double& _temp, bool _acceptedMove, EOT & _solution) { //cout << _temp << " g " << generated << endl; - prevTemp = _temp; + chainStat.temperature = _temp; + chainStat.stoppingReason = NULL; + chainStat.chainEndingReason = NULL; + chainStat.equilibriumNotReached = false; + chainStat.negativeTemp = false; + //chainStat.markovChainEnded = false; + chainStat.generatedSolutions = generated; + chainStat.acceptedSolutions = accepted; generated++; + cout << "gen " << generated << endl; + if (_acceptedMove) { @@ -145,78 +199,83 @@ public: momentStat(_solution); else momentStat.init(_solution), statIsInitialized = true; - //cout << _solution.fitness() << " avgCost=" << momentStat.value().first << endl; + //cout << _solution.fitness() << " avgFitness=" << momentStat.value().first << endl; } - markovChainEnded = false; - if (accepted > max_accepted || generated > max_generated) { + + chainStat.chainEndingReason = accepted > max_accepted ? chainEndingReasons[0]: chainEndingReasons[1]; - markovChainEnded = true; + //chainStat.markovChainEnded = true; if (accepted == 0) // ADDED! Otherwise the computed std dev is null; we're probably at equilibrium { /// - cout << "Stopping: no accepted solution" << endl; + //cout << "Stopping: no accepted solution" << endl; /// - terminated = true; - return; + chainStat.terminated = true, chainStat.stoppingReason = stoppingReasons[0]; + return; // FIXME nutgud } /// - cout << (accepted > max_accepted? "MAXACC ": "MAXGEN "); + //cout << (accepted > max_accepted? "MAXACC ": "MAXGEN "); /// - //double avgCost = costs_sum/(double)accepted; + //double avgFitness = costs_sum/(double)accepted; //double stdDev = sqrt(varStat.value()); // WARNING: IT'S NO MORE THE AVG COST, NOW IT'S THE STD DEV! //double variance = varStat.value(); - double avgCost = momentStat.value().first; + double avgFitness = momentStat.value().first; double variance = momentStat.value().second; - double stdDev = sqrt(variance); - double sigma = stdDev; + //double stdDev = sqrt(variance); + chainStat.stdDev = sqrt(variance); + double sigma = chainStat.stdDev; double delta = sigma/mu2; - //outf << avgCost << endl; - outf << _temp << endl; - //outf << prevAvgCost-delta << endl; + //outf << avgFitness << endl; + //outf << _temp << endl; + //outf << prevAvgFitness-delta << endl; - accepted = generated = costs_sum = 0; + accepted = generated = 0; // = costs_sum = 0; //momentStat.init(_solution);//TODONE use next chain's first sol statIsInitialized = false; /// - cout << "T=" << _temp << " avgCost=" << avgCost << " stdDev=" << stdDev << " currCost=" << _solution.fitness() << endl; + //cout << "T=" << _temp << " avgFitness=" << avgFitness << " stdDev=" << chainStat.stdDev << " currCost=" << _solution.fitness() << endl; /// - double alpha; - double oldprevAvgCost = prevAvgCost; + double alpha = 0; + double prevAvgFitness = chainStat.avgFitness; /// - cout << "negTemp: " << negative_temp << " / " << K2 << endl; + //cout << "negTemp: " << negative_temp << " / " << K2 << endl; /// if (negative_temp < K2) { + //if (!chainStat.reinitializing) if (!reinitializing) { /// - if (avgCost/(prevAvgCost-delta) > xi) cout << "/!\\ eq not reached!" << endl; + //if (avgFitness/(chainStat.prevAvgFitness-delta) > xi) cout << "/!\\ eq not reached!" << endl; /// - if (avgCost/(prevAvgCost-delta) > xi) + if (avgFitness/(prevAvgFitness-delta) > xi) equilibrium_not_reached++; else equilibrium_not_reached = 0; } if (equilibrium_not_reached > K1) { /// - cout << "/!\\ Reinitializing (eq not reached)" << endl; + //cout << "/!\\ Reinitializing (eq not reached)" << endl; /// - + + //chainStat.reinitializing = true; reinitializing = true; + chainStat.equilibriumNotReached = true; + alpha = lambda1; delta = sigma/mu1; equilibrium_not_reached = 0; // ADDED! Otherwise the algo gets trapped here! @@ -224,11 +283,13 @@ public: else if (_temp*delta/(sigma*sigma) >= 1) { /// - cout << "/!\\ neg temp!" << endl; + //cout << "/!\\ neg temp!" << endl; /// negative_temp++; reinitializing = true; + chainStat.negativeTemp = true; + if (negative_temp < K2) { alpha = lambda1; @@ -244,7 +305,7 @@ public: { cout << "ccc" << endl; reinitializing = false; - prevAvgCost = avgCost; + prevAvgFitness = avgFitness; alpha = 1-_temp*delta/(sigma*sigma); } */ @@ -253,37 +314,42 @@ public: else { /// - cout << "[normal decrease]" << endl; + //cout << "[normal decrease]" << endl; /// reinitializing = false; - prevAvgCost = avgCost; + //chainStat.avgFitness = avgFitness; //alpha = 1-_temp*delta/(sigma*sigma); alpha = 1-_temp*delta/variance; //alpha = (sigma==0? 1: 1-_temp*delta/(sigma*sigma)); // ADDED! but removed if (sigma == 0) // ADDED! When std dev is null, the solution is probably at eq, and the algo can't go on anyways - terminated = true, cout << "Stopping: null std dev" << endl; + chainStat.terminated = true, chainStat.stoppingReason = stoppingReasons[1]; //, cout << "Stopping: null std dev" << endl; } } // FIXME: else what? alpha=? - - /// - cout << "*=" << alpha << endl; + //cout << "*=" << alpha << endl; /// _temp *= alpha; + + chainStat.currentFitness = _solution.fitness(); // FIXME here? + chainStat.alpha = alpha; + chainStat.delta = delta; + chainStat.avgFitness = avgFitness; // Never seems to be used - if (avgCost == oldprevAvgCost) // use a neighborhood to approximate double equality? + if (avgFitness == prevAvgFitness) // use a neighborhood to approximate double equality? frozen++; else frozen = 0; + if (frozen >= theta) + chainStat.terminated = true, chainStat.stoppingReason = stoppingReasons[2];; //exit(0); //cin.get(); @@ -299,26 +365,134 @@ public: bool operator() (double temperature) { /// - if (terminated) cout << "TERMINATED" << endl; + //if (chainStat.terminated) cout << "TERMINATED" << endl; /// return frozen < theta - && !terminated ; // ADDED! because 'frozen' doesn't terminate anything + && !chainStat.terminated ; // ADDED! because 'frozen' doesn't terminate anything + + + return !chainStat.terminated ; // ADDED! because 'frozen' doesn't terminate anything + } - - bool markovChainJustEnded() + /* + bool markovChainJustEnded() const { return markovChainEnded; - } + }*/ + + + // Code for generating the getters: +/* +#define __triki_getterType double +#define __triki_makeGetter(name) __triki_getterType name() { return chainStat.name; } + + __triki_makeGetter(stdDev) + __triki_makeGetter(avgFitness) + __triki_makeGetter(prevTemp) + __triki_makeGetter(currentFitness) + __triki_makeGetter(alpha) - MarkovChainStats& getMarkovChainStats() +#undef __triki_getterType +#define __triki_getterType int + + __triki_makeGetter(generatedSolutions) + __triki_makeGetter(acceptedSolutions) + +#undef __triki_getterType +#define __triki_getterType const char * + + __triki_makeGetter(stoppingReason) + __triki_makeGetter(chainEndingReason) + +#undef __triki_getterType +#undef __triki_makeGetter + */ + +#define __triki_makeGetter(name, type) type name() { return chainStat.name; } + + __triki_makeGetter(stdDev, double) + __triki_makeGetter(avgFitness, double) + __triki_makeGetter(temperature, double) + __triki_makeGetter(currentFitness, double) + __triki_makeGetter(alpha, double) + __triki_makeGetter(delta, double) + + __triki_makeGetter(generatedSolutions, int) + __triki_makeGetter(acceptedSolutions, int) + + __triki_makeGetter(equilibriumNotReached, bool) + __triki_makeGetter(negativeTemp, bool) + __triki_makeGetter(terminated, bool) + + __triki_makeGetter(stoppingReason, const char *) + __triki_makeGetter(chainEndingReason, const char *) + +#undef __triki_makeGetter + + + const bool markovChainEnded() const { - return markovChainStats; + return chainStat.chainEndingReason != NULL; } + struct MarkovChainStats; + const MarkovChainStats& markovChainStats() const + { + return chainStat; + } - + struct MarkovChainStats { + + MarkovChainStats() + : stdDev(0), avgFitness(0), temperature(-1), currentFitness(-1), alpha(0), delta(0), + //equilibriumNotReached(false), negativeTemp(false), terminated(false), markovChainEnded(false) + equilibriumNotReached(false), negativeTemp(false), terminated(false) + { } + + double + stdDev, + avgFitness, + //prevAvgFitness, + //expectedDecreaseInCost, // delta + //costs_sum, + temperature, + currentFitness, + alpha, + delta + ; + int generatedSolutions, acceptedSolutions; + //bool reinitializing, terminated, markovChainEnded; + bool equilibriumNotReached, negativeTemp, terminated; //, markovChainEnded; + const char * stoppingReason; + const char * chainEndingReason; // if NULL, the chain has not ended + //EOT& currentSolution; + //moTrikiCoolingSchedule& coolingSchedule; + void print(std::ostream& os = std::cout, bool onlyWhenChainEnds = true) const + { + //if (markovChainEnded || !onlyWhenChainEnds) + if (chainEndingReason != NULL || !onlyWhenChainEnds) + { + //os << "T=" << prevTemp << " avgFitness=" << prevAvgFitness << " stdDev=" << stdDev << " currentFitness=" << currentSolution.fitness() << endl; + os << "T=" << temperature << " avgFitness=" << avgFitness << " stdDev=" << stdDev << " currentFitness=" << currentFitness << " expected decrease in cost=" << delta << endl; + //os << "T=" << prevTemp << " \t\tavgFitness=" << prevAvgFitness << " \t\tstdDev=" << stdDev << " \t\tcurrentFitness=" << currentFitness << endl; + //os << "T*=" << alpha << " reinitializing=" << reinitializing << " markovChainEnded=" << markovChainEnded << endl;// << " terminated=" << terminated; + //os << "T*=" << alpha << " markovChainEnded=" << markovChainEnded;// << " terminated=" << terminated; + // TODONE print delta ? (expected decrease in cost) + if (chainEndingReason != NULL) + os << "T*=" << alpha << " chain ended, because " << chainEndingReason; + if (equilibriumNotReached) + os << " /!\\ equilibrium not reached"; + if (negativeTemp) + os << " /!\\ negative temperature"; + os << endl; + if (terminated) + os << "Terminated, because " << stoppingReason << endl; + // os << endl; + } + } + }; private: @@ -340,13 +514,17 @@ private: xi // xi typically belongs to [1; 1.1] // private variables ; + /* double stdDev, - prevAvgCost, + prevAvgFitness, expectedDecreaseInCost, // delta costs_sum, prevTemp - ; + ;*/ + MarkovChainStats chainStat; + //double costs_sum; + const int max_accepted, max_generated, @@ -359,48 +537,65 @@ private: negative_temp, frozen ; - bool reinitializing, terminated, markovChainEnded; + //bool reinitializing, terminated, markovChainEnded; //moFitnessVarianceStat varStat; moFitnessMomentsStat momentStat; - bool statIsInitialized; - - ofstream outf; + bool statIsInitialized, reinitializing; + //ofstream outf; + /* + static const char * stoppingReasons[] = {"no accepted solution", "null std dev" , "frozen >= theta"}; + static const char * chainEndingReasons[] = {"MAX GENerated solutions", "MAX ACCepted solutions"}; + */ + static const char * stoppingReasons[]; + static const char * chainEndingReasons[]; protected: - class Monitor { - public: - - Monitor(moTrikiCoolingSchedule& _cooling) - : cooling(_cooling) - { } - - /*void setTemperatureOstream(ostream& os) - { - - }*/ - void getLatestTemperature() - { - return cooling.prevTemp; - } - - void printCurrentStatus(ostream& os) - { - if (accepted >= max_accepted || generated >= max_generated) - { - os << "Markov chain finished. Temp was " << getLatestTemperature(); // chain number - - } - } - - private: - moTrikiCoolingSchedule& cooling; - }; +// class Monitor { +// public: +// +// Monitor(moTrikiCoolingSchedule& _cooling) +// : cooling(_cooling) +// { } +// +// /*void setTemperatureOstream(ostream& os) +// { +// +// }*/ +// void getLatestTemperature() +// { +// return cooling.prevTemp; +// } +// +// void printCurrentStatus(ostream& os) +// { +// if (accepted >= max_accepted || generated >= max_generated) +// { +// os << "Markov chain finished. Temp was " << getLatestTemperature(); // chain number +// +// } +// } +// +// private: +// moTrikiCoolingSchedule& cooling; +// }; }; + + +template< class Neighbor > +const char * moTrikiCoolingSchedule::stoppingReasons[] = {"no accepted solution", "null std dev" , "frozen >= theta"}; + +template< class Neighbor > +//const char * moTrikiCoolingSchedule::chainEndingReasons[] = {"MAX GENerated solutions", "MAX ACCepted solutions"}; +const char * moTrikiCoolingSchedule::chainEndingReasons[] = {"MAX ACCepted solutions", "MAX GENerated solutions"}; + + + + #endif diff --git a/mo/src/mo.h b/mo/src/mo.h index ef83384be..922a2c01e 100755 --- a/mo/src/mo.h +++ b/mo/src/mo.h @@ -106,6 +106,7 @@ #include #include #include +#include #include #include @@ -113,7 +114,6 @@ #include #include #include -#include #include #include diff --git a/mo/test/t-moSA.cpp b/mo/test/t-moSA.cpp index b1cdfeb67..3c67865cb 100644 --- a/mo/test/t-moSA.cpp +++ b/mo/test/t-moSA.cpp @@ -51,12 +51,12 @@ int main() { //test second constructor moSimpleCoolingSchedule cool(10, 0.9, 100, 0.01); - moSA test2(nh, fullEval, eval, cool); + moSA test2(nh, fullEval, cool, eval); //test third constructor moTrueContinuator cont; moSolNeighborComparator comp; - moSA test3(nh, fullEval, eval, cool, comp, cont); + moSA test3(nh, fullEval, cool, eval, cont, comp); std::cout << "[t-moSA] => OK" << std::endl; diff --git a/problems/eval/queenEval.h b/problems/eval/queenEval.h index da8dd9ee3..daaed9220 100644 --- a/problems/eval/queenEval.h +++ b/problems/eval/queenEval.h @@ -40,17 +40,17 @@ class queenEval : public eoEvalFunc { public: - /** - * Count number of threat - * @param _queen a solution - */ + /** + * Count number of threat + * @param _queen a solution + */ void operator()(EOT& _queen){ - unsigned int fit=0; - for(unsigned int i=0; i<_queen.size()-1; i++) - for(unsigned int j=i+1; j< _queen.size(); j++) - if(((unsigned int)_queen[i]+j-i == (unsigned int)_queen[j]) || ((unsigned int)_queen[i]+i-j == (unsigned int)_queen[j])) - fit++; - _queen.fitness(fit); + unsigned int fit=0; + for(unsigned int i=0; i<_queen.size()-1; i++) + for(unsigned int j=i+1; j< _queen.size(); j++) + if(((unsigned int)_queen[i]+j-i == (unsigned int)_queen[j]) || ((unsigned int)_queen[i]+i-j == (unsigned int)_queen[j])) + fit++; + _queen.fitness(fit); } };