Advertisement
majkel26

Untitled

Apr 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. library(genalg)
  2.  
  3. dataset <- data.frame(item = c("pocketknife", "beans", "potatoes", "unions", "sleeping bag", "rope", "compass"),
  4. survivalpoints = c(10, 20, 15, 2, 30, 10, 30),
  5. weight = c(1, 5, 10, 1, 7, 5, 1))
  6. weightlimit <- 20
  7.  
  8. chromosone <- c(1, 0, 0, 1, 1, 0, 0)
  9. dataset[chromosone == 1, ]
  10.  
  11. evalFunc <- function(x)
  12. {
  13. current_solution_survivalpoints <- x %*% dataset$survivalpoints
  14. current_solution_weight <- x %*% dataset$weight
  15.  
  16. if (current_solution_weight > weightlimit)
  17. return(0)
  18. else
  19. return(-current_solution_survivalpoints)
  20. }
  21.  
  22. iter <- 100
  23. GAmodel <- rbga.bin(size = 7, popSize = 58, iters = iter, mutationChance = 0.01, elitism = T, evalFunc = evalFunc)
  24.  
  25. cat(summary(GAmodel))
  26. dataset[c(1,1,0,1,1,1,1) == 1,]
  27. plot(-GAmodel$best, type="l", col="red")
  28. lines(-GAmodel$mean, col="blue")
  29.  
  30. c(1,0,0,0,1,0)
  31. a <- "0"
  32. b <- "1"
  33. b <- paste(a, b, sep="")
  34.  
  35. strtoi("001111000000", base=2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement