R/operator.EAMutatorSwap.R

Defines functions EAMutatorSwap

#' @title
#' Swap mutator.
#'
#' @description
#' Chooses two positions at random and swaps the genes.
#'
#' @param ind [\code{integer}]\cr
#'   Permutation of integers, i.e., vector of integer values.
#' @return [\code{integer}]
#' @family mutators
#' @export
EAMutatorSwap = function() {
  EAMutator$new(
    name = "Swap mutation",
    representations = "permutation",
    params = list(),
    fun = function(ind) {
      n = length(ind)
      pos = sample(1:n, size = 2L)
      pos1 = pos[1L]
      pos2 = pos[2L]
      tmp = ind[pos1]
      ind[pos1] = ind[pos2]
      ind[pos2] = tmp
      return(ind)
    }
  )
} # EAMutatorSwap
jakobbossek/ecr3 documentation built on Nov. 14, 2019, 7:47 p.m.