Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 16956364 authored by hhakim's avatar hhakim
Browse files

Add the epsilon on error change for Faust::StoppingCriterion.

parent 6e77e55f
No related branches found
No related tags found
No related merge requests found
...@@ -52,19 +52,21 @@ namespace Faust ...@@ -52,19 +52,21 @@ namespace Faust
class StoppingCriterion class StoppingCriterion
{ {
public: public:
StoppingCriterion(): StoppingCriterion():
isCriterionError(false), isCriterionError(false),
nb_it(500){} nb_it(500),
epsErr(-1),
lastErr(-1){}
StoppingCriterion(int nb_it_): StoppingCriterion(int nb_it_):
isCriterionError(false),nb_it(nb_it_){check_validity();} isCriterionError(false),nb_it(nb_it_){check_validity();}
StoppingCriterion(T errorThreshold_, int maxIteration_=10000): StoppingCriterion(T errorThreshold_, int maxIteration_=10000):
isCriterionError(true),errorThreshold(errorThreshold_),maxIteration(maxIteration_){check_validity();} isCriterionError(true),errorThreshold(errorThreshold_),maxIteration(maxIteration_), epsErr(-1), lastErr(-1) {check_validity();}
StoppingCriterion(bool isCriterionError_); StoppingCriterion(bool isCriterionError_);
StoppingCriterion(int nb_it, bool isCriterionError, T errorThreshold, int maxIteration=10000); StoppingCriterion(int nb_it, bool isCriterionError, T errorThreshold, int maxIteration=10000, T epsErr=-1);
~StoppingCriterion(){} ~StoppingCriterion(){}
...@@ -72,6 +74,13 @@ namespace Faust ...@@ -72,6 +74,13 @@ namespace Faust
bool do_continue(int current_ite, T error=-2.0)const; bool do_continue(int current_ite, T error=-2.0)const;
int get_crit() const{return nb_it;} int get_crit() const{return nb_it;}
bool isCriterionErr() const {return isCriterionError;} bool isCriterionErr() const {return isCriterionError;}
bool isCriterionEpsErr() const {return epsErr > -1;}
/**
* Set the epsilon error.
*
* This function also reinitializes lastErr so the same StoppingCriterion can be reused on multiple algorithm executions.
*/
void setCriterionEpsErr(const T& epsErr) {this->epsErr = epsErr; lastErr = -1;}
void Display() const; void Display() const;
string to_string() const; string to_string() const;
static const T NO_ERROR_PASSED; static const T NO_ERROR_PASSED;
...@@ -83,6 +92,8 @@ namespace Faust ...@@ -83,6 +92,8 @@ namespace Faust
bool isCriterionError; bool isCriterionError;
int nb_it; // number of iterations if !isCriterionError int nb_it; // number of iterations if !isCriterionError
T errorThreshold; T errorThreshold;
T epsErr;
T lastErr;
int maxIteration; int maxIteration;
// only used as stopping criterion, if isCriterionError, when error is still greater than // only used as stopping criterion, if isCriterionError, when error is still greater than
static const char * m_className; static const char * m_className;
......
...@@ -52,7 +52,7 @@ template<typename T> ...@@ -52,7 +52,7 @@ template<typename T>
const char * Faust::StoppingCriterion<T>::m_className="Faust::StoppingCriterion::"; const char * Faust::StoppingCriterion<T>::m_className="Faust::StoppingCriterion::";
template<typename T> template<typename T>
Faust::StoppingCriterion<T>::StoppingCriterion(bool isCriterionError_) : isCriterionError(isCriterionError_) Faust::StoppingCriterion<T>::StoppingCriterion(bool isCriterionError_) : isCriterionError(isCriterionError_), epsErr(-1)
{ {
if (isCriterionError_) if (isCriterionError_)
{ {
...@@ -64,8 +64,8 @@ Faust::StoppingCriterion<T>::StoppingCriterion(bool isCriterionError_) : isCrite ...@@ -64,8 +64,8 @@ Faust::StoppingCriterion<T>::StoppingCriterion(bool isCriterionError_) : isCrite
} }
template<typename T> template<typename T>
Faust::StoppingCriterion<T>::StoppingCriterion(int nb_it, bool isCriterionError, T errorThreshold, int maxIteration /* default 10000 */): Faust::StoppingCriterion<T>::StoppingCriterion(int nb_it, bool isCriterionError, T errorThreshold, int maxIteration /* default 10000 */, T epsErr/*=-1*/):
isCriterionError(isCriterionError),nb_it(nb_it), errorThreshold(errorThreshold), maxIteration(maxIteration) isCriterionError(isCriterionError),nb_it(nb_it), errorThreshold(errorThreshold), maxIteration(maxIteration), epsErr(epsErr), lastErr(-1)
{ {
check_validity(); check_validity();
} }
...@@ -95,8 +95,25 @@ template<typename T> ...@@ -95,8 +95,25 @@ template<typename T>
bool Faust::StoppingCriterion<T>::do_continue(int current_ite, T current_error /* = NO_ERROR_PASSED */)const bool Faust::StoppingCriterion<T>::do_continue(int current_ite, T current_error /* = NO_ERROR_PASSED */)const
{ {
if(epsErr > -1)
{
// the stopping criterion compares the error delta between two calls
if(lastErr > -1)
{
// it is not the first time the function is call (not the first iteration of the algorithm behind)
auto edelta = lastErr - current_error;
if(edelta < epsErr)
{
std::cout << "error delta between last two iterations " << edelta << " < "<< epsErr << " the algorithm should stop." << std::endl;
return false;
}
}
const_cast<Faust::StoppingCriterion<T>*>(this)->lastErr = current_error; // harmless
}
if (!isCriterionError) // if criterion is number of iteration, current_error does not matter if (!isCriterionError) // if criterion is number of iteration, current_error does not matter
return current_ite<nb_it ? true : false; return current_ite<nb_it;
else if (isCriterionError && current_error >= 0) else if (isCriterionError && current_error >= 0)
if (current_error < errorThreshold) if (current_error < errorThreshold)
return false; return false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment