Skip to content

Commit f1c78ab

Browse files
committed
slight refactor
1 parent 9d49d84 commit f1c78ab

File tree

3 files changed

+45
-32
lines changed

3 files changed

+45
-32
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NonconvexBayesian"
22
uuid = "fb352abc-de7b-48de-9ebd-665b54b5d9b3"
33
authors = ["Mohamed Tarek <[email protected]> and contributors"]
4-
version = "0.1.4"
4+
version = "0.1.5"
55

66
[deps]
77
AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918"

src/bayesian.jl

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -135,31 +135,32 @@ function (s::ZeroOrderGPSurrogate)(x)
135135
return Interval.(y, y)
136136
else
137137
if eltype(s.y) <: Real
138-
_m, _v = mean_and_var(posterior(
139-
s.gps[1](s.X, s.noise), s.y,
140-
), [x])
141-
m, v = _m[1], _v[1]
138+
return _call_gp(s.gps[1], x, s.X, s.y, s.noise, s.std_multiple)
142139
else
143-
ms_vs = map(1:s.N) do i
144-
_gp = s.gps[i](s.X, s.noise)
145-
mean_and_var(posterior(_gp, getindex.(s.y, i)), [x])
146-
end
147-
m = reduce(vcat, getindex.(ms_vs, 1))
148-
v = reduce(vcat, getindex.(ms_vs, 2))
140+
return map(i -> _call_gp(s.gps[i], x, s.X, getindex.(s.y, i), s.noise, s.std_multiple), 1:length(s.gps))
149141
end
150-
r = s.std_multiple .* sqrt.(v)
151-
return Interval.(m .- r, m .+ r)
152142
end
153143
end
154144

145+
function _call_gp(gp, x, X, y, noise, std_multiple)
146+
_m, _v = mean_and_var(posterior(
147+
gp(X, noise), y,
148+
), [x])
149+
m, v = _m[1], _v[1]
150+
r = std_multiple * sqrt(v)
151+
return Interval(m - r, m + r)
152+
end
153+
155154
function surrogate(f, x; kwargs...)
156155
s = ZeroOrderGPSurrogate(f, x; mode = :exact, kwargs...)
157156
s(x)
158157
s.mode = :interval
159158
return s
160159
end
161160

162-
_lower_f(s) = x -> getproperty.(s(x), :lo)
161+
get_lo(x) = x.lo
162+
get_lo(x::AbstractVector) = map(get_lo, x)
163+
_lower_f(s) = get_lo s
163164
_equality_f(s) = x -> begin
164165
t = s(x)
165166
return [getproperty.(t, :lo); .- getproperty(t, :hi)]
@@ -187,14 +188,14 @@ function surrogate_model(vecmodel::VecModel; kwargs...)
187188
!(:expensive in c.flags)
188189
end
189190
eq_constraints = VectorOfFunctions(cheap_eq_constraints)
190-
ineq_constraints1 = mapreduce(vcat, expensive_eq_constraints; init = Union{}[]) do c
191+
ineq_constraints1 = Tuple(mapreduce(vcat, expensive_eq_constraints; init = Union{}[]) do c
191192
@assert c isa EqConstraint
192193
s = surrogate(c, copy(x0); kwargs...)
193194
push!(surrogates, s)
194195
return IneqConstraint(
195196
_equality_f(s), [zero.(c.rhs); zero.(c.rhs)], c.dim * 2, c.flags,
196197
)
197-
end
198+
end)
198199
ineq_constraints2 = map(vecmodel.ineq_constraints.fs) do c
199200
@assert c isa IneqConstraint
200201
if :expensive in c.flags
@@ -208,11 +209,11 @@ function surrogate_model(vecmodel::VecModel; kwargs...)
208209
end
209210
end
210211
ineq_constraints = VectorOfFunctions(
211-
vcat(ineq_constraints1, ineq_constraints2),
212+
(ineq_constraints1..., ineq_constraints2...),
212213
)
213214
return VecModel(
214215
obj, eq_constraints, ineq_constraints, vecmodel.sd_constraints, vecmodel.box_min, vecmodel.box_max, vecmodel.init, vecmodel.integer,
215-
), surrogates
216+
), Tuple(surrogates)
216217
end
217218

218219
function update_surrogates!(model, surrogates, x)
@@ -228,15 +229,15 @@ function update_surrogates!(model, surrogates, x)
228229
return o, i, e
229230
end
230231

231-
@params struct BayesOptOptions
232-
sub_options
232+
struct BayesOptOptions{S, N <: NamedTuple}
233+
sub_options::S
233234
maxiter::Int
234235
initialize::Bool
235236
ninit::Int
236237
ctol::Float64
237238
ftol::Float64
238239
postoptimize::Bool
239-
nt::NamedTuple
240+
nt::N
240241
end
241242
function BayesOptOptions(;
242243
sub_options = IpoptOptions(),
@@ -276,20 +277,20 @@ function BayesOptOptions(;
276277
)
277278
end
278279

279-
@params mutable struct BayesOptWorkspace <: Workspace
280-
model::VecModel
281-
sub_workspace
282-
x0::AbstractVector
283-
options::BayesOptOptions
284-
surrogates::AbstractVector
280+
mutable struct BayesOptWorkspace{M <: VecModel, S1, X <: AbstractVector, O <: BayesOptOptions, S2 <: Union{Tuple, AbstractVector}} <: Workspace
281+
model::M
282+
sub_workspace::S1
283+
x0::X
284+
options::O
285+
surrogates::S2
285286
end
286287

287-
@params struct BayesOptResult <: AbstractResult
288-
minimizer
289-
minimum
288+
struct BayesOptResult{M1, M2, S1 <: AbstractResult, S2} <: AbstractResult
289+
minimizer::M1
290+
minimum::M2
290291
niters::Int
291-
sub_result::AbstractResult
292-
surrogates
292+
sub_result::S1
293+
surrogates::S2
293294
end
294295

295296
# ScaledSobolIterator adapted from BayesianOptimization.jl

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ using LinearAlgebra, Test
44
f(x::AbstractVector) = sqrt(x[2])
55
g(x::AbstractVector, a, b) = (a*x[1] + b)^3 - x[2]
66

7+
@testset "Surrogate" begin
8+
x = zeros(2)
9+
10+
sf1 = NonconvexBayesian.ZeroOrderGPSurrogate(sum, x)
11+
lsf1 = NonconvexBayesian._lower_f(sf1)
12+
Zygote.gradient(lsf1, x)
13+
14+
sf2 = NonconvexBayesian.ZeroOrderGPSurrogate(x -> x.^2, x)
15+
lsf2 = NonconvexBayesian._lower_f(sf2)
16+
Zygote.jacobian(lsf2, x)
17+
end
18+
719
@testset "Cheap objective and constraints" begin
820
@testset "Fit prior: $fit_prior" for fit_prior in (true, false)
921
m = Model()

0 commit comments

Comments
 (0)