-
Notifications
You must be signed in to change notification settings - Fork 5
MBM: optional GP-based M sampling on InfiniteGDP models #139
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
Open
dnguyen227
wants to merge
14
commits into
infiniteopt:master
Choose a base branch
from
dnguyen227:mbm_gp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a930770
Initial
dnguyen227 ed08522
Simplify and code coverage
dnguyen227 fa4a7ca
Don't compute grids until we need them
dnguyen227 5a01011
detect_uniform_M arg
dnguyen227 55e8b3f
Bound info for parameter functions in disjunct constraints
dnguyen227 6d3909a
New API
dnguyen227 51b3fd9
AbstractGPsDisjunctiveProgramming
dnguyen227 5f90360
More keywords
dnguyen227 3721c7b
GPSampler config object
dnguyen227 081e722
datatypes and variable name updates
dnguyen227 da0253c
Multi-parameter functions, datatypes, sampler types
dnguyen227 cbeb579
splitting bound info
dnguyen227 8520ba9
Fix test
dnguyen227 0b49a6e
API Update
dnguyen227 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| module AbstractGPsDisjunctiveProgramming | ||
|
|
||
| import AbstractGPs | ||
| import AbstractGPs.KernelFunctions | ||
| import DisjunctiveProgramming as DP | ||
|
|
||
| ################################################################################ | ||
| # GP FITTING | ||
| ################################################################################ | ||
| # Normalized to [0, 1]^d so one lengthscale works across dimensions | ||
| function _support_coords( | ||
| grids::Tuple{Vararg{Vector{Float64}}} | ||
| )::Vector{Vector{Float64}} | ||
| indices = CartesianIndices(length.(grids)) | ||
| mins = [minimum(g) for g in grids] | ||
| ranges = [max(maximum(g) - minimum(g), eps()) for g in grids] | ||
| return [[(grids[d][I[d]] - mins[d]) / ranges[d] | ||
| for d in 1:length(grids)] for I in vec(indices)] | ||
| end | ||
|
|
||
| # Lengthscale selected by marginal likelihood over the candidates | ||
| function _fit_posterior( | ||
| kernel::KernelFunctions.Kernel, | ||
| X::Vector{Vector{Float64}}, | ||
| y::Vector{Float64}, | ||
| sampler::DP.GPSampler | ||
| ) | ||
| best_posterior, best_log_prob = nothing, -Inf | ||
| for lengthscale in sampler.lengthscales | ||
| scaled_kernel = KernelFunctions.with_lengthscale( | ||
| kernel, lengthscale) | ||
| finite_gp = AbstractGPs.GP(scaled_kernel)(X, sampler.jitter) | ||
| log_prob = AbstractGPs.logpdf(finite_gp, y) | ||
| if log_prob > best_log_prob | ||
| best_posterior = AbstractGPs.posterior(finite_gp, y) | ||
| best_log_prob = log_prob | ||
| end | ||
| end | ||
| return best_posterior | ||
| end | ||
|
|
||
| function _mean_sd( | ||
| sampler::DP.GPSampler, | ||
| X::Vector{Vector{Float64}}, | ||
| solved::Dict{Int, Float64} | ||
| ) | ||
| solved_indices = collect(keys(solved)) | ||
| y = [solved[i] for i in solved_indices] | ||
| y_mean = sum(y) / length(y) | ||
| # floored so near-equal solved values still cushion the filled ones | ||
| y_scale = max(sqrt(sum(abs2, y .- y_mean) / max(length(y) - 1, 1)), | ||
| 1e-2 * abs(y_mean), 1e-8) | ||
| kernel = something(sampler.kernel, | ||
| KernelFunctions.SqExponentialKernel()) | ||
| posterior = _fit_posterior( | ||
| kernel, X[solved_indices], (y .- y_mean) ./ y_scale, sampler) | ||
| posterior_mean = AbstractGPs.mean(posterior, X) | ||
| posterior_var = max.(AbstractGPs.var(posterior, X), 0.0) | ||
| return posterior_mean .* y_scale .+ y_mean, | ||
| sqrt.(posterior_var) .* y_scale | ||
| end | ||
|
|
||
| ################################################################################ | ||
| # M VALUE SAMPLING | ||
| ################################################################################ | ||
| # Solve M at max-UCB selected supports, fill the rest with the bound | ||
| function DP.sample_M_values( | ||
| sampler::DP.GPSampler, | ||
| objectives::AbstractArray, | ||
| sub::DP.GDPSubmodel, | ||
| method::DP._MBM, | ||
| support_grids::Function | ||
|
dnguyen227 marked this conversation as resolved.
Outdated
|
||
| ) | ||
| indices = collect(CartesianIndices(objectives)) | ||
| n = length(indices) | ||
| solved = Dict{Int, Float64}() | ||
| solve_at(index::Int) = begin | ||
| M_val = DP.raw_M(sub, objectives[indices[index]], method) | ||
| M_val === nothing && return false | ||
| solved[index] = M_val | ||
| return true | ||
| end | ||
| # an evenly spaced seed count, or user-given seed fractions | ||
| fractions = sampler.seeds isa Int ? | ||
| range(0, 1, length = sampler.seeds) : sampler.seeds | ||
| for index in unique(1 .+ round.(Int, fractions .* (n - 1))) | ||
| solve_at(index) || return nothing | ||
| end | ||
| if sampler.detect_uniform_M | ||
| # a uniform M needs no fit, and so no support grid either | ||
| probes = collect(values(solved)) | ||
| all(==(first(probes)), probes) && return first(probes) | ||
| end | ||
| budget = min(ceil(Int, sampler.budget * n), n) | ||
| X = _support_coords(support_grids()) | ||
| while length(solved) < budget | ||
| means, sds = _mean_sd(sampler, X, solved) | ||
| acquisition = means .+ sampler.kappa .* sds | ||
| for index in keys(solved) | ||
| acquisition[index] = -Inf | ||
| end | ||
| solve_at(argmax(acquisition)) || return nothing | ||
| end | ||
| M_vals = Array{Float64}(undef, size(objectives)) | ||
| if length(solved) == n # nothing left to estimate | ||
| for (index, I) in enumerate(indices) | ||
| M_vals[I] = solved[index] | ||
| end | ||
| return M_vals | ||
| end | ||
| means, sds = _mean_sd(sampler, X, solved) | ||
| for (index, I) in enumerate(indices) # exact M values are nonnegative | ||
| M_vals[I] = get(solved, index, | ||
| max(means[index] + sampler.kappa * sds[index], 0.0)) | ||
| end | ||
| return M_vals | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.