You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.0 KiB

3 years ago
  1. ea.crossover <- function(p1, p2, mask) {
  2. ch1 <- c()
  3. ch2 <- c()
  4. for(i in 1:length(mask)) {
  5. if(mask[i]) {
  6. ch1[i] <- p2[i]
  7. ch2[i] <- p1[i]
  8. } else {
  9. ch1[i] <- p1[i]
  10. ch2[i] <- p2[i]
  11. }
  12. }
  13. return(list(ch1, ch2))
  14. }
  15. indiv2int <- function(indiv) {
  16. res <- 0
  17. indiv_rev <- rev(indiv)
  18. for(i in 1:length(indiv)) {
  19. res <- res + indiv_rev[i] * 2^(i-1)
  20. }
  21. return(res)
  22. }
  23. ea.n_point_mask <- function(len, n_points) {
  24. return( function() {
  25. points <- sort(c(c(1), sample(2:(len - 1), n_points), c(len)))
  26. val <- TRUE
  27. mask <- c()
  28. for(i in 1:(length(points) - 1)) {
  29. mask <- append(mask, rep(val, points[i + 1] - points[i]))
  30. if(val) {val <- FALSE} else {val <- TRUE}
  31. }
  32. return(mask)
  33. })
  34. }
  35. ea.uniform_mask <- function(len) {
  36. return(function() sample(c(TRUE, FALSE), len, replace=TRUE))
  37. }
  38. rand.individual <- function(len) {
  39. return(sample(c(0,1), len, replace=TRUE))
  40. }