-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConvergenceChecker.ts
42 lines (31 loc) · 1.33 KB
/
ConvergenceChecker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
export class ConvergenceChecker{
private maxIter:number = 0;
private iterCount:number = 0;
private lastObjFnValue:number = null;
private minChange:number = 1; //Minimum fractional change of the objective function's value between iterations required to continue iterating
constructor(minPercentChange:number, maxIter:number){
this.maxIter = maxIter;
this.minChange = minPercentChange / 100;
}
hasConverged(objFnValue:number, iter:number = -1):boolean{
this.iterCount = (iter < 0)? this.iterCount + 1 : iter;
if(this.iterCount >= this.maxIter) return true;
if(this.lastObjFnValue == null){
//First iteration, store value & do nothing
this.lastObjFnValue = objFnValue;
return false;
}else{
let diff = Math.abs(objFnValue - this.lastObjFnValue);
let denominator = Math.abs(objFnValue);
//Update previous value;
this.lastObjFnValue = objFnValue;
//Fractional change is infinite
if(denominator == 0) return false;
let fractionalChange = (diff / denominator);
return fractionalChange < this.minChange; //Signal exit if the current change in value is too small
}
}
getCurrentIter(){
return this.iterCount;
}
}