Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(genalg)
- dataset <- data.frame(item = c("pocketknife", "beans", "potatoes", "unions", "sleeping bag", "rope", "compass"),
- survivalpoints = c(10, 20, 15, 2, 30, 10, 30),
- weight = c(1, 5, 10, 1, 7, 5, 1))
- weightlimit <- 20
- chromosone <- c(1, 0, 0, 1, 1, 0, 0)
- dataset[chromosone == 1, ]
- evalFunc <- function(x)
- {
- current_solution_survivalpoints <- x %*% dataset$survivalpoints
- current_solution_weight <- x %*% dataset$weight
- if (current_solution_weight > weightlimit)
- return(0)
- else
- return(-current_solution_survivalpoints)
- }
- iter <- 100
- GAmodel <- rbga.bin(size = 7, popSize = 58, iters = iter, mutationChance = 0.01, elitism = T, evalFunc = evalFunc)
- cat(summary(GAmodel))
- dataset[c(1,1,0,1,1,1,1) == 1,]
- plot(-GAmodel$best, type="l", col="red")
- lines(-GAmodel$mean, col="blue")
- c(1,0,0,0,1,0)
- a <- "0"
- b <- "1"
- b <- paste(a, b, sep="")
- strtoi("001111000000", base=2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement