Skip to content

Commit

Permalink
Added SOP ND and fixed two bugs in SOP 1D
Browse files Browse the repository at this point in the history
  • Loading branch information
ludoro committed Aug 20, 2020
1 parent 1d5cf46 commit a44ca7c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 45 deletions.
86 changes: 43 additions & 43 deletions src/Optimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1111,15 +1111,15 @@ function surrogate_optimize(obj::Function,sop1::SOP,lb::Number,ub::Number,surrSO
N_failures[i] = N_failures_global[i]
end

f_1 = new_points[i,1]
f_1 = obj(new_points[i,1])
f_2 = obj2_1D(f_1,surrSOP.x)

l = length(Fronts[1])
Pareto_set = zeros(eltype(surrSOP.x[1]),l,2)

for j = 1:l
val = obj2_1D(Fronts[1][j],surrSOP.x)
Pareto_set[j,1] = Fronts[1][j]
Pareto_set[j,1] = obj(Fronts[1][j])
Pareto_set[j,2] = val
end
if (Hypervolume_Pareto_improving(f_1,f_2,Pareto_set)<tau)
Expand All @@ -1144,9 +1144,7 @@ function surrogate_optimize(obj::Function,sop1::SOP,lb::Number,ub::Number,surrSO
end




function obj2_1D(value,points)
function obj2_ND(value,points)
min = +Inf
my_p = filter(x->norm(x.-value)>10^-6,points)
for i = 1:length(my_p)
Expand All @@ -1158,13 +1156,6 @@ function obj2_1D(value,points)
return min
end

function obj2_ND(value,points)
min = +Inf
m


end

function I_tier_ranking_ND(P,surrSOPD::AbstractSurrogate)
#obj1 = objective_function
#obj2 = obj2_1D
Expand Down Expand Up @@ -1214,9 +1205,17 @@ function I_tier_ranking_ND(P,surrSOPD::AbstractSurrogate)
return F
end

function II_tier_ranking_1D(D::Dict,srgD::AbstractSurrogate)


function II_tier_ranking_ND(D::Dict,srgD::AbstractSurrogate)
for i = 1:length(D)
pos = []
yn = []
for j = 1:length(D[i])
push!(pos,findall(e->e==D[i][j],srgD.x))
push!(yn,srgD.y[pos[j]])
end
D[i] = D[i][sortperm(D[i])]
end
return D
end

function surrogate_optimize(obj::Function,sopd::SOP,lb,ub,surrSOPD::AbstractSurrogate,sample_type::SamplingAlgorithm;maxiters=100,num_new_samples=min(500*length(lb),5000))
Expand Down Expand Up @@ -1247,34 +1246,33 @@ function surrogate_optimize(obj::Function,sopd::SOP,lb,ub,surrSOPD::AbstractSurr


##### P CENTERS ######
C = []
C = Array{eltype(surrSOPD.x),1}()

#S(x) set of points already evaluated
#Rank points in S with:
#1) Non dominated sorting
Fronts_I = I_tier_ranking_ND(centers_global,surrSOPD)
#2) Second tier ranking
Fronts = II_tier_ranking_ND(Fronts_I,surrSOPD)
ranked_list = []
ranked_list = Array{eltype(surrSOPD.x),1}()
for i = 1:length(Fronts)
for j = 1:length(Fronts[i])
push!(ranked_list,Fronts[i][j])
end
end
ranked_list = eltype(surrSOPD.x[1][1]).(ranked_list)

centers_full = 0
i = 1
while i <= length(ranked_list) && centers_full == 0
flag = 0
for j = 1:length(ranked_list)
for m = 1:length(tabu)
if abs(ranked_list[j]-tabu[m]) < tau
if norm(ranked_list[j].-tabu[m]) < tau
flag = 1
end
end
for l = 1:length(centers_global)
if abs(ranked_list[j]-centers_global[l]) < tau
if norm(ranked_list[j].-centers_global[l]) < tau
flag = 1
end
end
Expand All @@ -1298,7 +1296,7 @@ function surrogate_optimize(obj::Function,sopd::SOP,lb,ub,surrSOPD::AbstractSurr
flag = 0
for j = 1:length(ranked_list)
for m = 1:length(centers_global)
if abs(centers_global[j] - ranked_list[m]) < tau
if norm(centers_global[j] .- ranked_list[m]) < tau
flag = 1
end
end
Expand Down Expand Up @@ -1327,51 +1325,53 @@ function surrogate_optimize(obj::Function,sopd::SOP,lb,ub,surrSOPD::AbstractSurr
end
end

#Here I have selected C = [] containing the centers
#Here I have selected C = [(1.0,2.0),(3.0,4.0),.....] containing the centers
r_centers = 0.2*norm(ub.-lb)*ones(num_P)
N_failures = zeros(num_P)
#2.3 Candidate search
new_points = zeros(eltype(surrSOPD.x[1]),num_P,2)
new_points_x = Array{eltype(surrSOPD.x),1}()
new_points_y = zeros(eltype(surrSOPD.y[1]),num_P)
for i = 1:num_P
N_candidates = zeros(eltype(surrSOPD.x[1]),num_new_samples)
N_candidates = zeros(eltype(surrSOPD.x[1]),num_new_samples,d)
#Using phi(n) just like DYCORS, merit function = surrogate
#Like in DYCORS, I_perturb = 1 always
evaluations = zeros(eltype(surrSOPD.y[1]),num_new_samples)
for j = 1:num_new_samples
a = lb - C[i]
b = ub - C[i]
N_candidates[j] = C[i] + rand(truncated(Normal(0,r_centers[i]), a,b))
evaluations[j] = surrSOP(N_candidates[j])
for k = 1:d
a = lb[k] - C[i][k]
b = ub[k] - C[i][k]
N_candidates[j,k] = C[i][k] + rand(truncated(Normal(0,r_centers[i]), a,b))
end
evaluations[j] = surrSOPD(Tuple(N_candidates[j,:]))
end
x_best = N_candidates[argmin(evaluations)]
x_best = Tuple(N_candidates[argmin(evaluations),:])
y_best = minimum(evaluations)
new_points[i,1] = x_best
new_points[i,2] = y_best
push!(new_points_x,x_best)
new_points_y[i] = y_best
end

#new_points[i] now contains:
#new_points[i] is splitted in new_points_x and new_points_y now contains:
#[x_1,y_1; x_2,y_2,...,x_{num_new_samples},y_{num_new_samples}]


#2.4 Adaptive learning and tabu archive
for i=1:num_P
if new_points[i,1] in centers_global
if new_points_x[i] in centers_global
r_centers[i] = r_centers_global[i]
N_failures[i] = N_failures_global[i]
end

f_1 = new_points[i,1]
f_2 = obj2_1D(f_1,surrSOP.x)
f_1 = obj(Tuple(new_points_x[i]))
f_2 = obj2_ND(f_1,surrSOPD.x)

l = length(Fronts[1])
Pareto_set = zeros(eltype(surrSOP.x[1]),l,2)

Pareto_set = zeros(eltype(surrSOPD.x[1]),l,2)
for j = 1:l
val = obj2_1D(Fronts[1][j],surrSOP.x)
Pareto_set[j,1] = Fronts[1][j]
val = obj2_ND(Fronts[1][j],surrSOPD.x)
Pareto_set[j,1] = obj(Tuple(Fronts[1][j]))
Pareto_set[j,2] = val
end
if (Hypervolume_Pareto_improving(f_1,f_2,Pareto_set)<tau)
if (Hypervolume_Pareto_improving(f_1,f_2,Pareto_set)<tau)#check this
#failure
r_centers[i] = r_centers[i]/2
N_failures[i] += 1
Expand All @@ -1382,12 +1382,12 @@ function surrogate_optimize(obj::Function,sopd::SOP,lb,ub,surrSOPD::AbstractSurr
else
#P_i is success
#Adaptive_learning
add_point!(surrSOP,new_points[i,1],new_points[i,2])
add_point!(surrSOPD,new_points_x[i],new_points_y[i])
push!(r_centers_global,r_centers[i])
push!(N_failures_global,N_failures[i])
end
end
end
index = argmin(surrSOP.y)
return (surrSOP.x[index],surrSOP.y[index])
index = argmin(surrSOPD.y)
return (surrSOPD.x[index],surrSOPD.y[index])
end
13 changes: 11 additions & 2 deletions test/optimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ using Flux
using Flux: @epochs

#######SRBF############

##### 1D #####
lb = 0.0
ub = 15.0
Expand Down Expand Up @@ -220,7 +219,6 @@ p = [1.5,1.5]
theta = [2.0,2.0]
lb = [1.0,1.0]
ub = [6.0,6.0]
bounds = [lb,ub]


my_k_DYCORSN = Kriging(x,y,lb,ub)
Expand All @@ -243,3 +241,14 @@ ub = 6.0
num_centers = 2
my_k_SOP1 = Kriging(x,y,lb,ub,p=1.9)
surrogate_optimize(objective_function,SOP(num_centers),lb,ub,my_k_SOP1,SobolSample(),maxiters=60)
#ND
objective_function_ND = z -> 2*norm(z)+1
x = [(2.3,2.2),(1.4,1.5)]
y = objective_function_ND.(x)
p = [1.5,1.5]
theta = [2.0,2.0]
lb = [1.0,1.0]
ub = [6.0,6.0]
my_k_SOPND = Kriging(x,y,lb,ub)
num_centers = 2
surrogate_optimize(objective_function_ND,SOP(num_centers),lb,ub,my_k_SOPND,SobolSample(),maxiters=20)

0 comments on commit a44ca7c

Please sign in to comment.