intermediate commit 5

This commit is contained in:
LPTK 2013-07-09 16:25:30 +02:00
commit 816ea1553a
11 changed files with 377 additions and 157 deletions

View file

@ -73,7 +73,8 @@ eoMonitor& eoFileMonitor::operator()(void)
eoMonitor& eoFileMonitor::operator()(std::ostream& os) eoMonitor& eoFileMonitor::operator()(std::ostream& os)
{ {
if (vec.size() > 0)
{
iterator it = vec.begin(); iterator it = vec.begin();
os << (*it)->getValue(); os << (*it)->getValue();
@ -82,6 +83,7 @@ eoMonitor& eoFileMonitor::operator()(std::ostream& os)
{ {
os << delim.c_str() << (*it)->getValue(); os << delim.c_str() << (*it)->getValue();
} }
}
os << std::endl; os << std::endl;

View file

@ -78,3 +78,4 @@ protected :
}; };
#endif #endif

View file

@ -224,7 +224,9 @@ public :
void setValue(const std::string& _value) void setValue(const std::string& _value)
{ {
std::istringstream is(_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: protected:

View file

@ -57,9 +57,8 @@ public:
* @param _cont an external continuator (can be a checkpoint!) * @param _cont an external continuator (can be a checkpoint!)
* @param _fullEval a full evaluation function * @param _fullEval a full evaluation function
*/ */
moLocalSearch(NeighborhoodExplorer& _searchExpl, moLocalSearch(NeighborhoodExplorer& _searchExpl, moContinuator<Neighbor> & _cont, eoEvalFunc<EOT>& _fullEval)
moContinuator<Neighbor> & _cont, eoEvalFunc<EOT>& _fullEval) : searchExplorer(_searchExpl), cont(&_cont), fullEval(_fullEval), currentSolutionFitness(0)
: searchExplorer(_searchExpl), cont(&_cont), fullEval(_fullEval)
{ } { }
/** /**
@ -79,6 +78,10 @@ public:
bool b; bool b;
do { do {
currentSolutionFitness = _solution.fitness();
//std::cout << currentSolutionFitness << std::endl;
//std::cin.get();
// explore the neighborhood of the solution // explore the neighborhood of the solution
searchExplorer(_solution); searchExplorer(_solution);
// if a solution in the neighborhood can be accepted // if a solution in the neighborhood can be accepted
@ -129,6 +132,11 @@ public:
return searchExplorer; return searchExplorer;
} }
// TODO doc
double getCurrentSolutionFitness() const {
return currentSolutionFitness;
}
protected: protected:
// make the exploration of the neighborhood according to a local search heuristic // make the exploration of the neighborhood according to a local search heuristic
moNeighborhoodExplorer<Neighbor>& searchExplorer; moNeighborhoodExplorer<Neighbor>& searchExplorer;
@ -138,6 +146,10 @@ protected:
//full evaluation function //full evaluation function
eoEvalFunc<EOT>& fullEval; eoEvalFunc<EOT>& fullEval;
private:
double currentSolutionFitness;
}; };
#endif #endif

View file

@ -68,7 +68,7 @@ public:
{ }*/ { }*/
moSA(Neighborhood& _neighborhood, eoEvalFunc<EOT>& _fullEval, moEval<Neighbor>& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=0.01) moSA(Neighborhood& _neighborhood, eoEvalFunc<EOT>& _fullEval, moEval<Neighbor>& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=0.01)
: moLocalSearch<Neighbor> ( : moLocalSearch<Neighbor> (
explorer_ptr = defaultExplorer = new moSAExplorer<Neighbor>(_neighborhood, _eval, /*NULL,*/ *(defaultCool = new moSimpleCoolingSchedule<EOT>(_initT, _alpha, _span, _finalT)), NULL), *(explorer_ptr = defaultExplorer = new moSAExplorer<Neighbor>(_neighborhood, _eval, /*NULL,*/ *(defaultCool = new moSimpleCoolingSchedule<EOT>(_initT, _alpha, _span, _finalT)), NULL)),
*(defaultContinuator = new moTrueContinuator<Neighbor>()), *(defaultContinuator = new moTrueContinuator<Neighbor>()),
_fullEval ), _fullEval ),
explorer(*explorer_ptr), explorer(*explorer_ptr),
@ -150,7 +150,7 @@ public:
_cool ) _cool )
{ }*/ { }*/
: moLocalSearch<Neighbor> ( : moLocalSearch<Neighbor> (
explorer_ptr = defaultExplorer = new moSAExplorer<Neighbor> ( *(explorer_ptr = defaultExplorer = new moSAExplorer<Neighbor> (
_neighborhood, _neighborhood,
//_eval, //_eval.hasValue()? _eval.get(): *(defaultEval = new moFullEvalByCopy<Neighbor>(_fullEval)), //_eval, //_eval.hasValue()? _eval.get(): *(defaultEval = new moFullEvalByCopy<Neighbor>(_fullEval)),
_eval.hasValue()? defaultEval = NULL, _eval.get(): *(defaultEval = new moFullEvalByCopy<Neighbor>(_fullEval)), _eval.hasValue()? defaultEval = NULL, _eval.get(): *(defaultEval = new moFullEvalByCopy<Neighbor>(_fullEval)),
@ -158,7 +158,7 @@ public:
//_comp, //_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator<Neighbor>()), //_comp, //_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator<Neighbor>()),
//_cool ), //_cool ),
_cool, _cool,
_comp ), _comp )),
// ), // ),
_cont.hasValue()? defaultContinuator = NULL, _cont.get(): *(defaultContinuator = new moTrueContinuator<Neighbor>()), _cont.hasValue()? defaultContinuator = NULL, _cont.get(): *(defaultContinuator = new moTrueContinuator<Neighbor>()),
_fullEval ), _fullEval ),
@ -177,12 +177,13 @@ public:
) )
: moLocalSearch<Neighbor> ( : moLocalSearch<Neighbor> (
*(explorer_ptr = &_explorer), *(explorer_ptr = &_explorer),
_cont.hasValue()? _cont.get(): *(defaultContinuator = new moTrueContinuator<Neighbor>()), _fullEval ), _cont.hasValue()? defaultContinuator = NULL, _cont.get(): *(defaultContinuator = new moTrueContinuator<Neighbor>()), _fullEval ),
defaultEval(NULL), // removed in C++11 with unique_ptr
defaultExplorer(NULL), // removed in C++11 with unique_ptr defaultExplorer(NULL), // removed in C++11 with unique_ptr
explorer(*explorer_ptr), explorer(*explorer_ptr),
defaultCool(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 //defaultEval(NULL) // removed in C++11 with unique_ptr
defaultContinuator(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 //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr
{ } { }

View file

@ -107,13 +107,20 @@ public :
virtual void init(EOT& _sol) { virtual void init(EOT& _sol) {
for (unsigned i = 0; i < stats.size(); ++i) for (unsigned i = 0; i < stats.size(); ++i)
stats[i]->init(_sol); 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) for (unsigned i = 0; i < moupdaters.size(); ++i)
moupdaters[i]->init(); 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) for (unsigned int i = 0; i < monitors.size(); ++i)
(*monitors[i])(); (*monitors[i])();
*/
for (unsigned i = 0; i < continuators.size(); ++i) for (unsigned i = 0; i < continuators.size(); ++i)
continuators[i]->init(_sol); continuators[i]->init(_sol);

View file

@ -47,22 +47,28 @@ Lionel Parreaux <lionel.parreaux@gmail.com>
#include <iostream> #include <iostream>
using namespace std; 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 > class moTrikiCoolingSchedule: public moCoolingSchedule< EOT >
{ {
public: public:
//typedef typename Neighbor::EOT EOT ; //typedef typename Neighbor::EOT EOT ;
typedef moNeighborhood<Neighbor> Neighborhood ; //typedef moNeighborhood<Neighbor> Neighborhood ;
//! Constructor //! Constructor
/*! /*!
*/ */
/*
moTrikiCoolingSchedule (Neighborhood& _neighborhood, moEval<Neighbor>& _eval, double _initTemp) moTrikiCoolingSchedule (Neighborhood& _neighborhood, moEval<Neighbor>& _eval, double _initTemp) // FIXME rem useless params!!
: initTemp(_initTemp), : initTemp(_initTemp),
mu2(10), // mu2 typically belongs to [1; 20] 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 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_accepted(50), // depends on pb/neighborhood
max_generated(100), // depends on pb/neighborhood max_generated(100), // depends on pb/neighborhood
theta(10), // theta is typically set to 10 theta(10), // theta is typically set to 10
statIsInitialized(false), statIsInitialized(false)//,
outf("out.data") //outf("out.data")
{ } { }
*/
// moTrikiCoolingSchedule (
// Neighborhood& _neighborhood, moEval<Neighbor>& _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 ( moTrikiCoolingSchedule (
Neighborhood& _neighborhood, moEval<Neighbor>& _eval, double _initTemp, double _initTemp,
double _max_accepted, int _max_accepted = 50,
double _max_generated 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), : initTemp(_initTemp),
mu2(10), // mu2 typically belongs to [1; 20] mu2(_mu2), // mu2 typically belongs to [1; 20]
K1(2), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature K1(_K1), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature
K2(5), // ??? K2(_K2), // ???
lambda1(2), // the increase in temperature, typically in [1.5; 4] lambda1(_lambda1), // the increase in temperature (reheating factor), typically in [1.5; 4]
lambda2(.7), // lambda2 in [0.5; 0.99] lambda2(_lambda2), // lambda2 in [0.5; 0.99]
mu1(10), // target decrease in cost factor, in [2; 20] mu1(_mu1), // target decrease in cost factor, in [2; 20]
xi(1.05), // xi typically belongs to [1; 1.1] xi(_xi), // xi typically belongs to [1; 1.1]
max_accepted(_max_accepted), // depends on pb/neighborhood max_accepted(_max_accepted), // depends on pb/neighborhood
max_generated(_max_generated), // depends on pb/neighborhood max_generated(_max_generated), // depends on pb/neighborhood
theta(10), // theta is typically set to 10 theta(_theta), // theta is typically set to 10
statIsInitialized(false), statIsInitialized(false)
outf("out.data") {
{ } chainStat.temperature = initTemp;
}
/** /**
* Initial temperature * Initial temperature
@ -104,16 +140,25 @@ public:
*/ */
double init(EOT & _solution) { double init(EOT & _solution) {
accepted = generated = costs_sum = 0; accepted = generated = 0;// = costs_sum = 0;
negative_temp = equilibrium_not_reached = frozen = 0; negative_temp = equilibrium_not_reached = frozen = 0;
//cout << "acc " << max_accepted << " " << max_generated << endl;
/*
reinitializing = false; reinitializing = false;
terminated = false; terminated = false;
statIsInitialized = 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"); //outf.open("out");
@ -132,9 +177,18 @@ public:
void update(double& _temp, bool _acceptedMove, EOT & _solution) { void update(double& _temp, bool _acceptedMove, EOT & _solution) {
//cout << _temp << " g " << generated << endl; //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++; generated++;
cout << "gen " << generated << endl;
if (_acceptedMove) if (_acceptedMove)
{ {
@ -145,78 +199,83 @@ public:
momentStat(_solution); momentStat(_solution);
else momentStat.init(_solution), statIsInitialized = true; 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) { if (accepted > max_accepted || generated > max_generated) {
markovChainEnded = true; chainStat.chainEndingReason = accepted > max_accepted ? chainEndingReasons[0]: chainEndingReasons[1];
//chainStat.markovChainEnded = true;
if (accepted == 0) // ADDED! Otherwise the computed std dev is null; we're probably at equilibrium 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; chainStat.terminated = true, chainStat.stoppingReason = stoppingReasons[0];
return; 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 stdDev = sqrt(varStat.value()); // WARNING: IT'S NO MORE THE AVG COST, NOW IT'S THE STD DEV!
//double variance = varStat.value(); //double variance = varStat.value();
double avgCost = momentStat.value().first; double avgFitness = momentStat.value().first;
double variance = momentStat.value().second; double variance = momentStat.value().second;
double stdDev = sqrt(variance); //double stdDev = sqrt(variance);
double sigma = stdDev; chainStat.stdDev = sqrt(variance);
double sigma = chainStat.stdDev;
double delta = sigma/mu2; double delta = sigma/mu2;
//outf << avgCost << endl; //outf << avgFitness << endl;
outf << _temp << endl; //outf << _temp << endl;
//outf << prevAvgCost-delta << 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 //momentStat.init(_solution);//TODONE use next chain's first sol
statIsInitialized = false; 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 alpha = 0;
double oldprevAvgCost = prevAvgCost; double prevAvgFitness = chainStat.avgFitness;
/// ///
cout << "negTemp: " << negative_temp << " / " << K2 << endl; //cout << "negTemp: " << negative_temp << " / " << K2 << endl;
/// ///
if (negative_temp < K2) if (negative_temp < K2)
{ {
//if (!chainStat.reinitializing)
if (!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++; equilibrium_not_reached++;
else equilibrium_not_reached = 0; else equilibrium_not_reached = 0;
} }
if (equilibrium_not_reached > K1) if (equilibrium_not_reached > K1)
{ {
/// ///
cout << "/!\\ Reinitializing (eq not reached)" << endl; //cout << "/!\\ Reinitializing (eq not reached)" << endl;
/// ///
//chainStat.reinitializing = true;
reinitializing = true; reinitializing = true;
chainStat.equilibriumNotReached = true;
alpha = lambda1; alpha = lambda1;
delta = sigma/mu1; delta = sigma/mu1;
equilibrium_not_reached = 0; // ADDED! Otherwise the algo gets trapped here! equilibrium_not_reached = 0; // ADDED! Otherwise the algo gets trapped here!
@ -224,11 +283,13 @@ public:
else if (_temp*delta/(sigma*sigma) >= 1) else if (_temp*delta/(sigma*sigma) >= 1)
{ {
/// ///
cout << "/!\\ neg temp!" << endl; //cout << "/!\\ neg temp!" << endl;
/// ///
negative_temp++; negative_temp++;
reinitializing = true; reinitializing = true;
chainStat.negativeTemp = true;
if (negative_temp < K2) if (negative_temp < K2)
{ {
alpha = lambda1; alpha = lambda1;
@ -244,7 +305,7 @@ public:
{ {
cout << "ccc" << endl; cout << "ccc" << endl;
reinitializing = false; reinitializing = false;
prevAvgCost = avgCost; prevAvgFitness = avgFitness;
alpha = 1-_temp*delta/(sigma*sigma); alpha = 1-_temp*delta/(sigma*sigma);
} }
*/ */
@ -253,37 +314,42 @@ public:
else else
{ {
/// ///
cout << "[normal decrease]" << endl; //cout << "[normal decrease]" << endl;
/// ///
reinitializing = false; reinitializing = false;
prevAvgCost = avgCost; //chainStat.avgFitness = avgFitness;
//alpha = 1-_temp*delta/(sigma*sigma); //alpha = 1-_temp*delta/(sigma*sigma);
alpha = 1-_temp*delta/variance; alpha = 1-_temp*delta/variance;
//alpha = (sigma==0? 1: 1-_temp*delta/(sigma*sigma)); // ADDED! but removed //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 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=? // FIXME: else what? alpha=?
/// ///
cout << "*=" << alpha << endl; //cout << "*=" << alpha << endl;
/// ///
_temp *= alpha; _temp *= alpha;
chainStat.currentFitness = _solution.fitness(); // FIXME here?
chainStat.alpha = alpha;
chainStat.delta = delta;
chainStat.avgFitness = avgFitness;
// Never seems to be used // 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++; frozen++;
else frozen = 0; else frozen = 0;
if (frozen >= theta)
chainStat.terminated = true, chainStat.stoppingReason = stoppingReasons[2];;
//exit(0); //exit(0);
//cin.get(); //cin.get();
@ -299,26 +365,134 @@ public:
bool operator() (double temperature) bool operator() (double temperature)
{ {
/// ///
if (terminated) cout << "TERMINATED" << endl; //if (chainStat.terminated) cout << "TERMINATED" << endl;
/// ///
return frozen < theta 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; return markovChainEnded;
} }*/
MarkovChainStats& getMarkovChainStats()
// 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)
#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: private:
@ -340,13 +514,17 @@ private:
xi // xi typically belongs to [1; 1.1] xi // xi typically belongs to [1; 1.1]
// private variables // private variables
; ;
/*
double double
stdDev, stdDev,
prevAvgCost, prevAvgFitness,
expectedDecreaseInCost, // delta expectedDecreaseInCost, // delta
costs_sum, costs_sum,
prevTemp prevTemp
; ;*/
MarkovChainStats chainStat;
//double costs_sum;
const int const int
max_accepted, max_accepted,
max_generated, max_generated,
@ -359,48 +537,65 @@ private:
negative_temp, negative_temp,
frozen frozen
; ;
bool reinitializing, terminated, markovChainEnded; //bool reinitializing, terminated, markovChainEnded;
//moFitnessVarianceStat<EOT> varStat; //moFitnessVarianceStat<EOT> varStat;
moFitnessMomentsStat<EOT> momentStat; moFitnessMomentsStat<EOT> momentStat;
bool statIsInitialized; bool statIsInitialized, reinitializing;
ofstream outf;
//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: protected:
class Monitor { // class Monitor {
public: // public:
//
Monitor(moTrikiCoolingSchedule& _cooling) // Monitor(moTrikiCoolingSchedule& _cooling)
: cooling(_cooling) // : cooling(_cooling)
{ } // { }
//
/*void setTemperatureOstream(ostream& os) // /*void setTemperatureOstream(ostream& os)
{ // {
//
}*/ // }*/
void getLatestTemperature() // void getLatestTemperature()
{ // {
return cooling.prevTemp; // return cooling.prevTemp;
} // }
//
void printCurrentStatus(ostream& os) // void printCurrentStatus(ostream& os)
{ // {
if (accepted >= max_accepted || generated >= max_generated) // if (accepted >= max_accepted || generated >= max_generated)
{ // {
os << "Markov chain finished. Temp was " << getLatestTemperature(); // chain number // os << "Markov chain finished. Temp was " << getLatestTemperature(); // chain number
//
} // }
} // }
//
private: // private:
moTrikiCoolingSchedule& cooling; // moTrikiCoolingSchedule& cooling;
}; // };
}; };
template< class Neighbor >
const char * moTrikiCoolingSchedule<Neighbor>::stoppingReasons[] = {"no accepted solution", "null std dev" , "frozen >= theta"};
template< class Neighbor >
//const char * moTrikiCoolingSchedule<Neighbor>::chainEndingReasons[] = {"MAX GENerated solutions", "MAX ACCepted solutions"};
const char * moTrikiCoolingSchedule<Neighbor>::chainEndingReasons[] = {"MAX ACCepted solutions", "MAX GENerated solutions"};
#endif #endif

View file

@ -106,6 +106,7 @@
#include <coolingSchedule/moSimpleCoolingSchedule.h> #include <coolingSchedule/moSimpleCoolingSchedule.h>
#include <coolingSchedule/moDynSpanCoolingSchedule.h> #include <coolingSchedule/moDynSpanCoolingSchedule.h>
#include <coolingSchedule/moTrikiCoolingSchedule.h> #include <coolingSchedule/moTrikiCoolingSchedule.h>
#include <coolingSchedule/moHuangCoolingSchedule.h>
#include <eval/moDummyEval.h> #include <eval/moDummyEval.h>
#include <eval/moEval.h> #include <eval/moEval.h>
@ -113,7 +114,6 @@
#include <eval/moFullEvalByCopy.h> #include <eval/moFullEvalByCopy.h>
#include <eval/moFullEvalByModif.h> #include <eval/moFullEvalByModif.h>
#include <eval/moDoubleIncrNeighborhoodEval.h> #include <eval/moDoubleIncrNeighborhoodEval.h>
#include <eval/queenEval.h>
#include <explorer/moDummyExplorer.h> #include <explorer/moDummyExplorer.h>
#include <explorer/moFirstImprHCexplorer.h> #include <explorer/moFirstImprHCexplorer.h>

View file

@ -51,12 +51,12 @@ int main() {
//test second constructor //test second constructor
moSimpleCoolingSchedule<bitVector> cool(10, 0.9, 100, 0.01); moSimpleCoolingSchedule<bitVector> cool(10, 0.9, 100, 0.01);
moSA<bitNeighbor> test2(nh, fullEval, eval, cool); moSA<bitNeighbor> test2(nh, fullEval, cool, eval);
//test third constructor //test third constructor
moTrueContinuator<bitNeighbor> cont; moTrueContinuator<bitNeighbor> cont;
moSolNeighborComparator<bitNeighbor> comp; moSolNeighborComparator<bitNeighbor> comp;
moSA<bitNeighbor> test3(nh, fullEval, eval, cool, comp, cont); moSA<bitNeighbor> test3(nh, fullEval, cool, eval, cont, comp);
std::cout << "[t-moSA] => OK" << std::endl; std::cout << "[t-moSA] => OK" << std::endl;