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

@ -47,22 +47,28 @@ Lionel Parreaux <lionel.parreaux@gmail.com>
#include <iostream>
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<Neighbor> Neighborhood ;
//typedef moNeighborhood<Neighbor> Neighborhood ;
//! Constructor
/*!
*/
moTrikiCoolingSchedule (Neighborhood& _neighborhood, moEval<Neighbor>& _eval, double _initTemp)
/*
moTrikiCoolingSchedule (Neighborhood& _neighborhood, moEval<Neighbor>& _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<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 (
Neighborhood& _neighborhood, moEval<Neighbor>& _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<EOT> varStat;
moFitnessMomentsStat<EOT> 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<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