Skip to content

Commit

Permalink
Using SearchSpaces to solve #46
Browse files Browse the repository at this point in the history
  • Loading branch information
jmejia8 committed Mar 21, 2023
1 parent 251d729 commit e1c4fbc
Show file tree
Hide file tree
Showing 26 changed files with 166 additions and 121 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Metaheuristics"
uuid = "bcdb8e00-2c21-11e9-3065-2b553b22f898"
authors = ["Jesus Mejia <[email protected]>"]
version = "3.2.16"
version = "3.3.1"

[deps]
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
Expand All @@ -11,13 +11,15 @@ Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
SearchSpaces = "eb7571c6-2196-4f03-99b8-52a5a35b3163"
SnoopPrecompile = "66db9d55-30c0-4569-8b51-7e840670fc0c"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
Distances = "^0.10.7"
JMcDM = "^0.7.1"
Requires = "1"
SearchSpaces = "^0.1.2"
SnoopPrecompile = "1"
julia = "^1.5"

Expand Down
12 changes: 6 additions & 6 deletions src/algorithms/ABC/ABC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function initialize!(
options.parallel_evaluation &&
error("ABC is not supporting parallel evaluation. Put `options.parallel_evaluation=false`")

D = size(problem.bounds, 2)
D = getdim(problem)

if options.f_calls_limit == 0
options.f_calls_limit = 10000D
Expand Down Expand Up @@ -113,19 +113,19 @@ function update_state!(
kargs...
)

D = size(problem.bounds, 2)
D = getdim(problem)
fobj = problem.f
bees = status.population
Ne = parameters.Ne
No = parameters.No
bounds = problem.bounds
a = view(bounds, 1,:)
b = view(bounds, 2,:)
bounds = problem.search_space
a = bounds.lb
b = bounds.ub

employedPhase!(bees,problem, Ne)
outlookerPhase!(bees,problem, No)

@inline genPos(D=D, a=Array(a), b = Array(b)) = a + (b - a) .* rand(D)
@inline genPos(D=D, a=a, b=b) = a + (b - a) .* rand(D)
best = chooseBest(bees, status.best_sol)

status.f_calls += Ne + No + scoutPhase!(bees, problem, genPos, parameters.limit)
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/ABC/bee_dynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function updateBee!(bee, bee2, problem)
v = ϕ*(bee.sol.x - bee2.sol.x)

x_new = bee.sol.x + v
replace_with_random_in_bounds!(x_new, problem.bounds)
replace_with_random_in_bounds!(x_new, problem.search_space)

new_sol = create_solution(x_new, problem)

Expand Down
6 changes: 3 additions & 3 deletions src/algorithms/CCMO/CCMO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ function reproduction(status, parameters::CCMO{NSGA2}, problem)
# selection
_s = TournamentSelection(;N=base.N ÷ 2)
# crossover
_c = SBX=base.η_cr, p = base.p_cr, bounds = problem.bounds)
_c = SBX=base.η_cr, p = base.p_cr, bounds = problem.search_space)
# mutation
base.p_m < 0.0 && (base.p_m = 1/size(bounds,2))
_m = PolynomialMutation=base.η_m, p = base.p_m, bounds = problem.bounds)
base.p_m < 0.0 && (base.p_m = 1/getdim(problem))
_m = PolynomialMutation=base.η_m, p = base.p_m, bounds = problem.search_space)

# population 1
parent_mask = selection(population, _s, fitness=fitness1)
Expand Down
7 changes: 4 additions & 3 deletions src/algorithms/CGSA/CGSA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,13 @@ function initialize!(

Rnorm = parameters.Rnorm
N = parameters.N
D = size(problem.bounds, 2)
D = getdim(problem)
fobj = problem.f


# bounds vectors
low, up = problem.bounds[1,:], problem.bounds[2,:]
low = problem.search_space.lb
up = problem.search_space.ub

max_it = 500
options.iterations = options.iterations == 0 ? max_it : options.iterations
Expand Down Expand Up @@ -209,7 +210,7 @@ function update_state!(
# Checking allowable range.
# X = correctPop(X, low, up)
for i = 1:N
x = reset_to_violated_bounds!(X[i,:], problem.bounds)
x = reset_to_violated_bounds!(X[i,:], problem.search_space)
X[i,:] = x
end

Expand Down
4 changes: 2 additions & 2 deletions src/algorithms/DE/DE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function initialize!(
args...;
kargs...
)
D = size(problem.bounds, 2)
D = getdim(problem)



Expand Down Expand Up @@ -213,7 +213,7 @@ function reproduction(status, parameters::AbstractDifferentialEvolution, problem
x = get_position(population[i])
u = DE_mutation(population, F, strategy, 1)
v = DE_crossover(x, u, CR)
evo_boundary_repairer!(v, xBest, problem.bounds)
evo_boundary_repairer!(v, xBest, problem.search_space)
X[i,:] = v
end

Expand Down
4 changes: 2 additions & 2 deletions src/algorithms/ECA/CECA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function update_state!(
feasible_solutions = findall( s->s.is_feasible, population )
weights = compute_weights(population)

X_next = zeros(parameters.N, size(problem.bounds, 2))
X_next = zeros(parameters.N, getdim(problem))

# For each elements in Population
for i = 1:parameters.N
Expand Down Expand Up @@ -52,7 +52,7 @@ function update_state!(
mask = rand(length(y)) .< 1.0 / length(y)
y[mask] = v[mask]

evo_boundary_repairer!(y, c, problem.bounds)
evo_boundary_repairer!(y, c, problem.search_space)
X_next[i,:] = y
end

Expand Down
8 changes: 4 additions & 4 deletions src/algorithms/ECA/ECA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function eca_solution(status, parameters, options, problem, I, i)
end
# binary crossover
y, M_current = crossover(U[u_best].x, y, parameters.p_cr)
evo_boundary_repairer!(y, c, problem.bounds)
evo_boundary_repairer!(y, c, problem.search_space)
y, M_current
end

Expand All @@ -135,7 +135,7 @@ function update_state!(
)

I = randperm(parameters.N)
D = size(problem.bounds, 2)
D = getdim(problem)

parameters.adaptive && (Mcr_fail = zeros(D))

Expand Down Expand Up @@ -237,7 +237,7 @@ function initialize!(
args...;
kargs...
)
D = size(problem.bounds, 2)
D = getdim(problem)


if parameters.N <= parameters.K
Expand Down Expand Up @@ -299,7 +299,7 @@ function reproduction(status, parameters::ECA, problem)
parameters.K,
parameters.η_max;
i=i,
bounds = problem.bounds)
bounds = problem.search_space)
end

X
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/ECA/center_of_mass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function ECA_operator(
return y
end

!isempty(bounds) && evo_boundary_repairer!(y, c, bounds)
evo_boundary_repairer!(y, c, bounds)

return y
end
Expand Down
4 changes: 2 additions & 2 deletions src/algorithms/MCCGA/MCCGA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ function initialize!(
options.f_calls_limit = options.iterations * parameters.N + 1
end

lower = problem.bounds[1,:]
upper = problem.bounds[2,:]
lower = problem.search_space.lb
upper = problem.search_space.ub

parameters.probvector = initialprobs(lower, upper, maxsamples = parameters.maxsamples)

Expand Down
14 changes: 7 additions & 7 deletions src/algorithms/MOEAD_DE/MOEAD_DE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function initialize!(


status = gen_initial_state(problem,parameters,information,options,status)
D = size(problem.bounds, 2)
D = getdim(problem)
parameters.nobjectives = length(status.population[1].f)
parameters.p_m = parameters.p_m < 0.0 ? 1.0 / D : parameters.p_m

Expand All @@ -193,7 +193,7 @@ function update_state!(
F = parameters.F
CR = parameters.CR

D = size(problem.bounds, 2)
D = getdim(problem)


N = parameters.N
Expand All @@ -211,7 +211,7 @@ function update_state!(
# reproduction
v = MOEAD_DE_reproduction(i, P_idx, population, parameters, problem)
# repair
replace_with_random_in_bounds!(v, problem.bounds)
replace_with_random_in_bounds!(v, problem.search_space)
h = create_solution(v, problem)
# update z
update_reference_point!(parameters.z, h)
Expand Down Expand Up @@ -252,13 +252,13 @@ Perform Differential Evolution operators and polynomial mutation using three vec
`a, b, c` and parameters `F, CR, p_m, η`, i.e., stepsize, crossover and
mutation probability.
"""
function MOEAD_DE_reproduction(a, b, c, F, CR, p_m, η, bounds)
function MOEAD_DE_reproduction(a, b, c, F, CR, p_m, η, bounds::Bounds)
D = length(a)
# binomial crossover
v = zeros(length(a))

la = view(bounds, 1, :)
lb = view(bounds, 2, :)
la = bounds.lb
lb = bounds.ub

# binomial crossover
for j in 1:D
Expand Down Expand Up @@ -309,7 +309,7 @@ function MOEAD_DE_reproduction(i, P_idx, population, parameters::MOEAD_DE, probl
parameters.CR,
parameters.p_m,
parameters.η,
problem.bounds)
problem.search_space)
end

g_te_ap(gx, V, τ, s1, s2) = V < τ ? gx + s1*V^2 : gx + s1*τ^2 + s2*(V - τ)
Expand Down
12 changes: 6 additions & 6 deletions src/algorithms/NSGA2/NSGA2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ offspring. Return two vectors.
"""
function GA_reproduction(pa::AbstractVector{T},
pb::AbstractVector{T},
bounds;
bounds::Bounds;
η_cr = 20,
η_m = 15,
p_cr = 0.9,
Expand Down Expand Up @@ -156,7 +156,7 @@ Same that `GA_reproduction` but only returns one offspring.
"""
function GA_reproduction_half(pa::AbstractVector{T},
pb::AbstractVector{T},
bounds;
bounds::Bounds;
η_cr = 20,
η_m = 15,
p_cr = 0.9,
Expand All @@ -180,7 +180,7 @@ function reproduction(pa, pb, parameters::AbstractNSGA, problem)
# crossover and mutation
c1, c2 = GA_reproduction(get_position(pa),
get_position(pb),
problem.bounds;
problem.search_space;
η_cr = parameters.η_cr,
p_cr = parameters.p_cr,
η_m = parameters.η_m,
Expand All @@ -203,7 +203,7 @@ function initialize!(
args...;
kargs...
)
D = size(problem.bounds, 2)
D = getdim(problem)

if parameters.p_m < 0.0
parameters.p_m = 1.0 / D
Expand Down Expand Up @@ -256,15 +256,15 @@ function reproduction(status, parameters::AbstractNSGA, problem)
@assert !isempty(status.population)

N_half = parameters.N
Q = zeros(2N_half, size(problem.bounds, 2))
Q = zeros(2N_half, getdim(problem))

for i in 1:N_half
pa = tournament_selection(status.population)
pb = tournament_selection(status.population)

c1, c2 = GA_reproduction(get_position(pa),
get_position(pb),
problem.bounds;
problem.search_space;
η_cr = parameters.η_cr,
p_cr = parameters.p_cr,
η_m = parameters.η_m,
Expand Down
9 changes: 5 additions & 4 deletions src/algorithms/NSGA3/NSGA3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,9 @@ function initialize!(
options::Options,
args...;
kargs...
)
D = size(problem.bounds, 2)
)

D = getdim(problem)

if parameters.p_m < 0.0
parameters.p_m = 1.0 / D
Expand Down Expand Up @@ -324,15 +325,15 @@ function reproduction(status, parameters::NSGA3, problem)
@assert !isempty(status.population)

I = randperm(parameters.N)
Q = zeros(parameters.N, size(problem.bounds, 2))
Q = zeros(parameters.N, getdim(problem))
for i = 1:parameters.N ÷ 2

pa = status.population[I[2i-1]]
pb = status.population[I[2i]]

c1, c2 = GA_reproduction(get_position(pa),
get_position(pb),
problem.bounds;
problem.search_space;
η_cr = parameters.η_cr,
p_cr = parameters.p_cr,
η_m = parameters.η_m,
Expand Down
6 changes: 3 additions & 3 deletions src/algorithms/PSO/PSO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ function update_state!(
)
xGBest = get_position(status.best_sol)

X_new = zeros(parameters.N, size(problem.bounds, 2))
X_new = zeros(parameters.N, getdim(problem))

# For each elements in population
for i in 1:parameters.N
x = get_position(parameters.flock[i])
xPBest = get_position(status.population[i])
parameters.v[i, :] = velocity(x, parameters.v[i, :], xPBest, xGBest, parameters)
x += parameters.v[i, :]
reset_to_violated_bounds!(x, problem.bounds)
reset_to_violated_bounds!(x, problem.search_space)
X_new[i,:] = x
end

Expand Down Expand Up @@ -119,7 +119,7 @@ function initialize!(
args...;
kargs...
)
D = size(problem.bounds, 2)
D = getdim(problem)


if parameters.N == 0
Expand Down
10 changes: 5 additions & 5 deletions src/algorithms/SA/SA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ function initialize!(
)


l = view( problem.bounds, 1,:)
u = view( problem.bounds, 2,:)
l = problem.search_space.lb
u = problem.search_space.ub

if isempty(parameters.x_initial)
parameters.x_initial = l .+ (u .- l) .* rand(length(u))
Expand Down Expand Up @@ -139,8 +139,8 @@ function update_state!(
T = nevals / max_evals
μ = 10.0 ^( 100T )

l = view( problem.bounds, 1,:)
u = view( problem.bounds, 2,:)
l = problem.search_space.lb
u = problem.search_space.ub

# For each temperature we take 500 test points to simulate reach termal
# equilibrium.
Expand All @@ -153,7 +153,7 @@ function update_state!(

# Next step is to keep solution within bounds
#x1 = (x1 .< l).*l+(l .<= x1).*(x1 .<= u).*x1+(u .< x1).*u
reset_to_violated_bounds!(x1, problem.bounds)
reset_to_violated_bounds!(x1, problem.search_space)
fx1 = evaluate(x1, problem)

status.f_calls += 1
Expand Down
Loading

0 comments on commit e1c4fbc

Please sign in to comment.