Skip to content

Commit

Permalink
Merge 7382fe4 into a949542
Browse files Browse the repository at this point in the history
  • Loading branch information
ludoro committed Jun 29, 2019
2 parents a949542 + 7382fe4 commit eb4983e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/Optimization.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LinearAlgebra
using Distributions

function merit_function(point,w,surr::AbstractSurrogate,s_max,s_min,d_max,d_min,box_size)
if length(point)==1
Expand Down Expand Up @@ -430,3 +431,53 @@ function LCBS(lb,ub,krig::Kriging,maxiters::Int,
end
end
end


function EI(lb::Number,ub::Number,krig::Kriging,maxiters::Int,
sample_type::SamplingAlgorithm,num_new_samples::Int,obj::Function)
dtol = 1e-3*norm(ub-lb)
eps = 0.01
for i = 1:maxiters
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]))
while point_found == false
for j = 1:num_new_samples
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)
diff_x = abs.(krig.x .- x_new)
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
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
end
add_point!(krig,new_x_max,obj(new_x_max))
end
end
2 changes: 1 addition & 1 deletion src/Surrogates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ include("Optimization.jl")

export Kriging, RadialBasis, add_point!, current_estimate, std_error_at_point
export sample, SamplingAlgorithm, GridSample, UniformSample, SobolSample, LatinHypercubeSample, LowDiscrepancySample
export SRBF,LCBS
export SRBF,LCBS,EI

end
17 changes: 17 additions & 0 deletions test/optimization.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Surrogates
using LinearAlgebra


#=
#######SRBF############
##### 1D #####
Expand Down Expand Up @@ -75,3 +77,18 @@ ub = [6.0,6.0]
#Kriging
my_k_ND = Kriging(x,y,p,theta)
LCBS(lb,ub,my_k_ND,10,UniformSample(),10,objective_function_ND)
=#

##### EI ######

###1D###
objective_function = x -> 2*x+1
x = [2.0,4.0,6.0]
y = [5.0,9.0,13.0]
p = 2
a = 2
b = 6
my_k = Kriging(x,y,p)
EI(a,b,my_k,100,SobolSample(),100,objective_function)

###ND###

0 comments on commit eb4983e

Please sign in to comment.