File: crossover.R

package info (click to toggle)
r-cran-mlr 2.19.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,264 kB
  • sloc: ansic: 65; sh: 13; makefile: 5
file content (22 lines) | stat: -rw-r--r-- 685 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#' Crossover.
#'
#' Takes two bit strings and creates a new one of the same size by selecting the items from the first string or
#' the second, based on a given rate (the probability of choosing an element from the first string).
#'
#' @param x ([logical])\cr
#'   First parent string.
#' @param y ([logical])\cr
#'   Second parent string.
#' @param rate (`numeric(1)`)\cr
#'   A number representing the probability of selecting an element of the first string.
#'   Default is `0.5`.
#' @return ([crossover]).
#' @name crossover
#' @rdname crossover
#' @aliases crossover
NULL

crossover = function(x, y, rate = 0.5) {
  ratio = rbinom(length(x), 1, rate)
  ifelse(ratio == 1, x, y)
}