Skip to content

Commit

Permalink
Add de_evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
soldatmat committed Feb 14, 2024
1 parent b455ee7 commit 11755d2
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 1 deletion.
33 changes: 33 additions & 0 deletions scripts/benchmark.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using BenchmarkTools

include("de_sandbox.jl")

"""
A simple benchmark showcasing the speed-up achieved via parallelization of `de_evaluation`.
"""
function parallel_benchmark(runs::Int=160000, n_iterations::Int=5)
ss = SequenceSpace{Nothing}(initial_population)
seq = @elapsed de_evaluation(
ss,
runs;
screening,
selection_strategy,
mutagenesis,
n_iterations,
parallel=false,
)

ss = SequenceSpace{Nothing}(initial_population)
par = @elapsed de_evaluation(
ss,
runs;
screening,
selection_strategy,
mutagenesis,
n_iterations,
parallel=true,
)

println("SEQUENTIAL de_evaluation: $seq")
println("PARALLEL de_evaluation : $par")
end
1 change: 1 addition & 0 deletions scripts/runscripts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ include("custom_modules_example.jl")
#include("de_example.jl")
#include("de_sandbox.jl")
#include("profiling.jl")
#include("benchmark.jl")
3 changes: 2 additions & 1 deletion src/DESilico.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module DESilico

export Variant, SequenceSpace
export de!
export de!, de_evaluation

include("types/include.jl")
include("de.jl")
include("de_evaluation.jl")
include("selection_strategy/include.jl")
include("mutagenesis/include.jl")
include("screening/include.jl")
Expand Down
115 changes: 115 additions & 0 deletions src/de_evaluation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
"""
de_evaluation(sequence_space::SequenceSpace, runs::Int; kwargs...)
Perform directed evolution through `de!()` with defined options `runs` times.
Returns the top fitness obtained in each run as a `Vector{Float64}`.
# Arguments
- `sequence_space::SequenceSpace`: Initial `SeqeunceSpace` which will be used in each run of `de!()`.
- `runs::Int`: Number of times `de!()` will be called.
# Keywords
- `screening::Screening`: Assigns fitness value to a sequence.
- `selection_strategy::SelectionStrategy`: Defines the algorithm used to select new parents from a pool of screened variants.
- `mutagenesis:Mutagenesis`: Defines the algorithm used to create new mutants from current population.
- `n_iterations::Integer`: Specifies the number of iteration of `de!()`.
- `parallel::Bool=false`: If true, the calls of `de!()` will be run in parallel.
"""
function de_evaluation(
sequence_space::SequenceSpace,
runs::Int;
screening::Screening,
selection_strategy::SelectionStrategy,
mutagenesis::Mutagenesis,
n_iterations::Int,
parallel::Bool=false,
)
@assert runs > 0
results = Vector{Float64}(undef, runs)
get_sequence_space = r -> SequenceSpace{Nothing}(sequence_space.population, sequence_space.top_variant)
if parallel
_run_de_parallel!(results, get_sequence_space, runs; screening, selection_strategy, mutagenesis, n_iterations)
else
_run_de_sequential!(results, get_sequence_space, runs; screening, selection_strategy, mutagenesis, n_iterations)
end
return results
end

"""
de_evaluation(sequence_space::SequenceSpace, runs::Int; kwargs...)
Perform directed evolution through `de!()` with defined options `runs` times.
Returns the top fitness obtained in each run as a `Vector{Float64}`.
# Arguments
- `starting_variants::AbstractVector{Variant}`: Each of the `starting_variants` will be used as the sole initial parent in one run of `de!()`.
# Keywords
- `screening::Screening`: Assigns fitness value to a sequence.
- `selection_strategy::SelectionStrategy`: Defines the algorithm used to select new parents from a pool of screened variants.
- `mutagenesis:Mutagenesis`: Defines the algorithm used to create new mutants from current population.
- `n_iterations::Integer`: Specifies the number of iteration of `de!()`.
- `parallel::Bool=false`: If true, the calls of `de!()` will be run in parallel.
"""
function de_evaluation(
starting_variants::AbstractVector{Variant};
screening::Screening,
selection_strategy::SelectionStrategy,
mutagenesis::Mutagenesis,
n_iterations::Int,
parallel::Bool=false,
)
@assert length(starting_variants) > 0
results = Vector{Float64}(undef, length(starting_variants))
get_sequence_space = r -> SequenceSpace([starting_variants[r]])
if parallel
_run_de_parallel!(results, get_sequence_space, length(starting_variants); screening, selection_strategy, mutagenesis, n_iterations)
else
_run_de_sequential!(results, get_sequence_space, length(starting_variants); screening, selection_strategy, mutagenesis, n_iterations)
end
return results
end

function _run_de_sequential!(
results::Vector{Float64},
get_sequence_space::Function,
runs::Int;
screening::Screening,
selection_strategy::SelectionStrategy,
mutagenesis::Mutagenesis,
n_iterations::Int,
)
for r = 1:runs
ss = get_sequence_space(r)
de!(
ss;
screening,
selection_strategy,
mutagenesis,
n_iterations,
)
results[r] = ss.top_variant.fitness
end
end

function _run_de_parallel!(
results::Vector{Float64},
get_sequence_space::Function,
runs::Int;
screening::Screening,
selection_strategy::SelectionStrategy,
mutagenesis::Mutagenesis,
n_iterations::Int,
)
Threads.@threads :static for r = 1:runs
ss = get_sequence_space(r)
de!(
ss;
screening,
selection_strategy,
mutagenesis,
n_iterations,
)
results[r] = ss.top_variant.fitness
end
end

0 comments on commit 11755d2

Please sign in to comment.