-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from jmejia8/develop
Let the user set initial solutions
- Loading branch information
Showing
15 changed files
with
264 additions
and
73 deletions.
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
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.14" | ||
version = "3.2.15" | ||
|
||
[deps] | ||
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7" | ||
|
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
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
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 |
---|---|---|
@@ -1,42 +0,0 @@ | ||
""" | ||
gen_initial_state(problem,parameters,information,options) | ||
Generate an initial state, i.e., compute uniformly distributed random vectors in bounds, | ||
after that are evaluated in objective function. This method require that `parameters.N` | ||
is valid attribute. | ||
""" | ||
gen_initial_state(problem,parameters,information,options,status::State{Any}) = gen_initial_state(problem,parameters,information,options) | ||
|
||
|
||
function gen_initial_state(problem,parameters,information,options, status) | ||
parameters.N != length(status.population) && | ||
error("Population size in provided State differs from that in parameters") | ||
|
||
|
||
size(problem.bounds,2) != length(get_position(status.best_sol)) && | ||
error("Invalid population (dimension does not match with bounds)") | ||
|
||
return State(status.best_sol, status.population) | ||
|
||
|
||
end | ||
|
||
function gen_initial_state(problem,parameters,information,options) | ||
# population array | ||
population = generate_population(parameters.N, problem,ε=options.h_tol) | ||
|
||
# best solution | ||
best_solution = get_best(population) | ||
|
||
return State(best_solution, population; f_calls = length(population), iteration=1) | ||
end | ||
|
||
function Base.show(io::IO, parameters::AbstractParameters) | ||
s = typeof(parameters) | ||
|
||
vals = string.(map(f -> getfield(parameters, f), fieldnames(s))) | ||
str = string(s) * "(" * join(string.(fieldnames(s)) .* "=" .* vals, ", ") * ")" | ||
|
||
print(io, str) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
""" | ||
gen_initial_state(problem,parameters,information,options) | ||
Generate an initial state, i.e., compute uniformly distributed random vectors in bounds, | ||
after that are evaluated in objective function. This method require that `parameters.N` | ||
is valid attribute. | ||
""" | ||
gen_initial_state(problem,parameters,information,options,status::State{Any}) = gen_initial_state(problem,parameters,information,options) | ||
|
||
|
||
function gen_initial_state(problem,parameters,information,options, status) | ||
if parameters.N != length(status.population) | ||
options.debug && @warn("Population size in provided State differs from that in parameters") | ||
end | ||
|
||
size(problem.bounds,2) != length(get_position(status.best_sol)) && | ||
error("Invalid population (dimension does not match with bounds)") | ||
|
||
_complete_population!(status,problem,parameters,information,options) | ||
|
||
best_solution = get_best(status.population) | ||
# check if a better solution was found | ||
if is_better(status.best_sol, best_solution) | ||
best_solution = status.best_sol | ||
end | ||
|
||
return State(best_solution, status.population;f_calls = length(status.population)) | ||
end | ||
|
||
function gen_initial_state(problem,parameters,information,options) | ||
# population array | ||
population = generate_population(parameters.N, problem,ε=options.h_tol) | ||
|
||
# best solution | ||
best_solution = get_best(population) | ||
|
||
return State(best_solution, population; f_calls = length(population), iteration=1) | ||
end | ||
|
||
function Base.show(io::IO, parameters::AbstractParameters) | ||
s = typeof(parameters) | ||
|
||
vals = string.(map(f -> getfield(parameters, f), fieldnames(s))) | ||
str = string(s) * "(" * join(string.(fieldnames(s)) .* "=" .* vals, ", ") * ")" | ||
|
||
print(io, str) | ||
end | ||
|
||
function _complete_population!(status,problem,parameters,information,options) | ||
if parameters.N < length(status.population) | ||
# increase population if necessary | ||
parameters.N = length(status.population) | ||
# TODO: use this options.debug == true to show the message? | ||
@warn("Population size increased to $(parameters.N) due to initial solutions.") | ||
return | ||
end | ||
|
||
if parameters.N > length(status.population) | ||
# complete population with random in bounds | ||
n = parameters.N - length(status.population) | ||
missing_sols = generate_population(n, problem,ε=options.h_tol) | ||
# insert new solution into population | ||
append!(status.population, missing_sols) | ||
end | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
include("gen_initial_state.jl") | ||
|
||
""" | ||
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)` | ||
- `x` can be a matrix containing solutions in rows. | ||
### Example | ||
```julia-repl | ||
julia> f(x) = abs(x[1]) + x[2] + x[3]^2 # objective function | ||
f (generic function with 1 method) | ||
julia> algo = ECA(N = 61); # optimizer | ||
julia> # one solution can be provided | ||
x0 = [0.5, 0.5, 0.5]; | ||
julia> set_user_solutions!(algo, x0, f); | ||
julia> # providing multiple solutions | ||
X0 = rand(30, 3); # 30 solutions with dim 3 | ||
julia> set_user_solutions!(algo, X0, f); | ||
julia> optimize(f, [0 0 0; 1 1 1.0], algo) | ||
+=========== RESULT ==========+ | ||
iteration: 413 | ||
minimum: 0 | ||
minimizer: [0.0, 0.0, 0.0] | ||
f calls: 25132 | ||
total time: 0.0856 s | ||
stop reason: Small difference of objective function values. | ||
+============================+ | ||
``` | ||
""" | ||
function set_user_solutions!(algo::AbstractAlgorithm, solution::AbstractSolution) | ||
status = algo.status | ||
if !isnothing(status.best_sol) && !isempty(status.population) | ||
push!(status.population, solution) | ||
else | ||
algo.status = State(solution, [solution]) | ||
end | ||
algo | ||
end | ||
|
||
|
||
function set_user_solutions!(algo::AbstractAlgorithm, x::AbstractVector, fx) | ||
set_user_solutions!(algo, create_child(x, fx)) | ||
end | ||
|
||
function set_user_solutions!(algo::AbstractAlgorithm, x::AbstractVector, f::Function) | ||
set_user_solutions!(algo, x, f(x)) | ||
end | ||
|
||
|
||
function set_user_solutions!(algo::AbstractAlgorithm, X::AbstractMatrix, fX::AbstractVector) | ||
n = size(X, 1) | ||
m = length(fX) | ||
|
||
if n != m | ||
@warn "$(n) decision vectors provided but $(m) objective values." | ||
n = min(m, n) | ||
println("Taking ", n, " as the number of initial solutions.") | ||
end | ||
|
||
# nothing to do due to it is necessary the objective value | ||
n == 0 && (return algo) | ||
|
||
# TODO: this part can be parallelized | ||
for i in 1:n | ||
set_user_solutions!(algo, X[i,:], fX[i]) | ||
end | ||
|
||
# TODO check population size provided in algo.parameters.N | ||
|
||
algo | ||
end | ||
|
||
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 | ||
|
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
Oops, something went wrong.