Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use SurrogatesBase in SurrogatesAbstractGPs #489

Merged
merged 4 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/Downgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
version: ['1']
version: ['1.10']
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v2
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/Invalidations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ jobs:
evaluate-invalidations:
name: "Evaluate Invalidations"
uses: "SciML/.github/.github/workflows/invalidations.yml@v1"
with:
julia-version: "1.10"
10 changes: 7 additions & 3 deletions lib/SurrogatesAbstractGPs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ version = "0.1.0"

[deps]
AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918"
Surrogates = "6fc51010-71bc-11e9-0e15-a3fcc6593c49"
SurrogatesBase = "89f642e6-4179-4274-8202-c11f4bd9a72c"

[compat]
AbstractGPs = "0.5"
Surrogates = "6"
SafeTestsets = "0.1"
Surrogates = "6.9"
SurrogatesBase = "1.1"
Zygote = "0.6"
julia = "1.10"

[extras]
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Surrogates = "6fc51010-71bc-11e9-0e15-a3fcc6593c49"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[targets]
test = ["SafeTestsets", "Test", "Zygote"]
test = ["SafeTestsets", "Surrogates", "Test", "Zygote"]
34 changes: 14 additions & 20 deletions lib/SurrogatesAbstractGPs/src/SurrogatesAbstractGPs.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module SurrogatesAbstractGPs

import Surrogates: add_point!, AbstractSurrogate, std_error_at_point, _check_dimension
export AbstractGPSurrogate, var_at_point, logpdf_surrogate
using SurrogatesBase, AbstractGPs

using AbstractGPs
export AbstractGPSurrogate, logpdf_surrogate, update!, finite_posterior

mutable struct AbstractGPSurrogate{X, Y, GP, GP_P, S} <: AbstractSurrogate
mutable struct AbstractGPSurrogate{X, Y, GP, GP_P, S} <: AbstractStochasticSurrogate
x::X
y::Y
gp::GP
Expand All @@ -20,29 +19,24 @@ end

# predictor
function (g::AbstractGPSurrogate)(val)
# Check to make sure dimensions of input matches expected dimension of surrogate
_check_dimension(g, val)

return only(mean(g.gp_posterior([val])))
end

# for add point
# copies of x and y need to be made because we get
#"Error: cannot resize array with shared data " if we push! directly to x and y
function add_point!(g::AbstractGPSurrogate, new_x, new_y)
if new_x in g.x
println("Adding a sample that already exists, cannot build AbstracgGPSurrogate.")
return
function SurrogatesBase.update!(g::AbstractGPSurrogate, new_x, new_y)
for x in new_x
in(x, g.x) &&
error("Adding a sample that already exists, cannot update AbstractGPSurrogate!")
end
x_copy = copy(g.x)
push!(x_copy, new_x)
y_copy = copy(g.y)
push!(y_copy, new_y)
updated_posterior = posterior(g.gp(x_copy, g.Σy), y_copy)
g.x, g.y, g.gp_posterior = x_copy, y_copy, updated_posterior
g.x = vcat(g.x, new_x)
g.y = vcat(g.y, new_y)
g.gp_posterior = posterior(g.gp(g.x, g.Σy), g.y)
nothing
end

function SurrogatesBase.finite_posterior(g::AbstractGPSurrogate, xs)
g.gp_posterior(xs)
end

function std_error_at_point(g::AbstractGPSurrogate, val)
return sqrt(only(var(g.gp_posterior([val]))))
end
Expand Down
90 changes: 32 additions & 58 deletions lib/SurrogatesAbstractGPs/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using SafeTestsets, Test
using Surrogates: sample, SobolSample

@safetestset "AbstractGPSurrogate" begin
using Surrogates
using SurrogatesAbstractGPs
using AbstractGPs
using Zygote
using Surrogates: sample, SobolSample

@testset "1D -> 1D" begin
lb = 0.0
Expand All @@ -16,7 +15,7 @@ using Surrogates: sample, SobolSample
agp1D = AbstractGPSurrogate(x, y, gp = GP(SqExponentialKernel()), Σy = 0.05)
x_new = 2.5
y_actual = f.(x_new)
y_predicted = agp1D([x_new])
y_predicted = agp1D([x_new])[1]
@test isapprox(y_predicted, y_actual, atol = 0.1)
end

Expand All @@ -31,9 +30,9 @@ using Surrogates: sample, SobolSample
x_new = 2.5
y_actual = f.(x_new)
for i in 2:length(x_points)
add_point!(agp1D, x_points[i], y_points[i])
update!(agp1D, [x_points[i]], [y_points[i]])
end
y_predicted = agp1D([x_new])
y_predicted = agp1D(x_new)
@test isapprox(y_predicted, y_actual, atol = 0.1)
end

Expand All @@ -60,7 +59,7 @@ using Surrogates: sample, SobolSample
logpdf_vals = []
push!(logpdf_vals, logpdf_surrogate(agp_2D))
for i in 2:length(x)
add_point!(agp_2D, x[i], y[i])
update!(agp_2D, [x[i]], [y[i]])
push!(logpdf_vals, logpdf_surrogate(agp_2D))
end
@test first(logpdf_vals) < last(logpdf_vals) #as more points are added log marginal posterior predictive probability increases
Expand All @@ -77,31 +76,6 @@ using Surrogates: sample, SobolSample
@test agpND(x_new)≈f(x_new) atol=0.2
end

@testset "Optimization 1D" begin
objective_function = x -> 2 * x + 1
lb = 0.0
ub = 6.0
x = [2.0, 4.0, 6.0]
y = [5.0, 9.0, 13.0]
p = 2
a = 2
b = 6
my_k_EI1 = AbstractGPSurrogate(x, y)
surrogate_optimize(objective_function, EI(), a, b, my_k_EI1, RandomSample(),
maxiters = 200, num_new_samples = 155)
end

@testset "Optimization ND" begin
objective_function_ND = z -> 3 * hypot(z...) + 1
x = [(1.2, 3.0), (3.0, 3.5), (5.2, 5.7)]
y = objective_function_ND.(x)
theta = [2.0, 2.0]
lb = [1.0, 1.0]
ub = [6.0, 6.0]
my_k_E1N = AbstractGPSurrogate(x, y)
surrogate_optimize(objective_function_ND, EI(), lb, ub, my_k_E1N, RandomSample())
end

@testset "check working of logpdf_surrogate 1D" begin
lb = 0.0
ub = 3.0
Expand All @@ -122,32 +96,32 @@ using Surrogates: sample, SobolSample
logpdf_surrogate(agpND)
end

lb = 0.0
ub = 3.0
n = 10
x = sample(n, lb, ub, SobolSample())
f = x -> x^2
y = f.(x)

#AbstractGP 1D
@testset "AbstractGP 1D" begin
agp1D = AbstractGPSurrogate(x, y, gp = GP(SqExponentialKernel()), Σy = 0.05)
g = x -> agp1D'(x)
g([2.0])
end

lb = [0.0, 0.0]
ub = [10.0, 10.0]
n = 5
x = sample(n, lb, ub, SobolSample())
f = x -> x[1] * x[2]
y = f.(x)

# AbstractGP ND
@testset "AbstractGPSurrogate ND" begin
my_agp = AbstractGPSurrogate(x, y, gp = GP(SqExponentialKernel()), Σy = 0.05)
g = x -> Zygote.gradient(my_agp, x)
#g([(2.0,5.0)])
g((2.0, 5.0))
@testset "Gradients" begin
@testset "1D" begin
lb = 0.0
ub = 3.0
n = 100
x = sample(n, lb, ub, SobolSample())
f = x -> x^2
y = f.(x)
agp1D = AbstractGPSurrogate(x, y, gp = GP(SqExponentialKernel()), Σy = 0.05)
g = x -> Zygote.gradient(agp1D, x)
x_val = 2.0
@test g(x_val)[1]≈2 * x_val rtol=1e-1
end
@testset "ND" begin
lb = [0.0, 0.0]
ub = [10.0, 10.0]
n = 100
x = sample(n, lb, ub, SobolSample())
f = x -> x[1] * x[2]
y = f.(x)
my_agp = AbstractGPSurrogate(x, y, gp = GP(SqExponentialKernel()), Σy = 0.05)
g = x -> Zygote.gradient(my_agp, x)
x_val = (2.0, 5.0)
g_val = g(x_val)[1]
@test g_val[1]≈x_val[2] rtol=1e-1
@test g_val[2]≈x_val[1] rtol=1e-1
end
end
end
Loading