-
-
Notifications
You must be signed in to change notification settings - Fork 70
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
Section sampler #279
Merged
Merged
Section sampler #279
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
e449a46
WIP: SectionSamplers
marcoq 8d28344
WIP: SectionSamplers
marcoq dcc8439
WIP: SectionSamplers
marcoq b1c2083
WIP: SectionSamplers
marcoq 8dfa3fb
WIP: SectionSamplers
marcoq 6aa3ef8
WIP: SectionSamplers
marcoq 9fa28fb
WIP: SectionSamplers
marcoq 5716744
Fix endless while loop
marcoq 48c06a0
Change tests/SectionSamplers to test/SectionSampleTests
marcoq f483860
Change tests/SectionSamplers to test/SectionSampleTests
marcoq 5f0df8a
Change tests/SectionSamplers to test/SectionSampleTests
marcoq 4d24000
Fix endless while loop
marcoq b513a49
WIP: Section Samapler
marcoq 0cb5b95
Section Sampler
marcoq eed784b
Add SectionSample
marcoq 90131ba
Add SectionSample
marcoq 045f794
Add SectionSample
marcoq 591ec41
Simplify testing
marcoq 4fdc636
Fixes to tests
marcoq 99ee395
Improve documentation
marcoq dea6560
Add workaround suggested by @mcabbott for <https://github.com/FluxML/…
marcoq d5ea518
Update SectionSampleTests.jl
marcoq 33996d7
Fix whitespace
marcoq f29f9a4
Eliminated stale code with un-needed "workaround"
marcoq 00529c5
Fixed comment
marcoq 096f369
Update Sampling.jl
marcoq 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
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
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,5 +1,6 @@ | ||
using Flux | ||
using Flux: @epochs | ||
|
||
mutable struct NeuralSurrogate{X,Y,M,L,O,P,N,A,U} <: AbstractSurrogate | ||
x::X | ||
y::Y | ||
|
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 |
---|---|---|
|
@@ -697,11 +697,11 @@ function surrogate_optimize(obj::Function,::DYCORS,lb::Number,ub::Number,surr1:: | |
while new_points[i] < lb || new_points[i] > ub | ||
if new_points[i] > ub | ||
#reflection | ||
new_points[i] = maximum(surr1.x) - norm(new_points[i] - maximum(surr1.x)) | ||
new_points[i] = max(lb, maximum(surr1.x) - norm(new_points[i] - maximum(surr1.x))) | ||
end | ||
if new_points[i] < lb | ||
#reflection | ||
new_points[i] = minimum(surr1.x) + norm(new_points[i]-minimum(surr1.x)) | ||
new_points[i] = min(ub, minimum(surr1.x) + norm(new_points[i]-minimum(surr1.x))) | ||
end | ||
end | ||
end | ||
|
@@ -831,10 +831,10 @@ function surrogate_optimize(obj::Function,::DYCORS,lb,ub,surrn::AbstractSurrogat | |
for j = 1:d | ||
while new_points[i,j] < lb[j] || new_points[i,j] > ub[j] | ||
if new_points[i,j] > ub[j] | ||
new_points[i,j] = maximum(surrn.x)[j] - norm(new_points[i,j] - maximum(surrn.x)[j]) | ||
new_points[i,j] = max(lb[j], maximum(surrn.x)[j] - norm(new_points[i,j] - maximum(surrn.x)[j])) | ||
end | ||
if new_points[i,j] < lb[j] | ||
new_points[i,j] = minimum(surrn.x)[j] + norm(new_points[i]-minimum(surrn.x)[j]) | ||
new_points[i,j] = min(ub[j], minimum(surrn.x)[j] + norm(new_points[i]-minimum(surrn.x)[j])) | ||
end | ||
end | ||
end | ||
|
@@ -1711,3 +1711,82 @@ end | |
end | ||
return pareto_set,pareto_front | ||
end | ||
|
||
|
||
function surrogate_optimize(obj::Function,::EI,lb,ub,krig,sample_type::SectionSample;maxiters=100,num_new_samples=100) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This specialization may need docs. |
||
dtol = 1e-3*norm(ub-lb) | ||
eps = 0.01 | ||
for i = 1:maxiters | ||
d = length(krig.x) | ||
new_sample = sample(num_new_samples,lb,ub,sample_type) | ||
f_max = maximum(krig.y) | ||
evaluations = zeros(eltype(krig.x[1]),num_new_samples) | ||
point_found = false | ||
new_x_max = zero(eltype(krig.x[1])) | ||
new_y_max = zero(eltype(krig.x[1])) | ||
diff_x = zeros(eltype(krig.x[1]),d) | ||
while point_found == false | ||
for j = 1:length(new_sample) | ||
std = std_error_at_point(krig,new_sample[j]) | ||
u = krig(new_sample[j]) | ||
if abs(std) > 1e-6 | ||
z = (u - f_max - eps)/std | ||
else | ||
z = 0 | ||
end | ||
evaluations[j] = (u-f_max-eps)*cdf(Normal(),z) + std*pdf(Normal(),z) | ||
end | ||
index_max = argmax(evaluations) | ||
x_new = new_sample[index_max] | ||
y_new = maximum(evaluations) | ||
for l = 1:d | ||
diff_x[l] = norm(krig.x[l] .- x_new) | ||
end | ||
bit_x = diff_x .> dtol | ||
#new_min_x has to have some distance from krig.x | ||
if false in bit_x | ||
#The new_point is not actually that new, discard it! | ||
deleteat!(evaluations,index_max) | ||
deleteat!(new_sample,index_max) | ||
if length(new_sample) == 0 | ||
println("Out of sampling points") | ||
return section_sampler_returner(sample_type, krig.x, krig.y, lb, ub, krig) | ||
end | ||
else | ||
point_found = true | ||
new_x_max = x_new | ||
new_y_max = y_new | ||
end | ||
end | ||
if new_y_max < 1e-6*norm(maximum(krig.y)-minimum(krig.y)) | ||
return section_sampler_returner(sample_type, krig.x, krig.y, lb, ub, krig) | ||
end | ||
add_point!(krig,Tuple(new_x_max),obj(new_x_max)) | ||
end | ||
println("Completed maximum number of iterations") | ||
end | ||
|
||
function section_sampler_returner( | ||
sample_type::SectionSample, surrn_x, surrn_y, | ||
lb, ub, surrn) | ||
d_fixed = Surrogates.fixed_dimensions(sample_type) | ||
@assert length(surrn_y) == size(surrn_x)[1] | ||
surrn_xy = [(surrn_x[y], surrn_y[y]) for y in 1:length(surrn_y)] | ||
section_surr1_xy = filter( | ||
xyz->xyz[1][d_fixed]==Tuple(sample_type.x0[d_fixed]), | ||
surrn_xy) | ||
section_surr1_x = [xy[1] for xy in section_surr1_xy] | ||
section_surr1_y = [xy[2] for xy in section_surr1_xy] | ||
if length(section_surr1_xy) == 0 | ||
@debug "No new point added - surrogate locally stable" | ||
N_NEW_POINTS = 100 | ||
section_surr1_x = sample(N_NEW_POINTS, lb, ub, sample_type) | ||
section_surr1_y = zeros(N_NEW_POINTS) | ||
for i in 1:size(section_surr1_x, 1) | ||
xi = Tuple([section_surr1_x[i, :]...])[1] | ||
section_surr1_y[i] = surrn(xi) | ||
end | ||
end | ||
index = argmin(section_surr1_y) | ||
return (section_surr1_x[index, :][1], section_surr1_y[index]) | ||
end |
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
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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
Sobel-sample x+y in [0,10]x[0,10], | ||
then minimize it on Section([NaN,10.0]), | ||
and verify that the minimum is on x,y=(0,10) | ||
rather than in (0,0) | ||
""" | ||
|
||
using Surrogates | ||
using Test | ||
|
||
lb = [ 0.0, 0.0, 0.0] | ||
ub = [10.0,10.0,10.0] | ||
x = Surrogates.sample(10,lb,ub,LatinHypercubeSample()) | ||
f = x -> x[1]+x[2]+x[3] | ||
y = f.(x) | ||
f([0,0,0]) == 0 | ||
|
||
f_hat = Kriging(x,y,lb,ub) | ||
|
||
f_hat([0,0,0]) | ||
|
||
isapprox(f([0,0,0]), f_hat([0,0,0])) | ||
|
||
""" The global minimum is at (0,0) """ | ||
|
||
(xy_min, f_hat_min) = surrogate_optimize( | ||
f, | ||
DYCORS(), lb, ub, | ||
f_hat, | ||
SobolSample()) | ||
|
||
isapprox(xy_min[1], 0.0, atol=1e-3) | ||
|
||
""" The minimum on the (0,10) section is around (0,10) """ | ||
|
||
section_sampler_z_is_10 = SectionSample( | ||
[NaN64, NaN64, 10.0], | ||
Surrogates.UniformSample()) | ||
|
||
@test [3] == Surrogates.fixed_dimensions(section_sampler_z_is_10) | ||
@test [1,2] == Surrogates.free_dimensions(section_sampler_z_is_10) | ||
|
||
Surrogates.sample(5, lb, ub, section_sampler_z_is_10) | ||
|
||
(xy_min, f_hat_min) = surrogate_optimize( | ||
f, | ||
EI(), lb, ub, | ||
f_hat, | ||
section_sampler_z_is_10, maxiters=1000) | ||
|
||
isapprox(xy_min[1], 0.0, atol=0.1) | ||
isapprox(xy_min[2], 0.0, atol=0.1) | ||
isapprox(xy_min[3], 10.0, atol=0.1) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't have a docstring?