Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
danymukesha committed May 20, 2024
1 parent 3ec335d commit ee494c6
Show file tree
Hide file tree
Showing 17 changed files with 404 additions and 403 deletions.
8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ Description: Genetic algorithm are a class of optimization algorithms
License: MIT + file LICENSE
URL: https://danymukesha.github.io/BioGA/
BugReports: https://github.com/danymukesha/BioGA/issues
Depends:
Imports:
ggplot2,
graphics,
Rcpp,
sessioninfo,
SummarizedExperiment,
animation,
rlang,
biocViews
biocViews,
sessioninfo
Depends:
R (>= 4.3.3)
Suggests:
knitr,
rmarkdown,
Expand Down
45 changes: 24 additions & 21 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# Generated by roxygen2: do not edit by hand

export(crossover_cpp)
export(evaluate_fitness_cpp)
export(initialize_population_cpp)
export(mutation_cpp)
export(plot_fitness)
export(plot_fitness_history)
export(plot_population)
export(replacement_cpp)
export(selection_cpp)
import(SummarizedExperiment)
import(animation)
import(biocViews)
import(ggplot2)
import(graphics)
import(rlang)
import(sessioninfo)
importFrom(Rcpp,evalCpp)
importFrom(Rcpp,sourceCpp)
useDynLib(BioGA, .registration = TRUE)
# Generated by roxygen2: do not edit by hand

export(crossover_cpp)
export(evaluate_fitness_cpp)
export(initialize_population_cpp)
export(mutation_cpp)
export(plot_fitness)
export(plot_fitness_history)
export(plot_population)
export(replacement_cpp)
export(selection_cpp)
importFrom(Rcpp,evalCpp)
importFrom(Rcpp,sourceCpp)
importFrom(SummarizedExperiment,SummarizedExperiment)
importFrom(animation,saveGIF)
importFrom(biocViews,getBiocViews)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,ggtitle)
importFrom(graphics,boxplot)
importFrom(graphics,hist)
importFrom(graphics,par)
importFrom(rlang,local_options)
importFrom(sessioninfo,session_info)
useDynLib(BioGA, .registration = TRUE)
14 changes: 7 additions & 7 deletions R/BioGA-package.R
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"_PACKAGE"

## usethis namespace: start
#' @import biocViews
#' @import ggplot2
#' @import sessioninfo
#' @import SummarizedExperiment
#' @importFrom biocViews getBiocViews
#' @importFrom ggplot2 ggtitle ggplot
#' @importFrom sessioninfo session_info
#' @importFrom SummarizedExperiment SummarizedExperiment
#' @importFrom Rcpp evalCpp sourceCpp
#' @import graphics
#' @import animation
#' @import rlang
#' @importFrom graphics boxplot hist par
#' @importFrom animation saveGIF
#' @importFrom rlang local_options
#' @useDynLib BioGA, .registration = TRUE
## usethis namespace: end
NULL
1 change: 0 additions & 1 deletion R/Plottings.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ plot_fitness <- function(fitness_values) {
#'
#' @return Plot of population
#'
#' @import graphics
#'
#' @examples
#' # example of usage
Expand Down
4 changes: 2 additions & 2 deletions src/crossover.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ NumericMatrix crossover_cpp(const NumericMatrix& selected_parents, int offspring

// Perform crossover between selected parents
for (int i = 0; i < offspring_size; ++i) {
int parent1_index = rand() % num_parents;
int parent2_index = rand() % num_parents;
int parent1_index = R::unif_rand() / num_parents;
int parent2_index = R::unif_rand() / num_parents;
for (int j = 0; j < num_genes; ++j) {
offspring(i, j) = (selected_parents(parent1_index, j) + selected_parents(parent2_index, j)) / 2.0;
}
Expand Down
Empty file modified src/initialize_population.cpp
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion src/replacement.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ NumericMatrix replacement_cpp(const NumericMatrix& population, const NumericMatr

// Replace non-selected individuals in the population with offspring
for (int i = 0; i < num_to_replace; ++i) {
int index_to_replace = rand() % population_size;
int index_to_replace = R::unif_rand() / population_size;
for (int j = 0; j < num_genes; ++j) {
updated_population(index_to_replace, j) = offspring(i, j);
}
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions tests/testthat/test_crossover_cpp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_that("crossover_cpp not produce error or warning", {
# Load The PACKAGE
library(BioGA)

# example of usage
genomic_data <- matrix(rnorm(100), nrow = 10, ncol = 10)
population <- BioGA::initialize_population_cpp(genomic_data,
population_size = 5)
fitness <- BioGA::evaluate_fitness_cpp(genomic_data, population)
selected_parents <- BioGA::selection_cpp(population, fitness,
num_parents = 2)
BioGA::crossover_cpp(selected_parents, offspring_size = 2)

testthat::expect_no_warning(BioGA::crossover_cpp(selected_parents,
offspring_size = 2),
message = "bananas")

testthat::expect_no_error(BioGA::crossover_cpp(selected_parents,
offspring_size = 2),
message = "bananas")
})
19 changes: 19 additions & 0 deletions tests/testthat/test_evaluate_fitness_cpp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
test_that("evalutate_cpp not produce error or warning", {
# example of usage
genomic_data <- matrix(rnorm(100), nrow = 10, ncol = 10)
population <- BioGA::initialize_population_cpp(genomic_data,
population_size = 5)

output <- tryCatch(
BioGA::evaluate_fitness_cpp(genomic_data, population),
error = function(e) return(NA)
)

testthat::expect_no_warning(BioGA::evaluate_fitness_cpp(genomic_data,
population),
message = "bananas")
testthat::expect_no_error(BioGA::evaluate_fitness_cpp(genomic_data,
population),
message = "bananas")
})

6 changes: 4 additions & 2 deletions tests/testthat/test_initialization_population_cpp.R
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("initialize_population_cpp returns a matrix with correct dimensions", {
test_that("initialize_population_cpp returns a matrix
with correct dimensions", {
# Load The PACKAGE
library(BioGA)

Expand All @@ -14,7 +15,8 @@ test_that("initialize_population_cpp returns a matrix with correct dimensions",
expect_equal(ncol(population), nrow(genomic_data))
})

test_that("initialize_population_cpp returns a matrix with values from genomic data", {
test_that("initialize_population_cpp returns a matrix
with values from genomic data", {
# Load THE PACKAGE
library(BioGA)

Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test_mutation_cpp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
test_that("mutation_cpp not produce error or warning", {
# example of usage
genomic_data <- matrix(rnorm(100), nrow = 10, ncol = 10)
population <- BioGA::initialize_population_cpp(genomic_data,
population_size = 5)
fitness <- BioGA::evaluate_fitness_cpp(genomic_data, population)
selected_parents <- BioGA::selection_cpp(population,
fitness, num_parents = 2)
offspring <- BioGA::crossover_cpp(selected_parents, offspring_size = 2)
BioGA::mutation_cpp(offspring, mutation_rate = 0)

testthat::expect_no_warning(BioGA::mutation_cpp(offspring,
mutation_rate = 0),
message = "bananas")

testthat::expect_no_error(BioGA::mutation_cpp(offspring,
mutation_rate = 0),
message = "bananas")
})
11 changes: 11 additions & 0 deletions tests/testthat/test_plot_population_cpp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("crossover_cpp not produce error or warning", {
# Load The PACKAGE
library(BioGA)

# example of usage
population <- matrix(runif(100), nrow = 10, ncol = 10)
BioGA::plot_population(population)

testthat::expect_no_warning(BioGA::plot_population(population),
message = "bananas")
})
38 changes: 38 additions & 0 deletions tests/testthat/test_selection_cpp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
test_that("selection_cpp returns a matrix with correct dimensions", {
# Load The PACKAGE
library(BioGA)

# example of usage
genomic_data <- matrix(rnorm(100), nrow = 10, ncol = 10)
population <- BioGA::initialize_population_cpp(genomic_data,
population_size = 5)
fitness <- BioGA::evaluate_fitness_cpp(genomic_data, population)
selected_population <- BioGA::selection_cpp(population, fitness,
num_parents = 2)

# Check dimensions
expect_equal(2, nrow(selected_population))

})


test_that("selection_cpp returns a matrix with correct dimensions", {
# Load The PACKAGE
library(BioGA)

# example of usage
genomic_data <- matrix(rnorm(100), nrow = 10, ncol = 10)
population <- BioGA::initialize_population_cpp(genomic_data,
population_size = 5)
fitness <- BioGA::evaluate_fitness_cpp(genomic_data, population)
selected_population <- BioGA::selection_cpp(population, fitness,
num_parents = 2)

testthat::expect_no_warning(BioGA::evaluate_fitness_cpp(genomic_data,
population),
message = "bananas")
testthat::expect_no_error(BioGA::evaluate_fitness_cpp(genomic_data,
population),
message = "bananas")

})
Loading

0 comments on commit ee494c6

Please sign in to comment.