June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
21
Task/Knapsack-problem-Bounded/R/knapsack-problem-bounded-2.r
Normal file
21
Task/Knapsack-problem-Bounded/R/knapsack-problem-bounded-2.r
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
library(rgenoud)
|
||||
|
||||
fitness= function(x= rep(1, nrow(task_table))){
|
||||
total_value= sum(task_table$value * x)
|
||||
total_weight= sum(task_table$weight * x)
|
||||
ifelse(total_weight <= 400, total_value, 400-total_weight)
|
||||
}
|
||||
|
||||
allowed= matrix(c(rep(0, nrow(task_table)), task_table$pieces), ncol = 2)
|
||||
set.seed(42)
|
||||
evolution= genoud(fn= fitness,
|
||||
nvars= nrow(allowed),
|
||||
max= TRUE,
|
||||
pop.size= 10000,
|
||||
data.type.int= TRUE,
|
||||
Domains= allowed)
|
||||
|
||||
cat("Value: ", evolution$value, "\n")
|
||||
cat("Weight:", sum(task_table$weight * evolution$par), "dag", "\n")
|
||||
data.frame(item= task_table$items, pieces= as.integer(solution)) %>%
|
||||
filter(solution> 0)
|
||||
Loading…
Add table
Add a link
Reference in a new issue