-
Notifications
You must be signed in to change notification settings - Fork 4
/
base.py
68 lines (53 loc) · 1.96 KB
/
base.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'''This module contains the following:
BaseOptimizer
A class containing useful methods that are inherited by any optimizer.
Generally it is an attempt, to make the code more readable.
Todo:
* Add automatic verbose report.'''
import sys
class BaseOptimizer(object):
'''Baseclass with useful methods.
Inherited by all optimizers.'''
@staticmethod
def reachedFunctionTarget(function_target, candidate_fitness):
'''Check if fitness is below function_target.
... and maybe save some lines of code.
Args:
function_target (numeric):
Target function value f(y*).
candidate_fitness (numeric):
Function value of the candidate f(c).
Returns:
bool: A boolean in indicating if function_target is reached.
'''
if function_target != None:
return (candidate_fitness <= function_target)
else:
return False
@staticmethod
def reachedFunctionBudget(function_budget, function_evals):
'''Check if maximum number of function evaluations is reached.
... and maybe save some lines of code.
Args:
function_budget (int):
Budget of function evaluations.
function_evals (int):
Function evaluations executed.
Returns:
bool: A boolean in indicating if function_budget is reached.
'''
if function_budget != None:
return (function_evals >= function_budget)
else:
return False
@staticmethod
def report(string_to_print):
'''Report current state.
Makes a nice user interface.
**Maybe extend to verbose/non-verbose setting.**
Args:
string_to_print (str):
String to be printed.
'''
sys.stdout.write(string_to_print)
sys.stdout.flush()