Skip to content

Commit

Permalink
rename method set_user_solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
jmejia8 committed Mar 8, 2023
1 parent e657910 commit dcf87a4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Metaheuristics.gen_initial_state
```

```@docs
set_inital_solutions!
set_user_solutions!
```

## Variation
Expand Down
4 changes: 2 additions & 2 deletions src/Metaheuristics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export GenerationalReplacement, ElitistReplacement
export sample, LatinHypercubeSampling, Grid
export εDE, Restart
export optimize!
export set_inital_solutions!
export set_user_solutions!

include("externals.jl")

Expand All @@ -52,7 +52,7 @@ include("common/repair.jl")
include("common/stop.jl")
include("common/compare.jl")
include("common/non-dominated-sorting.jl")
include("common/set_inital_solutions.jl")
include("common/set_user_solutions.jl")



Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include("gen_initial_state.jl")

"""
set_inital_solutions!(optimizer, x, fx)
set_user_solutions!(optimizer, x, fx)
Provide initial solutions to the `optimizer`.
- `x` can be a `Vector` and `fx` a function or `fx = f(x)`
Expand All @@ -18,12 +18,12 @@ julia> algo = ECA(N = 61); # optimizer
julia> # one solution can be provided
x0 = [0.5, 0.5 0.5];
julia> set_inital_solutions!(algo, x0, f);
julia> set_user_solutions!(algo, x0, f);
julia> # providing multiple solutions
X0 = rand(30, 3); # 30 solutions with dim 3
julia> set_inital_solutions!(algo, X0, f);
julia> set_user_solutions!(algo, X0, f);
julia> optimize(f, [0 0 0; 1 1 1.0], algo)
+=========== RESULT ==========+
Expand All @@ -37,7 +37,7 @@ stop reason: Small difference of objective function values.
```
"""
function set_inital_solutions!(algo::AbstractAlgorithm, solution::AbstractSolution)
function set_user_solutions!(algo::AbstractAlgorithm, solution::AbstractSolution)
status = algo.status
if !isnothing(status.best_sol) && !isempty(status.population)
push!(status.population, solution)
Expand All @@ -48,16 +48,16 @@ function set_inital_solutions!(algo::AbstractAlgorithm, solution::AbstractSoluti
end


function set_inital_solutions!(algo::AbstractAlgorithm, x::AbstractVector, fx)
set_inital_solutions!(algo, create_child(x, fx))
function set_user_solutions!(algo::AbstractAlgorithm, x::AbstractVector, fx)
set_user_solutions!(algo, create_child(x, fx))
end

function set_inital_solutions!(algo::AbstractAlgorithm, x::AbstractVector, f::Function)
set_inital_solutions!(algo, x, f(x))
function set_user_solutions!(algo::AbstractAlgorithm, x::AbstractVector, f::Function)
set_user_solutions!(algo, x, f(x))
end


function set_inital_solutions!(algo::AbstractAlgorithm, X::AbstractMatrix, fX::AbstractVector)
function set_user_solutions!(algo::AbstractAlgorithm, X::AbstractMatrix, fX::AbstractVector)
n = size(X, 1)
m = length(fX)

Expand All @@ -72,15 +72,15 @@ function set_inital_solutions!(algo::AbstractAlgorithm, X::AbstractMatrix, fX::A

# TODO: this part can be parallelized
for i in 1:n
set_inital_solutions!(algo, X[i,:], fX[i])
set_user_solutions!(algo, X[i,:], fX[i])
end

# TODO check population size provided in algo.parameters.N

algo
end

function set_inital_solutions!(algo::AbstractAlgorithm, X::AbstractMatrix, f::Function)
set_inital_solutions!(algo, X, [f(X[i,:]) for i in 1:size(X,1)])
function set_user_solutions!(algo::AbstractAlgorithm, X::AbstractMatrix, f::Function)
set_user_solutions!(algo, X, [f(X[i,:]) for i in 1:size(X,1)])
end

4 changes: 2 additions & 2 deletions test/initial_solution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# initial solutions
X = rand(10, size(bounds, 2))
X[1,:] = x_optimum #zeros(size(bounds, 2))
set_inital_solutions!(algo, X, f)
set_user_solutions!(algo, X, f)

res = optimize(f, bounds, algo)

Expand Down Expand Up @@ -59,7 +59,7 @@
# initial solutions
X = rand(10, size(bounds, 2))
X[1,:] = x_optimum
set_inital_solutions!(algo, X, f)
set_user_solutions!(algo, X, f)

res = optimize(f, bounds, algo)
@test length(res.population) == algo.parameters.N
Expand Down

0 comments on commit dcf87a4

Please sign in to comment.