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
class StoppingCriterion
{
public:
StoppingCriterion():
isCriterionError(false),
nb_it(500){}
StoppingCriterion():
isCriterionError(false),
nb_it(500),
epsErr(-1),
lastErr(-1){}
StoppingCriterion(int nb_it_):
isCriterionError(false),nb_it(nb_it_){check_validity();}
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(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(){}
......@@ -72,6 +74,13 @@ namespace Faust
bool do_continue(int current_ite, T error=-2.0)const;
int get_crit() const{return nb_it;}
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;
string to_string() const;
static const T NO_ERROR_PASSED;
......@@ -83,6 +92,8 @@ namespace Faust
bool isCriterionError;
int nb_it; // number of iterations if !isCriterionError
T errorThreshold;
T epsErr;
T lastErr;
int maxIteration;
// only used as stopping criterion, if isCriterionError, when error is still greater than
static const char * m_className;
......
......@@ -52,7 +52,7 @@ template<typename T>
const char * Faust::StoppingCriterion<T>::m_className="Faust::StoppingCriterion::";
template<typename T>
Faust::StoppingCriterion<T>::StoppingCriterion(bool isCriterionError_) : isCriterionError(isCriterionError_)
Faust::StoppingCriterion<T>::StoppingCriterion(bool isCriterionError_) : isCriterionError(isCriterionError_), epsErr(-1)
{
if (isCriterionError_)
{
......@@ -64,8 +64,8 @@ Faust::StoppingCriterion<T>::StoppingCriterion(bool isCriterionError_) : isCrite
}
template<typename T>
Faust::StoppingCriterion<T>::StoppingCriterion(int nb_it, bool isCriterionError, T errorThreshold, int maxIteration /* default 10000 */):
isCriterionError(isCriterionError),nb_it(nb_it), errorThreshold(errorThreshold), maxIteration(maxIteration)
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), epsErr(epsErr), lastErr(-1)
{
check_validity();
}
......@@ -95,8 +95,25 @@ template<typename T>
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
return current_ite<nb_it ? true : false;
return current_ite<nb_it;
else if (isCriterionError && current_error >= 0)
if (current_error < errorThreshold)
return false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment