-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
192 lines (164 loc) · 4.98 KB
/
utils.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import random
import numpy as np
import testfunctions as tf
from constants import *
def progress_bar(current, max):
"""
current/max [████████████████____]
:param current: counter (usually from 0 to max - 1)
:param max: max value exclusively
:return: void
"""
current += 1
percent = "["
five_percent = max / 20.0
current_percent = five_percent
while current_percent <= max:
if current >= current_percent:
percent += "█"
else:
percent += " "
current_percent += five_percent
percent += "]"
print(f"\r{current}/{max} {percent}", end="")
if current == max:
print()
def random_data(domain_list):
"""
:param domain_list:
:return: list of randomly generated arguments
"""
data = []
for i in range(len(domain_list)):
data.append(random.uniform(domain_list[i][0], domain_list[i][1]))
return data
def sigmoid(x):
"""
:param x: real number
:return: number beetwen 0 and 1
"""
return 1 / (1 + np.exp(-x))
def sigmoid_der(x):
"""
Derivative of sigmoid
:param x: number
:return: number beetwen 0 and 1
"""
if isinstance(x, np.matrix):
return np.multiply(sigmoid(x), (1 - sigmoid(x)))
return sigmoid(x) * (1 - sigmoid(x))
def derivative(func, args, n):
"""
Simple derivative by definition
:param func: function which take args as arguments
:param args: list of numbers
:param n: index of argument from args derivatise is about
:return: number
"""
h = 0.000001
new_args = args.copy()
new_args[n] += h
return (func(new_args) - func(args)) / h
def gradient(func, args_values):
"""
Simple gradient for traditional multiarguments functions
:param func: function which take args_values as arguments
:param args_values: list of numbers
:return: vector of derivatives (numbers)
"""
result = []
for i in range(len(args_values)):
result.append(derivative(func, args_values, i))
return result
class Function:
def __init__(self, func, domain, min_max=MIN, solutions=None):
self.function = func
self.domain = domain
self.arg_num = len(domain)
self.min_max = min_max
self.solutions = solutions
def random_data(self):
"""
:param domain_list:
:return: list of randomly generated arguments
"""
return random_data(self.domain)
def __call__(self, *args, **kwargs):
return self.function(*args, **kwargs)
@staticmethod
def get_all() -> list:
"""
:return: list of all functions to test (Function type elements)
"""
return [
Function(
tf.polynomial, # 0
[[-10, 10], [-10, 10]],
MIN,
solutions=[[0.55672, 0.55672], [-0.55672, 0.55672],
[0.55672, -0.55672], [-0.55672, -0.55672]]
), Function(
tf.rosenbrock, # 1
[[-10, 10], [-10, 10]],
MIN,
solutions=[[1, 1]]
), Function(
tf.zangwill, # 2
[[-150, 150], [-150, 150], [-150, 150]],
MIN,
solutions=[[0, 0, 0]]
), Function(
tf.goldenstein, # 3
[[-2, 2], [-2, 2]],
MIN,
solutions=[[0, -1]]
), Function(
tf.exp_sin, # 4
[[-10, 10]],
MAX,
solutions=[[0, 1]]
), Function(
tf.himmbelblau, # 5
[[-10, 10], [-10, 10]],
MIN,
solutions=[[-5, 5]]
), Function(
tf.ackley, # 6
[[-30, 30]] * 10, # N=10
MIN,
solutions=[[0] * 10] # N=10
), Function(
tf.rastrigin, # 7
[[-1, 1]] * 10, # N=10
MIN,
solutions=[[0] * 10] # N=10
), Function(
tf.geem, # 8
[[-10, 10], [-10, 10]],
MIN,
solutions=[[-0.08984, 0.71266], [0.08984, -0.71266]]
), Function(
tf.sin_sin_exp, # 9
[[-10, 10], [-10, 10]],
MIN
), Function(
tf.sin_sin_exp, # 10
[[-10, 10], [-10, 10]],
MAX
), Function(
tf.lin_exp, # 11
[[-10, 10], [-10, 10]],
MIN,
), Function(
tf.lin_exp, # 12
[[-10, 10], [-10, 10]],
MAX,
), Function(
tf.sin_power6, # 13
[[-100000, 100000]],
MIN,
), Function(
tf.sin_power6, # 14
[[-100000, 100000]],
MAX,
)]