-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_kazda_li.jl
More file actions
130 lines (110 loc) · 3.81 KB
/
Copy pathtest_kazda_li.jl
File metadata and controls
130 lines (110 loc) · 3.81 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
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
@testset "Progressive fitting" begin
# 1D
x = collect(range(-1, 1; length = 30))
z = x .^ 2
f = FunctionEvaluations(tuple.(x), z)
for (tol, pen) ∈ [(0.05, :max), (2.5, :l1), (0.5, :l2), (0.5, :none)]
pwa_red = approx(
f,
Convex(),
Progressive(optimizer = optimizer, tolerance = tol, metric = pen),
)
for (x, z) ∈ f
pv = evaluate(pwa_red, x)
@test z ≥ pv || z ≈ pv
end
end
# 2D
g(x, y) = x^2 + y^2
vals = PiecewiseAffineApprox._sample_uniform(g, [(-1, 1), (-1, 1)], 10)
pwa_red = approx(
vals,
Convex(),
Progressive(optimizer = optimizer, tolerance = 0.2, metric = :max),
)
@test_broken length(pwa_red.planes) == 10
@test evaluate(pwa_red, (0, 0)) ≈ 0.024 atol = 0.001
@testset "Nonconvex" begin
h(x, y) = sin(5 * x) * (x^2 + y^2)
vals = PiecewiseAffineApprox._sample_uniform(h, [(-1, 1), (-1, 1)], 10)
@test_throws ErrorException approx(
vals,
Convex(),
Progressive(optimizer = optimizer, tolerance = 2.0, metric = :l2),
)
vals_c = enforce_curvature(vals, Convex(), optimizer, :l1)
@test length(vals_c) == length(vals)
pwa_con = approx(
vals_c,
Convex(),
Progressive(optimizer = optimizer, tolerance = 0.1, metric = :l2),
)
x = collect(range(-1, 1; length = 30))
z = x .^ 2
z[5] += 0.1
f = FunctionEvaluations(tuple.(x), z)
fc = enforce_curvature(f, Convex(), optimizer)
@test length(fc) == length(f)
@test fc.values[5] ≈ 0.5291 atol = 0.001
pwa_con = approx(
fc,
Convex(),
Progressive(optimizer = optimizer, tolerance = 0.2, metric = :l1),
)
@test PWA._planes(pwa_con) == 8
x = collect(range(-1, 1; length = 30))
z = -x .^ 2
z[8] -= 0.2
z[12] += 0.1
f = FunctionEvaluations(tuple.(x), z)
fc = enforce_curvature(f, Concave(), optimizer)
@test fc.values[8] ≈ -0.2722 atol = 0.001
end
end
@testset "Full Order fitting" begin
# 1D
x = collect(range(-1, 1; length = 30))
z = x .^ 2
f = FunctionEvaluations(tuple.(x), z)
for (tol, pen) ∈ [(0.05, :max), (2.5, :l1), (0.5, :l2), (0.5, :none)]
pwa_red =
approx(f, Convex(), FullOrder(optimizer = optimizer, metric = pen))
for (x, z) ∈ f
pv = evaluate(pwa_red, x)
@test z ≥ pv || z ≈ pv
end
end
# 2D
g(x, y) = x^2 + y^2
vals = PiecewiseAffineApprox._sample_uniform(g, [(-1, 1), (-1, 1)], 10)
pwa_red =
approx(vals, Convex(), FullOrder(optimizer = optimizer, metric = :max))
@test length(pwa_red.planes) == 100
@test evaluate(pwa_red, (0, 0)) ≈ 0.024 atol = 0.001
@testset "Nonconvex" begin
h(x, y) = sin(5 * x) * (x^2 + y^2)
vals = PiecewiseAffineApprox._sample_uniform(h, [(-1, 1), (-1, 1)], 10)
@test_throws ErrorException approx(
vals,
Convex(),
FullOrder(optimizer = optimizer, metric = :l2),
)
vals_c = PWA.convexify(vals, optimizer, :l1)
@test length(vals_c) == length(vals)
pwa_con = approx(
vals_c,
Convex(),
FullOrder(optimizer = optimizer, metric = :l2),
)
x = collect(range(-1, 1; length = 30))
z = x .^ 2
z[5] += 0.1
f = FunctionEvaluations(tuple.(x), z)
fc = PWA.convexify(f, optimizer)
@test length(fc) == length(f)
@test fc.values[5] ≈ 0.5291 atol = 0.001
pwa_con =
approx(fc, Convex(), FullOrder(optimizer = optimizer, metric = :l1))
@test PWA._planes(pwa_con) == 30
end
end