add target andsteady state iters
This commit is contained in:
parent
0d40f5c246
commit
948c289396
2 changed files with 46 additions and 4 deletions
34
sho/iters.py
34
sho/iters.py
|
|
@ -1,16 +1,50 @@
|
|||
import sys
|
||||
from collections import deque
|
||||
|
||||
########################################################################
|
||||
# Stopping criterions
|
||||
########################################################################
|
||||
|
||||
def max(i, val, sol, nb_it):
|
||||
"""Stop after reaching nb_it iterations."""
|
||||
if i < nb_it:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def target(i, val, sol, target):
|
||||
"""Stop after reaching target value."""
|
||||
if val < target:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
class steady:
|
||||
"""Stop if improvement is lesser than epsilon, in the last delta iterations."""
|
||||
|
||||
def __init__(self, delta, epsilon = 0):
|
||||
self.epsilon = epsilon
|
||||
self.delta = delta
|
||||
self.delta_vals = deque()
|
||||
|
||||
def __call__(self, i, val, sol):
|
||||
if i < self.delta: # Always wait the first delta iterations.
|
||||
self.delta_vals.append(val)
|
||||
return True
|
||||
|
||||
else:
|
||||
#FILO stack.
|
||||
self.delta_vals.popleft()
|
||||
self.delta_vals.append(val)
|
||||
|
||||
if val - self.delta_vals[0] <= self.epsilon:
|
||||
return False # Stop here.
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
# Stopping criterions that are actually just checkpoints.
|
||||
|
||||
def several(i, val, sol, agains):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue