June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,26 +1,27 @@
|
|||
using MathProgBase
|
||||
using MathProgBase, Cbc
|
||||
|
||||
immutable KPDSupply{S<:String, T<:Integer}
|
||||
item::S
|
||||
struct KPDSupply{T<:Integer}
|
||||
item::String
|
||||
weight::T
|
||||
value::T
|
||||
quant::T
|
||||
end
|
||||
Base.show(io::IO, kdps::KPDSupply) = print(io, kdps.quant, " ", kdps.item, " ($(kdps.weight) kg, $(kdps.value) €)")
|
||||
|
||||
function solve{S<:String, T<:Integer}(gear::Array{KPDSupply{S,T},1},
|
||||
capacity::T)
|
||||
w = map(x->x.weight, gear)
|
||||
v = map(x->x.value, gear)
|
||||
q = map(x->x.quant, gear)
|
||||
sol = mixintprog(-v, w', '<', capacity, :Int, 0, q)
|
||||
sol.status == :Optimal || error("This Problem could not be solved")
|
||||
if all(q .== 1)
|
||||
return gear[sol.sol .== 1.0]
|
||||
function solve(gear::Vector{KPDSupply{T}}, capacity::Integer) where T<:Integer
|
||||
w = getfield.(gear, :weight)
|
||||
v = getfield.(gear, :value)
|
||||
q = getfield.(gear, :quant)
|
||||
sol = mixintprog(-v, w', '<', capacity, :Int, 0, q, CbcSolver())
|
||||
sol.status == :Optimal || error("this problem could not be solved")
|
||||
|
||||
if all(q .== 1) # simpler case
|
||||
return gear[sol.sol == 1.0]
|
||||
else
|
||||
pack = KPDSupply[]
|
||||
s = int(sol.sol)
|
||||
pack = similar(gear, 0)
|
||||
s = round.(Int, sol.sol)
|
||||
for (i, g) in enumerate(gear)
|
||||
s[i] != 0 || continue
|
||||
iszero(s[i]) && continue
|
||||
push!(pack, KPDSupply(g.item, g.weight, g.value, s[i]))
|
||||
end
|
||||
return pack
|
||||
|
|
|
|||
|
|
@ -22,11 +22,6 @@ gear = [KPDSupply("map", 9, 150, 1),
|
|||
KPDSupply("book", 30, 10, 2)]
|
||||
|
||||
pack = solve(gear, 400)
|
||||
|
||||
println("The hiker should pack:")
|
||||
for s in pack
|
||||
println(" ", s.quant, " ", s.item)
|
||||
end
|
||||
println()
|
||||
println("Packed Weight: ", mapreduce(x->x.weight*x.quant, +, pack))
|
||||
println("Packed Value: ", mapreduce(x->x.value*x.quant, +, pack))
|
||||
println("The hiker should pack: \n - ", join(pack, "\n - "))
|
||||
println("\nPacked weight: ", sum(getfield.(pack, :weight)), " kg")
|
||||
println("Packed value: ", sum(getfield.(pack, :value)), " €")
|
||||
|
|
|
|||
11
Task/Knapsack-problem-Bounded/R/knapsack-problem-bounded-1.r
Normal file
11
Task/Knapsack-problem-Bounded/R/knapsack-problem-bounded-1.r
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
library(tidyverse)
|
||||
library(rvest)
|
||||
|
||||
task_html= read_html("http://rosettacode.org/wiki/Knapsack_problem/Bounded")
|
||||
task_table= html_nodes(html, "table")[[1]] %>%
|
||||
html_table(table, header= T, trim= T) %>%
|
||||
set_names(c("items", "weight", "value", "pieces")) %>%
|
||||
filter(items != "knapsack") %>%
|
||||
mutate(weight= as.numeric(weight),
|
||||
value= as.numeric(value),
|
||||
pieces= as.numeric(pieces))
|
||||
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