-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_dim_conditional.py
87 lines (58 loc) · 2.29 KB
/
multi_dim_conditional.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
import numpy as np
from multi_dim import nelder_mead
def penalty_functions(f, gs: np.ndarray, x: np.ndarray, accuracy: float, r=1, C=5) -> (np.ndarray, int):
"""Метод штрафных функций"""
iterations = 0
x = np.copy(x)
penalty = lambda g_res: 0 if g_res <= 0 else g_res
while True:
iterations += 1
P = lambda x: (r / 2) * sum([penalty(g(x)) for g in gs])
F = lambda x: f(x) + P(x)
x = nelder_mead(F, x, 2, 0.0001)[0]
if P(x) < accuracy:
break
else:
r *= C
return x, iterations
def barrier_functions(f, gs: np.ndarray, x: np.ndarray, accuracy: float, r=1, C=10) -> (np.ndarray, int):
"""Метод барьерных функций"""
iterations = 0
x = np.copy(x)
while True:
iterations += 1
P = lambda x: -r * sum([1/g(x) for g in gs])
F = lambda x: f(x) + P(x)
x = nelder_mead(F, x, 2, 0.0001)[0]
if P(x) < accuracy:
break
else:
r /= C
return x, iterations
def penalty_barrier_functions(f, gs: np.ndarray, x: np.ndarray, accuracy: float, r=1, C=5) -> (np.ndarray, int):
"""Метод, взаимодополнения штрафных и барьерных функций"""
if any([g(x) > 0 for g in gs]):
return penalty_functions(f, gs, x, accuracy, r, C)
else:
return barrier_functions(f, gs, x, accuracy, r, C)
def lagrange_mody_functions(f, gs: np.ndarray, x: np.ndarray, accuracy: float, mu=None, r=3., C=5.) -> (np.ndarray, int):
"""Метод модифицированных функций Лагранжа"""
if mu is None:
mu = np.zeros((len(gs)))
iterations = 0
x = np.copy(x)
penalty = lambda g_res: 0 if g_res <= 0 else g_res
while True:
if iterations > 100:
break
iterations += 1
P = lambda x: sum([max([0] + mu[i] + r*penalty(gs[i](x)))**2 - mu[i]**2 for i in range(len(gs))]) / (2*r)
L = lambda x: f(x) + P(x)
x = nelder_mead(L, x, 2, 0.0001)[0]
if P(x) < accuracy:
break
else:
mu += np.array(list(map(lambda g: r*penalty(g(x)), gs)))
mu[mu < 0] = 0
r *= C
return x, iterations