Learn R Programming

gor (version 2.0)

crossover_sequences: Crossover of sequences

Description

Crossover sequence operation for use in the genetic cut-search algorithm.

Usage

crossover_sequences(s1, s2, cpoint = NA)

Value

A two-row matrix. Rows are the offsprings produced by the crossover

Arguments

s1

Sequence

s2

Sequence of the same length as s1

cpoint

Crossover point, an integer between 1 and length(s1)-1. Defaults to NA, in which case it will be randomly chosen

Author

Cesar Asensio

Details

This operation takes two sequences of the same length "n" and splits them in two at a crossover point between 1 and "n-1". Then it produces two "offsprings" by interchanging the pieces and gluing them together.

The crossover point can be specified in argument cpoint. By providing NA (the default), cpoint is chosen randomly.

Note that this crossover operation is the "classic" crossover included in the original genetic algorithm, and it is adequate when applied to binary sequences. However, when applied to permutations, the result of this function can have repeated elements; hence, it is not adequate for the TSP.

See Also

search_cut_genetic genetic algorithm cut-search, mutate_binary_sequence binary sequence mutation

Examples

Run this code
set.seed(1)
s1 <- sample(0:1, 10, replace = TRUE)
s2 <- sample(0:1, 10, replace = TRUE)
crossover_sequences(s1, s2)

set.seed(1)
s1 <- sample(1:10, 10)
s2 <- sample(1:10, 10)
crossover_sequences(s1, s2, cpoint = 5)

Run the code above in your browser using DataLab