
ea.crossover <- function(p1, p2, mask) {
    ch1 <- c()
    ch2 <- c()

    for(i in 1:length(mask)) {
        if(mask[i]) {
            ch1[i] <- p2[i]
            ch2[i] <- p1[i]
        } else {
            ch1[i] <- p1[i]
            ch2[i] <- p2[i]
        }
    }

    return(list(ch1, ch2))
}

indiv2int <- function(indiv) {
    res <- 0
    
    indiv_rev <- rev(indiv)

    for(i in 1:length(indiv)) {
        res <- res + indiv_rev[i] * 2^(i-1)
    }

    return(res)
}

ea.n_point_mask <- function(len, n_points) {
    return( function() {
        points <- sort(c(c(1), sample(2:(len - 1), n_points), c(len)))

        val <- TRUE    
        mask <- c()

        for(i in 1:(length(points) - 1)) {
            mask <- append(mask, rep(val, points[i + 1] - points[i]))
        
            if(val) {val <- FALSE} else {val <- TRUE}
        }

        return(mask)
    })
}

ea.uniform_mask <- function(len) {
    return(function() sample(c(TRUE, FALSE), len, replace=TRUE))
}

rand.individual <- function(len) {
    return(sample(c(0,1), len, replace=TRUE))
}
