-
Notifications
You must be signed in to change notification settings - Fork 0
/
StoppingCondition.py
44 lines (28 loc) · 923 Bytes
/
StoppingCondition.py
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
43
44
from abc import ABCMeta, abstractmethod
import datetime
class StoppingCondition:
__metaclass__ = ABCMeta
@abstractmethod
def is_satisfied(self):
pass
@abstractmethod
def update(self):
pass
class ElapsedTimeStoppingCondition(StoppingCondition):
# Time in seconds must be provided
def __init__(self, time):
self.initTime = datetime.datetime.now()
self.time = datetime.timedelta(seconds=time)
def is_satisfied(self):
now = datetime.datetime.now()
return (now - self.initTime) >= self.time
def update(self):
pass
class NumGenerationsStoppingCondition(StoppingCondition):
def __init__(self, max_generations):
self.maxGenerations = max_generations
self.currentGen = 0
def is_satisfied(self):
return self.currentGen == self.maxGenerations
def update(self):
self.currentGen += 1