diff --git a/.github/workflows/Downgrade.yml b/.github/workflows/Downgrade.yml index cf17c8d8f..28be0ec63 100644 --- a/.github/workflows/Downgrade.yml +++ b/.github/workflows/Downgrade.yml @@ -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 diff --git a/.github/workflows/Invalidations.yml b/.github/workflows/Invalidations.yml index 34eb7a92a..0133ad349 100644 --- a/.github/workflows/Invalidations.yml +++ b/.github/workflows/Invalidations.yml @@ -13,3 +13,5 @@ jobs: evaluate-invalidations: name: "Evaluate Invalidations" uses: "SciML/.github/.github/workflows/invalidations.yml@v1" + with: + julia-version: "1.10" diff --git a/lib/SurrogatesAbstractGPs/Project.toml b/lib/SurrogatesAbstractGPs/Project.toml index 734dc72a1..79e0a7af6 100644 --- a/lib/SurrogatesAbstractGPs/Project.toml +++ b/lib/SurrogatesAbstractGPs/Project.toml @@ -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"] diff --git a/lib/SurrogatesAbstractGPs/src/SurrogatesAbstractGPs.jl b/lib/SurrogatesAbstractGPs/src/SurrogatesAbstractGPs.jl index 11eab4432..04ef521aa 100644 --- a/lib/SurrogatesAbstractGPs/src/SurrogatesAbstractGPs.jl +++ b/lib/SurrogatesAbstractGPs/src/SurrogatesAbstractGPs.jl @@ -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 @@ -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 diff --git a/lib/SurrogatesAbstractGPs/test/runtests.jl b/lib/SurrogatesAbstractGPs/test/runtests.jl index fb553f0e0..f8a291e16 100644 --- a/lib/SurrogatesAbstractGPs/test/runtests.jl +++ b/lib/SurrogatesAbstractGPs/test/runtests.jl @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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