-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_algs.py
More file actions
65 lines (52 loc) · 2.08 KB
/
Copy pathtest_algs.py
File metadata and controls
65 lines (52 loc) · 2.08 KB
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
import oracles
import optimization
import numpy as np
EPS = 1e-9
def test_nesterov(p, k, x_0):
oracle = oracles.PowerOracle(p, k)
y, stats = optimization.NesterovAcceleratedSolver(x_0, oracle, {
'Li': np.array([p * k, p * k]), 'S': np.array([p * k, p * k]), 'K': 200})
print('nesterov p =', oracle.p, ':', y)
# print(stats)
assert np.sum(y ** 2) < EPS
print('ok')
def test_metaalg(a, ak, b, bk, x_0, y_0):
f = oracles.PowerOracle(a, ak)
g = oracles.PowerOracle(b, bk)
y, stats = optimization.AcceleratedMetaalgorithmSolver(
x_0, f, g, a * ak, 200,
lambda y_00, oracle: optimization.NesterovAcceleratedSolver(
y_00, oracle,
{
'Li': np.array([3 * b * bk, 3 * b * bk]),
'S': np.array([3 * b * bk, 3 * b * bk]),
'K': 120
}),
lambda x: False,
lambda f, g, x: 0)
print('metaalg a =', a, ', b =', b, ':', y)
# print(stats)
assert np.sum(y ** 2) < EPS
print('ok')
def test_saddle():
f = oracles.PowerOracle(2, 1)
G = oracles.MultiplyOracle(1)
h = oracles.PowerOracle(2, 1)
x_0 = np.array([5, 5])
y_0 = np.array([1, 1])
x, y, stats = optimization.SolveSaddle(x_0, y_0, f, G, h,
{'H': 20, 'K': 50,
'stop_callback': None},
{'Li': np.array([20, 20]), 'S': np.array(
[20, 20]), 'K': 10},
{'H': 20, 'K': 20,
'stop_callback': None},
{'Li': np.array([20, 20]), 'S': np.array([20, 20]), 'K': 10})
print('saddle:', x, y)
assert np.sum(x ** 2 + y**2) < 1e-2
print('ok')
test_nesterov(2, 1, np.array([500, 700]))
test_nesterov(2, 10, np.array([500, -700]))
test_metaalg(2, 1, 2, 1, np.array([500, 700]), np.array([-500, 700]))
test_metaalg(2, 10, 2, 3, np.array([500, -700]), np.array([-500, -700]))
test_saddle()