-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(SurrogatesPolyChaos): update tests
- Loading branch information
1 parent
af1794f
commit e20d9df
Showing
1 changed file
with
45 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,119 +1,69 @@ | ||
using SafeTestsets | ||
using SafeTestsets, Test | ||
|
||
@safetestset "PolynomialChaosSurrogates" begin | ||
using Surrogates | ||
using PolyChaos | ||
using Surrogates: sample, SobolSample | ||
using SurrogatesPolyChaos | ||
using Zygote | ||
|
||
#1D | ||
n = 20 | ||
lb = 0.0 | ||
ub = 4.0 | ||
f = x -> 2 * x | ||
x = sample(n, lb, ub, SobolSample()) | ||
y = f.(x) | ||
|
||
my_pce = PolynomialChaosSurrogate(x, y, lb, ub) | ||
val = my_pce(2.0) | ||
add_point!(my_pce, 3.0, 6.0) | ||
my_pce_changed = PolynomialChaosSurrogate(x, y, lb, ub, op = Uniform01OrthoPoly(1)) | ||
|
||
#ND | ||
n = 60 | ||
lb = [0.0, 0.0] | ||
ub = [5.0, 5.0] | ||
f = x -> x[1] * x[2] | ||
x = sample(n, lb, ub, SobolSample()) | ||
y = f.(x) | ||
|
||
my_pce = PolynomialChaosSurrogate(x, y, lb, ub) | ||
val = my_pce((2.0, 2.0)) | ||
add_point!(my_pce, (2.0, 3.0), 6.0) | ||
|
||
op1 = Uniform01OrthoPoly(1) | ||
op2 = Beta01OrthoPoly(2, 2, 1.2) | ||
ops = [op1, op2] | ||
multi_poly = MultiOrthoPoly(ops, min(1, 2)) | ||
my_pce_changed = PolynomialChaosSurrogate(x, y, lb, ub, op = multi_poly) | ||
|
||
# Surrogate optimization test | ||
lb = 0.0 | ||
ub = 15.0 | ||
p = 1.99 | ||
a = 2 | ||
b = 6 | ||
objective_function = x -> 2 * x + 1 | ||
x = sample(20, lb, ub, SobolSample()) | ||
y = objective_function.(x) | ||
my_poly1d = PolynomialChaosSurrogate(x, y, lb, ub) | ||
@test_broken surrogate_optimize(objective_function, SRBF(), a, b, my_poly1d, | ||
LowDiscrepancySample(; base = 2)) | ||
|
||
lb = [0.0, 0.0] | ||
ub = [10.0, 10.0] | ||
obj_ND = x -> log(x[1]) * exp(x[2]) | ||
x = sample(40, lb, ub, RandomSample()) | ||
y = obj_ND.(x) | ||
my_polyND = PolynomialChaosSurrogate(x, y, lb, ub) | ||
surrogate_optimize(obj_ND, SRBF(), lb, ub, my_polyND, SobolSample(), maxiters = 15) | ||
|
||
# AD Compatibility | ||
@testset "Scalar Inputs" begin | ||
n = 20 | ||
lb = 0.0 | ||
ub = 4.0 | ||
f = x -> 2 * x | ||
x = sample(n, lb, ub, SobolSample()) | ||
y = f.(x) | ||
my_pce = PolynomialChaosSurrogate(x, y, lb, ub) | ||
x_val = 1.2 | ||
@test my_pce(x_val) ≈ f(x_val) | ||
update!(my_pce, [3.0], [6.0]) | ||
my_pce_changed = PolynomialChaosSurrogate( | ||
x, y, lb, ub; orthopolys = Uniform01OrthoPoly(1)) | ||
@test my_pce_changed(x_val) ≈ f(x_val) | ||
end | ||
|
||
lb = 0.0 | ||
ub = 3.0 | ||
n = 10 | ||
x = sample(n, lb, ub, SobolSample()) | ||
f = x -> x^2 | ||
y = f.(x) | ||
@testset "Vector Inputs" begin | ||
n = 60 | ||
lb = [0.0, 0.0] | ||
ub = [5.0, 5.0] | ||
f = x -> x[1] * x[2] | ||
x = collect.(sample(n, lb, ub, SobolSample())) | ||
y = f.(x) | ||
my_pce = PolynomialChaosSurrogate(x, y, lb, ub) | ||
x_val = [1.2, 1.4] | ||
@test my_pce(x_val) ≈ f(x_val) | ||
update!(my_pce, [[2.0, 3.0]], [6.0]) | ||
@test my_pce(x_val) ≈ f(x_val) | ||
op1 = Uniform01OrthoPoly(1) | ||
op2 = Beta01OrthoPoly(2, 2, 1.2) | ||
ops = [op1, op2] | ||
multi_poly = MultiOrthoPoly(ops, min(1, 2)) | ||
my_pce_changed = PolynomialChaosSurrogate(x, y, lb, ub, orthopolys = multi_poly) | ||
end | ||
|
||
# #Polynomialchaos | ||
@testset "Polynomial Chaos" begin | ||
@testset "Derivative" begin | ||
lb = 0.0 | ||
ub = 3.0 | ||
f = x -> x^2 | ||
n = 50 | ||
x = sample(n, lb, ub, SobolSample()) | ||
x = collect(sample(n, lb, ub, SobolSample())) | ||
y = f.(x) | ||
my_poli = PolynomialChaosSurrogate(x, y, lb, ub) | ||
g = x -> my_poli'(x) | ||
g(3.0) | ||
x_val = 3.0 | ||
@test g(x_val) ≈ 2 * x_val | ||
end | ||
|
||
# #PolynomialChaos | ||
@testset "Polynomial Chaos ND" begin | ||
@testset "Gradient" begin | ||
n = 50 | ||
lb = [0.0, 0.0] | ||
ub = [10.0, 10.0] | ||
x = sample(n, lb, ub, SobolSample()) | ||
x = collect.(sample(n, lb, ub, SobolSample())) | ||
f = x -> x[1] * x[2] | ||
y = f.(x) | ||
my_poli_ND = PolynomialChaosSurrogate(x, y, lb, ub) | ||
g = x -> Zygote.gradient(my_poli_ND, x) | ||
g((1.0, 1.0)) | ||
|
||
n = 10 | ||
d = 2 | ||
lb = [0.0, 0.0] | ||
ub = [5.0, 5.0] | ||
x = sample(n, lb, ub, SobolSample()) | ||
f = x -> x[1]^2 + x[2]^2 | ||
y1 = f.(x) | ||
grad1 = x -> 2 * x[1] | ||
grad2 = x -> 2 * x[2] | ||
function create_grads(n, d, grad1, grad2, y) | ||
c = 0 | ||
y2 = zeros(eltype(y[1]), n * d) | ||
for i in 1:n | ||
y2[i + c] = grad1(x[i]) | ||
y2[i + c + 1] = grad2(x[i]) | ||
c = c + 1 | ||
end | ||
return y2 | ||
end | ||
y2 = create_grads(n, d, grad1, grad2, y) | ||
y = vcat(y1, y2) | ||
my_gek_ND = GEK(x, y, lb, ub) | ||
g = x -> Zygote.gradient(my_gek_ND, x) | ||
@test_broken g((2.0, 5.0)) #breaks after Zygote version 0.6.43 | ||
g = x -> Zygote.gradient(my_poli_ND, x)[1] | ||
x_val = [1.0, 2.0] | ||
@test g(x_val) ≈ [x_val[2], x_val[1]] | ||
end | ||
end |