Anonymous user
Knapsack problem/Bounded: Difference between revisions
→{{header|R}}
Line 3,138:
Solution of the task using genetic algorithm.
<lang R>library(tidyverse)
library(rvest)
library(rgenoud)
Line 3,158 ⟶ 3,157:
total_value= sum(task_table$value * x)
total_weight= sum(task_table$weight * x)
ifelse(total_weight <= 400, total_value,
}
Line 3,168 ⟶ 3,167:
evolution= genoud(fn= fitness,
nvars= nrow(allowed),
max= TRUE,
data.type.int= TRUE,
Domains= allowed)
# Solution
cat("Weight:", sum(task_table$weight * evolution$par), "dag", "\n")▼
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)</lang>
Line 3,183 ⟶ 3,180:
{{out}}
<pre>
item pieces
1 map 1
Line 3,200 ⟶ 3,197:
13 socks 1
</pre>
=={{header|Racket}}==
|