Usage

Genetic Algorithm Optimization Vignette

This vignette provides a step-by-step explanation of a genetic algorithm optimization process using the BioGA package.

Chapter 1: Initialization

# Load the BioGA package
library(BioGA)

# Example genomic data
genomic_data <- matrix(rnorm(50), nrow = 10, ncol = 10)

# Initialize population
population <- initialize_population_cpp(genomic_data, population_size = 5)

# Initialize fitness history
fitness_history <- list()

# Initialize time progress
start_time <- Sys.time()

Chapter 2: Genetic Algorithm Optimization

# Run genetic algorithm optimization
generation <- 0
while (TRUE) {
    generation <- generation + 1

    # Evaluate fitness
    fitness <- evaluate_fitness_cpp(genomic_data, population)
    fitness_history[[generation]] <- fitness

    # Check termination condition
    if (generation == 6) {
        break
    }

    # Selection
    selected_parents <- selection_cpp(population, fitness,
        num_parents = 2
    )

    # Crossover and Mutation
    offspring <- crossover_cpp(selected_parents, offspring_size = 2)
    # (no mutation in this example)
    mutated_offspring <- mutation_cpp(offspring, mutation_rate = 0)

    # Replacement
    population <- replacement_cpp(population, mutated_offspring,
        num_to_replace = 1
    )

    # Calculate time progress
    elapsed_time <- difftime(Sys.time(), start_time, units = "secs")

    # Print time progress
    cat(
        "\rGeneration:", generation, "- Elapsed Time:",
        format(elapsed_time, units = "secs"), "     "
    )
}
#> Generation: 1 - Elapsed Time: 0.03701258 secs      Generation: 2 - Elapsed Time: 0.03731847 secs      Generation: 3 - Elapsed Time: 0.03748512 secs      Generation: 4 - Elapsed Time: 0.03763103 secs      Generation: 5 - Elapsed Time: 0.0377574 secs

Chapter 3: Results Visualization

# Plot fitness change over generations
plot_fitness_history(fitness_history)

This vignette demonstrates how to implement a genetic algorithm optimization process using the BioGA package, from initialization to results visualization.

Session Info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R Under development (unstable) (2024-03-18 r86148)
#>  os       Ubuntu 22.04.4 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  C
#>  ctype    en_US.UTF-8
#>  tz       America/New_York
#>  date     2024-04-11
#>  pandoc   2.7.3 @ /usr/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package              * version     date (UTC) lib source
#>  abind                  1.4-5       2016-07-21 [3] CRAN (R 4.4.0)
#>  animation              2.7         2021-10-07 [3] CRAN (R 4.4.0)
#>  Biobase              * 2.63.1      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  BiocGenerics         * 0.49.1      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  BiocManager            1.30.22     2023-08-08 [2] CRAN (R 4.4.0)
#>  biocViews              1.71.1      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  BioGA                * 0.99.3      2024-04-11 [1] Bioconductor
#>  bitops                 1.0-7       2021-04-24 [3] CRAN (R 4.4.0)
#>  bslib                  0.7.0       2024-03-29 [3] CRAN (R 4.4.0)
#>  cachem                 1.0.8       2023-05-01 [3] CRAN (R 4.4.0)
#>  cli                    3.6.2       2023-12-11 [3] CRAN (R 4.4.0)
#>  colorspace             2.1-0       2023-01-23 [3] CRAN (R 4.4.0)
#>  crayon                 1.5.2       2022-09-29 [3] CRAN (R 4.4.0)
#>  DelayedArray           0.29.9      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  digest                 0.6.35      2024-03-11 [3] CRAN (R 4.4.0)
#>  dplyr                  1.1.4       2023-11-17 [3] CRAN (R 4.4.0)
#>  evaluate               0.23        2023-11-01 [3] CRAN (R 4.4.0)
#>  fansi                  1.0.6       2023-12-08 [3] CRAN (R 4.4.0)
#>  farver                 2.1.1       2022-07-06 [3] CRAN (R 4.4.0)
#>  fastmap                1.1.1       2023-02-24 [3] CRAN (R 4.4.0)
#>  generics               0.1.3       2022-07-05 [3] CRAN (R 4.4.0)
#>  GenomeInfoDb         * 1.39.13     2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  GenomeInfoDbData       1.2.12      2024-03-27 [3] Bioconductor
#>  GenomicRanges        * 1.55.4      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  ggplot2                3.5.0       2024-02-23 [3] CRAN (R 4.4.0)
#>  glue                   1.7.0       2024-01-09 [3] CRAN (R 4.4.0)
#>  graph                  1.81.0      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  gtable                 0.3.4       2023-08-21 [3] CRAN (R 4.4.0)
#>  highr                  0.10        2022-12-22 [3] CRAN (R 4.4.0)
#>  htmltools              0.5.8.1     2024-04-04 [3] CRAN (R 4.4.0)
#>  httr                   1.4.7       2023-08-15 [3] CRAN (R 4.4.0)
#>  IRanges              * 2.37.1      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  jquerylib              0.1.4       2021-04-26 [3] CRAN (R 4.4.0)
#>  jsonlite               1.8.8       2023-12-04 [3] CRAN (R 4.4.0)
#>  knitr                  1.46        2024-04-06 [3] CRAN (R 4.4.0)
#>  labeling               0.4.3       2023-08-29 [3] CRAN (R 4.4.0)
#>  lattice                0.22-6      2024-03-20 [4] CRAN (R 4.4.0)
#>  lifecycle              1.0.4       2023-11-07 [3] CRAN (R 4.4.0)
#>  magrittr               2.0.3       2022-03-30 [3] CRAN (R 4.4.0)
#>  Matrix                 1.7-0       2024-03-22 [4] CRAN (R 4.4.0)
#>  MatrixGenerics       * 1.15.0      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  matrixStats          * 1.3.0       2024-04-11 [3] CRAN (R 4.4.0)
#>  munsell                0.5.1       2024-04-01 [3] CRAN (R 4.4.0)
#>  pillar                 1.9.0       2023-03-22 [3] CRAN (R 4.4.0)
#>  pkgconfig              2.0.3       2019-09-22 [3] CRAN (R 4.4.0)
#>  R6                     2.5.1       2021-08-19 [3] CRAN (R 4.4.0)
#>  RBGL                   1.79.0      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  Rcpp                   1.0.12      2024-01-09 [3] CRAN (R 4.4.0)
#>  RCurl                  1.98-1.14   2024-01-09 [3] CRAN (R 4.4.0)
#>  rlang                  1.1.3       2024-01-10 [3] CRAN (R 4.4.0)
#>  rmarkdown              2.26        2024-03-05 [3] CRAN (R 4.4.0)
#>  RUnit                  0.4.33      2024-02-22 [3] CRAN (R 4.4.0)
#>  S4Arrays               1.3.7       2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  S4Vectors            * 0.41.6      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  sass                   0.4.9       2024-03-15 [3] CRAN (R 4.4.0)
#>  scales                 1.3.0       2023-11-28 [3] CRAN (R 4.4.0)
#>  sessioninfo            1.2.2       2021-12-06 [3] CRAN (R 4.4.0)
#>  SparseArray            1.3.5       2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  SummarizedExperiment * 1.33.3      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  tibble                 3.2.1       2023-03-20 [3] CRAN (R 4.4.0)
#>  tidyselect             1.2.1       2024-03-11 [3] CRAN (R 4.4.0)
#>  UCSC.utils             0.99.6      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  utf8                   1.2.4       2023-10-22 [3] CRAN (R 4.4.0)
#>  vctrs                  0.6.5       2023-12-01 [3] CRAN (R 4.4.0)
#>  withr                  3.0.0       2024-01-16 [3] CRAN (R 4.4.0)
#>  xfun                   0.43        2024-03-25 [3] CRAN (R 4.4.0)
#>  XML                    3.99-0.16.1 2024-01-22 [3] CRAN (R 4.4.0)
#>  XVector                0.43.1      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#>  yaml                   2.3.8       2023-12-11 [2] CRAN (R 4.4.0)
#>  zlibbioc               1.49.3      2024-04-11 [3] Bioconductor 3.19 (R 4.4.0)
#> 
#>  [1] /tmp/RtmphEIHRe/Rinst69f98221ba567
#>  [2] /home/pkgbuild/packagebuilder/workers/jobs/3315/R-libs
#>  [3] /home/biocbuild/bbs-3.19-bioc/R/site-library
#>  [4] /home/biocbuild/bbs-3.19-bioc/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────